libMesh
Functions | Variables
projection.C File Reference

Go to the source code of this file.

Functions

void usage_error (const char *progname)
 
template<typename T >
assert_argument (GetPot &cl, const std::string &argname, const char *progname, const T &defaultarg)
 
Number fptr (const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
 
Gradient gptr (const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
 
int main (int argc, char **argv)
 

Variables

std::string current_sys_name
 
std::map< std::string, std::unique_ptr< MeshFunction > > mesh_functions
 

Function Documentation

◆ assert_argument()

template<typename T >
T assert_argument ( GetPot &  cl,
const std::string &  argname,
const char *  progname,
const T &  defaultarg 
)

Definition at line 60 of file projection.C.

References libMesh::err, and usage_error().

Referenced by main().

64 {
65  if (!cl.search(argname))
66  {
67  libMesh::err << ("No " + argname + " argument found!") << std::endl;
68  usage_error(progname);
69  }
70  return cl.next(defaultarg);
71 }
OStreamProxy err
void usage_error(const char *progname)
Definition: projection.C:45

◆ fptr()

Number fptr ( const Point p,
const Parameters ,
const std::string &  libmesh_dbg_varsys_name,
const std::string &  unknown_name 
)

◆ gptr()

Gradient gptr ( const Point p,
const Parameters ,
const std::string &  libmesh_dbg_varsys_name,
const std::string &  unknown_name 
)

Definition at line 96 of file projection.C.

References current_sys_name, libMesh::MeshFunction::gradient(), libMesh::libmesh_assert(), and mesh_functions.

Referenced by libMesh::ExactErrorEstimator::attach_exact_deriv(), libMesh::ExactSolution::attach_exact_deriv(), libMesh::System::boundary_project_solution(), libMesh::System::boundary_project_vector(), main(), libMesh::System::project_solution(), and libMesh::System::project_vector().

100 {
101  libmesh_assert_equal_to (sys_name, current_sys_name);
102  libmesh_assert(mesh_functions.count(unknown_name));
103  libmesh_assert(mesh_functions[unknown_name]);
104 
105  MeshFunction & meshfunc = *mesh_functions[unknown_name];
106 
107  return meshfunc.gradient(p);
108 }
Gradient gradient(const Point &p, const Real time=0.)
std::map< std::string, std::unique_ptr< MeshFunction > > mesh_functions
Definition: projection.C:78
libmesh_assert(ctx)
std::string current_sys_name
Definition: projection.C:77
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:54

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 111 of file projection.C.

References assert_argument(), libMesh::NumericVector< T >::build(), libMesh::ParallelObject::comm(), libMesh::Utility::contains(), current_sys_name, libMesh::DECODE, fptr(), libMesh::System::get_dof_map(), libMesh::EquationSystems::get_system(), gptr(), libMesh::EquationSystems::has_system(), libMesh::TriangleWrapper::init(), libMesh::libmesh_assert(), mesh_functions, libMesh::EquationSystems::n_systems(), n_vars, libMesh::System::n_vars(), libMesh::System::name(), libMesh::out, libMesh::EquationSystems::parameters, libMesh::EquationSystems::print_info(), libMesh::READ, libMesh::UnstructuredMesh::read(), libMesh::EquationSystems::read(), libMesh::EquationSystems::READ_ADDITIONAL_DATA, libMesh::EquationSystems::READ_BASIC_ONLY, libMesh::EquationSystems::READ_DATA, libMesh::EquationSystems::READ_HEADER, libMesh::SERIAL, libMesh::System::solution, libMesh::System::update_global_solution(), libMesh::System::variable_name(), libMesh::EquationSystems::write(), libMesh::EquationSystems::WRITE_ADDITIONAL_DATA, and libMesh::EquationSystems::WRITE_DATA.

112 {
113  LibMeshInit init(argc, argv);
114 
115  GetPot cl(argc, argv);
116 
117  // In case the mesh file doesn't let us auto-infer dimension, we let
118  // the user specify it on the command line
119  const unsigned char requested_dim =
120  cast_int<unsigned char>(cl.follow(3, "--dim"));
121 
122  // Load the old mesh from --inmesh filename.
123  // Keep it serialized; we don't want elements on the new mesh to be
124  // looking for data on old mesh elements that live off-processor.
125  ReplicatedMesh old_mesh(init.comm(), requested_dim);
126 
127  const std::string meshname =
128  assert_argument(cl, "--inmesh", argv[0], std::string("mesh.xda"));
129 
130  old_mesh.read(meshname);
131  std::cout << "Old Mesh:" << std::endl;
132  old_mesh.print_info();
133 
134  // Load the new mesh from --outmesh filename
135  Mesh new_mesh(init.comm(), requested_dim);
136 
137  const std::string outmeshname = cl.follow(std::string("out_"+meshname), "--outmesh");
138 
139  new_mesh.read(outmeshname);
140  std::cout << "New Mesh:" << std::endl;
141  new_mesh.print_info();
142 
143  // Load the old solution from --insoln filename
144  // Construct the new solution from the old solution's headers, so
145  // that we get the system names and types, variable names and types,
146  // etc.
147  const std::string solnname =
148  assert_argument(cl, "--insoln", argv[0], std::string("soln.xda"));
149 
150  EquationSystems old_es(old_mesh);
151  EquationSystems new_es(new_mesh);
152 
153  XdrMODE read_mode;
154 
155  if (Utility::contains(solnname, ".xdr"))
156  read_mode = DECODE;
157  else if (Utility::contains(solnname, ".xda"))
158  read_mode = READ;
159  else
160  libmesh_error_msg("Unrecognized file extension on " << solnname);
161 
162  old_es.read(solnname, read_mode,
163  EquationSystems::READ_HEADER |
164  EquationSystems::READ_DATA |
165  EquationSystems::READ_ADDITIONAL_DATA |
166  EquationSystems::READ_BASIC_ONLY);
167 
168  new_es.read(solnname, read_mode,
169  EquationSystems::READ_HEADER |
170  EquationSystems::READ_BASIC_ONLY);
171 
172  old_es.print_info();
173 
174  unsigned int n_systems = old_es.n_systems();
175  libmesh_assert_equal_to (new_es.n_systems(), n_systems);
176 
177  // For each system, serialize the solution so we can project it onto
178  // a potentially-very-different partitioning
179  for (unsigned int i = 0; i != n_systems; ++i)
180  {
181  System & old_sys = old_es.get_system(i);
182  current_sys_name = old_sys.name();
183 
184  libMesh::out << "Projecting system " << current_sys_name << std::endl;
185 
186  libmesh_assert (new_es.has_system(current_sys_name));
187 
188  System & new_sys = new_es.get_system(current_sys_name);
189  unsigned int n_vars = old_sys.n_vars();
190  libmesh_assert_equal_to (new_sys.n_vars(), n_vars);
191 
192  std::unique_ptr<NumericVector<Number>> comparison_soln =
194  std::vector<Number> global_soln;
195  old_sys.update_global_solution(global_soln);
196  comparison_soln->init(old_sys.solution->size(), true, SERIAL);
197  (*comparison_soln) = global_soln;
198 
199  // For each variable, construct a MeshFunction returning that
200  // variable's value
201  for (unsigned int j = 0; j != n_vars; ++j)
202  {
203  libMesh::out << " with variable " << old_sys.variable_name(j) << std::endl;
204 
205  auto mesh_func =
206  std::make_unique<MeshFunction>(old_es, *comparison_soln,
207  old_sys.get_dof_map(), j);
208  mesh_func->init();
209  mesh_functions[old_sys.variable_name(j)] = std::move(mesh_func);
210  }
211 
212  // Project all variables to the new system
213  new_sys.project_solution(fptr, gptr, old_es.parameters);
214  }
215 
216  // Write out the new solution file
217  const std::string outsolnname = cl.follow(std::string("out_"+solnname), "--outsoln");
218 
219  new_es.write(outsolnname.c_str(),
220  EquationSystems::WRITE_DATA |
221  EquationSystems::WRITE_ADDITIONAL_DATA);
222  libMesh::out << "Wrote solution " << outsolnname << std::endl;
223 
224  return 0;
225 }
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
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: vector_fe_ex5.C:44
const Parallel::Communicator & comm() const
std::map< std::string, std::unique_ptr< MeshFunction > > mesh_functions
Definition: projection.C:78
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:90
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition: projection.C:81
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:35
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)
const std::string & variable_name(const unsigned int i) const
Definition: system.h:2478
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
Definition: utility.C:205
T assert_argument(GetPot &cl, const std::string &argname, const char *progname, const T &defaultarg)
Definition: projection.C:60
std::string current_sys_name
Definition: projection.C:77
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:96
const std::string & name() const
Definition: system.h:2342
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

◆ usage_error()

void usage_error ( const char *  progname)

Definition at line 45 of file projection.C.

References libMesh::out.

Referenced by assert_argument().

46 {
47  libMesh::out << "Options: " << progname << '\n'
48  << " --dim d mesh dimension [default: autodetect]\n"
49  << " --inmesh filename input mesh file\n"
50  << " --insoln filename input solution file\n"
51  << " --outmesh filename output mesh file [default: out_<inmesh>]\n"
52  << " --outsoln filename output solution file [default: out_<insoln>]\n"
53  << std::endl;
54 
55  exit(1);
56 }
OStreamProxy out

Variable Documentation

◆ current_sys_name

std::string current_sys_name

Definition at line 77 of file projection.C.

Referenced by fptr(), gptr(), and main().

◆ mesh_functions

std::map<std::string, std::unique_ptr<MeshFunction> > mesh_functions

Definition at line 78 of file projection.C.

Referenced by fptr(), gptr(), and main().