Chi-Tech
chi_volumemesher_01c_set_IDs.cc
Go to the documentation of this file.
1#include "chi_volumemesher.h"
2
6
7#include "chi_runtime.h"
8#include "chi_log.h"
10
11#include "utils/chi_timer.h"
12#include "chi_lua.h"
13
14
15//###################################################################
16/**Sets material id's using a logical volume.*/
19 bool sense, int mat_id)
20{
23 << " Setting material id from logical volume.";
24 //============================================= Get current mesh handler
25 auto& handler = chi_mesh::GetCurrentHandler();
26
27 //============================================= Get back mesh
28 chi_mesh::MeshContinuumPtr vol_cont = handler.GetGrid();
29
30 int num_cells_modified = 0;
31 for (auto& cell : vol_cont->local_cells)
32 {
33 if (log_vol.Inside(cell.centroid_) && sense){
34 cell.material_id_ = mat_id;
35 ++num_cells_modified;
36 }
37 }
38
39 const auto& ghost_ids = vol_cont->cells.GetGhostGlobalIDs();
40 for (uint64_t ghost_id : ghost_ids)
41 {
42 auto& cell = vol_cont->cells[ghost_id];
43 if (log_vol.Inside(cell.centroid_) && sense)
44 cell.material_id_ = mat_id;
45 }
46
47 int global_num_cells_modified;
48 MPI_Allreduce(&num_cells_modified, //sendbuf
49 &global_num_cells_modified, //recvbuf
50 1, MPI_INT, //count + datatype
51 MPI_SUM, //operation
52 Chi::mpi.comm); //comm
53
56 << " Done setting material id from logical volume. "
57 << "Number of cells modified = " << global_num_cells_modified << ".";
58}
59
60//###################################################################
61/**Sets material id's using a logical volume.*/
64 bool sense,
65 const std::string& bndry_name)
66{
69 << " Setting boundary id from logical volume.";
70 //============================================= Get current mesh handler
71 auto& handler = chi_mesh::GetCurrentHandler();
72
73 //============================================= Get back mesh
74 chi_mesh::MeshContinuumPtr vol_cont = handler.GetGrid();
75
76 //============================================= Check if name already has id
77 auto& grid_bndry_id_map = vol_cont->GetBoundaryIDMap();
78 uint64_t bndry_id = vol_cont->MakeBoundaryID(bndry_name);
79
80 //============================================= Loop over cells
81 int num_faces_modified = 0;
82 for (auto& cell : vol_cont->local_cells)
83 {
84 for (auto& face : cell.faces_)
85 {
86 if (face.has_neighbor_) continue;
87 if (log_vol.Inside(face.centroid_) && sense){
88 face.neighbor_id_ = bndry_id;
89 ++num_faces_modified;
90 }
91 }
92 }
93
94 int global_num_faces_modified;
95 MPI_Allreduce(&num_faces_modified, //sendbuf
96 &global_num_faces_modified, //recvbuf
97 1, MPI_INT, //count + datatype
98 MPI_SUM, //operation
99 Chi::mpi.comm); //comm
100
101
102 if (global_num_faces_modified > 0 and
103 grid_bndry_id_map.count(bndry_id) == 0)
104 grid_bndry_id_map[bndry_id] = bndry_name;
105
106 Chi::log.Log()
108 << " Done setting boundary id from logical volume. "
109 << "Number of faces modified = " << global_num_faces_modified << ".";
110}
111
112//###################################################################
113/**Sets material id's for all cells to the specified material id.*/
115{
116 Chi::log.Log()
118 << " Setting material id " << mat_id << " to all cells.";
119
120 //============================================= Get current mesh handler
121 auto& handler = chi_mesh::GetCurrentHandler();
122
123 //============================================= Get back mesh
124 auto vol_cont = handler.GetGrid();
125
126 for (auto& cell : vol_cont->local_cells)
127 cell.material_id_ = mat_id;
128
129 const auto& ghost_ids = vol_cont->cells.GetGhostGlobalIDs();
130 for (uint64_t ghost_id : ghost_ids)
131 vol_cont->cells[ghost_id].material_id_ = mat_id;
132
134 Chi::log.Log()
136 << " Done setting material id " << mat_id << " to all cells";
137}
138
139
140//###################################################################
141/**Sets material id's using a lua function. The lua function is called
142with for each cell with 4 arguments, the cell's centroid x,y,z values
143and the cell's current material id.
144
145The lua function's prototype should be:
146\code
147function LuaFuncName(x,y,z,id)
148 --stuff
149end
150\endcode
151*/
153 SetMatIDFromLuaFunction(const std::string& lua_fname)
154{
155 const std::string fname = "chi_mesh::VolumeMesher::SetMatIDFromLuaFunction";
156
159 << " Setting material id from lua function.";
160
161 //============================================= Define console call
162 auto L = Chi::console.GetConsoleState();
163 auto CallLuaXYZFunction = [&L,&lua_fname,&fname](const chi_mesh::Cell& cell)
164 {
165 //============= Load lua function
166 lua_getglobal(L, lua_fname.c_str());
167
168 //============= Error check lua function
169 if (not lua_isfunction(L, -1))
170 throw std::logic_error(fname + " attempted to access lua-function, " +
171 lua_fname + ", but it seems the function"
172 " could not be retrieved.");
173
174 const auto& xyz = cell.centroid_;
175
176 //============= Push arguments
177 lua_pushnumber(L, xyz.x);
178 lua_pushnumber(L, xyz.y);
179 lua_pushnumber(L, xyz.z);
180 lua_pushinteger(L, cell.material_id_);
181
182 //============= Call lua function
183 //4 arguments, 1 result (double), 0=original error object
184 int lua_return;
185 if (lua_pcall(L,4,1,0) == 0)
186 {
187 LuaCheckNumberValue(fname, L, -1);
188 lua_return = lua_tointeger(L,-1);
189 }
190 else
191 throw std::logic_error(fname + " attempted to call lua-function, " +
192 lua_fname + ", but the call failed.");
193
194 lua_pop(L,1); //pop the int, or error code
195
196 return lua_return;
197 };
198
199 //============================================= Get current mesh handler
200 auto& handler = chi_mesh::GetCurrentHandler();
201
202 //============================================= Get back mesh
203 chi_mesh::MeshContinuum& grid = *handler.GetGrid();
204
205 int local_num_cells_modified = 0;
206 for (auto& cell : grid.local_cells)
207 {
208 int new_matid = CallLuaXYZFunction(cell);
209
210 if (cell.material_id_ != new_matid)
211 {
212 cell.material_id_ = new_matid;
213 ++local_num_cells_modified;
214 }
215 }//for local cell
216
217 const auto& ghost_ids = grid.cells.GetGhostGlobalIDs();
218 for (uint64_t ghost_id : ghost_ids)
219 {
220 auto& cell = grid.cells[ghost_id];
221 int new_matid = CallLuaXYZFunction(cell);
222
223 if (cell.material_id_ != new_matid)
224 {
225 cell.material_id_ = new_matid;
226 ++local_num_cells_modified;
227 }
228 }//for ghost cell id
229
230 int globl_num_cells_modified;
231 MPI_Allreduce(&local_num_cells_modified, //sendbuf
232 &globl_num_cells_modified, //recvbuf
233 1, MPI_INT, //count+datatype
234 MPI_SUM, //operation
235 Chi::mpi.comm); //comm
236
239 << " Done setting material id from lua function. "
240 << "Number of cells modified = " << globl_num_cells_modified << ".";
241}
242
243
244//###################################################################
245/**Sets boundary id's using a lua function. The lua function is called
246for each boundary face with 7 arguments, the face's centroid x,y,z values,
247the face's normal x,y,z values and the face's current boundary id. The function
248must return a new_bndry_name (string).
249
250The lua function's prototype should be:
251\code
252function LuaFuncName(x,y,z,nx,ny,nz,id)
253 --stuff
254end
255\endcode
256*/
258 SetBndryIDFromLuaFunction(const std::string& lua_fname)
259{
260 const std::string fname = "chi_mesh::VolumeMesher::SetBndryIDFromLuaFunction";
261
262 if (Chi::mpi.process_count != 1)
263 throw std::logic_error(fname + ": Can for now only be used in serial.");
264
267 << " Setting boundary id from lua function.";
268
269 //============================================= Define console call
270 auto L = Chi::console.GetConsoleState();
271 auto CallLuaXYZFunction = [&L,&lua_fname,&fname]
272 (const chi_mesh::CellFace& face)
273 {
274 //============= Load lua function
275 lua_getglobal(L, lua_fname.c_str());
276
277 //============= Error check lua function
278 if (not lua_isfunction(L, -1))
279 throw std::logic_error(fname + " attempted to access lua-function, " +
280 lua_fname + ", but it seems the function"
281 " could not be retrieved.");
282
283 const auto& xyz = face.centroid_;
284 const auto& n = face.normal_;
285
286 //============= Push arguments
287 lua_pushnumber(L, xyz.x);
288 lua_pushnumber(L, xyz.y);
289 lua_pushnumber(L, xyz.z);
290 lua_pushnumber(L, n.x);
291 lua_pushnumber(L, n.y);
292 lua_pushnumber(L, n.z);
293 lua_pushinteger(L, static_cast<lua_Integer>(face.neighbor_id_));
294
295 //============= Call lua function
296 //7 arguments, 1 result (string), 0=original error object
297 std::string lua_return_bname;
298 if (lua_pcall(L,7,1,0) == 0)
299 {
300 LuaCheckNumberValue(fname, L, -1);
301 LuaCheckStringValue(fname, L, -2);
302 lua_return_bname = lua_tostring(L,-1);
303 }
304 else
305 throw std::logic_error(fname + " attempted to call lua-function, " +
306 lua_fname + ", but the call failed.");
307
308 lua_pop(L,1); //pop the string, or error code
309
310 return lua_return_bname;
311 };
312
313 //============================================= Get current mesh handler
314 auto& handler = chi_mesh::GetCurrentHandler();
315
316 //============================================= Get back mesh
317 chi_mesh::MeshContinuum& grid = *handler.GetGrid();
318
319 //============================================= Check if name already has id
320 auto& grid_bndry_id_map = grid.GetBoundaryIDMap();
321
322 int local_num_faces_modified = 0;
323 for (auto& cell : grid.local_cells)
324 for (auto& face : cell.faces_)
325 if (not face.has_neighbor_)
326 {
327 const std::string bndry_name = CallLuaXYZFunction(face);
328 const uint64_t bndry_id = grid.MakeBoundaryID(bndry_name);
329
330 if (face.neighbor_id_ != bndry_id)
331 {
332 face.neighbor_id_ = bndry_id;
333 ++local_num_faces_modified;
334
335 if (grid_bndry_id_map.count(bndry_id) == 0)
336 grid_bndry_id_map[bndry_id] = bndry_name;
337 }
338 }//for bndry face
339
340 const auto& ghost_ids = grid.cells.GetGhostGlobalIDs();
341 for (uint64_t ghost_id : ghost_ids)
342 {
343 auto& cell = grid.cells[ghost_id];
344 for (auto& face : cell.faces_)
345 if (not face.has_neighbor_)
346 {
347 const std::string bndry_name = CallLuaXYZFunction(face);
348 const uint64_t bndry_id = grid.MakeBoundaryID(bndry_name);
349
350 if (face.neighbor_id_ != bndry_id)
351 {
352 face.neighbor_id_ = bndry_id;
353 ++local_num_faces_modified;
354
355 if (grid_bndry_id_map.count(bndry_id) == 0)
356 grid_bndry_id_map[bndry_id] = bndry_name;
357 }
358 }//for bndry face
359 }//for ghost cell id
360
361 int globl_num_faces_modified;
362 MPI_Allreduce(&local_num_faces_modified, //sendbuf
363 &globl_num_faces_modified, //recvbuf
364 1, MPI_INT, //count+datatype
365 MPI_SUM, //operation
366 Chi::mpi.comm); //comm
367
370 << " Done setting boundary id from lua function. "
371 << "Number of cells modified = " << globl_num_faces_modified << ".";
372}
static chi::Timer program_timer
Definition: chi_runtime.h:79
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
static chi::Console & console
Definition: chi_runtime.h:80
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
LogStream Log0Verbose1()
Definition: chi_log.h:234
lua_State *& GetConsoleState()
Definition: chi_console.h:130
void Barrier() const
Definition: mpi_info.cc:38
std::string GetTimeString() const
Definition: chi_timer.cc:38
std::vector< uint64_t > GetGhostGlobalIDs() const
virtual bool Inside(const chi_mesh::Vector3 &point) const
Definition: LogicalVolume.h:20
LocalCellHandler local_cells
std::map< uint64_t, std::string > & GetBoundaryIDMap()
GlobalCellHandler cells
uint64_t MakeBoundaryID(const std::string &boundary_name) const
static void SetBndryIDFromLogical(const chi_mesh::LogicalVolume &log_vol, bool sense, const std::string &bndry_name)
static void SetMatIDFromLogical(const chi_mesh::LogicalVolume &log_vol, bool sense, int mat_id)
static void SetMatIDFromLuaFunction(const std::string &lua_fname)
static void SetBndryIDFromLuaFunction(const std::string &lua_fname)
static void SetMatIDToAll(int mat_id)
std::shared_ptr< MeshContinuum > MeshContinuumPtr
Definition: chi_mesh.h:44
MeshHandler & GetCurrentHandler()