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 59 of file projection.C.

References libMesh::err, and usage_error().

Referenced by main().

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 }
OStreamProxy err
void usage_error(const char *progname)
Definition: projection.C:44

◆ 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 95 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().

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 }
Gradient gradient(const Point &p, const Real time=0.)
std::map< std::string, std::unique_ptr< MeshFunction > > mesh_functions
Definition: projection.C:77
libmesh_assert(ctx)
std::string current_sys_name
Definition: projection.C:76
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 110 of file projection.C.

References assert_argument(), libMesh::NumericVector< T >::build(), libMesh::ParallelObject::comm(), 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.

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,
162  EquationSystems::READ_HEADER |
163  EquationSystems::READ_DATA |
164  EquationSystems::READ_ADDITIONAL_DATA |
165  EquationSystems::READ_BASIC_ONLY);
166 
167  new_es.read(solnname, read_mode,
168  EquationSystems::READ_HEADER |
169  EquationSystems::READ_BASIC_ONLY);
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 
185  libmesh_assert (new_es.has_system(current_sys_name));
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(),
219  EquationSystems::WRITE_DATA |
220  EquationSystems::WRITE_ADDITIONAL_DATA);
221  libMesh::out << "Wrote solution " << outsolnname << std::endl;
222 
223  return 0;
224 }
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:77
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:80
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
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
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 44 of file projection.C.

References libMesh::out.

Referenced by assert_argument().

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 }
OStreamProxy out

Variable Documentation

◆ current_sys_name

std::string current_sys_name

Definition at line 76 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 77 of file projection.C.

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