libMesh
amr.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #include "libmesh/coupling_matrix.h"
19 #include "libmesh/dense_matrix.h"
20 #include "libmesh/dense_vector.h"
21 #include "libmesh/dof_map.h"
22 #include "libmesh/elem.h"
23 #include "libmesh/equation_systems.h"
24 #include "libmesh/fe.h"
25 #include "libmesh/gmv_io.h"
26 #include "libmesh/libmesh.h"
27 #include "libmesh/linear_implicit_system.h"
28 #include "libmesh/mesh.h"
29 #include "libmesh/mesh_refinement.h"
30 #include "libmesh/numeric_vector.h"
31 #include "libmesh/quadrature_gauss.h"
32 #include "libmesh/sparse_matrix.h"
33 
34 
35 using namespace libMesh;
36 
37 
38 void assemble(EquationSystems & es,
39  const std::string & system_name);
40 
41 
42 
43 
44 
45 #ifdef LIBMESH_ENABLE_AMR
46 int main (int argc, char ** argv)
47 {
48  LibMeshInit init(argc, argv);
49 
50  if (argc < 4)
51  {
52  std::cout << "Usage: ./prog -d DIM filename" << std::endl;
53  libmesh_terminate();
54  }
55 
56  // Variables to get us started
57  const unsigned char dim = cast_int<unsigned char>(atoi(argv[2]));
58 
59  std::string meshname (argv[3]);
60 
61  // declare a mesh...
62  Mesh mesh(init.comm(), dim);
63 
64  // Read a mesh
65  mesh.read(meshname);
66 
67  GMVIO(mesh).write ("out_0.gmv");
68 
70 
71  MeshRefinement mesh_refinement (mesh);
72 
73  mesh_refinement.refine_and_coarsen_elements ();
74  mesh_refinement.uniformly_refine (2);
75 
76  mesh.print_info();
77 
78 
79  // Set up the equation system(s)
80  EquationSystems es (mesh);
81 
82  LinearImplicitSystem & primary =
83  es.add_system<LinearImplicitSystem>("primary");
84 
85  primary.add_variable ("U", FIRST);
86  primary.add_variable ("V", FIRST);
87 
88  primary.get_dof_map()._dof_coupling->resize(2);
89  (*primary.get_dof_map()._dof_coupling)(0,0) = 1;
90  (*primary.get_dof_map()._dof_coupling)(1,1) = 1;
91 
93 
94  es.init ();
95 
96  es.print_info ();
98 
99  // call the solver.
100  primary.solve ();
101 
102  GMVIO(mesh).write_equation_systems ("out_1.gmv",
103  es);
104 
105 
106 
107  // Refine uniformly
108  mesh_refinement.uniformly_refine (1);
109  es.reinit ();
110 
111  // Write out the projected solution
112  GMVIO(mesh).write_equation_systems ("out_2.gmv",
113  es);
114 
115  // Solve again. Output the refined solution
116  primary.solve ();
117  GMVIO(mesh).write_equation_systems ("out_3.gmv",
118  es);
119 
120  return 0;
121 }
122 #else
123 int main (int, char **)
124 {
125  std::cout << "This libMesh was built with --disable-amr" << std::endl;
126  return 1;
127 }
128 #endif // ENABLE_AMR
129 
130 
131 
132 
133 
134 
136  const std::string & libmesh_dbg_var(system_name))
137 {
138  libmesh_assert_equal_to (system_name, "primary");
139 
140  const MeshBase & mesh = es.get_mesh();
141  const unsigned int dim = mesh.mesh_dimension();
142 
143  // Also use a 3x3x3 quadrature rule (3D). Then tell the FE
144  // about the geometry of the problem and the quadrature rule
145  FEType fe_type (FIRST);
146 
147  std::unique_ptr<FEBase> fe(FEBase::build(dim, fe_type));
148  QGauss qrule(dim, FIFTH);
149 
150  fe->attach_quadrature_rule (&qrule);
151 
152  std::unique_ptr<FEBase> fe_face(FEBase::build(dim, fe_type));
153  QGauss qface(dim-1, FIFTH);
154 
155  fe_face->attach_quadrature_rule(&qface);
156 
157  LinearImplicitSystem & system =
158  es.get_system<LinearImplicitSystem>("primary");
159 
160 
161  // These are references to cell-specific data
162  const std::vector<Real> & JxW_face = fe_face->get_JxW();
163  const std::vector<Real> & JxW = fe->get_JxW();
164  const std::vector<Point> & q_point = fe->get_xyz();
165  const std::vector<std::vector<Real>> & phi = fe->get_phi();
166  const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
167 
168  std::vector<dof_id_type> dof_indices_U;
169  std::vector<dof_id_type> dof_indices_V;
170  const DofMap & dof_map = system.get_dof_map();
171 
176 
177  Real vol=0., area=0.;
178 
179  SparseMatrix<Number> & matrix = system.get_system_matrix();
180 
181  for (const auto & elem : mesh.active_local_element_ptr_range())
182  {
183  // recompute the element-specific data for the current element
184  fe->reinit (elem);
185 
186 
187  //fe->print_info();
188 
189  dof_map.dof_indices(elem, dof_indices_U, 0);
190  dof_map.dof_indices(elem, dof_indices_V, 1);
191 
192  const unsigned int n_phi = cast_int<unsigned int>(phi.size());
193 
194  // zero the element matrix and vector
195  Kuu.resize (n_phi, n_phi);
196 
197  Kvv.resize (n_phi, n_phi);
198 
199  Fu.resize (n_phi);
200  Fv.resize (n_phi);
201 
202  // standard stuff... like in code 1.
203  for (unsigned int gp=0; gp<qrule.n_points(); gp++)
204  {
205  for (unsigned int i=0; i<n_phi; ++i)
206  {
207  // this is tricky. ig is the _global_ dof index corresponding
208  // to the _global_ vertex number elem->node_id(i). Note that
209  // in general these numbers will not be the same (except for
210  // the case of one unknown per node on one subdomain) so
211  // we need to go through the dof_map
212 
213  const Real f = q_point[gp]*q_point[gp];
214  // const Real f = (q_point[gp](0) +
215  // q_point[gp](1) +
216  // q_point[gp](2));
217 
218  // add jac*weight*f*phi to the RHS in position ig
219 
220  Fu(i) += JxW[gp]*f*phi[i][gp];
221  Fv(i) += JxW[gp]*f*phi[i][gp];
222 
223  for (unsigned int j=0; j != n_phi; ++j)
224  {
225 
226  Kuu(i,j) += JxW[gp]*((phi[i][gp])*(phi[j][gp]));
227 
228  Kvv(i,j) += JxW[gp]*((phi[i][gp])*(phi[j][gp]) +
229  1.*((dphi[i][gp])*(dphi[j][gp])));
230  };
231  };
232  vol += JxW[gp];
233  };
234 
235 
236  // You can't compute "area" (perimeter) if you are in 2D
237  if (dim == 3)
238  {
239  for (auto side : elem->side_index_range())
240  if (elem->neighbor_ptr(side) == nullptr)
241  {
242  fe_face->reinit (elem, side);
243 
244  for (const auto & val : JxW_face)
245  area += val;
246  }
247  }
248 
249  // Constrain the DOF indices.
250  dof_map.constrain_element_matrix_and_vector(Kuu, Fu, dof_indices_U);
251  dof_map.constrain_element_matrix_and_vector(Kvv, Fv, dof_indices_V);
252 
253 
254  system.rhs->add_vector(Fu, dof_indices_U);
255  system.rhs->add_vector(Fv, dof_indices_V);
256 
257  matrix.add_matrix(Kuu, dof_indices_U);
258  matrix.add_matrix(Kvv, dof_indices_V);
259  }
260 
261  libMesh::out << "Vol=" << vol << std::endl;
262 
263  if (dim == 3)
264  libMesh::out << "Area=" << area << std::endl;
265 }
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:196
This is the EquationSystems class.
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false, bool skip_detect_interior_parents=false)=0
Interfaces for reading/writing a mesh to/from a file.
virtual void write(const std::string &) override
This method implements writing a mesh to a specified file.
Definition: gmv_io.C:271
void constrain_element_matrix_and_vector(DenseMatrix< Number > &matrix, DenseVector< Number > &rhs, std::vector< dof_id_type > &elem_dofs, bool asymmetric_constraint_rows=true) const
Constrains the element matrix and vector.
Definition: dof_map.h:2498
virtual void write_equation_systems(const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
This method implements writing a mesh with data to a specified file where the data is taken from the ...
Definition: mesh_output.C:31
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition: dof_map.C:2201
unsigned int dim
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and linear solvers ...
void resize(const unsigned int n)
Resize the vector.
Definition: dense_vector.h:396
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i]...
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
void set_refinement_flag(const RefinementState rflag)
Sets the value of the refinement flag for the element.
Definition: elem.h:3232
void resize(const std::size_t n)
Resizes the matrix and initializes all entries to be 0.
NumericVector< Number > * rhs
The system matrix.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:91
The libMesh namespace provides an interface to certain functionality in the library.
This class implements writing meshes in the GMV format.
Definition: gmv_io.h:46
virtual void solve() override
Assembles & solves the linear system A*x=b.
const T_sys & get_system(std::string_view name) const
This is the MeshBase class.
Definition: mesh_base.h:80
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
void assemble(EquationSystems &es, const std::string &system_name)
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
Add the full matrix dm to the SparseMatrix.
Implements (adaptive) mesh refinement algorithms for a MeshBase.
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
Definition: mesh_base.C:1748
virtual void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
void print_dof_constraints(std::ostream &os=libMesh::out, bool print_nonlocal=false) const
Prints (from processor 0) all DoF and Node constraints.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1344
unsigned int n_points() const
Definition: quadrature.h:131
CouplingMatrix * _dof_coupling
Degree of freedom coupling.
Definition: dof_map.h:1741
void attach_assemble_function(void fptr(EquationSystems &es, const std::string &name))
Register a user function to use in assembling the system matrix and RHS.
Definition: system.C:1959
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
int main(int argc, char **argv)
Definition: amr.C:46
OStreamProxy out
bool refine_and_coarsen_elements()
Refines and coarsens user-requested elements.
const MeshBase & get_mesh() const
virtual const Elem & elem_ref(const dof_id_type i) const
Definition: mesh_base.h:778
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
Definition: dense_matrix.h:895
This class implements specific orders of Gauss quadrature.
unsigned int mesh_dimension() const
Definition: mesh_base.C:430
virtual void init()
Initialize all the systems.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
const DofMap & get_dof_map() const
Definition: system.h:2417
const SparseMatrix< Number > & get_system_matrix() const
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.