23 bool checkpoint_to_disk)
24 : name_(physics_name),
26 comm_(mesh_->getComm()),
27 shape_displacement_(mesh_->newShapeDisplacement()),
28 shape_displacement_dual_(mesh_->newShapeDisplacementDual()),
29 bcs_(mesh_->mfemParMesh()),
30 checkpoint_to_disk_(checkpoint_to_disk)
78 SLIC_ERROR_ROOT_IF(parameter_index >=
parameters_.size(),
79 std::format(
"Parameter '{}' requested when only '{}' parameters exist in physics module '{}'",
83 std::format(
"Mesh of parameter '{}' is not the same as the physics mesh", parameter_index));
86 parameter_state.
space().GetTrueVSize() !=
parameters_[parameter_index].state->space().GetTrueVSize(),
87 std::format(
"Physics module parameter '{}' has size '{}' while given state has size '{}'. The finite element "
88 "spaces are inconsistent.",
89 parameter_index,
parameters_[parameter_index].state->space().GetTrueVSize(),
90 parameter_state.
space().GetTrueVSize()));
91 *
parameters_[parameter_index].state = parameter_state;
105 SLIC_ERROR_ROOT(std::format(
"Parameter sensitivities not enabled in physics module {}",
name_));
111 SLIC_ERROR_ROOT(std::format(
"Shape sensitivities not enabled in physics module {}",
name_));
117 std::string output_name =
name_.empty() ?
"default" :
name_;
120 std::make_unique<mfem::ParaViewDataCollection>(output_name,
const_cast<mfem::ParMesh*
>(&
mfemParMesh()));
138 std::make_unique<mfem::ParGridFunction>(&
const_cast<mfem::ParFiniteElementSpace&
>(shape_sensitivity_space));
143 std::make_unique<mfem::ParGridFunction>(
const_cast<mfem::ParFiniteElementSpace*
>(&
dual->space()));
149 int max_order_in_fields =
mfemParMesh().GetNodalFESpace()->GetMaxElementOrder();
151 for (
const auto& [_, field] :
paraview_dc_->GetFieldMap()) {
152 max_order_in_fields =
std::max(field->FESpace()->GetMaxElementOrder(), max_order_in_fields);
208 if (paraview_output_dir) {
242 const std::string summary_group_name =
"smith_summary";
243 axom::sidre::Group* sidre_root = datastore.getRoot();
245 sidre_root->hasGroup(summary_group_name),
246 std::format(
"Sidre Group '{0}' cannot exist when initializeSummary is called", summary_group_name));
247 axom::sidre::Group* summary_group = sidre_root->createGroup(summary_group_name);
250 summary_group->createViewScalar(
"mpi_rank_count", count);
253 axom::sidre::Group* curves_group = summary_group->createGroup(
"curves");
256 axom::IndexType array_size =
static_cast<axom::IndexType
>(ceil(t_final / dt));
259 axom::sidre::View* t_array_view = curves_group->createView(
"t");
260 axom::sidre::Array<double> ts(t_array_view, 0, array_size);
264 axom::sidre::Group* state_group = curves_group->createGroup(
state->
name());
267 for (std::string stat_name : {
"l1norms",
"l2norms",
"linfnorms",
"avgs",
"mins",
"maxs"}) {
268 axom::sidre::View* curr_array_view = state_group->createView(stat_name);
269 axom::sidre::Array<double> array(curr_array_view, 0, array_size);
279 axom::sidre::Group* curves_group =
nullptr;
282 axom::sidre::Group* sidre_root = datastore.getRoot();
283 const std::string curves_group_name =
"smith_summary/curves";
284 SLIC_ERROR_IF(!sidre_root->hasGroup(curves_group_name),
285 std::format(
"Sidre Group '{0}' did not exist when saveCurves was called", curves_group_name));
286 curves_group = sidre_root->getGroup(curves_group_name);
289 axom::sidre::Array<double> ts(curves_group->getView(
"t"));
294 double l1norm_value, l2norm_value, linfnorm_value, avg_value, max_value, min_value;
300 linfnorm_value =
norm(*
state, mfem::infinity());
308 axom::sidre::Group* state_group = curves_group->getGroup(
state->
name());
311 axom::sidre::View* l1norms_view = state_group->getView(
"l1norms");
312 axom::sidre::Array<double> l1norms(l1norms_view);
313 l1norms.push_back(l1norm_value);
315 axom::sidre::View* l2norms_view = state_group->getView(
"l2norms");
316 axom::sidre::Array<double> l2norms(l2norms_view);
317 l2norms.push_back(l2norm_value);
319 axom::sidre::View* linfnorms_view = state_group->getView(
"linfnorms");
320 axom::sidre::Array<double> linfnorms(linfnorms_view);
321 linfnorms.push_back(linfnorm_value);
323 axom::sidre::View* avgs_view = state_group->getView(
"avgs");
324 axom::sidre::Array<double> avgs(avgs_view);
325 avgs.push_back(avg_value);
327 axom::sidre::View* maxs_view = state_group->getView(
"maxs");
328 axom::sidre::Array<double> maxs(maxs_view);
329 maxs.push_back(max_value);
331 axom::sidre::View* mins_view = state_group->getView(
"mins");
332 axom::sidre::Array<double> mins(mins_view);
333 mins.push_back(min_value);
350 std::format(
"Requested state name {} does not exist in physics module {}.", state_name,
name_));
356 std::format(
"Requested state name {} does not exist in physics module {}.", state_name,
name_));
363 std::vector<FiniteElementState*> previous_states_ptrs;
372 std::unordered_map<std::string, FiniteElementState> previous_states_map;
377 previous_states_map.emplace(state_name,
state(state_name));
379 return previous_states_map;
382 previous_states_map.emplace(state_name,
checkpoint_states_.at(state_name)[
static_cast<size_t>(cycle_to_load)]);
386 return previous_states_map;
391 SLIC_ERROR_ROOT_IF(
cycle < 0, std::format(
"Negative cycle number requested for physics module {}.",
name_));
393 std::format(
"Timestep for cycle {} requested, but physics module has only reached cycle {}.",
399 std::string addPrefix(
const std::string& prefix,
const std::string& target)
401 if (prefix.empty()) {
404 return prefix +
"_" + target;
407 std::string removePrefix(
const std::string& prefix,
const std::string& target)
409 std::string modified_target{target};
411 if (!prefix.empty()) {
413 auto index = modified_target.find(prefix +
"_");
416 modified_target.erase(0, prefix.size() + 1);
419 return modified_target;
This file contains the interface used for retrieving information about how the driver is configured.
The base interface class for a generic PDE solver.
virtual const FiniteElementState & parameter(const std::string ¶meter_name) const
Accessor for getting named finite element state parameter fields from the physics modules.
std::string name_
Name of the physics module.
virtual int maxCycle() const
The maximum cycle (timestep iteration number) reached by the forward solver.
virtual const std::vector< double > & timesteps() const
Get a vector of the timestep sizes (i.e. s) taken by the forward solver.
virtual void setParameter(const size_t parameter_index, const FiniteElementState ¶meter_state)
Deep copy a parameter field into the internally-owned parameter used for simulations.
std::shared_ptr< smith::Mesh > mesh_
The primary mesh.
void UpdateParaviewDataCollection(const std::string ¶view_output_dir) const
Update the paraview states, duals, parameters, and metadata (cycle, time) in preparation for output.
void CreateParaviewDataCollection() const
Create a paraview data collection for the physics package if requested.
std::vector< const smith::FiniteElementState * > states_
List of finite element primal states associated with this physics module.
double min_time_
The time the forward solver was initialized to.
int cycle_
Current cycle (forward pass time iteration count)
double ode_time_point_
The value of time at which the ODE solver wants to evaluate the residual.
std::unordered_map< std::string, FiniteElementState > getCheckpointedStates(int cycle)
Accessor for getting all of the primal solutions from the physics modules at a given checkpointed cyc...
std::vector< const smith::FiniteElementDual * > duals_
List of finite element duals associated with this physics module.
virtual void initializeSummary(axom::sidre::DataStore &datastore, const double t_final, const double dt) const
Initializes the Sidre structure for simulation summary data.
std::string name() const
Return the name of the physics.
std::vector< double > timesteps_
A vector of the timestep sizes (i.e. ) taken by the forward solver.
std::vector< ParameterInfo > parameters_
A vector of the parameters associated with this physics module.
virtual void setShapeDisplacement(const FiniteElementState &shape_displacement)
Set the current shape displacement for the underlying mesh.
virtual double time() const
Get the current forward-solution time.
std::optional< int > cached_checkpoint_cycle_
An optional int for disk-based checkpointing containing the cycle number of the last retrieved checkp...
int max_cycle_
The maximum cycle (forward pass iteration count) reached by the forward solver.
smith::FiniteElementDual shape_displacement_dual_
The shape displacement field sensitivity.
BasePhysics(std::string physics_name, std::shared_ptr< smith::Mesh > mesh, int cycle=0, double time=0.0, bool checkpoint_to_disk=false)
Empty constructor.
virtual void outputStateToDisk(std::optional< std::string > paraview_output_dir={}) const
Output the current state of the PDE fields in Sidre format and optionally in Paraview format if parav...
MPI_Comm comm_
The MPI communicator.
virtual double maxTime() const
Get the maximum time reached by the forward solver.
std::unordered_map< std::string, std::vector< smith::FiniteElementState > > checkpoint_states_
A map containing optionally in-memory checkpointed primal states for transient adjoint solvers.
std::unique_ptr< mfem::ParaViewDataCollection > paraview_dc_
DataCollection pointer for optional paraview output.
virtual const FiniteElementState & shapeDisplacement() const
Accessor for getting the shape displacement field from the physics modules.
const smith::Mesh & mesh() const
Returns a reference to the mesh object.
double dt_
Current time step.
double time_
Current time for the forward pass.
virtual int cycle() const
Get the current forward-solution cycle iteration number.
virtual const FiniteElementDual & computeTimestepShapeSensitivity()
Compute the implicit sensitivity of the quantity of interest used in defining the adjoint load with r...
std::unique_ptr< mfem::ParGridFunction > shape_sensitivity_grid_function_
A optional view of the shape sensitivity in grid function form for paraview output.
std::unordered_map< std::string, smith::FiniteElementState > cached_checkpoint_states_
A container relating a checkpointed cycle and the associated finite element state fields.
std::unordered_map< std::string, std::unique_ptr< mfem::ParGridFunction > > paraview_dual_grid_functions_
A optional map of the dual names and duals in grid function form for paraview output.
virtual int minCycle() const
Get the initial cycle (timestep iteration number) used by the forward solver.
virtual double getCheckpointedTimestep(int cycle) const
Get a timestep increment which has been previously checkpointed at the give cycle.
double max_time_
The maximum time reached for the forward solver.
virtual FiniteElementDual computeTimestepSensitivity(size_t parameter_index)
Compute the implicit sensitivity of the quantity of interest used in defining the adjoint load with r...
const FiniteElementDual & shapeDisplacementSensitivity() const
Internally used accessor for getting the shape displacement sensitivity from the physics modules.
virtual void saveSummary(axom::sidre::DataStore &datastore, const double t) const
Saves the summary data to the Sidre Datastore.
bool checkpoint_to_disk_
A flag denoting whether to save the state to disk or memory as needed for dynamic adjoint solves.
virtual const FiniteElementDual & dual(const std::string &dual_name) const
Accessor for getting named finite element state dual (reaction) solution from the physics modules.
virtual const FiniteElementState & state(const std::string &state_name) const =0
Accessor for getting named finite element state primal solution from the physics modules.
int min_cycle_
The cycle the forward solver was initialized to.
const mfem::ParMesh & mfemParMesh() const
Returns a reference to the mfem ParMesh object.
void initializeBasePhysicsStates(int cycle, double time)
Protected, non-virtual method to reset physics states to zero. This does not reset design parameters ...
void loadCheckpointedStatesFromDisk(int cycle)
load checkpointed states from disk into states array
virtual FiniteElementState loadCheckpointedState(const std::string &state_name, int cycle)
Accessor for getting a single named finite element state primal solution from the physics modules at ...
virtual double minTime() const
Get the initial time used by the forward solver.
smith::FiniteElementState shape_displacement_
The shape displacement field.
virtual std::vector< std::string > stateNames() const =0
Get a vector of the finite element state primal solution names.
Class for encapsulating the dual vector space of a finite element space (i.e. the space of linear for...
mfem::ParLinearForm & linearForm() const
Construct a linear form from the finite element dual true vector.
Class for encapsulating the critical MFEM components of a primal finite element field.
mfem::ParGridFunction & gridFunction() const
Construct a grid function from the finite element state true vector.
mfem::ParFiniteElementSpace & space()
Returns a non-owning reference to the internal FESpace.
std::string name() const
Returns the name of the FEState (field)
mfem::ParMesh & mesh()
Returns a non-owning reference to the internal mesh object.
Helper class for constructing a mesh consistent with Smith.
static void updateDual(const FiniteElementDual &dual)
Updates the StateManager-owned grid function using the values from a given FiniteElementDual.
static void save(const double t, const int cycle, const std::string &mesh_tag)
Updates the Conduit Blueprint state in the datastore and saves to a file.
static void updateState(const FiniteElementState &state)
Updates the StateManager-owned grid function using the values from a given FiniteElementState.
static void loadCheckpointedStates(int cycle_to_load, std::vector< FiniteElementState * > states_to_load)
loads the finite element states from a previously checkpointed cycle
This file contains the declaration of structure that manages the MFEM objects that make up the state ...
This file contains the declaration of structure that manages vectors derived from an MFEM finite elem...
This file contains the all the necessary functions and macros required for logging as well as a helpe...
Smith mesh class which assists in constructing the appropriate parallel mfem meshes and registering a...
Accelerator functionality.
std::pair< int, int > getMPIInfo(MPI_Comm comm)
Get MPI Info.
constexpr SMITH_HOST_DEVICE auto norm(const isotropic_tensor< T, m, m > &I)
compute the Frobenius norm (sqrt(tr(dot(transpose(I), I)))) of an isotropic tensor
double avg(const FiniteElementVector &fe_vector)
Find the average value of a finite element vector across all dofs.
SMITH_HOST_DEVICE auto max(dual< gradient_type > a, double b)
Implementation of max for dual numbers.
SMITH_HOST_DEVICE auto min(dual< gradient_type > a, double b)
Implementation of min for dual numbers.
This file contains the declaration of the StateManager class.
Dual number struct (value plus gradient)