8 #ifndef WONTON_STATE_FLAT_FLAT_STATE_MM_WRAPPER_H_ 9 #define WONTON_STATE_FLAT_FLAT_STATE_MM_WRAPPER_H_ 13 #include <unordered_map> 15 #include <type_traits> 32 template <
class MeshWrapper>
46 std::unordered_map<std::string, int>
const&
names = {},
47 std::unordered_map<int, std::vector<int>>
const& material_cells = {})
76 template <
class State_Wrapper>
78 std::vector<std::string>
const& var_names) {
84 int nmats = input.num_materials();
88 std::vector<std::string> distribute_var_names(var_names.begin(), var_names.end());
95 std::unordered_map<std::string, int> name_map;
96 for (
int i=0; i<nmats; ++i){
109 for (
int m=0; m<nmats; ++m){
112 std::vector<int> mat_cells;
113 input.mat_get_cells(m, &mat_cells);
121 distribute_var_names.emplace_back(
"mat_volfracs");
123 distribute_var_names.emplace_back(
"mat_centroids");
128 for (std::string varname : distribute_var_names) {
134 Field_type type = input.field_type(entity, varname);
139 auto& input_nonconst =
const_cast<State_Wrapper&
>(input);
142 const std::type_info& data_type = input_nonconst.get_data_type(varname);
146 if (data_type ==
typeid(
double)){
149 std::shared_ptr<StateVectorMulti<double>> pv =
150 std::make_shared<StateVectorMulti<double>> (varname);
157 for (
int m=0; m<nmats; ++m){
161 input.mat_get_celldata(varname, m, &data);
172 std::shared_ptr<StateVectorMulti<Wonton::Point<2>>> pv =
173 std::make_shared<StateVectorMulti<Wonton::Point<2>>> (varname);
180 for (
int m=0; m<nmats; ++m){
184 input.mat_get_celldata(varname, m, &data);
194 std::shared_ptr<StateVectorMulti<Wonton::Point<3>>> pv =
195 std::make_shared<StateVectorMulti<Wonton::Point<3>>> (varname);
202 for (
int m=0; m<nmats; ++m){
206 input.mat_get_celldata(varname, m, &data);
214 throw std::runtime_error(
"found an unknown field type!");
222 input.mesh_get_data(entity, varname, &data);
226 size_t dataSize = input.get_data_size(entity, varname);
229 std::shared_ptr<StateVectorUni<double>> pv =
230 std::make_shared<StateVectorUni<double>> (varname, data,
231 data+dataSize, entity);
253 std::vector<double>
pack(std::string field_name) {
256 std::shared_ptr<StateVectorBase> pv =
260 const std::type_info& data_type = pv->data_type();
269 if ( data_type ==
typeid(
double)){
287 std::unordered_map<int, std::vector<Wonton::Point<2>>> mm_data =
291 std::vector<double> result;
295 for (
auto& d : mm_data.at(
id)){
296 result.emplace_back(d[0]);
297 result.emplace_back(d[1]);
310 std::unordered_map<int, std::vector<Wonton::Point<3>>> mm_data =
314 std::vector<double> result;
318 for (
auto& d : mm_data.at(
id)){
319 result.emplace_back(d[0]);
320 result.emplace_back(d[1]);
321 result.emplace_back(d[2]);
326 }
else if (data_type ==
typeid(
double)){
334 std::unordered_map<int, std::vector<double>> mm_data =
338 std::vector<double> result;
342 for (
auto& d : mm_data.at(
id)){
343 result.emplace_back(d);
351 throw std::runtime_error(
"invalid field type");
363 void unpack(std::string field_name,
const std::vector<double> & flat_data,
364 const std::vector<int> & all_material_ids={},
365 const std::vector<int> & all_material_shapes={},
366 const std::map<int, std::vector<int>>& distributedMaterialCellIds_={} ) {
369 std::shared_ptr<StateVectorBase> pv =
373 const std::type_info& data_type = pv->data_type();
382 if ( data_type ==
typeid(
double)){
403 std::vector<Wonton::Point<2>> correctly_typed_data(flat_data.size()/2);
406 int const num_flat_data = flat_data.size();
407 for (
int i = 0; i < num_flat_data; i += 2) {
412 std::unordered_map<int,std::vector<Wonton::Point<2>>> material_data;
424 int running_counter=0;
427 int const num_all_material_ids = all_material_ids.size();
428 for (
int i = 0; i < num_all_material_ids; ++i) {
431 int mat_id = all_material_ids[i];
434 int nmat_cells = all_material_shapes[i];
437 auto& these_material_data = material_data[mat_id];
440 for (
int j=0; j<nmat_cells; ++j){
441 these_material_data.push_back(correctly_typed_data[running_counter++]);
447 for (
auto& kv: material_data){
455 std::vector<Wonton::Point<2>> & current_material_data=kv.second;
458 std::vector<int> distributedMaterialCellIds =distributedMaterialCellIds_.at(m);
461 std::vector<Wonton::Point<2>> merged_material_data(distributedMaterialCellIds.size());
465 for (
auto id: distributedMaterialCellIds)
466 merged_material_data[c++]=current_material_data[id];
477 std::vector<Wonton::Point<3>> correctly_typed_data(flat_data.size()/3);
480 int const num_flat_data = flat_data.size();
481 for (
int i = 0; i < num_flat_data; i += 3) {
482 correctly_typed_data[i/3] =
Wonton::Point<3>(flat_data[i], flat_data[i+1], flat_data[i+2]);
486 std::unordered_map<int,std::vector<Wonton::Point<3>>> material_data;
498 int running_counter=0;
501 int const num_all_material_ids = all_material_ids.size();
502 for (
int i = 0; i < num_all_material_ids; ++i) {
505 int mat_id = all_material_ids[i];
508 int nmat_cells = all_material_shapes[i];
511 auto& these_material_data = material_data[mat_id];
514 for (
int j=0; j<nmat_cells; ++j){
515 these_material_data.push_back(correctly_typed_data[running_counter++]);
522 for (
auto& kv: material_data){
530 std::vector<Wonton::Point<3>> & current_material_data=kv.second;
533 std::vector<int> distributedMaterialCellIds =distributedMaterialCellIds_.at(m);
536 std::vector<Wonton::Point<3>> merged_material_data(distributedMaterialCellIds.size());
540 for (
auto id: distributedMaterialCellIds)
541 merged_material_data[c++]=current_material_data[id];
547 }
else if ( data_type ==
typeid(
double)){
552 std::unordered_map<int,std::vector<double>> material_data;
564 int running_counter=0;
567 int const num_all_material_ids = all_material_ids.size();
568 for (
int i = 0; i < num_all_material_ids; ++i) {
571 int mat_id = all_material_ids[i];
574 int nmat_cells = all_material_shapes[i];
577 auto& these_material_data = material_data[mat_id];
580 for (
int j=0; j<nmat_cells; ++j){
581 these_material_data.push_back(flat_data[running_counter++]);
588 for (
auto& kv: material_data){
594 std::vector<double> & current_material_data=kv.second;
597 std::vector<int> distributedMaterialCellIds =distributedMaterialCellIds_.at(m);
600 std::vector<double> merged_material_data(distributedMaterialCellIds.size());
604 for (
auto id: distributedMaterialCellIds)
605 merged_material_data[c++]=current_material_data[id];
613 throw std::runtime_error(
"invalid field type");
622 std::shared_ptr<StateVectorBase> pv =
625 const std::type_info& data_type = pv->data_type();
627 if ( data_type ==
typeid(
double))
return 1;
631 throw std::runtime_error(
"invalid data type");
645 n += kv.second.size();
658 std::vector<int> material_ids;
661 std::sort(material_ids.begin(), material_ids.end());
675 std::vector<int> material_shapes;
677 for (
auto m : material_ids)
680 return material_shapes;
693 std::vector<int> material_cells;
695 for (
auto m : material_ids){
697 material_cells.insert(material_cells.end(), this_material_cells.begin(), this_material_cells.end());
700 return material_cells;
707 #endif // WONTON_STATE_FLAT_FLAT_STATE_MM_WRAPPER_H_ std::string material_name(int m) const
Return the name of a material from its material id.
Definition: state_manager.h:95
std::unordered_map< int, std::vector< int > > const & get_material_cells() const
Return the material cells.
Definition: state_manager.h:149
std::vector< int > get_material_ids() const
Return the sorted vector of material ids actually used in the cell mat data.
Definition: flat_state_mm_wrapper.h:656
Factorize a number N into D equal (or nearly equal) factors.
Definition: adaptive_refinement_mesh.h:31
std::vector< double > pack(std::string field_name)
Turn the field into a vector of doubles.
Definition: flat_state_mm_wrapper.h:253
Definition: state_vector_multi.h:28
A state manager wrapper that allows redistribution of data across nodes.
Definition: flat_state_mm_wrapper.h:33
std::vector< int > get_material_cells() const
Return the sorted vector of material shapes actually used in the cell mat data.
Definition: flat_state_mm_wrapper.h:690
void mat_add_celldata(std::string const &name, int m, T const *values)
Add cell data for a new material in a state vector to the state manager.
Definition: state_manager.h:751
std::vector< int > get_material_shapes() const
Return the sorted vector of material shapes actually used in the cell mat data.
Definition: flat_state_mm_wrapper.h:672
void mat_add_cells(int m, std::vector< int > const &newcells)
Add cells for a new material to the material cells.
Definition: state_manager.h:723
void add_material_names(const std::unordered_map< std::string, int > &names)
Add the names of the materials.
Definition: state_manager.h:61
int num_material_cells() const
Return the number of material cells for this node.
Definition: flat_state_mm_wrapper.h:642
Field_type
Field type - whether it is mesh field or multi-material field.
Definition: wonton.h:187
Represents a point in an N-dimensional space.
Definition: Point.h:50
size_t get_num_vectors()
Get the number of data vectors.
Definition: flat_state_mm_wrapper.h:243
void initialize(State_Wrapper const &input, std::vector< std::string > const &var_names)
Initialize the state wrapper with another state wrapper and a list of names.
Definition: flat_state_mm_wrapper.h:77
Entity_kind
The type of mesh entity.
Definition: wonton.h:81
Flat_State_Wrapper(const MeshWrapper &mesh, std::unordered_map< std::string, int > const &names={}, std::unordered_map< int, std::vector< int >> const &material_cells={})
Constructor for the state wrapper.
Definition: flat_state_mm_wrapper.h:45
Definition: state_manager.h:29
Field_type field_type(Entity_kind on_what, std::string const &var_name) const
Type of field (MESH_FIELD or MULTIMATERIAL_FIELD)
Definition: flat_state_wrapper.h:205
~Flat_State_Wrapper()=default
Destructor.
std::vector< std::string > const names() const
Return the names registered by the state manager.
Definition: state_manager.h:79
std::shared_ptr< StateVectorBase > get(std::string name)
Get a shared pointer to a StateVectorBase from the state manager.
Definition: state_manager.h:359
void add(std::shared_ptr< StateVectorBase > sv)
Add a StateVectorBase to the state manager.
Definition: state_manager.h:271
Definition: state_vector_uni.h:27
void clear()
Definition: state_manager.h:864
size_t get_field_stride(std::string field_name)
Get field stride.
Definition: flat_state_mm_wrapper.h:619
Flat_State_Wrapper & operator=(Flat_State_Wrapper const &)=delete
Assignment operator (disabled).
void unpack(std::string field_name, const std::vector< double > &flat_data, const std::vector< int > &all_material_ids={}, const std::vector< int > &all_material_shapes={}, const std::map< int, std::vector< int >> &distributedMaterialCellIds_={})
Turn a vector of doubles into the correct shape object.
Definition: flat_state_mm_wrapper.h:363