libMesh
Functions | Variables
meshdiff.C File Reference

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Variables

unsigned char dim = 2
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 37 of file meshdiff.C.

References libMesh::ExactSolution::attach_reference_solution(), libMesh::System::calculate_norm(), libMesh::ExactSolution::compute_error(), dim, libMesh::System::get_dof_map(), libMesh::H1, libMesh::ExactSolution::h1_error(), libMesh::H2, libMesh::ExactSolution::h2_error(), libMesh::System::has_variable(), libMesh::index_range(), libMesh::TriangleWrapper::init(), libMesh::MeshFunction::init(), libMesh::invalid_uint, libMesh::L2, libMesh::ExactSolution::l2_error(), libMesh::System::n_vars(), libMesh::out, libMesh::System::project_solution(), libMesh::System::solution, libMesh::System::variable_name(), libMesh::System::variable_number(), and libMesh::NameBasedIO::write_equation_systems().

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 }
This class handles the computation of the L2 and/or H1 error for the Systems in the EquationSystems o...
This class supports simple reads and writes in any libMesh-supported format, by dispatching to one of...
Definition: namebased_io.h:44
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:530
This is the EquationSystems class.
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:310
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:90
unsigned int variable_number(std::string_view var) const
Definition: system.C:1609
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
bool has_variable(std::string_view var) const
Definition: system.C:1602
unsigned char dim
Definition: meshdiff.C:35
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1593
Real calculate_norm(const NumericVector< Number > &v, unsigned int var, FEMNormType norm_type, std::set< unsigned int > *skip_dimensions=nullptr) const
Definition: system.C:1724
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
virtual void init() override
Override the FunctionBase::init() member function.
const std::string & variable_name(const unsigned int i) const
Definition: system.h:2478
OStreamProxy out
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:54
unsigned int n_vars() const
Definition: system.h:2430
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:2374
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:117

Variable Documentation

◆ dim

unsigned char dim = 2

Definition at line 35 of file meshdiff.C.

Referenced by main().