libMesh
meshdiff.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 
19 // Open the two solution files named on standard input, find all
20 // variables they have in common, and output the Hilbert norms of the
21 // differences between them.
22 
23 #include "libmesh/libmesh.h"
24 #include "libmesh/mesh.h"
25 #include "libmesh/equation_systems.h"
26 #include "libmesh/exact_solution.h"
27 #include "libmesh/mesh_function.h"
28 #include "libmesh/namebased_io.h"
29 #include "libmesh/numeric_vector.h"
30 #include "libmesh/enum_norm_type.h"
31 #include "libmesh/int_range.h"
32 
33 using namespace libMesh;
34 
35 unsigned char dim = 2; // This gets overridden by most mesh formats
36 
37 int main(int argc, char ** argv)
38 {
39  LibMeshInit init(argc, argv);
40 
41  Mesh coarse_mesh(init.comm(), dim), fine_mesh(init.comm(), dim);
42  EquationSystems coarse_es(coarse_mesh), fine_es(fine_mesh);
43 
44  libMesh::out << "Usage: " << argv[0]
45  << " coarsemesh coarsesolution finemesh finesolution [outputdiff]" << std::endl;
46 
47  if (argc < 5)
48  libmesh_error();
49 
50  coarse_mesh.read(argv[1]);
51  libMesh::out << "Loaded coarse mesh " << argv[1] << std::endl;
52  coarse_es.read(argv[2]);
53  libMesh::out << "Loaded coarse solution " << argv[2] << std::endl;
54  fine_mesh.read(argv[3]);
55  libMesh::out << "Loaded fine mesh " << argv[3] << std::endl;
56  fine_es.read(argv[4]);
57  libMesh::out << "Loaded fine solution " << argv[4] << std::endl;
58 
59  ExactSolution exact_sol(coarse_es);
60  exact_sol.attach_reference_solution(&fine_es);
61 
62  std::vector<std::string> sysnames;
63  sysnames.reserve(coarse_es.n_systems());
64 
65  for (unsigned int i = 0; i != coarse_es.n_systems(); ++i)
66  if (fine_es.has_system(coarse_es.get_system(i).name()))
67  sysnames.push_back(coarse_es.get_system(i).name());
68  else
69  libMesh::out << "Coarse solution system "
70  << coarse_es.get_system(i).name()
71  << " not found in fine solution!" << std::endl;
72 
73  for (unsigned int i = 0; i != fine_es.n_systems(); ++i)
74  if (!coarse_es.has_system(fine_es.get_system(i).name()))
75  libMesh::out << "Fine solution system "
76  << fine_es.get_system(i).name()
77  << " not found in coarse solution!" << std::endl;
78 
79  if (!coarse_es.n_systems() && !fine_es.n_systems())
80  libMesh::out << "No systems found in fine or coarse solution!"
81  << std::endl;
82 
83  for (auto i : index_range(sysnames))
84  {
85  const std::string sysname = sysnames[i];
86  const System & coarse_sys = coarse_es.get_system(sysname);
87  const System & fine_sys = fine_es.get_system(sysname);
88 
89  for (unsigned int j = 0; j != coarse_sys.n_vars(); ++j)
90  {
91  const std::string varname = coarse_sys.variable_name(j);
92 
93  if (!fine_sys.has_variable(varname))
94  {
95  libMesh::out << "Fine solution system " << sysname
96  << " variable " << varname
97  << " not found in coarse solution!" << std::endl;
98  continue;
99  }
100  const unsigned int j2 = fine_sys.variable_number(varname);
101 
102  exact_sol.compute_error(sysname, varname);
103 
104  libMesh::out << "Errors in system " << sysname << ", variable " << varname << ":" << std::endl;
105  libMesh::out << "L2 error: " << exact_sol.l2_error(sysname, varname)
106  << ", fine norm: " << coarse_sys.calculate_norm(*coarse_sys.solution, j, L2)
107  << ", coarse norm: " << fine_sys.calculate_norm(*fine_sys.solution, j2, L2) << std::endl;
108  libMesh::out << "H1 error: " << exact_sol.h1_error(sysname, varname)
109  << ", fine norm: " << coarse_sys.calculate_norm(*coarse_sys.solution, j, H1)
110  << ", coarse norm: " << fine_sys.calculate_norm(*fine_sys.solution, j2, H1) << std::endl;
111  libMesh::out << "H2 error: " << exact_sol.h2_error(sysname, varname)
112  << ", fine norm: " << coarse_sys.calculate_norm(*coarse_sys.solution, j, H2)
113  << ", coarse norm: " << fine_sys.calculate_norm(*fine_sys.solution, j2, H2) << std::endl;
114  }
115 
116  for (unsigned int j = 0; j != fine_sys.n_vars(); ++j)
117  {
118  const std::string varname = fine_sys.variable_name(j);
119 
120  if (!coarse_sys.has_variable(varname))
121  {
122  libMesh::out << "Coarse solution system " << sysname
123  << " variable " << varname
124  << " not found in fine solution!" << std::endl;
125  continue;
126  }
127  }
128 
129  if (!coarse_sys.n_vars() && !fine_sys.n_vars())
130  libMesh::out << "No variables found in fine or coarse solution system "
131  << sysname << '!' << std::endl;
132  }
133 
134  if (argc > 5)
135  {
136  libMesh::out << "Writing diff solution " << argv[5] << std::endl;
137 
138  for (auto i : index_range(sysnames))
139  {
140  const std::string sysname = sysnames[i];
141  const System & coarse_sys = coarse_es.get_system(sysname);
142  const System & fine_sys = fine_es.get_system(sysname);
143 
144  std::unique_ptr<NumericVector<Number>> fine_solution = fine_sys.solution->clone();
145  fine_sys.solution->zero();
146 
147  std::vector<unsigned int>
148  var_remapping(fine_sys.n_vars(), libMesh::invalid_uint);
149 
150  for (unsigned int j = 0; j != fine_sys.n_vars(); ++j)
151  {
152  const std::string varname = fine_sys.variable_name(j);
153 
154  if (coarse_sys.has_variable(varname))
155  var_remapping[j] = coarse_sys.variable_number(varname);
156  }
157 
158  MeshFunction coarse_solution
159  (coarse_es, *coarse_sys.solution,
160  coarse_sys.get_dof_map(), var_remapping);
161  coarse_solution.init();
162  const DenseVector<Number> oom_value(fine_sys.n_vars(), 0);
163  coarse_solution.enable_out_of_mesh_mode(oom_value);
164 
165  fine_sys.project_solution(&coarse_solution);
166  *fine_sys.solution -= *fine_solution;
167  }
168 
169  NameBasedIO(fine_mesh).write_equation_systems (argv[5], fine_es);
170  }
171 
172  return 0;
173 }
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::ExactSolution
This class handles the computation of the L2 and/or H1 error for the Systems in the EquationSystems o...
Definition: exact_solution.h:73
libMesh::System::n_vars
unsigned int n_vars() const
Definition: system.h:2155
libMesh::invalid_uint
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value.
Definition: libmesh.h:249
libMesh::Mesh
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
libMesh::MeshFunction::init
virtual void init() override
Override the FunctionBase::init() member function by calling our own and specifying the Trees::NODES ...
Definition: mesh_function.h:110
libMesh::MeshFunction
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:53
libMesh::System::variable_name
const std::string & variable_name(const unsigned int i) const
Definition: system.h:2203
libMesh::index_range
IntRange< std::size_t > index_range(const std::vector< T > &vec)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:106
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::ExactSolution::h1_error
Real h1_error(const std::string &sys_name, const std::string &unknown_name)
Definition: exact_solution.C:393
libMesh::H2
Definition: enum_norm_type.h:38
libMesh::ExactSolution::h2_error
Real h2_error(const std::string &sys_name, const std::string &unknown_name)
Definition: exact_solution.C:425
main
int main(int argc, char **argv)
Definition: meshdiff.C:37
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::System::has_variable
bool has_variable(const std::string &var) const
Definition: system.C:1225
libMesh::ExactSolution::attach_reference_solution
void attach_reference_solution(const EquationSystems *es_fine)
Attach function similar to system.h which allows the user to attach a second EquationSystems object w...
Definition: exact_solution.C:81
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
libMesh::ExactSolution::l2_error
Real l2_error(const std::string &sys_name, const std::string &unknown_name)
Definition: exact_solution.C:333
libMesh::ExactSolution::compute_error
void compute_error(const std::string &sys_name, const std::string &unknown_name)
Computes and stores the error in the solution value e = u-u_h, the gradient grad(e) = grad(u) - grad(...
Definition: exact_solution.C:227
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
dim
unsigned char dim
Definition: meshdiff.C:35
libMesh::System::solution
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1539
libMesh::NameBasedIO::write_equation_systems
virtual void write_equation_systems(const std::string &filename, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
This method implements writing a mesh with data to a specified file where the data is taken from the ...
Definition: namebased_io.C:506
libMesh::L2
Definition: enum_norm_type.h:36
libMesh::System::variable_number
unsigned short int variable_number(const std::string &var) const
Definition: system.C:1232
libMesh::System::calculate_norm
Real calculate_norm(const NumericVector< Number > &v, unsigned int var, FEMNormType norm_type, std::set< unsigned int > *skip_dimensions=nullptr) const
Definition: system.C:1356
libMesh::System::get_dof_map
const DofMap & get_dof_map() const
Definition: system.h:2099
libMesh::NameBasedIO
This class supports simple reads and writes in any libMesh-supported format, by dispatching to one of...
Definition: namebased_io.h:44
libMesh::H1
Definition: enum_norm_type.h:37
libMesh::System::project_solution
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
Definition: system_projection.C:950
libMesh::out
OStreamProxy out
libMesh::DenseVector< Number >