ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
BuildingsIF Class Reference

Buildings implicit function backed by STL triangle mesh. More...

#include <ERF_EBIFBuildings.H>

Collaboration diagram for BuildingsIF:

Public Member Functions

 BuildingsIF (std::string const &stl_file, amrex::Real scale, amrex::Array< amrex::Real, 3 > const &center, int reverse_normal, amrex::Geometry const &geom)
 Construct a buildings implicit function from an STL file. More...
 
BuildingsLevelsetIF getIF () const &noexcept
 Return the GPU-copyable evaluator for the sampled levelset. More...
 
BuildingsLevelsetIF getIF () const &&=delete
 

Protected Attributes

amrex::Geometry m_geom
 Domain geometry (for coordinate transformations) More...
 
amrex::STLtools m_stl_tools
 STL tools object (for loading and processing STL files) More...
 
std::shared_ptr< amrex::MultiFab > m_levelset_mf
 Owns the replicated levelset referenced by m_if on each rank. More...
 
BuildingsLevelsetIF m_if
 

Detailed Description

Buildings implicit function backed by STL triangle mesh.

This host-side class owns the sampled data for building geometry loaded from STL files. It uses AMReX's STL infrastructure with BVH (Bounding Volume Hierarchy) acceleration for efficient signed distance queries.

The evaluator returned by getIF() evaluates signed distance, following the AMReX EB convention: positive = inside solid (building), zero = boundary, negative = fluid.

Note: This class pre-samples the STL geometry onto a reference grid and interpolates at query points. For very large or complex geometries, consider using AMReX's IndexSpaceSTL directly instead.

Constructor & Destructor Documentation

◆ BuildingsIF()

BuildingsIF::BuildingsIF ( std::string const &  stl_file,
amrex::Real  scale,
amrex::Array< amrex::Real, 3 > const &  center,
int  reverse_normal,
amrex::Geometry const &  geom 
)
inline

Construct a buildings implicit function from an STL file.

Parameters
stl_filePath to the STL mesh file (binary or ASCII format).
scaleUniform scaling factor applied to STL coordinates (typically 1.0).
centerTranslation vector to recenter the mesh [m].
reverse_normalNon-zero to flip triangle normals if needed.
geomDomain geometry for grid setup and BVH optimization.

The constructor:

  1. Loads the STL file using AMReX::STLtools
  2. Builds a BVH acceleration structure for fast distance queries
  3. Pre-samples signed distances onto a reference grid
  4. Stores interpolation data for the evaluator returned by getIF()
179  : m_geom(geom)
180  {
181  using namespace amrex;
182 
183  // Load STL file with BVH optimization
184  // This broadcasts triangle data to all ranks internally
185  m_stl_tools.setBVHOptimization(true);
186  m_stl_tools.read_stl_file(stl_file, scale, center, reverse_normal);
187 
188  // Pre-sample signed distance field onto a reference grid
189  // This grid covers the entire domain with sufficient resolution
190  // to capture building features (match computational grid)
191  Box const& domain = geom.Domain();
192  IntVect ngrow_vec(2); // Ghost cells for interpolation
193 
194  Box sampled_box = domain;
195  sampled_box.grow(ngrow_vec);
196 
197  // Create distributed BoxArray and DistributionMapping for parallel computation
198  // Chunk the domain into reasonable-sized boxes for load balancing
199  BoxArray ba(sampled_box);
200  ba.maxSize(32);
201  DistributionMapping dm(ba);
202 
203  // Create distributed MultiFab to hold signed distance values
204  // Each rank will compute its portion in parallel
205  MultiFab mf_distributed(ba, dm, 1, ngrow_vec);
206 
207  // Compute signed distances in parallel across all ranks
208  // fillSignedDistance uses ParallelFor internally, distributing work
209  // across the MultiFab's boxes according to the DistributionMapping
210  m_stl_tools.fillSignedDistance(mf_distributed, ngrow_vec, geom);
211 
212  // Now create a replicated MultiFab where each rank has a copy of the full domain
213  // This is needed because operator() must access data locally (no MPI in GPU kernels)
214 
215  // Create BoxArray with one box per rank, all covering the same spatial region
216  int nprocs = ParallelDescriptor::NProcs();
217  Vector<Box> boxes(nprocs, sampled_box); // All boxes cover the full domain
218  BoxList bl;
219  for (const auto& bx : boxes) {
220  bl.push_back(bx);
221  }
222  BoxArray ba_replicated(std::move(bl));
223 
224  // Create DistributionMapping so each rank owns exactly one box
225  Vector<int> pmap(nprocs);
226  std::iota(pmap.begin(), pmap.end(), 0); // pmap = [0, 1, 2, ..., nprocs-1]
227  DistributionMapping dm_replicated(std::move(pmap));
228 
229  // Each rank will own one box covering the full domain
230  m_levelset_mf = std::make_shared<MultiFab>(ba_replicated, dm_replicated, 1, 0);
231 
232  // ParallelCopy from distributed to replicated
233  // Source: mf_distributed (each rank has different spatial portions)
234  // Dest: m_levelset_mf (each rank will receive the full domain)
235  IntVect dst_nghost(0);
236  m_levelset_mf->ParallelCopy(mf_distributed, 0, 0, 1, ngrow_vec, dst_nghost);
237 
238  // Store bounds for interpolation
239  m_if.m_data_box = sampled_box;
240 
241  // Cache geometry data for GPU-compatible access
242  const Real* problo_ptr = geom.ProbLo();
243  const Real* dx_ptr = geom.CellSize();
244  for (int d = 0; d < AMREX_SPACEDIM; ++d) {
245  m_if.m_problo[d] = problo_ptr[d];
246  m_if.m_dx[d] = dx_ptr[d];
247  }
248 
249  // Set up local array view for operator() access
250  // Each rank iterates its own local box (which contains the full domain)
251  for (MFIter mfi(*m_levelset_mf); mfi.isValid(); ++mfi) {
252  m_if.m_local_array = (*m_levelset_mf)[mfi].const_array();
253  break; // Only one box per rank
254  }
255  }
BuildingsLevelsetIF m_if
Definition: ERF_EBIFBuildings.H:278
amrex::STLtools m_stl_tools
STL tools object (for loading and processing STL files)
Definition: ERF_EBIFBuildings.H:273
amrex::Geometry m_geom
Domain geometry (for coordinate transformations)
Definition: ERF_EBIFBuildings.H:270
std::shared_ptr< amrex::MultiFab > m_levelset_mf
Owns the replicated levelset referenced by m_if on each rank.
Definition: ERF_EBIFBuildings.H:276
amrex::Array4< const amrex::Real > m_local_array
Local array view for GPU-compatible operator() access.
Definition: ERF_EBIFBuildings.H:135
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > m_problo
Cached geometry data for GPU-compatible access.
Definition: ERF_EBIFBuildings.H:131
amrex::Box m_data_box
Bounding box of sampled data.
Definition: ERF_EBIFBuildings.H:128
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > m_dx
Definition: ERF_EBIFBuildings.H:132
Definition: ERF_ConsoleIO.cpp:17

Member Function Documentation

◆ getIF() [1/2]

BuildingsLevelsetIF BuildingsIF::getIF ( ) const &&
delete

◆ getIF() [2/2]

BuildingsLevelsetIF BuildingsIF::getIF ( ) const &
inlinenoexcept

Return the GPU-copyable evaluator for the sampled levelset.

This owner must remain alive until all evaluations have finished, including EB2::Build and EB2::BuildFC. In GPU builds, evaluate on the device. Calling it on a temporary owner does not compile, since the evaluator would point at the owner's freed levelset.

265 { return m_if; }

Referenced by ERF::initializeEB().

Here is the caller graph for this function:

Member Data Documentation

◆ m_geom

amrex::Geometry BuildingsIF::m_geom
protected

Domain geometry (for coordinate transformations)

◆ m_if

BuildingsLevelsetIF BuildingsIF::m_if
protected

◆ m_levelset_mf

std::shared_ptr<amrex::MultiFab> BuildingsIF::m_levelset_mf
protected

Owns the replicated levelset referenced by m_if on each rank.

◆ m_stl_tools

amrex::STLtools BuildingsIF::m_stl_tools
protected

STL tools object (for loading and processing STL files)


The documentation for this class was generated from the following file: