libMesh
Loading...
Searching...
No Matches
meshdiff.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
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
33using namespace libMesh;
34
35unsigned char dim = 2; // This gets overridden by most mesh formats
36
37int 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(), std::move(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}
Defines a dense vector for use in Finite Element-type computations.
This is the EquationSystems class.
unsigned int n_systems() const
void read(std::string_view name, const XdrMODE, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
Read & initialize the systems from disk using the XDR data format.
bool has_system(std::string_view name) const
const T_sys & get_system(std::string_view name) const
This class handles the computation of the L2 and/or H1 error for the Systems in the EquationSystems o...
Real l2_error(std::string_view sys_name, std::string_view unknown_name)
Real h1_error(std::string_view sys_name, std::string_view unknown_name)
Real h2_error(std::string_view sys_name, std::string_view unknown_name)
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...
void compute_error(std::string_view sys_name, std::string_view unknown_name)
Computes and stores the error in the solution value e = u-u_h, the gradient grad(e) = grad(u) - grad(...
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
This class provides function-like objects for data distributed over a mesh.
virtual void init() override
Override the FunctionBase::init() member function.
void enable_out_of_mesh_mode(const DenseVector< Number > &value)
Enables out-of-mesh mode.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
This class supports simple reads and writes in any libMesh-supported format, by dispatching to one of...
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 ...
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr, std::optional< ConstElemRange > active_local_range=std::nullopt, std::optional< std::vector< unsigned int > > variable_numbers=std::nullopt) const
Projects arbitrary functions onto the current solution.
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition system.h:1655
const std::string & variable_name(const unsigned int i) const
Definition system.C:2679
unsigned int variable_number(std::string_view var) const
Definition system.C:1398
bool has_variable(std::string_view var) const
Definition system.C:1393
Real calculate_norm(const NumericVector< Number > &v, unsigned int var, FEMNormType norm_type, std::set< unsigned int > *skip_dimensions=nullptr) const
Definition system.C:1511
unsigned int n_vars() const
Definition system.C:2674
const DofMap & get_dof_map() const
Definition system.h:2417
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) override
Reads the file specified by name.
unsigned char dim
Definition meshdiff.C:35
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition libmesh.h:303
OStreamProxy out
int main()