20 const std::string fname = __FUNCTION__;
24 std::set<int> set_group_numbers;
26 for (
const auto& group : groupset.groups_)
27 set_group_numbers.insert(group.id_);
29 const auto& m_to_ell_em_map =
30 groupsets_.front().quadrature_->GetMomentToHarmonicsIndexMap();
33 typedef std::vector<Arr4> MGVec4;
34 typedef std::vector<MGVec4> VecOfMGVec4;
35 const size_t num_groups = set_group_numbers.size();
36 const size_t num_cells =
grid_ptr_->local_cells.size();
38 VecOfMGVec4 cell_avg_p1_moments(num_cells, MGVec4(num_groups));
41 for (
const auto& cell :
grid_ptr_->local_cells)
44 const int num_nodes = cell_view.NumNodes();
47 VecOfMGVec4 nodal_p1_moments(num_nodes);
48 for (
int i = 0; i < num_nodes; ++i)
51 MGVec4 p1_moments(set_group_numbers.size(),
VecDbl{0.0,0.0,0.0,0.0});
52 for (
int m=0; m < std::max(static_cast<int>(
num_moments_), 4); ++m)
54 const auto& ell = m_to_ell_em_map[m].ell;
55 const auto& em = m_to_ell_em_map[m].m;
57 size_t dof_map_g0 = cell_view.MapDOF(i, m, 0);
59 for (
int g : set_group_numbers)
61 if (ell==0 and em== 0) p1_moments[g](0) = std::fabs(
phi_old_local_[dof_map_g0 + g]);
62 if (ell==1 and em== 1) p1_moments[g](1) =
phi_old_local_[dof_map_g0 + g];
63 if (ell==1 and em==-1) p1_moments[g](2) =
phi_old_local_[dof_map_g0 + g];
64 if (ell==1 and em== 0) p1_moments[g](3) =
phi_old_local_[dof_map_g0 + g];
68 nodal_p1_moments[i] = std::move(p1_moments);
73 for (
int g : set_group_numbers)
77 double volume_total = 0.0;
78 for (
int i=0; i < num_nodes; ++i)
80 double IntV_shapeI = fe_values.Vi_vectors[i];
81 cell_p1_avg += nodal_p1_moments[i][g] * IntV_shapeI;
82 volume_total += IntV_shapeI;
84 cell_p1_avg /= volume_total;
86 cell_avg_p1_moments[cell.local_id_][g] = cell_p1_avg;
93 typedef std::pair<double, double> ABCoeffsPair;
94 typedef std::vector<ABCoeffsPair> VecOfABCoeffsPair;
95 typedef std::vector<VecOfABCoeffsPair> ExpReps;
96 ExpReps cell_exp_reps(num_cells,VecOfABCoeffsPair(num_groups, {0.0,0.0}));
98 for (
const auto& cell :
grid_ptr_->local_cells)
100 for (
int g : set_group_numbers)
102 const auto& p1_moments = cell_avg_p1_moments[cell.local_id_][g];
109 cell_exp_reps[cell.local_id_][g] = std::make_pair(a_b[0], a_b[1]);
114 Chi::log.
Log() <<
"Exporting importance map to binary file " << file_name;
116 const auto locJ_io_flags = std::ofstream::binary | std::ofstream::out;
117 const auto loc0_io_flags = locJ_io_flags | std::ofstream::trunc;
121 std::string header_info =
122 "Chi-Tech LinearBoltzmann: Importance map file\n"
123 "Header size: 500 bytes\n"
124 "Structure(type-info):\n"
125 "uint64_t num_global_cells\n"
126 "uint64_t num_groups\n"
127 "uint64_t num_records\n"
129 " uint64_t cell_global_id\n"
130 " unsigned int group_num\n"
135 " double a_coefficient\n"
136 " double b_coefficient\n";
138 int header_size = (int)header_info.length();
140 char header_bytes[400];
141 memset(header_bytes,
'-', 400);
142 strncpy(header_bytes, header_info.c_str(),std::min(header_size,399));
143 header_bytes[399]=
'\0';
146 uint64_t num_global_cells =
grid_ptr_->GetGlobalNumberOfCells();
151 if (
Chi::mpi.location_id != locationJ)
continue;
153 Chi::log.
LogAll() <<
" Location " << locationJ <<
" appending data.";
155 std::ofstream file(file_name, is_home? loc0_io_flags : locJ_io_flags);
157 if (not file.is_open())
159 std::stringstream outstr;
162 <<
", failed to open file " << file_name;
163 throw std::logic_error(outstr.str());
168 file << header_bytes;
169 uint64_t num_groups_t =
groups_.size();
170 uint64_t num_records = num_global_cells * num_groups;
172 file.write((
char*)&num_global_cells ,
sizeof(uint64_t));
173 file.write((
char*)&num_groups_t ,
sizeof(uint64_t));
174 file.write((
char*)&num_records ,
sizeof(uint64_t));
177 for (
const auto& cell :
grid_ptr_->local_cells)
179 MGVec4 p1_moments = cell_avg_p1_moments[cell.local_id_];
180 VecOfABCoeffsPair exp_rep = cell_exp_reps[cell.local_id_];
182 auto cell_global_id =
static_cast<uint64_t
>(cell.global_id_);
183 for (
int group : set_group_numbers)
185 auto g =
static_cast<unsigned int>(group);
186 file.write((
char *) &cell_global_id,
sizeof(uint64_t));
187 file.write((
char *) &g,
sizeof(
unsigned int));
189 for (
int m=0; m<4; ++m)
190 file.write((
char *) &p1_moments[group](m),
sizeof(
double));
192 file.write((
char *) &exp_rep[group].first ,
sizeof(double));
193 file.write((
char *) &exp_rep[group].second,
sizeof(double));
200 Chi::log.
LogAll() <<
"Done exporting importance map to binary file " << file_name;
static chi::MPI_Info & mpi
LogStream Log(LOG_LVL level=LOG_0)
const int & process_count
Total number of processes.
const int & location_id
Current process rank.
void ExportImportanceMap(const std::string &file_name)
chi_mesh::MeshContinuumPtr grid_ptr_
std::vector< lbs::CellLBSView > cell_transport_views_
std::vector< UnitCellMatrices > unit_cell_matrices_
std::vector< LBSGroup > groups_
std::vector< double > phi_old_local_
std::vector< LBSGroupset > groupsets_
std::array< double, 2 > MakeExpRepFromP1(const std::array< double, 4 > &P1_moments, bool verbose=false)
std::vector< double > VecDbl