Serac  0.1
Serac is an implicit thermal strucural mechanics simulation code.
finite_element_vector.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2024, Lawrence Livermore National Security, LLC and
2 // other Serac Project Developers. See the top-level LICENSE file for
3 // details.
4 //
5 // SPDX-License-Identifier: (BSD-3-Clause)
6 
14 #pragma once
15 
16 #include <optional>
17 
18 #include "mfem.hpp"
19 
22 
23 namespace serac {
24 
28 using GeneralCoefficient = variant<std::shared_ptr<mfem::Coefficient>, std::shared_ptr<mfem::VectorCoefficient>>;
29 
33 enum class ElementType
34 {
35  H1,
36  HCURL,
37  HDIV,
38  L2
39 };
40 
49 class FiniteElementVector : public mfem::HypreParVector {
50 public:
56  FiniteElementVector(const mfem::ParFiniteElementSpace& space, const std::string& name = "");
57 
65  template <typename FunctionSpace>
66  FiniteElementVector(mfem::ParMesh& mesh, FunctionSpace, const std::string& name = "") : mesh_(mesh), name_(name)
67  {
68  const int dim = mesh.Dimension();
69 
70  const auto ordering = mfem::Ordering::byNODES;
71 
72  switch (FunctionSpace::family) {
73  case Family::H1:
74  coll_ = std::make_unique<mfem::H1_FECollection>(FunctionSpace::order, dim);
75  break;
76  case Family::HCURL:
77  coll_ = std::make_unique<mfem::ND_FECollection>(FunctionSpace::order, dim);
78  break;
79  case Family::HDIV:
80  coll_ = std::make_unique<mfem::RT_FECollection>(FunctionSpace::order, dim);
81  break;
82  case Family::L2:
83  // We use GaussLobatto basis functions as this is what is used for the serac::Functional FE kernels
84  coll_ = std::make_unique<mfem::L2_FECollection>(FunctionSpace::order, dim, mfem::BasisType::GaussLobatto);
85  break;
86  default:
87  SLIC_ERROR_ROOT("Unknown finite element space requested.");
88  break;
89  }
90 
91  space_ = std::make_unique<mfem::ParFiniteElementSpace>(&mesh, coll_.get(), FunctionSpace::components, ordering);
92 
93  // Construct a hypre par vector based on the new finite element space
94  HypreParVector new_vector(space_.get());
95 
96  // Move the data from this new hypre vector into this object without doubly allocating the data
97  auto* parallel_vec = new_vector.StealParVector();
98  WrapHypreParVector(parallel_vec);
99 
100  // Initialize the vector to zero
101  HypreParVector::operator=(0.0);
102  }
103 
110  {
111  HypreParVector::operator=(rhs);
112  }
113 
120 
128 
136 
143  FiniteElementVector& operator=(const mfem::HypreParVector& rhs);
144 
151  FiniteElementVector& operator=(const mfem::Vector& rhs);
152 
157  MPI_Comm comm() const { return space_->GetComm(); }
158 
163  mfem::ParMesh& mesh() { return mesh_; }
165  const mfem::ParMesh& mesh() const { return mesh_; }
166 
171  mfem::ParFiniteElementSpace& space() { return *space_; }
173  const mfem::ParFiniteElementSpace& space() const { return *space_; }
174 
179  std::string name() const { return name_; }
180 
189  FiniteElementVector& operator=(const double value);
190 
194  virtual ~FiniteElementVector() {}
195 
196 protected:
200  std::reference_wrapper<mfem::ParMesh> mesh_;
201 
206  std::unique_ptr<mfem::FiniteElementCollection> coll_;
207 
211  std::unique_ptr<mfem::ParFiniteElementSpace> space_;
212 
216  std::string name_ = "";
217 };
218 
227 double avg(const FiniteElementVector& fe_vector);
228 
237 double max(const FiniteElementVector& fe_vector);
238 
247 double min(const FiniteElementVector& fe_vector);
248 
256 double innerProduct(const FiniteElementVector& vec1, const FiniteElementVector& vec2);
257 
258 } // namespace serac
Class for encapsulating the data associated with a vector derived from a MFEM finite element space....
std::string name_
The name of the finite element vector.
std::unique_ptr< mfem::FiniteElementCollection > coll_
Handle to the FiniteElementCollection, which is owned by MFEMSidreDataCollection.
FiniteElementVector(const FiniteElementVector &rhs)
Copy constructor.
MPI_Comm comm() const
Returns the MPI communicator for the state.
mfem::ParFiniteElementSpace & space()
Returns a non-owning reference to the internal FESpace.
virtual ~FiniteElementVector()
Destroy the Finite Element Vector object.
const mfem::ParMesh & mesh() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const mfem::ParFiniteElementSpace & space() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
FiniteElementVector & operator=(const FiniteElementVector &rhs)
Copy assignment.
std::reference_wrapper< mfem::ParMesh > mesh_
A reference to the mesh object on which the field is defined.
FiniteElementVector(mfem::ParMesh &mesh, FunctionSpace, const std::string &name="")
Construct a new Finite Element Vector object given a templated function space.
std::unique_ptr< mfem::ParFiniteElementSpace > space_
Handle to the mfem::ParFiniteElementSpace, which is owned by MFEMSidreDataCollection.
std::string name() const
Returns the name of the FEState (field)
FiniteElementVector(const mfem::ParFiniteElementSpace &space, const std::string &name="")
Minimal constructor for a FiniteElementVector given a finite element space.
mfem::ParMesh & mesh()
Returns a non-owning reference to the internal mesh object.
Implementation of the quadrature-function-based functional enabling rapid development of FEM formulat...
Accelerator functionality.
Definition: serac.cpp:38
double innerProduct(const FiniteElementVector &v1, const FiniteElementVector &v2)
Find the inner prodcut between two finite element vectors across all dofs.
SERAC_HOST_DEVICE auto max(dual< gradient_type > a, double b)
Implementation of max for dual numbers.
Definition: dual.hpp:230
SERAC_HOST_DEVICE auto min(dual< gradient_type > a, double b)
Implementation of min for dual numbers.
Definition: dual.hpp:256
double avg(const FiniteElementVector &fe_vector)
Find the average value of a finite element vector across all dofs.
ElementType
The type of a finite element basis function.
@ H1
Nodal scalar-valued basis functions.
@ HDIV
Raviart-Thomas (continuous normal) vector-valued basis functions.
@ HCURL
Nedelec (continuous tangent) vector-valued basis functions.
Discontinuous elements of order p.
Arbitrary-rank tensor class.
Definition: tensor.hpp:29
This file contains the declaration of a two-element variant type.