Smith  0.1
Smith is an implicit thermal structural mechanics simulation code.
dfem_mass_weak_form.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 "smith/smith_config.hpp"
14 
15 #ifdef SMITH_USE_ENZYME
16 
17 #include "mfem.hpp"
18 
20 #include "smith/physics/mesh.hpp"
23 
24 namespace smith {
25 
30 class LumpedMassExplicitNewmark {
31  public:
32  LumpedMassExplicitNewmark(const std::shared_ptr<WeakForm>& weak_form, const std::shared_ptr<WeakForm>& mass_weak_form,
33  std::shared_ptr<BoundaryConditionManager> bc_manager)
34  : weak_form_(weak_form), mass_weak_form_(mass_weak_form), bc_manager_(bc_manager)
35  {
36  }
37 
38  std::tuple<std::vector<FiniteElementState>, double> advanceState(const std::vector<ConstFieldPtr>& states,
39  const std::vector<ConstFieldPtr>& params,
40  double time, double dt)
41  {
42  SLIC_ERROR_ROOT_IF(states.size() != 4, "Expected 4 states: displacement, velocity, acceleration, and coordinates");
43 
44  enum States
45  {
46  DISPLACEMENT,
47  VELOCITY,
48  ACCELERATION,
49  COORDINATES
50  };
51 
52  enum Params
53  {
54  DENSITY
55  };
56 
57  const auto& u = *states[DISPLACEMENT];
58  const auto& v = *states[VELOCITY];
59  const auto& a = *states[ACCELERATION];
60 
61  auto v_pred = v;
62  v_pred.Add(0.5 * dt, a);
63  auto u_pred = u;
64  u_pred.Add(dt, v_pred);
65 
66  if (bc_manager_) {
67  u_pred.SetSubVector(bc_manager_->allEssentialTrueDofs(), 0.0);
68  }
69 
70  TimeInfo time_info(time, dt);
71 
72  auto m_inv = mass_weak_form_->residual(time_info, &u, {states[COORDINATES], params[DENSITY]});
73  m_inv.Reciprocal();
74 
75  std::vector<ConstFieldPtr> pred_states = {&u_pred, &v_pred, &a, states[COORDINATES], params[DENSITY]};
76 
77  auto force_resid = weak_form_->residual(time_info, &u_pred, pred_states);
78 
79  FiniteElementState a_pred(a.space(), "acceleration_pred");
80  auto a_pred_ptr = a_pred.Write();
81  auto m_inv_ptr = m_inv.Read();
82  auto force_resid_ptr = force_resid.Read();
83  mfem::forall_switch(a_pred.UseDevice(), a_pred.Size(),
84  [=] MFEM_HOST_DEVICE(int i) { a_pred_ptr[i] = m_inv_ptr[i] * force_resid_ptr[i]; });
85 
86  v_pred.Add(0.5 * dt, a_pred);
87 
88  return {{u_pred, v_pred, a_pred, *states[COORDINATES]}, time + dt};
89  }
90 
91  private:
92  std::shared_ptr<WeakForm> weak_form_;
93  std::shared_ptr<WeakForm> mass_weak_form_;
94  std::shared_ptr<BoundaryConditionManager> bc_manager_;
95 };
96 
97 template <int MassDim, int SpatialDim>
98 auto create_solid_mass_weak_form(const std::string& physics_name, std::shared_ptr<smith::Mesh>& mesh,
99  const FiniteElementState& lumped_field, const FiniteElementState& density,
100  const mfem::IntegrationRule& ir)
101 {
102  enum FieldIDs
103  {
104  COORDINATES,
105  DENSITY,
106  TEST
107  };
108 
109  auto residual = std::make_shared<DfemWeakForm>(
110  physics_name, mesh, lumped_field.space(),
111  DfemWeakForm::SpacesT{static_cast<mfem::ParFiniteElementSpace*>(mesh->mfemParMesh().GetNodes()->FESpace()),
112  &density.space()});
113 
114  mfem::future::tuple<mfem::future::Gradient<COORDINATES>, mfem::future::Weight, mfem::future::Value<DENSITY>>
115  mass_integral_inputs{};
116  mfem::future::tuple<mfem::future::Value<TEST>> mass_integral_outputs{};
117 
118  residual->addBodyIntegral(
119  mesh->mfemParMesh().attributes,
120  [](mfem::future::tensor<mfem::real_t, SpatialDim, SpatialDim> dX_dxi, mfem::real_t weight, double rho) {
121  auto ones = mfem::future::make_tensor<MassDim>([](int) { return 1.0; });
122  auto J = mfem::future::det(dX_dxi) * weight;
123  return mfem::future::tuple{rho * ones * J};
124  },
125  mass_integral_inputs, mass_integral_outputs, ir, std::index_sequence<>{});
126  return residual;
127 }
128 
129 } // namespace smith
130 
131 #endif
This file contains the declaration of the boundary condition manager class.
Implements the WeakForm interface using dfem. Allows for generic specification of body and boundary i...
This file contains the declaration of structure that manages the MFEM objects that make up the state ...
Smith mesh class which assists in constructing the appropriate parallel mfem meshes and registering a...
Accelerator functionality.
Definition: smith.cpp:36
tuple(T...) -> tuple< T... >
Class template argument deduction rule for tuples.
constexpr SMITH_HOST_DEVICE auto det(const isotropic_tensor< T, m, m > &I)
compute the determinant of an isotropic tensor