Smith  0.1
Smith is an implicit thermal structural mechanics simulation code.
evaluate_objective.cpp
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 
7 #include "gretl/data_store.hpp"
9 
10 namespace smith {
11 
12 DoubleState evaluateObjective(const ScalarObjective& objective, const FieldState& shape_disp,
13  const std::vector<FieldState>& inputs, const TimeInfo& time_info)
14 {
15  const ScalarObjective* objective_ptr = &objective;
16  std::vector<gretl::StateBase> all_states{shape_disp};
17  all_states.insert(all_states.end(), inputs.begin(), inputs.end());
18  DoubleState value =
19  shape_disp.create_state<double, double>(all_states, gretl::defaultInitializeZeroDual<double, double>{});
20 
21  value.set_eval(
22  [time_info, objective_ptr](const gretl::UpstreamStates& upstreams, gretl::DownstreamState& downstream) {
23  auto shape = upstreams[0].get<FEFieldPtr>();
24  std::vector<FEFieldPtr> Inputs;
25  for (size_t i = 1; i < upstreams.size(); ++i) {
26  Inputs.push_back(upstreams[i].get<FEFieldPtr>());
27  }
28  downstream.set<double, double>(objective_ptr->evaluate(time_info, shape.get(), getConstFieldPointers(Inputs)));
29  });
30 
31  value.set_vjp([time_info, objective_ptr](gretl::UpstreamStates& upstreams, const gretl::DownstreamState& downstream) {
32  auto shape = upstreams[0].get<FEFieldPtr>();
33  std::vector<FEFieldPtr> fields;
34  for (size_t i = 1; i < upstreams.size(); ++i) {
35  fields.push_back(upstreams[i].get<FEFieldPtr>());
36  }
37 
38  const double downstream_dual = downstream.get_dual<double, double>();
39 
40  upstreams[0].get_dual<FEDualPtr, FEFieldPtr>()->Add(
41  downstream_dual,
42  objective_ptr->mesh_coordinate_gradient(time_info, shape.get(), getConstFieldPointers(fields)));
43  for (size_t i = 1; i < upstreams.size(); ++i) {
44  upstreams[i].get_dual<FEDualPtr, FEFieldPtr>()->Add(
45  downstream_dual, objective_ptr->gradient(time_info, shape.get(), getConstFieldPointers(fields), i - 1));
46  }
47  });
48 
49  return value.finalize();
50 }
51 
52 } // namespace smith
Abstract residual class.
virtual double evaluate(TimeInfo time_info, ConstFieldPtr shape_disp, const std::vector< ConstFieldPtr > &fields) const =0
Virtual interface for computing the scale value for the objective/constrant, given a vector of smith:...
virtual mfem::Vector mesh_coordinate_gradient(TimeInfo time_info, ConstFieldPtr shape_disp, const std::vector< ConstFieldPtr > &fields) const =0
Virtual interface for computing objective gradient with respect to the mesh coordinates.
virtual mfem::Vector gradient(TimeInfo time_info, ConstFieldPtr shape_disp, const std::vector< ConstFieldPtr > &fields, size_t field_ordinal) const =0
Virtual interface for computing objective gradient from a vector of smith::FiniteElementState*.
Methods for evaluating objective functions and tracking these operations on the gretl graph with a cu...
Accelerator functionality.
Definition: smith.cpp:36
gretl::State< double, double > DoubleState
typedef
Definition: field_state.hpp:25
std::shared_ptr< FiniteElementState > FEFieldPtr
typedef
Definition: field_state.hpp:20
gretl::State< FEFieldPtr, FEDualPtr > FieldState
typedef
Definition: field_state.hpp:22
DoubleState evaluateObjective(const ScalarObjective &objective, const FieldState &shape_disp, const std::vector< FieldState > &inputs, const TimeInfo &time_info)
Evaluates a DoubleState using a provided ScalarObjective reference, and the input arguments to that o...
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.
struct storing time and timestep information
Definition: common.hpp:18