7 #ifndef SRC_DRIVER_MESH_SWARM_MESH_H_ 8 #define SRC_DRIVER_MESH_SWARM_MESH_H_ 18 #include <type_traits> 35 #ifdef PORTAGE_ENABLE_MPI 66 template <
template <
int,
class,
class>
class Search,
67 template<
int,
class,
class>
class Accumulate,
68 template<
int,
class>
class Estimate,
70 class SourceMesh_Wrapper,
71 class SourceState_Wrapper,
72 class TargetMesh_Wrapper = SourceMesh_Wrapper,
73 class TargetState_Wrapper = SourceState_Wrapper>
93 SourceState_Wrapper
const& source_state,
94 TargetMesh_Wrapper
const& target_mesh,
95 TargetState_Wrapper& target_state,
96 double smoothing_factor = 1.25,
97 double boundary_factor = 0.5,
100 Meshfree::WeightCenter center = Meshfree::Gather,
101 std::string part_field =
"NONE",
102 double part_tolerance = std::numeric_limits<double>::infinity())
104 : source_mesh_(source_mesh),
105 target_mesh_(target_mesh),
106 source_state_(source_state),
107 target_state_(target_state),
108 smoothing_factor_(smoothing_factor),
109 boundary_factor_(boundary_factor),
113 part_field_(part_field),
114 part_tolerance_(part_tolerance),
115 dim_(source_mesh.space_dimension()) {
117 assert(source_mesh.space_dimension() == target_mesh.space_dimension());
118 assert(source_mesh.space_dimension() == dim);
159 Meshfree::EstimateType
const& estimator_type = Meshfree::LocalRegression,
165 assert(source_vars.size() == target_vars.size());
167 int nvars = source_vars.size();
168 for (
int i = 0; i < nvars; ++i) {
169 auto const& source_entity = source_state_.get_entity(source_vars[i]);
170 auto const& target_entity = target_state_.get_entity(target_vars[i]);
171 assert(source_entity == target_entity);
177 estimate_ = estimator_type;
179 operator_spec_ = operator_spec;
180 operator_domains_ = operator_domains;
181 operator_data_ = operator_data;
189 std::vector<std::string>
source_vars()
const {
return source_vars_; }
196 std::vector<std::string>
target_vars()
const {
return target_vars_; }
207 void run(Wonton::Executor_type
const *executor =
nullptr) {
209 using namespace Meshfree;
211 using SwarmRemap = SwarmDriver<Search, Accumulate, Estimate, dim,
212 Swarm<dim>, SwarmState<dim>>;
217 #ifdef PORTAGE_ENABLE_MPI 218 MPI_Comm mycomm = MPI_COMM_NULL;
219 auto mpiexecutor =
dynamic_cast<Wonton::MPIExecutor_type
const *
>(executor);
220 if (mpiexecutor && mpiexecutor->mpicomm != MPI_COMM_NULL) {
221 mycomm = mpiexecutor->mpicomm;
222 MPI_Comm_rank(mycomm, &rank);
223 MPI_Comm_size(mycomm, &nprocs);
225 std::cerr <<
"Cannot run Mesh-Swarm-Mesh driver in distributed mode yet";
226 std::cerr << std::endl;
233 std::cout <<
"in MSM_Driver::run() ... " << std::endl;
235 int ncells = target_mesh_.num_owned_cells();
236 std::cout <<
"Number of owned target cells on rank "<< rank <<
": "<< ncells;
237 std::cout << std::endl;
241 int nvars = source_vars_.size();
246 std::vector<std::string> source_cellvar_names;
247 std::vector<std::string> target_cellvar_names;
249 for (
int i = 0; i < nvars; ++i) {
250 auto onwhat = source_state_.get_entity(source_vars_[i]);
251 if (onwhat == Wonton::CELL) {
252 source_cellvar_names.emplace_back(source_vars_[i]);
253 target_cellvar_names.emplace_back(target_vars_[i]);
258 if (not source_cellvar_names.empty()) {
260 Swarm<dim> source_swarm(source_mesh_, Wonton::CELL);
261 Swarm<dim> target_swarm(target_mesh_, Wonton::CELL);
262 SwarmState<dim> source_swarm_state(source_state_, Wonton::CELL);
263 SwarmState<dim> target_swarm_state(target_state_, Wonton::CELL);
267 std::vector<std::vector<double>> default_lengths(1, std::vector<double>(dim));
272 using Weight::faceted_setup_cell;
274 if (part_field_ ==
"NONE") {
276 case Scatter: faceted_setup_cell<dim>(source_mesh_,
280 boundary_factor_);
break;
281 case Gather: faceted_setup_cell<dim>(target_mesh_,
285 boundary_factor_);
break;
294 faceted_setup_cell<dim>(source_mesh_, part_smoothing,
295 dummy_extents, 0.25, 0.25);
297 faceted_setup_cell<dim>(source_mesh_,
298 smoothing_lengths, weight_extents,
299 smoothing_factor_, boundary_factor_);
301 case Gather: faceted_setup_cell<dim>(target_mesh_, target_state_,
302 part_field_, part_tolerance_,
303 smoothing_lengths, weight_extents,
304 smoothing_factor_, boundary_factor_);
310 int ncells = (center_ == Scatter ? source_mesh_.num_owned_cells()
311 : target_mesh_.num_owned_cells());
313 smoothing_lengths.resize(ncells, default_lengths);
315 for (
int i = 0; i < ncells; i++) {
318 case Scatter: Wonton::cell_radius<dim>(source_mesh_, i, &radius);
break;
319 case Gather: Wonton::cell_radius<dim>(target_mesh_, i, &radius);
break;
323 std::vector<std::vector<double>> h = smoothing_lengths[i];
324 h[0] = std::vector<double>(dim, 2. * radius * smoothing_factor_);
325 smoothing_lengths[i] = h;
330 SwarmRemap* swarm_remap_ptr =
nullptr;
333 if (center_ == Scatter) {
334 swarm_remap_ptr =
new SwarmRemap(source_swarm, source_swarm_state,
335 target_swarm, target_swarm_state,
337 weight_extents, other_extents,
339 }
else if (center_ == Gather) {
340 swarm_remap_ptr =
new SwarmRemap(source_swarm, source_swarm_state,
341 target_swarm, target_swarm_state,
343 other_extents, weight_extents,
347 swarm_remap_ptr =
new SwarmRemap(source_swarm, source_swarm_state,
348 target_swarm, target_swarm_state,
350 kernel_, geometry_, center_);
353 assert(swarm_remap_ptr !=
nullptr);
354 SwarmRemap& swarm_remap(*swarm_remap_ptr);
355 swarm_remap.set_remap_var_names(source_cellvar_names, target_cellvar_names,
356 estimate_, basis_, operator_spec_,
357 operator_domains_, operator_data_,
358 part_field_, part_tolerance_, part_smoothing);
360 swarm_remap.run(executor,
true);
363 for (
auto&& name : target_cellvar_names) {
364 auto& swarm_field = target_swarm_state.get_field(name);
366 target_state_.mesh_get_data(Wonton::CELL, name, &mesh_field);
367 for (
int i = 0; i < target_swarm_state.get_size(); i++)
368 mesh_field[i] = swarm_field[i];
371 delete swarm_remap_ptr;
377 std::vector<std::string> source_nodevar_names;
378 std::vector<std::string> target_nodevar_names;
379 for (
int i = 0; i < nvars; ++i) {
380 auto onwhat = source_state_.get_entity(source_vars_[i]);
381 if (onwhat == Wonton::NODE) {
382 source_nodevar_names.emplace_back(source_vars_[i]);
383 target_nodevar_names.emplace_back(target_vars_[i]);
387 if (not source_nodevar_names.empty()) {
389 Swarm<dim> source_swarm(source_mesh_, Wonton::NODE);
390 Swarm<dim> target_swarm(target_mesh_, Wonton::NODE);
391 SwarmState<dim> source_swarm_state(source_state_, Wonton::NODE);
392 SwarmState<dim> target_swarm_state(target_state_, Wonton::NODE);
396 std::vector<std::vector<double>> default_lengths(1, std::vector<double>(dim));
399 std::cerr <<
"Cannot do FACETED weights for nodal variables yet." << std::endl;
403 int nnodes = (center_ == Scatter ? source_mesh_.num_owned_nodes()
404 : target_mesh_.num_owned_nodes());
406 smoothing_lengths.resize(nnodes, default_lengths);
408 for (
int i = 0; i < nnodes; i++) {
411 case Scatter: Wonton::node_radius<dim>(source_mesh_, i, &radius);
break;
412 case Gather: Wonton::node_radius<dim>(target_mesh_, i, &radius);
break;
416 std::vector<std::vector<double>> h = smoothing_lengths[i];
417 h[0] = std::vector<double>(dim, radius * smoothing_factor_);
418 smoothing_lengths[i] = h;
422 SwarmRemap swarm_remap(source_swarm, source_swarm_state,
423 target_swarm, target_swarm_state,
424 smoothing_lengths, kernel_,
427 swarm_remap.set_remap_var_names(source_nodevar_names, target_nodevar_names,
428 LocalRegression, basis_);
431 swarm_remap.run(executor,
true);
434 for (
auto&& name : target_nodevar_names) {
435 auto& swarm_field = target_swarm_state.get_field(name);
437 target_state_.mesh_get_data(Wonton::NODE, name, &mesh_field);
438 for (
int i = 0; i < target_swarm_state.get_size(); i++)
439 mesh_field[i] = swarm_field[i];
445 std::cout <<
"Mesh-Swarm-Mesh Time for Rank " << rank <<
" (s): " << elapsed << std::endl;
450 SourceMesh_Wrapper
const& source_mesh_;
451 TargetMesh_Wrapper
const& target_mesh_;
452 SourceState_Wrapper
const& source_state_;
453 TargetState_Wrapper& target_state_;
454 std::vector<std::string> source_vars_;
455 std::vector<std::string> target_vars_;
456 double smoothing_factor_ = 0.0;
457 double boundary_factor_ = 0.0;
460 Meshfree::WeightCenter center_ {};
461 std::string part_field_;
462 double part_tolerance_ = 0.0;
463 Meshfree::EstimateType estimate_ {};
473 #endif // SRC_DRIVER_MESH_SWARM_MESH_H_ std::chrono::high_resolution_clock::time_point now()
Get current time.
Definition: timer.h:19
std::vector< T > vector
Definition: portage.h:238
MSM_Driver(SourceMesh_Wrapper const &source_mesh, SourceState_Wrapper const &source_state, TargetMesh_Wrapper const &target_mesh, TargetState_Wrapper &target_state, double smoothing_factor=1.25, double boundary_factor=0.5, Meshfree::Weight::Geometry geometry=Meshfree::Weight::TENSOR, Meshfree::Weight::Kernel kernel=Meshfree::Weight::B4, Meshfree::WeightCenter center=Meshfree::Gather, std::string part_field="NONE", double part_tolerance=std::numeric_limits< double >::infinity())
Constructor for running the interpolation driver.
Definition: driver_mesh_swarm_mesh.h:92
float elapsed(std::chrono::high_resolution_clock::time_point &tic, bool reset=false)
Get elapsed time in seconds.
Definition: timer.h:27
MSM_Driver & operator=(const MSM_Driver &)=delete
Assignment operator (disabled)
~MSM_Driver()=default
Destructor.
MSM_Driver provides the API to mapping from one mesh to another using particles.
Definition: driver_mesh_swarm_mesh.h:74
std::vector< std::string > target_vars() const
Get the names of the variables to be remapped to the target mesh.
Definition: driver_mesh_swarm_mesh.h:196
Definition: operator.h:30
Type
Definition: basis.h:20
std::vector< std::string > source_vars() const
Get the names of the variables to be remapped from the source mesh.
Definition: driver_mesh_swarm_mesh.h:189
void run(Wonton::Executor_type const *executor=nullptr)
Execute the remapping process.
Definition: driver_mesh_swarm_mesh.h:207
Geometry
Geometry types.
Definition: weight.h:333
Type
Definition: operator.h:30
void set_remap_var_names(std::vector< std::string > const &remap_var_names)
Specify the names of the variables to be interpolated.
Definition: driver_mesh_swarm_mesh.h:139
Definition: coredriver.h:42
double kernel(const Kernel kern, double x)
General kernel function.
Definition: weight.h:313
int dimension() const
Get the dimensionality of the meshes.
Definition: driver_mesh_swarm_mesh.h:202
void set_remap_var_names(std::vector< std::string > const &source_vars, std::vector< std::string > const &target_vars, Meshfree::EstimateType const &estimator_type=Meshfree::LocalRegression, Meshfree::basis::Type const &basis_type=Meshfree::basis::Unitary, Meshfree::oper::Type operator_spec=Meshfree::oper::LastOperator, Portage::vector< Meshfree::oper::Domain > const &operator_domains={}, Portage::vector< std::vector< Point< dim >>> const &operator_data={})
Specify the names of the variables to be interpolated.
Definition: driver_mesh_swarm_mesh.h:157
Kernel
Kernel types.
Definition: weight.h:304