-
Notifications
You must be signed in to change notification settings - Fork 4
test(u4): add regression for sphere phi-cut conversion coverage #334
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
Open
plexoos
wants to merge
5
commits into
main
Choose a base branch
from
test-sphere-phicut-assert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7184c99
test(u4): add regression for sphere phi-cut conversion coverage
plexoos 9e648a9
test(u4): only accept expected child abort signals in sphere phi-cut …
plexoos 304e22d
test(u4): enforce phi-cut regression invariants and safe accepted path
Copilot 9a4dc0c
style(u4): use EXIT_SUCCESS and EXIT_FAILURE in sphere phi-cut test
plexoos db0b7f5
Potential fix for pull request finding
plexoos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
| <gdml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation=""> | ||
|
|
||
| <materials> | ||
| <element name="O" formula="O" Z="8"> | ||
| <atom value="15.999" unit="g/mole"/> | ||
| </element> | ||
|
|
||
| <material name="Dummy" state="solid"> | ||
| <D value="1.0" unit="g/cm3"/> | ||
| <fraction n="1.0" ref="O"/> | ||
| </material> | ||
| </materials> | ||
|
|
||
| <solids> | ||
| <box name="WorldBox" x="400" y="400" z="400" lunit="mm"/> | ||
|
|
||
| <sphere name="OuterPatch" | ||
| rmin="0" | ||
| rmax="100" | ||
| startphi="0" | ||
| deltaphi="1.57079632679" | ||
| starttheta="0" | ||
| deltatheta="3.14159265359" | ||
| aunit="rad" | ||
| lunit="mm"/> | ||
|
|
||
| <sphere name="InnerPatch" | ||
| rmin="0" | ||
| rmax="95" | ||
| startphi="0" | ||
| deltaphi="1.57079632679" | ||
| starttheta="0" | ||
| deltatheta="3.14159265359" | ||
| aunit="rad" | ||
| lunit="mm"/> | ||
|
|
||
| <subtraction name="QuarterShell"> | ||
| <first ref="OuterPatch"/> | ||
| <second ref="InnerPatch"/> | ||
| </subtraction> | ||
| </solids> | ||
|
|
||
| <structure> | ||
| <volume name="QuarterShell_lv"> | ||
| <materialref ref="Dummy"/> | ||
| <solidref ref="QuarterShell"/> | ||
| </volume> | ||
|
|
||
| <volume name="World_lv"> | ||
| <materialref ref="Dummy"/> | ||
| <solidref ref="WorldBox"/> | ||
| <physvol name="QuarterShell_pv"> | ||
| <volumeref ref="QuarterShell_lv"/> | ||
| <position name="QuarterShell_pos" unit="mm" x="0" y="0" z="0"/> | ||
| </physvol> | ||
| </volume> | ||
| </structure> | ||
|
|
||
| <setup name="Default" version="1.0"> | ||
| <world ref="World_lv"/> | ||
| </setup> | ||
| </gdml> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| #include <cassert> | ||
| #include <csignal> | ||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <set> | ||
| #include <sys/types.h> | ||
| #include <sys/wait.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include <plog/Appenders/ConsoleAppender.h> | ||
| #include <plog/Formatters/TxtFormatter.h> | ||
| #include <plog/Init.h> | ||
| #include <plog/Log.h> | ||
|
|
||
| using plog::error; | ||
| using plog::fatal; | ||
| using plog::info; | ||
|
|
||
| #include "G4BooleanSolid.hh" | ||
| #include "G4IntersectionSolid.hh" | ||
| #include "G4LogicalVolume.hh" | ||
| #include "G4PhysicalConstants.hh" | ||
| #include "G4Sphere.hh" | ||
| #include "G4SubtractionSolid.hh" | ||
| #include "G4SystemOfUnits.hh" | ||
| #include "G4VPhysicalVolume.hh" | ||
| #include "G4VSolid.hh" | ||
|
|
||
| #include "config_path.h" | ||
|
|
||
| #include "U4GDML.h" | ||
| #include "U4Solid.h" | ||
| #include "U4Volume.h" | ||
|
|
||
| #include "s_csg.h" | ||
|
|
||
| namespace | ||
| { | ||
| inline constexpr char testGeomFile[] = GPHOX_TEST_GEOM_DIR "/sphere_phicut_quarter_shell.gdml"; | ||
|
|
||
| enum ConvertOutcome | ||
| { | ||
| CONVERT_REJECTED, | ||
| CONVERT_ACCEPTED, | ||
| CONVERT_ERROR | ||
| }; | ||
|
|
||
| bool IsExpectedRejectSignal(int signal) | ||
| { | ||
| return signal == SIGABRT || signal == SIGINT; | ||
| } | ||
|
|
||
| bool HasNonSpherePrimitive(const sn* nd) | ||
| { | ||
| if (nd == nullptr) | ||
| return false; | ||
|
|
||
| std::set<int> typecodes; | ||
| nd->typecodes(typecodes); | ||
|
|
||
| for (int typecode : typecodes) | ||
| { | ||
| if (CSG::IsPrimitive(typecode) == false) | ||
| continue; | ||
|
|
||
| if (typecode == CSG_SPHERE || typecode == CSG_ZSPHERE) | ||
| continue; | ||
|
|
||
| if (typecode == CSG_NOTSUPPORTED || typecode == CSG_UNDEFINED) | ||
| continue; | ||
|
|
||
| if (typecode == CSG_PHICUT || typecode == CSG_HALFSPACE) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| int CountPartialPhiSpheres(const G4VSolid* solid) | ||
| { | ||
| const G4Sphere* sphere = dynamic_cast<const G4Sphere*>(solid); | ||
| if (sphere) | ||
| { | ||
| double start_phi = sphere->GetStartPhiAngle() / CLHEP::radian; | ||
| double delta_phi = sphere->GetDeltaPhiAngle() / CLHEP::radian; | ||
| bool partial_phi = start_phi != 0. || delta_phi != 2. * CLHEP::pi; | ||
| return partial_phi ? 1 : 0; | ||
| } | ||
|
|
||
| const G4BooleanSolid* boolean = dynamic_cast<const G4BooleanSolid*>(solid); | ||
| if (boolean == nullptr) | ||
| return 0; | ||
|
|
||
| const G4VSolid* left = boolean->GetConstituentSolid(0); | ||
| const G4VSolid* right = boolean->GetConstituentSolid(1); | ||
| return CountPartialPhiSpheres(left) + CountPartialPhiSpheres(right); | ||
| } | ||
|
|
||
| ConvertOutcome ConvertInChildProcess(const G4VSolid* solid) | ||
| { | ||
| pid_t pid = fork(); | ||
| if (pid < 0) | ||
| { | ||
| perror("fork"); | ||
| return CONVERT_ERROR; | ||
| } | ||
|
|
||
| if (pid == 0) | ||
| { | ||
| s_csg* csg = new s_csg; | ||
| assert(csg); | ||
|
|
||
| int lvid = 0; | ||
| int depth = 0; | ||
| int level = 1; | ||
| sn* nd = U4Solid::Convert(solid, lvid, depth, level); | ||
| int exit_code = 4; | ||
| if (nd != nullptr) | ||
| { | ||
| bool has_phi_cut_representation = HasNonSpherePrimitive(nd); | ||
| exit_code = has_phi_cut_representation ? 0 : 5; | ||
| if (has_phi_cut_representation == false) | ||
| { | ||
| std::cerr | ||
| << "child conversion produced non-null tree without any non-sphere primitive; " | ||
| << "phi-cut geometry is not represented" | ||
| << std::endl; | ||
| } | ||
| } | ||
| delete nd; | ||
| _exit(exit_code); | ||
| } | ||
|
|
||
| int status = 0; | ||
| int rc = waitpid(pid, &status, 0); | ||
| if (rc != pid) | ||
| { | ||
| perror("waitpid"); | ||
| return CONVERT_ERROR; | ||
| } | ||
|
|
||
| if (WIFSIGNALED(status)) | ||
| { | ||
| int signal = WTERMSIG(status); | ||
| if (IsExpectedRejectSignal(signal)) | ||
| { | ||
| std::cout << "child rejected conversion with expected signal " << signal << std::endl; | ||
| return CONVERT_REJECTED; | ||
| } | ||
|
|
||
| std::cerr << "child crashed with unexpected signal " << signal << std::endl; | ||
| return CONVERT_ERROR; | ||
| } | ||
|
|
||
| if (WIFEXITED(status) && WEXITSTATUS(status) == 0) | ||
| { | ||
| std::cout | ||
| << "child converted partial-phi sphere with non-sphere primitive(s) in the CSG tree" | ||
| << std::endl; | ||
| return CONVERT_ACCEPTED; | ||
|
plexoos marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (WIFEXITED(status)) | ||
| { | ||
| std::cerr << "child exited unexpectedly with status " << WEXITSTATUS(status) << std::endl; | ||
| return CONVERT_ERROR; | ||
| } | ||
|
|
||
| std::cerr << "child ended in unexpected state " << status << std::endl; | ||
| return CONVERT_ERROR; | ||
| } | ||
| } // namespace | ||
|
|
||
| int main(int argc, char** argv) | ||
| { | ||
| static plog::ConsoleAppender<plog::TxtFormatter> consoleAppender; | ||
| plog::init(plog::info, &consoleAppender); | ||
|
|
||
| const G4VPhysicalVolume* world = U4GDML::Read(testGeomFile); | ||
| LOG_IF(plog::fatal, world == nullptr) | ||
| << "failed to load GDML path " << (testGeomFile ? testGeomFile : "-"); | ||
| if (world == nullptr) | ||
| return EXIT_FAILURE; | ||
|
|
||
| const G4VPhysicalVolume* quarter_shell_pv = U4Volume::FindPV(world, "QuarterShell_pv"); | ||
| LOG_IF(plog::fatal, quarter_shell_pv == nullptr) | ||
| << "failed to find QuarterShell_pv in GDML path " << testGeomFile; | ||
| if (quarter_shell_pv == nullptr) | ||
| return EXIT_FAILURE; | ||
|
|
||
| const G4LogicalVolume* quarter_shell_lv = quarter_shell_pv->GetLogicalVolume(); | ||
| LOG_IF(plog::fatal, quarter_shell_lv == nullptr) | ||
| << "QuarterShell_pv lacks a logical volume"; | ||
| if (quarter_shell_lv == nullptr) | ||
| return EXIT_FAILURE; | ||
|
|
||
| const G4VSolid* quarter_shell_solid = quarter_shell_lv->GetSolid(); | ||
| LOG_IF(plog::fatal, quarter_shell_solid == nullptr) | ||
| << "QuarterShell_pv lacks a solid"; | ||
|
plexoos marked this conversation as resolved.
|
||
| if (quarter_shell_solid == nullptr) | ||
| return EXIT_FAILURE; | ||
|
|
||
| const G4IntersectionSolid* intersection = dynamic_cast<const G4IntersectionSolid*>(quarter_shell_solid); | ||
| LOG_IF(plog::fatal, intersection != nullptr) | ||
| << "test geometry unexpectedly uses a parent IntersectionSolid"; | ||
| if (intersection != nullptr) | ||
| return EXIT_FAILURE; | ||
|
|
||
| const G4SubtractionSolid* subtraction = dynamic_cast<const G4SubtractionSolid*>(quarter_shell_solid); | ||
| LOG_IF(plog::fatal, subtraction == nullptr) | ||
| << "test geometry expected a subtraction shell solid " << quarter_shell_solid->GetName(); | ||
| if (subtraction == nullptr) | ||
| return EXIT_FAILURE; | ||
|
|
||
| int partial_phi_spheres = CountPartialPhiSpheres(quarter_shell_solid); | ||
| LOG_IF(plog::fatal, partial_phi_spheres == 0) | ||
| << "test geometry expected partial-phi sphere primitives"; | ||
|
plexoos marked this conversation as resolved.
plexoos marked this conversation as resolved.
Comment on lines
+180
to
+217
Comment on lines
+180
to
+217
|
||
| if (partial_phi_spheres == 0) | ||
| return EXIT_FAILURE; | ||
|
|
||
| ConvertOutcome outcome = ConvertInChildProcess(quarter_shell_solid); | ||
| switch (outcome) | ||
| { | ||
| case CONVERT_REJECTED: | ||
| std::cout | ||
| << "partial-phi sphere conversion is rejected, matching current fail-fast behavior" | ||
| << std::endl; | ||
| return EXIT_SUCCESS; | ||
|
|
||
| case CONVERT_ACCEPTED: | ||
| std::cout | ||
| << "partial-phi sphere conversion succeeded with a non-null CSG tree" | ||
| << std::endl; | ||
| return EXIT_SUCCESS; | ||
|
|
||
| case CONVERT_ERROR: | ||
| break; | ||
| } | ||
|
|
||
| std::cerr | ||
| << "SpherePhiCutQuarterShellTest could neither confirm fail-fast rejection nor " | ||
| << "successful conversion of the partial-phi spherical shell." | ||
| << std::endl; | ||
|
|
||
| return EXIT_FAILURE; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.