Smith  0.1
Smith is an implicit thermal structural mechanics simulation code.
field_state.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 
11 #pragma once
12 
13 #include "gretl/data_store.hpp"
14 #include "gretl/state.hpp"
15 #include "gretl/create_state.hpp"
17 
18 namespace smith {
19 
20 using FEFieldPtr = std::shared_ptr<FiniteElementState>;
21 using FEDualPtr = std::shared_ptr<FiniteElementDual>;
22 using FieldState = gretl::State<FEFieldPtr, FEDualPtr>;
23 using ReactionState = gretl::State<FEDualPtr, FEFieldPtr>;
24 using FieldVecState = gretl::State<std::vector<FEFieldPtr>, std::vector<FEDualPtr>>;
25 using DoubleState = gretl::State<double, double>;
26 
31  auto operator()(const smith::FEFieldPtr& f) const
32  {
33  return std::make_shared<smith::FiniteElementDual>(f->space(), f->name() + "_dual");
34  };
35 };
36 
41  auto operator()(const smith::FEDualPtr& f) const
42  {
43  return std::make_shared<smith::FiniteElementState>(f->space(), f->name() + "_undual");
44  };
45 };
46 
48 inline FieldState createFieldState(gretl::DataStore& dataStore, const smith::FEFieldPtr& s)
49 {
50  return dataStore.create_state<smith::FEFieldPtr, smith::FEDualPtr>(s, zero_dual_from_state());
51 }
52 
54 template <typename function_space>
55 FieldState createFieldState(gretl::DataStore& dataStore, function_space space, const std::string& name,
56  const std::string& mesh_tag)
57 {
58  auto f = std::make_shared<FiniteElementState>(StateManager::newState(space, name, mesh_tag));
59  return createFieldState(dataStore, f);
60 }
61 
63 inline ReactionState createReactionState(gretl::DataStore& dataStore, const smith::FEDualPtr& s)
64 {
65  return dataStore.create_state<smith::FEDualPtr, smith::FEFieldPtr>(s, zero_state_from_dual());
66 }
67 
69 template <typename function_space>
70 ReactionState createReactionState(gretl::DataStore& dataStore, function_space space, const std::string& name,
71  const std::string& mesh_tag)
72 {
73  auto f = std::make_shared<FiniteElementDual>(StateManager::newDual(space, name, mesh_tag));
74  return createReactionState(dataStore, f);
75 }
76 
78 FieldState square(const FieldState& state);
79 
81 gretl::State<double> innerProduct(const FieldState& a, const FieldState& b);
82 
84 gretl::State<double> innerProduct(const ReactionState& a, const ReactionState& b);
85 
87 FieldState axpby(double a, const FieldState& x, double b, const FieldState& y);
88 
91 
93 inline FieldState weighted_average(const FieldState& a, const FieldState& b, double weight)
94 {
95  return axpby(weight, a, 1.0 - weight, b);
96 }
97 
99 FieldState axpby(const gretl::State<double>& a, const FieldState& x, const gretl::State<double>& b,
100  const FieldState& y);
101 
105  FieldStateWeightedSum(const std::vector<double>& w, const std::vector<FieldState>& f)
106  : weights_(w), weighted_fields_(f)
107  {
108  }
109 
111  FieldStateWeightedSum(const std::vector<gretl::State<double>>& w, const std::vector<FieldState>& f,
112  double initial_scaling)
115  differentiable_scale_factors_(w.size(), initial_scaling)
116  {
117  }
118 
121 
124 
127 
130 
132  FieldStateWeightedSum& operator*=(double weight);
133 
136 
137  std::vector<double> weights_;
138  std::vector<FieldState> weighted_fields_;
139  std::vector<gretl::State<double>> differentiable_weights_;
140  std::vector<FieldState> differentiably_weighted_fields_;
141  std::vector<double> differentiable_scale_factors_;
142 
144  operator FieldState() const;
145 };
146 
148 FieldStateWeightedSum operator*(double a, const FieldState& b);
149 
151 FieldStateWeightedSum operator*(const FieldState& b, double a);
152 
156 
160 
162 FieldStateWeightedSum operator*(const gretl::State<double>& a, const FieldState& b);
163 
165 FieldStateWeightedSum operator*(const FieldState& b, const gretl::State<double>& a);
166 
169 
172 
175 
178 
181 
184 
187 
190 
191 // TODO:
192 // Add multplication of WeightedSum by a differentiable State<double> for improve efficiency
193 // Consider adding divide operators, maybe component-wise things as well
194 
195 // Utilty functions for easily getting spaces from FieldStates
196 
198 inline mfem::ParFiniteElementSpace& space(FieldState field) { return field.get()->space(); }
199 
201 inline std::vector<const mfem::ParFiniteElementSpace*> spaces(const std::vector<FieldState>& states,
202  const std::vector<FieldState>& params = {})
203 {
204  std::vector<const mfem::ParFiniteElementSpace*> spaces;
205  for (auto s : states) {
206  spaces.push_back(&s.get()->space());
207  }
208  for (auto s : params) {
209  spaces.push_back(&s.get()->space());
210  }
211  return spaces;
212 };
213 
215 inline std::vector<FiniteElementState*> getFieldPointers(std::vector<FieldState>& states,
216  std::vector<FieldState> params = {})
217 {
218  std::vector<FiniteElementState*> pointers;
219  for (auto& t : states) {
220  pointers.push_back(t.get().get());
221  }
222  for (auto& p : params) {
223  pointers.push_back(p.get().get());
224  }
225  return pointers;
226 }
227 
229 inline std::vector<const FiniteElementState*> getConstFieldPointers(const std::vector<FieldState>& states,
230  const std::vector<FieldState>& params = {})
231 {
232  std::vector<const FiniteElementState*> pointers;
233  for (auto& t : states) {
234  pointers.push_back(t.get().get());
235  }
236  for (auto& p : params) {
237  pointers.push_back(p.get().get());
238  }
239  return pointers;
240 }
241 
242 } // namespace smith
static FiniteElementDual newDual(FunctionSpace space, const std::string &dual_name, const std::string &mesh_tag)
Factory method for creating a new FEDual object.
static FiniteElementState newState(FunctionSpace space, const std::string &state_name, const std::string &mesh_tag)
Factory method for creating a new FEState object.
Accelerator functionality.
Definition: smith.cpp:36
gretl::State< FEDualPtr, FEFieldPtr > ReactionState
typedef
Definition: field_state.hpp:23
std::vector< const mfem::ParFiniteElementSpace * > spaces(const std::vector< FieldState > &states, const std::vector< FieldState > &params={})
Get the spaces from the primal fields of a vector of field states.
gretl::State< double, double > DoubleState
typedef
Definition: field_state.hpp:25
std::shared_ptr< FiniteElementState > FEFieldPtr
typedef
Definition: field_state.hpp:20
gretl::State< std::vector< FEFieldPtr >, std::vector< FEDualPtr > > FieldVecState
typedef
Definition: field_state.hpp:24
FieldStateWeightedSum operator+(const FieldState &x, const FieldState &y)
add two FieldState
std::vector< FiniteElementState * > getFieldPointers(std::vector< FieldState > &states, std::vector< FieldState > params={})
Get a vector of FieldPtr or DualFieldPtr from a vector of FieldState.
gretl::State< FEFieldPtr, FEDualPtr > FieldState
typedef
Definition: field_state.hpp:22
FieldState createFieldState(gretl::DataStore &dataStore, const smith::FEFieldPtr &s)
initialize on the gretl::DataStore a FieldState with values from s
Definition: field_state.hpp:48
constexpr SMITH_HOST_DEVICE int size(const tensor< T, n... > &)
returns the total number of stored values in a tensor
Definition: tensor.hpp:1939
ReactionState createReactionState(gretl::DataStore &dataStore, const smith::FEDualPtr &s)
initialize on the gretl::DataStore a ReactionState with values from s
Definition: field_state.hpp:63
FieldState axpby(double a, const FieldState &x, double b, const FieldState &y)
gretl-function to compute a*x + b*y
Definition: field_state.cpp:53
std::shared_ptr< FiniteElementDual > FEDualPtr
typedef
Definition: field_state.hpp:21
std::vector< const FiniteElementState * > getConstFieldPointers(const std::vector< FieldState > &states, const std::vector< FieldState > &params={})
Get a vector of ConstFieldPtr or ConstDualFieldPtr from a vector of FieldState.
FieldState square(const FieldState &state)
gretl-function to square (x^2) every component of the Field
Definition: field_state.cpp:11
gretl::State< double > innerProduct(const FieldState &a, const FieldState &b)
gretl-function to compute the inner product (vector l2-norm) of a and b
Definition: field_state.cpp:29
FieldStateWeightedSum operator*(double a, const FieldState &b)
multiply scalar by a FieldState to get a temporary FieldStateWeightedSum which can cast back to a Fie...
FieldState zeroCopy(const FieldState &x)
gretl-function to make a deep-copy of a FieldState and initialize it to 0.
Definition: field_state.cpp:76
mfem::ParFiniteElementSpace & space(FieldState field)
Get the space from the primal field of a field states.
FieldStateWeightedSum operator-(const FieldState &x, const FieldState &y)
subtract two FieldState
FieldState weighted_average(const FieldState &a, const FieldState &b, double weight)
gretl-function to compute the weighted average a * weight + b * (1-weight)
Definition: field_state.hpp:93
This file contains the declaration of the StateManager class.
temporary object to register the multiplication of a gretl::State<double> with a FieldState....
FieldStateWeightedSum operator-() const
negate
FieldStateWeightedSum & operator*=(double weight)
mulitply by a fixed scalar
FieldStateWeightedSum(const std::vector< double > &w, const std::vector< FieldState > &f)
construct from double weights, and fields
std::vector< double > weights_
non-differentiable weights
FieldStateWeightedSum & operator+=(const FieldStateWeightedSum &b)
add another weighted sum in place
FieldStateWeightedSum & operator=(const FieldStateWeightedSum &old)=default
default assignment
std::vector< FieldState > weighted_fields_
fields to weight by non-differentiable weights
FieldStateWeightedSum(const FieldStateWeightedSum &old)=default
default copy
FieldStateWeightedSum(const std::vector< gretl::State< double >> &w, const std::vector< FieldState > &f, double initial_scaling)
construct from State<double> weights, and fields
std::vector< double > differentiable_scale_factors_
flag differentiable weights to be negated
std::vector< FieldState > differentiably_weighted_fields_
fields to weight by differentiable weights
FieldStateWeightedSum & operator-=(const FieldStateWeightedSum &b)
subtract another weighted sum in place
std::vector< gretl::State< double > > differentiable_weights_
differentiable weights
functor which takes a std::shared_ptr<FiniteElementState>, and returns a zero-valued std::shared_ptr<...
Definition: field_state.hpp:29
auto operator()(const smith::FEFieldPtr &f) const
functor operator
Definition: field_state.hpp:31
functor which takes a std::shared_ptr<FiniteElementDual>, and returns a zero-valued std::shared_ptr<F...
Definition: field_state.hpp:39
auto operator()(const smith::FEDualPtr &f) const
functor operator
Definition: field_state.hpp:41