libMesh
Loading...
Searching...
No Matches
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.

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

References libMesh::err, and usage_error().

Referenced by main().

◆ fptr()

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

Definition at line 81 of file projection.C.

85{
86 libmesh_assert_equal_to (sys_name, current_sys_name);
87 libmesh_assert(mesh_functions.count(unknown_name));
88 libmesh_assert(mesh_functions[unknown_name]);
89
90 MeshFunction & meshfunc = *mesh_functions[unknown_name];
91
92 return meshfunc(p);
93}
This class provides function-like objects for data distributed over a mesh.
libmesh_assert(ctx)
std::map< std::string, std::unique_ptr< MeshFunction > > mesh_functions
Definition projection.C:78
std::string current_sys_name
Definition projection.C:77

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

Referenced by libMesh::AnalyticFunction< Output >::AnalyticFunction(), libMesh::AnalyticFunction< Output >::AnalyticFunction(), libMesh::System::attach_assemble_function(), libMesh::System::attach_constraint_function(), libMesh::DiscontinuityMeasure::attach_essential_bc_function(), libMesh::ExactErrorEstimator::attach_exact_value(), libMesh::ExactSolution::attach_exact_value(), libMesh::KellyErrorEstimator::attach_flux_bc_function(), libMesh::System::attach_init_function(), libMesh::System::attach_QOI_derivative(), libMesh::System::attach_QOI_function(), libMesh::FrequencySystem::attach_solve_function(), libMesh::System::boundary_project_solution(), libMesh::System::boundary_project_vector(), main(), libMesh::System::project_solution(), and libMesh::System::project_vector().

◆ gptr()

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 111 of file projection.C.

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,
167
168 new_es.read(solnname, read_mode,
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(),
222 libMesh::out << "Wrote solution " << outsolnname << std::endl;
223
224 return 0;
225}
unsigned int n_vars
This is the EquationSystems class.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
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...
const Parallel::Communicator & comm() const
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
const std::string & name() const
Definition system.h:2385
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
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:733
unsigned int n_vars() const
Definition system.C:2674
const DofMap & get_dof_map() const
Definition system.h:2417
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
Definition utility.C:205
OStreamProxy out
XdrMODE
Defines an enum for read/write mode in Xdr format.
Gradient gptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition projection.C:96
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition projection.C:81
T assert_argument(GetPot &cl, const std::string &argname, const char *progname, const T &defaultarg)
Definition projection.C:60

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::libmesh_assert(), main(), 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::MeshBase::print_info(), libMesh::System::project_solution(), 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.

◆ usage_error()

void usage_error ( const char *  progname)

Definition at line 45 of file projection.C.

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}

References libMesh::out.

Referenced by assert_argument().

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