Smith  0.1
Smith is an implicit thermal structural mechanics simulation code.
differentiable_physics.hpp
Go to the documentation of this file.
1 // Copyright (c) Lawrence Livermore National Security, LLC and
2 // other Smith Project Developers. See the top-level LICENSE file for
3 // details.
4 //
5 // SPDX-License-Identifier: (BSD-3-Clause)
6 
13 #pragma once
14 
15 #include "gretl/data_store.hpp"
19 #include <vector>
20 #include <map>
21 
22 namespace smith {
23 
24 class Mesh;
25 class WeakForm;
26 class StateAdvancer;
27 class TimestepEstimator;
28 class Reaction;
29 
32  public:
45  DifferentiablePhysics(std::shared_ptr<Mesh> mesh, std::shared_ptr<gretl::DataStore> graph,
46  const FieldState& shape_disp, const std::vector<FieldState>& states,
47  const std::vector<FieldState>& params, std::shared_ptr<StateAdvancer> advancer,
48  std::string physics_name, const std::vector<ReactionInfo>& reaction_infos = {});
51 
53  void resetStates(int cycle = 0, double time = 0.0) override;
54 
56  virtual void resetAdjointStates() override;
57 
59  void completeSetup() override;
60 
62  std::vector<std::string> stateNames() const override;
63 
65  std::vector<std::string> parameterNames() const override;
66 
68  std::vector<std::string> dualNames() const override;
69 
71  const FiniteElementState& state(const std::string& state_name) const override;
72 
74  const FiniteElementDual& dual(const std::string& dual_name) const override;
75 
77  const FiniteElementState& shapeDisplacement() const override;
78 
80  const FiniteElementState& parameter(std::size_t parameter_index) const override;
81 
83  const FiniteElementState& parameter(const std::string& parameter_name) const override;
84 
94  FiniteElementState loadCheckpointedState(const std::string& state_name, int cycle) override;
95 
97  FiniteElementDual loadCheckpointedDual(const std::string& state_name, int cycle) override;
98 
100  void setState(const std::string& state_name, const FiniteElementState& s) override;
101 
103  void setShapeDisplacement(const FiniteElementState& shape_displacement) override;
104 
106  void setParameter(const size_t parameter_index, const FiniteElementState& parameter_state) override;
107 
109  void setAdjointLoad(std::unordered_map<std::string, const smith::FiniteElementDual&> string_to_dual) override;
110 
112  void setDualAdjointBcs(std::unordered_map<std::string, const smith::FiniteElementState&> string_to_bc) override;
113 
115  const FiniteElementState& adjoint(const std::string& adjoint_name) const override;
116 
118  virtual void advanceTimestep(double dt) override;
119 
125  void reverseAdjointTimestep() override;
126 
128  FiniteElementDual computeTimestepSensitivity(size_t parameter_index) override;
129 
132 
134  const std::unordered_map<std::string, const smith::FiniteElementDual&> computeInitialConditionSensitivity()
135  const override;
136 
139  std::vector<FieldState> getInitialFieldStates() const { return initial_field_states_; }
140 
143  std::vector<FieldState> getFieldStates() const { return field_states_; }
144 
146  std::vector<FieldState> getFieldParams() const { return field_params_; }
147 
150  std::vector<FieldState> getFieldStatesAndParamStates() const;
151 
155 
158  std::vector<ReactionState> getReactionStates() const { return reaction_states_; }
159 
162  std::shared_ptr<StateAdvancer> getStateAdvancer() const { return advancer_; }
163 
164  private:
165  void initializeReactionStates();
166 
167  std::shared_ptr<gretl::DataStore> checkpointer_;
168  std::shared_ptr<StateAdvancer> advancer_;
169 
170  std::vector<FieldState> initial_field_states_;
172  std::vector<FieldState> field_states_;
173  std::vector<FieldState> field_params_;
174  std::unique_ptr<FieldState>
175  field_shape_displacement_;
176 
177  std::map<std::string, size_t> state_name_to_field_index_;
178  std::map<std::string, size_t> param_name_to_field_index_;
179  std::vector<std::string> state_names_;
180  std::vector<std::string> param_names_;
181 
182  std::vector<ReactionInfo> reaction_infos_;
183  mutable std::vector<ReactionState> reaction_states_;
184  std::map<std::string, size_t> reaction_name_to_reaction_index_;
185  std::vector<std::string> reaction_names_;
186 
187  std::vector<gretl::Int> milestones_;
191 
192  double time_prev_ =
193  0.0;
194  double dt_prev_ =
195  0.0;
196  int cycle_prev_ =
197  0;
198 };
199 
200 } // namespace smith
The base interface class for a generic PDE solver.
This is the abstract base class for a generic forward solver.
virtual double time() const
Get the current forward-solution time.
const smith::Mesh & mesh() const
Returns a reference to the mesh object.
virtual int cycle() const
Get the current forward-solution cycle iteration number.
BasePhysics implementation that stores differentiable states in gretl for checkpointed reverse solves...
void resetStates(int cycle=0, double time=0.0) override
Base method to reset physics states to the initial time. This does not reset design parameters or sha...
FiniteElementDual loadCheckpointedDual(const std::string &state_name, int cycle) override
Accessor for getting a single named finite element dual solution from the physics modules at a given ...
void reverseAdjointTimestep() override
Reverse one recorded timestep through the gretl graph.
void completeSetup() override
Complete the setup and allocate the necessary data structures.
std::vector< FieldState > getFieldStates() const
Get the current primal state fields.
std::vector< std::string > parameterNames() const override
Get a vector of the finite element state parameter names.
std::vector< FieldState > getInitialFieldStates() const
Get the initial state fields captured before any timesteps were advanced.
void setShapeDisplacement(const FiniteElementState &shape_displacement) override
Set the current shape displacement for the underlying mesh.
std::vector< FieldState > getFieldParams() const
Get all the parameter FieldStates.
FieldState getShapeDispFieldState() const
Get the tracked shape displacement field.
const FiniteElementState & adjoint(const std::string &adjoint_name) const override
Accessor for getting named finite element state adjoint solution from the physics modules.
void setDualAdjointBcs(std::unordered_map< std::string, const smith::FiniteElementState & > string_to_bc) override
Set the dual loads (dirichlet values) for the adjoint reverse timestep solve This must be called afte...
std::vector< std::string > dualNames() const override
Get a vector of the finite element state dual (reaction) solution names.
FiniteElementDual computeTimestepSensitivity(size_t parameter_index) override
Compute the implicit sensitivity of the quantity of interest used in defining the adjoint load with r...
virtual void resetAdjointStates() override
Base method to reset physics states back to the end of time to start adjoint calculations again....
DifferentiablePhysics(std::shared_ptr< Mesh > mesh, std::shared_ptr< gretl::DataStore > graph, const FieldState &shape_disp, const std::vector< FieldState > &states, const std::vector< FieldState > &params, std::shared_ptr< StateAdvancer > advancer, std::string physics_name, const std::vector< ReactionInfo > &reaction_infos={})
Construct a differentiable physics wrapper around a state advancer and its tracked fields.
void setAdjointLoad(std::unordered_map< std::string, const smith::FiniteElementDual & > string_to_dual) override
Set the loads for the adjoint reverse timestep solve.
const FiniteElementDual & computeTimestepShapeSensitivity() override
Compute the implicit sensitivity of the quantity of interest used in defining the adjoint load with r...
const FiniteElementState & parameter(std::size_t parameter_index) const override
Accessor for getting indexed finite element state parameter fields from the physics modules.
void setState(const std::string &state_name, const FiniteElementState &s) override
Set the primal solution field values of the underlying physics solver.
const FiniteElementState & shapeDisplacement() const override
Accessor for getting the shape displacement field from the physics modules.
FiniteElementState loadCheckpointedState(const std::string &state_name, int cycle) override
Return a state for a stored checkpoint cycle.
std::vector< FieldState > getFieldStatesAndParamStates() const
Get the tracked state fields followed by the tracked parameter fields.
virtual void advanceTimestep(double dt) override
Advance the state variables according to the chosen time integrator.
const FiniteElementState & state(const std::string &state_name) const override
Accessor for getting named finite element state primal solution from the physics modules.
std::shared_ptr< StateAdvancer > getStateAdvancer() const
Get the state advancer used for forward solves.
std::vector< ReactionState > getReactionStates() const
Get the current differentiable reaction outputs.
std::vector< std::string > stateNames() const override
Get a vector of the finite element state primal solution names.
const FiniteElementDual & dual(const std::string &dual_name) const override
Accessor for getting named finite element state dual (reaction) solution from the physics modules.
const std::unordered_map< std::string, const smith::FiniteElementDual & > computeInitialConditionSensitivity() const override
Compute the implicit sensitivity of the quantity of interest with respect to the initial condition fi...
void setParameter(const size_t parameter_index, const FiniteElementState &parameter_state) override
Deep copy a parameter field into the internally-owned parameter used for simulations.
Class for encapsulating the dual vector space of a finite element space (i.e. the space of linear for...
Class for encapsulating the critical MFEM components of a primal finite element field.
Accelerator functionality.
Definition: smith.cpp:36
gretl::State< FEFieldPtr, FEDualPtr > FieldState
typedef
Definition: field_state.hpp:22
Defines the SystemBase struct for common system functionality.