Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify displacement BC interface #1274

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions examples/contact/ironing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "serac/physics/solid_mechanics_contact.hpp"
#include "serac/infrastructure/terminator.hpp"
#include "serac/mesh/mesh_utils.hpp"
#include "serac/numerics/functional/domain.hpp"
#include "serac/physics/state/state_manager.hpp"
#include "serac/physics/materials/parameterized_solid_material.hpp"
#include "serac/serac_config.hpp"
Expand All @@ -36,7 +37,7 @@ int main(int argc, char* argv[])
std::string filename = SERAC_REPO_DIR "/data/meshes/ironing.mesh";

auto mesh = serac::mesh::refineAndDistribute(serac::buildMeshFromFile(filename), 2, 0);
serac::StateManager::setMesh(std::move(mesh), "ironing_mesh");
auto& pmesh = serac::StateManager::setMesh(std::move(mesh), "ironing_mesh");

serac::LinearSolverOptions linear_options{.linear_solver = serac::LinearSolver::Strumpack, .print_level = 1};
#ifndef MFEM_USE_STRUMPACK
Expand Down Expand Up @@ -80,21 +81,24 @@ int main(int argc, char* argv[])
serac::solid_mechanics::ParameterizedNeoHookeanSolid mat{1.0, 0.0, 0.0};
solid_solver.setMaterial(serac::DependsOn<0, 1>{}, mat);

// Pass the BC information to the solver object
solid_solver.setDisplacementBCs({5}, [](const mfem::Vector&, mfem::Vector& u) {
u.SetSize(dim);
u = 0.0;
});
solid_solver.setDisplacementBCs({12}, [](const mfem::Vector&, double t, mfem::Vector& u) {
u.SetSize(dim);
u = 0.0;
if (t <= 2.0 + 1.0e-12) {
u[2] = -t * 0.15;
} else {
u[0] = -(t - 2.0) * 0.25;
u[2] = -0.3;
}
});
// Pass the BC information to the solver object/.
serac::Domain bottom = serac::Domain::ofBoundaryElements(pmesh, [](std::vector<serac::vec3>, int attr) { return attr == 5; });
solid_solver.setFixedBCs(bottom);

serac::Domain top_of_iron = serac::Domain::ofBoundaryElements(pmesh, [](std::vector<serac::vec3>, int attr){ return attr == 12; });
auto applied_displacements = [](auto, double t) {
serac::tensor<double, dim> u{};
if (t <= 2.0 + 1.0e-12) {
u[2] = -t * 0.15;
} else {
u[0] = -(t - 2.0) * 0.25;
u[2] = -0.3;
}
return u;
};
solid_solver.setDisplacementBCs(applied_displacements, top_of_iron, 0);
solid_solver.setDisplacementBCs(applied_displacements, top_of_iron, 1);
solid_solver.setDisplacementBCs(applied_displacements, top_of_iron, 2);

// Add the contact interaction
auto contact_interaction_id = 0;
Expand Down
69 changes: 0 additions & 69 deletions src/serac/numerics/functional/domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,75 +458,6 @@ mfem::Array<int> Domain::dof_list(mfem::FiniteElementSpace* fes) const
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

Domain EntireDomain(const mfem::Mesh& mesh)
{
Domain output{mesh, mesh.SpaceDimension() /* elems can be 2 or 3 dimensional */};

int tri_id = 0;
int quad_id = 0;
int tet_id = 0;
int hex_id = 0;

// faces that satisfy the predicate are added to the domain
int num_elems = mesh.GetNE();
for (int i = 0; i < num_elems; i++) {
auto geom = mesh.GetElementGeometry(i);

switch (geom) {
case mfem::Geometry::TRIANGLE:
output.tri_ids_.push_back(tri_id++);
break;
case mfem::Geometry::SQUARE:
output.quad_ids_.push_back(quad_id++);
break;
case mfem::Geometry::TETRAHEDRON:
output.tet_ids_.push_back(tet_id++);
break;
case mfem::Geometry::CUBE:
output.hex_ids_.push_back(hex_id++);
break;
default:
SLIC_ERROR("unsupported element type");
break;
}
}

return output;
}

Domain EntireBoundary(const mfem::Mesh& mesh)
{
Domain output{mesh, mesh.SpaceDimension() - 1, Domain::Type::BoundaryElements};

int edge_id = 0;
int tri_id = 0;
int quad_id = 0;

for (int f = 0; f < mesh.GetNumFaces(); f++) {
// discard faces with the wrong type
if (mesh.GetFaceInformation(f).IsInterior()) continue;

auto geom = mesh.GetFaceGeometry(f);

switch (geom) {
case mfem::Geometry::SEGMENT:
output.edge_ids_.push_back(edge_id++);
break;
case mfem::Geometry::TRIANGLE:
output.tri_ids_.push_back(tri_id++);
break;
case mfem::Geometry::SQUARE:
output.quad_ids_.push_back(quad_id++);
break;
default:
SLIC_ERROR("unsupported element type");
break;
}
}

return output;
}

/// @cond
using c_iter = std::vector<int>::const_iterator;
using b_iter = std::back_insert_iterator<std::vector<int>>;
Expand Down
14 changes: 12 additions & 2 deletions src/serac/numerics/functional/domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,20 @@ struct Domain {
};

/// @brief constructs a domain from all the elements in a mesh
Domain EntireDomain(const mfem::Mesh& mesh);
template <int dim>
Domain EntireDomain(const mfem::Mesh& mesh)
{
auto tautological_predicate = [](std::vector<tensor<double, dim>>, int) { return true; };
return Domain::ofElements(mesh, tautological_predicate);
}

/// @brief constructs a domain from all the boundary elements in a mesh
Domain EntireBoundary(const mfem::Mesh& mesh);
template <int dim>
Domain EntireBoundary(const mfem::Mesh& mesh)
{
auto tautological_predicate = [](std::vector<tensor<double, dim>>, int) { return true; };
return Domain::ofBoundaryElements(mesh, tautological_predicate);
}

/// @brief create a new domain that is the union of `a` and `b`
Domain operator|(const Domain& a, const Domain& b);
Expand Down
4 changes: 2 additions & 2 deletions src/serac/numerics/functional/functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class Functional<test(trials...), exec> {

using signature = test(decltype(serac::type<args>(trial_spaces))...);
integrals_.push_back(
MakeDomainIntegral<signature, Q, dim>(EntireDomain(domain), integrand, qdata, std::vector<uint32_t>{args...}));
MakeDomainIntegral<signature, Q, dim>(EntireDomain<dim>(domain), integrand, qdata, std::vector<uint32_t>{args...}));
}

/// @overload
Expand Down Expand Up @@ -335,7 +335,7 @@ class Functional<test(trials...), exec> {

using signature = test(decltype(serac::type<args>(trial_spaces))...);
integrals_.push_back(
MakeBoundaryIntegral<signature, Q, dim>(EntireBoundary(domain), integrand, std::vector<uint32_t>{args...}));
MakeBoundaryIntegral<signature, Q, dim>(EntireBoundary<dim>(domain), integrand, std::vector<uint32_t>{args...}));
}

/// @overload
Expand Down
4 changes: 2 additions & 2 deletions src/serac/numerics/functional/functional_qoi.inl
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public:

using signature = test(decltype(serac::type<args>(trial_spaces))...);
integrals_.push_back(
MakeDomainIntegral<signature, Q, dim>(EntireDomain(mesh), integrand, qdata, std::vector<uint32_t>{args...}));
MakeDomainIntegral<signature, Q, dim>(EntireDomain<dim>(mesh), integrand, qdata, std::vector<uint32_t>{args...}));
}

/// @overload
Expand Down Expand Up @@ -210,7 +210,7 @@ public:

using signature = test(decltype(serac::type<args>(trial_spaces))...);
integrals_.push_back(
MakeBoundaryIntegral<signature, Q, dim>(EntireBoundary(mesh), integrand, std::vector<uint32_t>{args...}));
MakeBoundaryIntegral<signature, Q, dim>(EntireBoundary<dim>(mesh), integrand, std::vector<uint32_t>{args...}));
}

/// @overload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ void BoundaryConditionManager::addEssential(const mfem::Array<int>&
all_dofs_valid_ = false;
}

void BoundaryConditionManager::addEssential(const mfem::Array<int>& true_dofs,
std::shared_ptr<mfem::Coefficient> ess_bdr_coef,
mfem::ParFiniteElementSpace& space,
std::optional<int> component)
{
ess_bdr_.emplace_back(ess_bdr_coef, component, space, true_dofs);
all_dofs_valid_ = false;
}

void BoundaryConditionManager::updateAllDofs() const
{
all_true_dofs_.DeleteAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class BoundaryConditionManager {
*/
void addEssential(const mfem::Array<int>& true_dofs, std::shared_ptr<mfem::VectorCoefficient> ess_bdr_coef,
mfem::ParFiniteElementSpace& space);

void addEssential(const mfem::Array<int>& true_dofs, std::shared_ptr<mfem::Coefficient> ess_bdr_coef,
mfem::ParFiniteElementSpace& space, std::optional<int> component = {});

/**
* @brief Returns all the true degrees of freedom associated with all the essential BCs
Expand Down
85 changes: 38 additions & 47 deletions src/serac/physics/solid_mechanics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "serac/numerics/odes.hpp"
#include "serac/numerics/stdfunction_operator.hpp"
#include "serac/numerics/functional/shape_aware_functional.hpp"
#include "serac/numerics/functional/domain.hpp"
#include "serac/physics/state/state_manager.hpp"
#include "serac/physics/materials/solid_material.hpp"

Expand Down Expand Up @@ -416,68 +417,58 @@ class SolidMechanics<order, dim, Parameters<parameter_space...>, std::integer_se
template <typename T>
qdata_type<T> createQuadratureDataBuffer(T initial_state, const std::optional<Domain>& optional_domain = std::nullopt)
{
Domain domain = (optional_domain) ? *optional_domain : EntireDomain(mesh_);
Domain domain = (optional_domain) ? *optional_domain : EntireDomain<dim>(mesh_);
return StateManager::newQuadratureDataBuffer(domain, order, dim, initial_state);
}

/**
* @brief Set essential displacement boundary conditions (strongly enforced)
* @brief Set essential displacement boundary conditions on one component
*
* @param[in] disp_bdr The boundary attributes from the mesh on which to enforce a displacement
* @param[in] disp The prescribed boundary displacement function
* @param[in] applied_displacement Function specifying the applied displacement vector.
* Only the value in the component @p component will be used, the others are ignored.
* @param[in] domain Domain over which to apply the boundary condition.
* @param[in] component Index of the displacement component that will be constrained
*
* @note This method must be called prior to completeSetup()
*
* For the displacement function, the first argument is the input position and the second argument is the output
* prescribed displacement.
* The signature of the applied_displacement callable is:
* tensor<double, dim> applied_displacement(tensor<double, dim> X, double t)
* Parameters:
* X - coordinates of node
* t - time
* Returns:
* u, vector of applied displacements
*/
void setDisplacementBCs(const std::set<int>& disp_bdr, std::function<void(const mfem::Vector&, mfem::Vector&)> disp)
template <typename AppliedDisplacementFunction>
void setDisplacementBCs(AppliedDisplacementFunction applied_displacement, const Domain& domain, int component)
{
// Project the coefficient onto the grid function
disp_bdr_coef_ = std::make_shared<mfem::VectorFunctionCoefficient>(dim, disp);

bcs_.addEssential(disp_bdr, disp_bdr_coef_, displacement_.space());
}
auto mfem_coefficient_function = [applied_displacement, component](const mfem::Vector& X_mfem, double t) {
auto X = make_tensor<dim>([&X_mfem](int i) { return X_mfem[i]; });
return applied_displacement(X, t)[component];
};

/**
* @brief Set essential displacement boundary conditions (strongly enforced)
*
* @param[in] disp_bdr The boundary attributes from the mesh on which to enforce a displacement
* @param[in] disp The prescribed boundary displacement function
*
* For the displacement function, the first argument is the input position, the second argument is the time, and the
* third argument is the output prescribed displacement.
*
* @note This method must be called prior to completeSetup()
*/
void setDisplacementBCs(const std::set<int>& disp_bdr,
std::function<void(const mfem::Vector&, double, mfem::Vector&)> disp)
{
// Project the coefficient onto the grid function
disp_bdr_coef_ = std::make_shared<mfem::VectorFunctionCoefficient>(dim, disp);
component_disp_bdr_coef_ = std::make_shared<mfem::FunctionCoefficient>(mfem_coefficient_function);

auto dof_list = domain.dof_list(&displacement_.space());
displacement_.space().DofsToVDofs(component, dof_list);

bcs_.addEssential(disp_bdr, disp_bdr_coef_, displacement_.space());
bcs_.addEssential(dof_list, component_disp_bdr_coef_, displacement_.space(), component);
}

/**
* @brief Set the displacement essential boundary conditions on a single component
* @brief Shortcut to set all displacements to zero for all time
*
* @param[in] disp_bdr The set of boundary attributes to set the displacement on
* @param[in] disp The vector function containing the set displacement values
* @param[in] component The component to set the displacment on
*
* For the displacement function, the argument is the input position and the output is the value of the component of
* the displacement.
* @param[in] domain Domain to apply the homogeneous boundary condition to
*
* @note This method must be called prior to completeSetup()
*/
void setDisplacementBCs(const std::set<int>& disp_bdr, std::function<double(const mfem::Vector& x)> disp,
int component)
void setFixedBCs(const Domain& domain)
{
// Project the coefficient onto the grid function
component_disp_bdr_coef_ = std::make_shared<mfem::FunctionCoefficient>(disp);

bcs_.addEssential(disp_bdr, component_disp_bdr_coef_, displacement_.space(), component);
auto zero_vector_function = [](tensor<double, dim>, double) { return tensor<double, dim>{}; };
for (int i = 0; i < dim; ++i) {
setDisplacementBCs(zero_vector_function, domain, i);
}
}

/**
Expand Down Expand Up @@ -713,7 +704,7 @@ class SolidMechanics<order, dim, Parameters<parameter_space...>, std::integer_se
void addCustomBoundaryIntegral(DependsOn<active_parameters...>, callable qfunction,
const std::optional<Domain>& optional_domain = std::nullopt)
{
Domain domain = (optional_domain) ? *optional_domain : EntireBoundary(mesh_);
Domain domain = (optional_domain) ? *optional_domain : EntireBoundary<dim>(mesh_);

residual_->AddBoundaryIntegral(Dimension<dim - 1>{}, DependsOn<0, 1, active_parameters + NUM_STATE_VARS...>{},
qfunction, domain);
Expand Down Expand Up @@ -910,7 +901,7 @@ class SolidMechanics<order, dim, Parameters<parameter_space...>, std::integer_se
void setMaterial(DependsOn<active_parameters...>, const MaterialType& material,
qdata_type<StateType> qdata = EmptyQData)
{
setMaterial(DependsOn<active_parameters...>{}, material, EntireDomain(mesh_), qdata);
setMaterial(DependsOn<active_parameters...>{}, material, EntireDomain<dim>(mesh_), qdata);
}

/// @overload
Expand All @@ -925,7 +916,7 @@ class SolidMechanics<order, dim, Parameters<parameter_space...>, std::integer_se
template <typename MaterialType, typename StateType = Empty>
void setMaterial(const MaterialType& material, std::shared_ptr<QuadratureData<StateType>> qdata = EmptyQData)
{
setMaterial(DependsOn<>{}, material, EntireDomain(mesh_), qdata);
setMaterial(DependsOn<>{}, material, EntireDomain<dim>(mesh_), qdata);
}

/**
Expand Down Expand Up @@ -1013,7 +1004,7 @@ class SolidMechanics<order, dim, Parameters<parameter_space...>, std::integer_se
void addBodyForce(DependsOn<active_parameters...>, BodyForceType body_force,
const std::optional<Domain>& optional_domain = std::nullopt)
{
Domain domain = (optional_domain) ? *optional_domain : EntireDomain(mesh_);
Domain domain = (optional_domain) ? *optional_domain : EntireDomain<dim>(mesh_);
residual_->AddDomainIntegral(Dimension<dim>{}, DependsOn<0, 1, active_parameters + NUM_STATE_VARS...>{},
BodyForceIntegrand<BodyForceType>(body_force), domain);
}
Expand Down Expand Up @@ -1052,7 +1043,7 @@ class SolidMechanics<order, dim, Parameters<parameter_space...>, std::integer_se
void setTraction(DependsOn<active_parameters...>, TractionType traction_function,
const std::optional<Domain>& optional_domain = std::nullopt)
{
Domain domain = (optional_domain) ? *optional_domain : EntireBoundary(mesh_);
Domain domain = (optional_domain) ? *optional_domain : EntireBoundary<dim>(mesh_);

residual_->AddBoundaryIntegral(
Dimension<dim - 1>{}, DependsOn<0, 1, active_parameters + NUM_STATE_VARS...>{},
Expand Down Expand Up @@ -1098,7 +1089,7 @@ class SolidMechanics<order, dim, Parameters<parameter_space...>, std::integer_se
void setPressure(DependsOn<active_parameters...>, PressureType pressure_function,
const std::optional<Domain>& optional_domain = std::nullopt)
{
Domain domain = (optional_domain) ? *optional_domain : EntireBoundary(mesh_);
Domain domain = (optional_domain) ? *optional_domain : EntireBoundary<dim>(mesh_);

residual_->AddBoundaryIntegral(
Dimension<dim - 1>{}, DependsOn<0, 1, active_parameters + NUM_STATE_VARS...>{},
Expand Down
Loading