libMesh
projection.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 input mesh and corresponding solution file named in command line
20 // arguments, open the output mesh, project that solution onto the
21 // output mesh, and write a corresponding output solution file.
22 
23 // C++ includes
24 #include <map>
25 #include <string>
26 
27 // libMesh includes
28 #include "libmesh/libmesh.h"
29 #include "libmesh/dof_map.h"
30 #include "libmesh/equation_systems.h"
31 #include "libmesh/getpot.h"
32 #include "libmesh/mesh.h"
33 #include "libmesh/mesh_function.h"
34 #include "libmesh/numeric_vector.h"
35 #include "libmesh/point.h"
36 #include "libmesh/replicated_mesh.h"
37 #include "libmesh/enum_xdr_mode.h"
38 
39 
40 using namespace libMesh;
41 
42 
43 // If there's a missing input argument, then print a help message
44 void usage_error(const char * progname)
45 {
46  libMesh::out << "Options: " << progname << '\n'
47  << " --dim d mesh dimension [default: autodetect]\n"
48  << " --inmesh filename input mesh file\n"
49  << " --insoln filename input solution file\n"
50  << " --outmesh filename output mesh file [default: out_<inmesh>]\n"
51  << " --outsoln filename output solution file [default: out_<insoln>]\n"
52  << std::endl;
53 
54  exit(1);
55 }
56 
57 // Get an input argument, or print a help message if it's missing
58 template <typename T>
59 T assert_argument (GetPot & cl,
60  const std::string & argname,
61  const char * progname,
62  const T & defaultarg)
63 {
64  if (!cl.search(argname))
65  {
66  libMesh::err << ("No " + argname + " argument found!") << std::endl;
67  usage_error(progname);
68  }
69  return cl.next(defaultarg);
70 }
71 
72 // Global collections of MeshFunctions are necessary to work with
73 // global functions fptr and gptr.
74 // TODO: libMesh needs functor-based alternatives to these types of
75 // function arguments
76 std::string current_sys_name;
77 std::map<std::string, std::unique_ptr<MeshFunction>> mesh_functions;
78 
79 // Return the function value on the old mesh and solution
80 Number fptr(const Point & p,
81  const Parameters &,
82  const std::string & libmesh_dbg_var(sys_name),
83  const std::string & unknown_name)
84 {
85  libmesh_assert_equal_to (sys_name, current_sys_name);
86  libmesh_assert(mesh_functions.count(unknown_name));
87  libmesh_assert(mesh_functions[unknown_name]);
88 
89  MeshFunction & meshfunc = *mesh_functions[unknown_name];
90 
91  return meshfunc(p);
92 }
93 
94 // Return the function gradient on the old mesh and solution
95 Gradient gptr(const Point & p,
96  const Parameters &,
97  const std::string & libmesh_dbg_var(sys_name),
98  const std::string & unknown_name)
99 {
100  libmesh_assert_equal_to (sys_name, current_sys_name);
101  libmesh_assert(mesh_functions.count(unknown_name));
102  libmesh_assert(mesh_functions[unknown_name]);
103 
104  MeshFunction & meshfunc = *mesh_functions[unknown_name];
105 
106  return meshfunc.gradient(p);
107 }
108 
109 
110 int main(int argc, char ** argv)
111 {
112  LibMeshInit init(argc, argv);
113 
114  GetPot cl(argc, argv);
115 
116  // In case the mesh file doesn't let us auto-infer dimension, we let
117  // the user specify it on the command line
118  const unsigned char requested_dim =
119  cast_int<unsigned char>(cl.follow(3, "--dim"));
120 
121  // Load the old mesh from --inmesh filename.
122  // Keep it serialized; we don't want elements on the new mesh to be
123  // looking for data on old mesh elements that live off-processor.
124  ReplicatedMesh old_mesh(init.comm(), requested_dim);
125 
126  const std::string meshname =
127  assert_argument(cl, "--inmesh", argv[0], std::string("mesh.xda"));
128 
129  old_mesh.read(meshname);
130  std::cout << "Old Mesh:" << std::endl;
131  old_mesh.print_info();
132 
133  // Load the new mesh from --outmesh filename
134  Mesh new_mesh(init.comm(), requested_dim);
135 
136  const std::string outmeshname = cl.follow(std::string("out_"+meshname), "--outmesh");
137 
138  new_mesh.read(outmeshname);
139  std::cout << "New Mesh:" << std::endl;
140  new_mesh.print_info();
141 
142  // Load the old solution from --insoln filename
143  // Construct the new solution from the old solution's headers, so
144  // that we get the system names and types, variable names and types,
145  // etc.
146  const std::string solnname =
147  assert_argument(cl, "--insoln", argv[0], std::string("soln.xda"));
148 
149  EquationSystems old_es(old_mesh);
150  EquationSystems new_es(new_mesh);
151 
152  XdrMODE read_mode;
153 
154  if (solnname.rfind(".xdr") < solnname.size())
155  read_mode = DECODE;
156  else if (solnname.rfind(".xda") < solnname.size())
157  read_mode = READ;
158  else
159  libmesh_error_msg("Unrecognized file extension on " << solnname);
160 
161  old_es.read(solnname, read_mode,
166 
167  new_es.read(solnname, read_mode,
170 
171  old_es.print_info();
172 
173  unsigned int n_systems = old_es.n_systems();
174  libmesh_assert_equal_to (new_es.n_systems(), n_systems);
175 
176  // For each system, serialize the solution so we can project it onto
177  // a potentially-very-different partitioning
178  for (unsigned int i = 0; i != n_systems; ++i)
179  {
180  System & old_sys = old_es.get_system(i);
181  current_sys_name = old_sys.name();
182 
183  libMesh::out << "Projecting system " << current_sys_name << std::endl;
184 
186 
187  System & new_sys = new_es.get_system(current_sys_name);
188  unsigned int n_vars = old_sys.n_vars();
189  libmesh_assert_equal_to (new_sys.n_vars(), n_vars);
190 
191  std::unique_ptr<NumericVector<Number>> comparison_soln =
193  std::vector<Number> global_soln;
194  old_sys.update_global_solution(global_soln);
195  comparison_soln->init(old_sys.solution->size(), true, SERIAL);
196  (*comparison_soln) = global_soln;
197 
198  // For each variable, construct a MeshFunction returning that
199  // variable's value
200  for (unsigned int j = 0; j != n_vars; ++j)
201  {
202  libMesh::out << " with variable " << old_sys.variable_name(j) << std::endl;
203 
204  auto mesh_func =
205  std::make_unique<MeshFunction>(old_es, *comparison_soln,
206  old_sys.get_dof_map(), j);
207  mesh_func->init();
208  mesh_functions[old_sys.variable_name(j)] = std::move(mesh_func);
209  }
210 
211  // Project all variables to the new system
212  new_sys.project_solution(fptr, gptr, old_es.parameters);
213  }
214 
215  // Write out the new solution file
216  const std::string outsolnname = cl.follow(std::string("out_"+solnname), "--outsoln");
217 
218  new_es.write(outsolnname.c_str(),
221  libMesh::out << "Wrote solution " << outsolnname << std::endl;
222 
223  return 0;
224 }
OStreamProxy err
This is the EquationSystems class.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
void update_global_solution(std::vector< Number > &global_soln) const
Fill the input vector global_soln so that it contains the global solution on all processors.
Definition: system.C:745
void write(std::string_view name, const XdrMODE, const unsigned int write_flags=(WRITE_DATA), bool partition_agnostic=true) const
Write the systems to disk using the XDR data format.
unsigned int n_systems() const
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:67
bool has_system(std::string_view name) const
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
Gradient gradient(const Point &p, const Real time=0.)
const Parallel::Communicator & comm() const
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
std::map< std::string, std::unique_ptr< MeshFunction > > mesh_functions
Definition: projection.C:77
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:90
The libMesh namespace provides an interface to certain functionality in the library.
const T_sys & get_system(std::string_view name) const
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition: projection.C:80
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:35
int main(int argc, char **argv)
Definition: projection.C:110
void usage_error(const char *progname)
Definition: projection.C:44
unsigned int n_vars
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
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libmesh_assert(ctx)
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.
const std::string & variable_name(const unsigned int i) const
Definition: system.h:2478
T assert_argument(GetPot &cl, const std::string &argname, const char *progname, const T &defaultarg)
Definition: projection.C:59
std::string current_sys_name
Definition: projection.C:76
OStreamProxy out
Gradient gptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition: projection.C:95
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, SolverPackage solver_package=libMesh::default_solver_package(), ParallelType parallel_type=AUTOMATIC)
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
Parameters parameters
Data structure holding arbitrary parameters.
const std::string & name() const
Definition: system.h:2342
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
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false) override
Reads the file specified by name.
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
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39