Smith  0.1
Smith is an implicit thermal structural mechanics simulation code.
fit.hpp
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 #pragma once
8 
9 #include <fstream>
10 #include <iostream>
11 #include <array>
12 #include <utility>
13 
14 #include "mpi.h"
15 #include "mfem.hpp"
16 
20 #include "smith/numerics/functional/differentiate_wrt.hpp"
22 #include "smith/numerics/functional/function_signature.hpp"
23 #include "smith/numerics/functional/geometry.hpp"
25 
26 namespace smith {
27 
28 namespace detail {
29 
31 template <int dim, typename signature, int... i, typename func, typename... T>
32 FiniteElementState fit(std::integer_sequence<int, i...>, func f, mfem::ParMesh& pmesh, const T&... solution_fields)
33 {
34  // signature looks like return_type(arg0_type, arg1_type);
35  // so this unpacks the return type
36  using output_space = typename FunctionSignature<signature>::return_type;
37 
38  FiniteElementState fitted_field(pmesh, output_space{});
39  fitted_field = 0.0;
40 
41  // mass term
42  Domain whole_domain = EntireDomain(pmesh);
43  smith::Functional<output_space(output_space)> phi_phi(&fitted_field.space(), {&fitted_field.space()});
44  phi_phi.AddDomainIntegral(
45  Dimension<dim>{}, DependsOn<0>{}, [](double /*t*/, auto /*x*/, auto u) { return tuple{get<0>(u), zero{}}; },
46  whole_domain);
47  auto M = get<1>(phi_phi(DifferentiateWRT<0>{}, 0.0 /* t */, fitted_field));
48 
49  // rhs
50  std::array<const mfem::ParFiniteElementSpace*, sizeof...(T)> trial_spaces = {&solution_fields.space()...};
51  smith::Functional<signature> phi_f(&fitted_field.space(), trial_spaces);
52  phi_f.AddDomainIntegral(Dimension<dim>{}, DependsOn<i...>{}, f, whole_domain);
53  mfem::Vector b = phi_f(0.0, solution_fields...);
54 
55  mfem::CGSolver cg(MPI_COMM_WORLD);
56  cg.SetOperator(M);
57  cg.SetRelTol(1e-12);
58  cg.SetMaxIter(500);
59  cg.SetPrintLevel(2);
60  cg.Mult(b, fitted_field);
61 
62  return fitted_field;
63 }
64 
65 } // namespace detail
66 
75 template <int dim, typename signature, int... n, typename func, typename... T>
76 FiniteElementState fit(func f, mfem::ParMesh& pmesh, const T&... solution_fields)
77 {
78  auto iseq = std::make_integer_sequence<int, sizeof...(T)>{};
79  return detail::fit<dim, signature>(iseq, f, pmesh, solution_fields...);
80 }
81 
82 } // namespace smith
Class for encapsulating the critical MFEM components of a primal finite element field.
many of the functions in this file amount to extracting element indices from an mesh_t like
This file contains the declaration of structure that manages the MFEM objects that make up the state ...
Implementation of the quadrature-function-based functional enabling rapid development of FEM formulat...
Accelerator functionality.
Definition: smith.cpp:36
tuple(T...) -> tuple< T... >
Class template argument deduction rule for tuples.
FiniteElementState fit(func f, mfem::ParMesh &pmesh, const T &... solution_fields)
determine field parameters to approximate the output of a user-provided q-function
Definition: fit.hpp:76
Domain EntireDomain(const mesh_t &mesh)
constructs a domain from all the elements in a mesh
Definition: domain.cpp:594
Implementation of the tensor class used by Functional.
Implements a std::tuple-like object that works in CUDA kernels.