Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "background" volume support to ORANGE #530

Merged
merged 8 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 1 addition & 69 deletions app/data/simple-cms.org.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,74 +165,6 @@
"iron_muon_chambers_outer.coz"
],
"surfaces": {
"connectivity": [
[
0,
7
],
[
0,
7
],
[
0,
7
],
[
0,
7
],
[
0,
7
],
[
0,
7
],
[
1,
2
],
[
1,
2,
3,
4,
5,
6,
7
],
[
1,
2,
3,
4,
5,
6,
7
],
[
2,
3
],
[
3,
4
],
[
4,
5
],
[
5,
6
],
[
6,
7
]
],
"data": [
-1000.0,
1000.0,
Expand Down Expand Up @@ -284,4 +216,4 @@
}
}
]
}
}
11 changes: 8 additions & 3 deletions src/orange/Data.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ struct OrangeParamsScalars
size_type max_intersections{};
size_type max_logic_depth{};

// Multiplicative and additive values for bumping particles
real_type bump_rel{1e-8};
real_type bump_abs{1e-8};
paulromano marked this conversation as resolved.
Show resolved Hide resolved

//! True if assigned
explicit CELER_FUNCTION operator bool() const
{
return max_level > 0 && max_faces > 0 && max_intersections > 0;
return max_level > 0 && max_faces > 0 && max_intersections > 0
&& bump_rel > 0 && bump_abs > 0;
}
};

Expand Down Expand Up @@ -122,8 +127,8 @@ struct SimpleUnitRecord

// TODO: transforms
// TODO: acceleration structure (bvh/kdtree/grid)
// TODO: background
bool simple_safety{};
VolumeId background{}; //!< Default if not in any other volume
bool simple_safety{};

//! True if defined
explicit CELER_FUNCTION operator bool() const
Expand Down
7 changes: 6 additions & 1 deletion src/orange/construct/OrangeInput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ struct VolumeInput

//! Special flags
logic_int flags{0};
//! Masking priority (2 for regular, 1 for background)
int zorder{};

//! Whether the volume definition is valid
explicit operator bool() const { return !logic.empty(); }
explicit operator bool() const
{
return !logic.empty() || (flags & Flags::implicit_cell);
}
};

//---------------------------------------------------------------------------//
Expand Down
10 changes: 8 additions & 2 deletions src/orange/construct/OrangeInputIO.json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,17 @@ void from_json(const nlohmann::json& j, VolumeInput& value)
const auto& temp_logic = j.at("logic").get<std::string>();
value.logic = parse_logic(temp_logic.c_str());

// Parse bbox
if (j.contains("bbox"))
{
auto bbox = j.at("bbox").get<Array<Real3, 2>>();
value.bbox = {bbox[0], bbox[1]};
}

// Read scalars, including optional flags
auto flag_iter = j.find("flags");
value.flags = (flag_iter == j.end() ? 0 : flag_iter->get<int>());

// TODO: bbox
j.at("zorder").get_to(value.zorder);
}

//---------------------------------------------------------------------------//
Expand Down
40 changes: 32 additions & 8 deletions src/orange/detail/UnitInserter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace detail
namespace
{
//---------------------------------------------------------------------------//
// HELPER FUNCTIONS
constexpr int invalid_max_depth = -1;

//---------------------------------------------------------------------------//
/*!
* Calculate the maximum logic depth of a volume definition.
Expand All @@ -39,6 +40,7 @@ namespace
int calc_max_depth(Span<const logic_int> logic)
{
CELER_EXPECT(!logic.empty());

// Calculate max depth
int max_depth = 1;
int cur_depth = 0;
Expand All @@ -58,8 +60,9 @@ int calc_max_depth(Span<const logic_int> logic)
if (cur_depth != 1)
{
// Input definition is invalid; return a sentinel value
max_depth = 0;
max_depth = invalid_max_depth;
}
CELER_ENSURE(max_depth > 0 || max_depth == invalid_max_depth);
return max_depth;
}

Expand Down Expand Up @@ -157,11 +160,14 @@ SimpleUnitId UnitInserter::operator()(const UnitInput& inp)
vol_records[i] = this->insert_volume(unit.surfaces, inp.volumes[i]);
CELER_ASSERT(!vol_records.empty());

// Add connectivity
for (SurfaceId f : inp.volumes[i].faces)
// Add connectivity for explicitly connected cells
if (!(vol_records[i].flags & VolumeRecord::implicit_cell))
{
CELER_ASSERT(f < connectivity.size());
connectivity[f.unchecked_get()].insert(VolumeId(i));
for (SurfaceId f : inp.volumes[i].faces)
{
CELER_ASSERT(f < connectivity.size());
connectivity[f.unchecked_get()].insert(VolumeId(i));
}
}
}

Expand All @@ -186,6 +192,10 @@ SimpleUnitId UnitInserter::operator()(const UnitInput& inp)
}

// Save unit scalars
if (inp.volumes.back().zorder == 1)
{
unit.background = VolumeId(inp.volumes.size() - 1);
}
paulromano marked this conversation as resolved.
Show resolved Hide resolved
unit.simple_safety = std::all_of(
vol_records.begin(), vol_records.end(), [](const VolumeRecord& v) {
return supports_simple_safety(v.flags);
Expand Down Expand Up @@ -286,12 +296,26 @@ VolumeRecord UnitInserter::insert_volume(const SurfacesRecord& surf_record,
max_intersections += get_num_intersections(sid);
}

auto input_logic = make_span(v.logic);
if (v.flags & VolumeRecord::Flags::implicit_cell)
paulromano marked this conversation as resolved.
Show resolved Hide resolved
{
// Currently SCALE ORANGE writes background cells as having "empty"
// logic, whereas we really want them to be "nowhere" (at least
// nowhere *explicitly* using the 'inside' logic). It gets away with
// this because it always uses BVH to initialize, and the implicit
// cells get an empty bbox. To avoid special cases in Celeritas, set
// the logic to be explicitly "not true".
CELER_EXPECT(input_logic.empty());
static const logic_int nowhere_logic[] = {logic::ltrue, logic::lnot};
input_logic = make_span(nowhere_logic);
}

auto faces = make_builder(&orange_data_->surface_ids);
auto logic = make_builder(&orange_data_->logic_ints);

VolumeRecord output;
output.faces = faces.insert_back(v.faces.begin(), v.faces.end());
output.logic = logic.insert_back(v.logic.begin(), v.logic.end());
output.logic = logic.insert_back(input_logic.begin(), input_logic.end());
output.max_intersections = max_intersections;
output.flags = v.flags;
if (simple_safety)
Expand All @@ -300,7 +324,7 @@ VolumeRecord UnitInserter::insert_volume(const SurfacesRecord& surf_record,
}

// Calculate the maximum stack depth of the volume definition
int max_depth = calc_max_depth(make_span(v.logic));
int max_depth = calc_max_depth(input_logic);
CELER_VALIDATE(max_depth > 0,
<< "invalid logic definition: operators do not balance");

Expand Down
Loading