19 bool sense,
int mat_id)
23 <<
" Setting material id from logical volume.";
30 int num_cells_modified = 0;
31 for (
auto& cell : vol_cont->local_cells)
33 if (log_vol.
Inside(cell.centroid_) && sense){
34 cell.material_id_ = mat_id;
39 const auto& ghost_ids = vol_cont->cells.GetGhostGlobalIDs();
40 for (uint64_t ghost_id : ghost_ids)
42 auto& cell = vol_cont->cells[ghost_id];
43 if (log_vol.
Inside(cell.centroid_) && sense)
44 cell.material_id_ = mat_id;
47 int global_num_cells_modified;
48 MPI_Allreduce(&num_cells_modified,
49 &global_num_cells_modified,
56 <<
" Done setting material id from logical volume. "
57 <<
"Number of cells modified = " << global_num_cells_modified <<
".";
65 const std::string& bndry_name)
69 <<
" Setting boundary id from logical volume.";
77 auto& grid_bndry_id_map = vol_cont->GetBoundaryIDMap();
78 uint64_t bndry_id = vol_cont->MakeBoundaryID(bndry_name);
81 int num_faces_modified = 0;
82 for (
auto& cell : vol_cont->local_cells)
84 for (
auto& face : cell.faces_)
86 if (face.has_neighbor_)
continue;
87 if (log_vol.
Inside(face.centroid_) && sense){
88 face.neighbor_id_ = bndry_id;
94 int global_num_faces_modified;
95 MPI_Allreduce(&num_faces_modified,
96 &global_num_faces_modified,
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;
108 <<
" Done setting boundary id from logical volume. "
109 <<
"Number of faces modified = " << global_num_faces_modified <<
".";
118 <<
" Setting material id " << mat_id <<
" to all cells.";
124 auto vol_cont = handler.GetGrid();
126 for (
auto& cell : vol_cont->local_cells)
127 cell.material_id_ = mat_id;
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;
136 <<
" Done setting material id " << mat_id <<
" to all cells";
155 const std::string fname =
"chi_mesh::VolumeMesher::SetMatIDFromLuaFunction";
159 <<
" Setting material id from lua function.";
163 auto CallLuaXYZFunction = [&L,&lua_fname,&fname](
const chi_mesh::Cell& cell)
166 lua_getglobal(L, lua_fname.c_str());
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.");
174 const auto& xyz = cell.centroid_;
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_);
185 if (lua_pcall(L,4,1,0) == 0)
187 LuaCheckNumberValue(fname, L, -1);
188 lua_return = lua_tointeger(L,-1);
191 throw std::logic_error(fname +
" attempted to call lua-function, " +
192 lua_fname +
", but the call failed.");
205 int local_num_cells_modified = 0;
208 int new_matid = CallLuaXYZFunction(cell);
210 if (cell.material_id_ != new_matid)
212 cell.material_id_ = new_matid;
213 ++local_num_cells_modified;
218 for (uint64_t ghost_id : ghost_ids)
220 auto& cell = grid.
cells[ghost_id];
221 int new_matid = CallLuaXYZFunction(cell);
223 if (cell.material_id_ != new_matid)
225 cell.material_id_ = new_matid;
226 ++local_num_cells_modified;
230 int globl_num_cells_modified;
231 MPI_Allreduce(&local_num_cells_modified,
232 &globl_num_cells_modified,
239 <<
" Done setting material id from lua function. "
240 <<
"Number of cells modified = " << globl_num_cells_modified <<
".";
260 const std::string fname =
"chi_mesh::VolumeMesher::SetBndryIDFromLuaFunction";
263 throw std::logic_error(fname +
": Can for now only be used in serial.");
267 <<
" Setting boundary id from lua function.";
271 auto CallLuaXYZFunction = [&L,&lua_fname,&fname]
275 lua_getglobal(L, lua_fname.c_str());
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.");
283 const auto& xyz = face.centroid_;
284 const auto& n = face.normal_;
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_));
297 std::string lua_return_bname;
298 if (lua_pcall(L,7,1,0) == 0)
300 LuaCheckNumberValue(fname, L, -1);
301 LuaCheckStringValue(fname, L, -2);
302 lua_return_bname = lua_tostring(L,-1);
305 throw std::logic_error(fname +
" attempted to call lua-function, " +
306 lua_fname +
", but the call failed.");
310 return lua_return_bname;
322 int local_num_faces_modified = 0;
324 for (
auto& face : cell.faces_)
325 if (not face.has_neighbor_)
327 const std::string bndry_name = CallLuaXYZFunction(face);
330 if (face.neighbor_id_ != bndry_id)
332 face.neighbor_id_ = bndry_id;
333 ++local_num_faces_modified;
335 if (grid_bndry_id_map.count(bndry_id) == 0)
336 grid_bndry_id_map[bndry_id] = bndry_name;
341 for (uint64_t ghost_id : ghost_ids)
343 auto& cell = grid.
cells[ghost_id];
344 for (
auto& face : cell.faces_)
345 if (not face.has_neighbor_)
347 const std::string bndry_name = CallLuaXYZFunction(face);
350 if (face.neighbor_id_ != bndry_id)
352 face.neighbor_id_ = bndry_id;
353 ++local_num_faces_modified;
355 if (grid_bndry_id_map.count(bndry_id) == 0)
356 grid_bndry_id_map[bndry_id] = bndry_name;
361 int globl_num_faces_modified;
362 MPI_Allreduce(&local_num_faces_modified,
363 &globl_num_faces_modified,
370 <<
" Done setting boundary id from lua function. "
371 <<
"Number of cells modified = " << globl_num_faces_modified <<
".";
static chi::Timer program_timer
static chi::MPI_Info & mpi
static chi::Console & console
LogStream Log(LOG_LVL level=LOG_0)
lua_State *& GetConsoleState()
std::string GetTimeString() const
std::vector< uint64_t > GetGhostGlobalIDs() const
virtual bool Inside(const chi_mesh::Vector3 &point) const
LocalCellHandler local_cells
std::map< uint64_t, std::string > & GetBoundaryIDMap()
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
MeshHandler & GetCurrentHandler()