Chi-Tech
lbs_04b_angular_fluxes.cc
Go to the documentation of this file.
1#include "lbs_solver.h"
2
4
5#include "chi_runtime.h"
6#include "chi_log.h"
8
9
10#include <fstream>
11#include <cstring>
12
13//###################################################################
14/**Writes the groupset's angular fluxes to file.*/
17 const std::string& file_base)
18{
19 std::string file_name =
20 file_base + std::to_string(Chi::mpi.location_id) + ".data";
21
22 //============================================= Open file
23 std::ofstream file(file_name,
24 std::ofstream::binary | //binary file
25 std::ofstream::out | //no accidental reading
26 std::ofstream::trunc); //clear file contents when opened
27
28 //============================================= Check file is open
29 if (not file.is_open())
30 {
32 << __FUNCTION__ << "Failed to open " << file_name;
33 return;
34 }
35
36 //============================================= Write header
37 std::string header_info =
38 "Chi-Tech LinearBoltzmann::Groupset angular flux file\n"
39 "Header size: 320 bytes\n"
40 "Structure(type-info):\n"
41 "size_t-num_local_nodes\n"
42 "size_t-num_angles\n"
43 "size_t-num_groups\n"
44 "size_t-num_records\n"
45 "Each record:\n"
46 "size_t-cell_global_id\n"
47 "unsigned int-node_number\n"
48 "unsigned int-angle_num_\n"
49 "unsigned int-group_num\n"
50 "double-angular_flux\n";
51
52 int header_size = (int)header_info.length();
53
54 char header_bytes[320];
55 memset(header_bytes, '-', 320);
56 strncpy(header_bytes, header_info.c_str(),std::min(header_size,319));
57 header_bytes[319]='\0';
58
59 file << header_bytes;
60
61 //============================================= Get relevant items
63
64 size_t num_local_nodes = discretization_->GetNumLocalDOFs(NODES_ONLY);
65 size_t num_angles = groupset.quadrature_->abscissae_.size();
66 size_t num_groups = groupset.groups_.size();
67 size_t num_local_dofs = psi_new_local_[groupset.id_].size();
68 auto dof_handler = groupset.psi_uk_man_;
69
70 //============================================= Write num_ quantities
71 file.write((char*)&num_local_nodes,sizeof(size_t));
72 file.write((char*)&num_angles ,sizeof(size_t));
73 file.write((char*)&num_groups ,sizeof(size_t));
74 file.write((char*)&num_local_dofs ,sizeof(size_t));
75
76 auto& sdm = discretization_;
77
78 //============================================= Write per dof data
79 size_t dof_count=0;
80 for (const auto& cell : grid_ptr_->local_cells)
81 {
82 const size_t num_nodes = sdm->GetCellNumNodes(cell);
83 for (unsigned int i=0; i < num_nodes; ++i)
84 for (unsigned int n=0; n<num_angles; ++n)
85 for (unsigned int g=0; g<num_groups; ++g)
86 {
87 if (++dof_count > num_local_dofs) goto close_file;
88
89 uint64_t dof_map = sdm->MapDOFLocal(cell,i,dof_handler,n,g);
90 double value = psi_new_local_[groupset.id_][dof_map];
91
92 file.write((char*)&cell.global_id_, sizeof(size_t));
93 file.write((char*)&i ,sizeof(unsigned int));
94 file.write((char*)&n ,sizeof(unsigned int));
95 file.write((char*)&g ,sizeof(unsigned int));
96 file.write((char*)&value ,sizeof(double));
97 }
98 }
99
100 //============================================= Clean-up
101 close_file:
102 file.close();
103}
104
105//###################################################################
106/**Prints the groupset's angular fluxes to file.*/
109 const std::string& file_base)
110{
111 std::string file_name =
112 file_base + std::to_string(Chi::mpi.location_id) + ".data";
113
114 //============================================= Open file
115 std::ifstream file(file_name,
116 std::ofstream::binary | //binary file
117 std::ofstream::in); //no accidental writing
118
119 //============================================= Check file is open
120 if (not file.is_open())
121 {
123 << __FUNCTION__ << "Failed to open " << file_name;
124 return;
125 }
126
127 //============================================= Get relevant items
129
130 size_t num_local_nodes = discretization_->GetNumLocalDOFs(NODES_ONLY);
131 size_t num_angles = groupset.quadrature_->abscissae_.size();
132 size_t num_groups = groupset.groups_.size();
133 size_t num_local_dofs = psi_new_local_[groupset.id_].size();
134 std::vector<double>& psi = psi_new_local_[groupset.id_];
135 auto dof_handler = groupset.psi_uk_man_;
136
137 size_t file_num_local_nodes;
138 size_t file_num_angles ;
139 size_t file_num_groups ;
140 size_t file_num_local_dofs ;
141
142
143 //============================================= Read header
144 Chi::log.Log() << "Reading angular flux file " << file_name;
145 char header_bytes[320]; header_bytes[319] = '\0';
146 file.read(header_bytes,319);
147
148 file.read((char*)&file_num_local_nodes, sizeof(size_t));
149 file.read((char*)&file_num_angles , sizeof(size_t));
150 file.read((char*)&file_num_groups , sizeof(size_t));
151 file.read((char*)&file_num_local_dofs , sizeof(size_t));
152
153 //============================================= Check compatibility
154 if (file_num_local_nodes != num_local_nodes or
155 file_num_angles != num_angles or
156 file_num_groups != num_groups or
157 file_num_local_dofs != num_local_dofs)
158 {
159 std::stringstream outstr;
160 outstr << "num_local_nodes: " << file_num_local_nodes << "\n";
161 outstr << "num_angles : " << file_num_angles << "\n";
162 outstr << "num_groups : " << file_num_groups << "\n";
163 outstr << "num_local_dofs : " << file_num_local_dofs << "\n";
165 << "Incompatible DOF data found in file " << file_name << "\n"
166 << outstr.str();
167 file.close();
168 return;
169 }
170
171 auto& sdm = discretization_;
172
173 //============================================= Commit to reading the file
174 psi.reserve(file_num_local_dofs);
175 std::set<uint64_t> cells_touched;
176 for (size_t dof=0; dof < file_num_local_dofs; ++dof)
177 {
178 uint64_t cell_global_id;
179 unsigned int node;
180 unsigned int angle_num;
181 unsigned int group;
182 double psi_value;
183
184 file.read((char*)&cell_global_id,sizeof(uint64_t));
185 file.read((char*)&node ,sizeof(unsigned int));
186 file.read((char*)&angle_num ,sizeof(unsigned int));
187 file.read((char*)&group ,sizeof(unsigned int));
188 file.read((char*)&psi_value ,sizeof(double));
189
190 cells_touched.insert(cell_global_id);
191
192 const auto& cell = grid_ptr_->cells[cell_global_id];
193
194 size_t imap = sdm->MapDOFLocal(cell,node,dof_handler,angle_num,group);
195
196 psi[imap] = psi_value;
197 }
198
199 Chi::log.LogAll() << "Number of cells read: " << cells_touched.size();
200
201 //============================================= Clean-up
202 file.close();
203}
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream LogAll()
Definition: chi_log.h:237
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
LogStream LogAllWarning()
Definition: chi_log.h:238
static UnknownManager GetUnitaryUnknownManager()
std::shared_ptr< chi_math::AngularQuadrature > quadrature_
Definition: lbs_groupset.h:42
chi_math::UnknownManager psi_uk_man_
Definition: lbs_groupset.h:83
std::vector< LBSGroup > groups_
Definition: lbs_groupset.h:41
std::vector< std::vector< double > > psi_new_local_
Definition: lbs_solver.h:96
chi_mesh::MeshContinuumPtr grid_ptr_
Definition: lbs_solver.h:75
std::shared_ptr< chi_math::SpatialDiscretization > discretization_
Definition: lbs_solver.h:74
void ReadGroupsetAngularFluxes(LBSGroupset &groupset, const std::string &file_base)
void WriteGroupsetAngularFluxes(const LBSGroupset &groupset, const std::string &file_base)