diff --git a/CSG/csg_classify.h b/CSG/csg_classify.h index 22bdb18942..07df106bf2 100644 --- a/CSG/csg_classify.h +++ b/CSG/csg_classify.h @@ -7,17 +7,7 @@ enum { CTRL_RETURN_FLIP_B = 3, CTRL_LOOP_A = 4, CTRL_LOOP_B = 5 -}; - - -/* -// moved to common action enum sflow.h -// typedef enum { -// UNDEFINED=0, -// CONTINUE=1, -// BREAK=2 -// } Action_t ; -*/ +}; #if defined(DEBUG) || defined(DEBUG_RECORD) static const char* CTRL_RETURN_MISS_ = "RETURN_MISS" ; @@ -99,4 +89,3 @@ struct LUT } }; - diff --git a/CSG/csg_intersect_tree.h b/CSG/csg_intersect_tree.h index c4391b64a9..c3f31d1018 100644 --- a/CSG/csg_intersect_tree.h +++ b/CSG/csg_intersect_tree.h @@ -22,13 +22,13 @@ Flip off the dumpxyz code without changing DEBUG_PIDXYZ with vim replace:: using std::signbit ; #endif -#include "sflow.h" +#include "FlowAction.h" +#include "csg_classify.h" #include "csg_error.h" -#include "csg_tranche.h" -#include "csg_stack.h" -#include "csg_postorder.h" #include "csg_pack.h" -#include "csg_classify.h" +#include "csg_postorder.h" +#include "csg_stack.h" +#include "csg_tranche.h" #include "f4_stack.h" @@ -547,8 +547,7 @@ void intersect_tree( bool& valid_isect, float4& isect, const CSGNode* node, cons } #endif - - unsigned act = UNDEFINED ; + FlowAction act = FlowAction::Undefined; if(ctrl < CTRL_LOOP_A) // non-looping : CTRL_RETURN_MISS/CTRL_RETURN_A/CTRL_RETURN_B/CTRL_RETURN_FLIP_B "returning" with a push { @@ -566,7 +565,7 @@ void intersect_tree( bool& valid_isect, float4& isect, const CSGNode* node, cons ierr = csg_pop0(csg); if(ierr) break ; ierr = csg_push(csg, result, nodeIdx ); if(ierr) break ; - act = CONTINUE ; + act = FlowAction::Continue; #ifdef DEBUG_RECORD if(CSGRecord::ENABLED) @@ -625,20 +624,23 @@ void intersect_tree( bool& valid_isect, float4& isect, const CSGNode* node, cons // push the tranche from here to endTree before pushing the backtracking tranche so known how to proceed after backtracking done // (hmm: using tmin onwards to endTree looks a bit funny, maybe it should be advanced?) - ierr = tranche_push( tr, endTree, tmin ); if(ierr) break ; - ierr = tranche_push( tr, loopTree, tminAdvanced ); if(ierr) break ; + ierr = tranche_push(tr, endTree, tmin); + if (ierr) + break; + ierr = tranche_push(tr, loopTree, tminAdvanced); + if (ierr) + break; - act = BREAK ; + act = FlowAction::Break; #ifdef DEBUG_RECORD if(CSGRecord::ENABLED) printf("// %3d : looping : act BREAK \n", nodeIdx ); #endif - } // "return" or "recursive call" + } // "return" or "recursive call" - - if(act == BREAK) + if (act == FlowAction::Break) { #ifdef DEBUG_RECORD if(CSGRecord::ENABLED) @@ -739,5 +741,3 @@ float distance_prim( const float3& global_position, const CSGNode* node, const f } return distance ; } - - diff --git a/qudarap/qsim.h b/qudarap/qsim.h index c927124969..a52c8c606b 100644 --- a/qudarap/qsim.h +++ b/qudarap/qsim.h @@ -33,11 +33,11 @@ Canonical use is from CSGOptiX/CSGOptiX7.cu:simulate #include "OpticksGenstep.h" #include "OpticksPhoton.h" -#include "sflow.h" -#include "sqat4.h" +#include "FlowAction.h" #include "sc4u.h" -#include "sxyz.h" #include "sphoton.h" +#include "sqat4.h" +#include "sxyz.h" #include "storch.h" #include "scarrier.h" @@ -105,21 +105,19 @@ struct qsim QSIM_METHOD static void lambertian_direction(float3* dir, const float3* normal, float orient, RNG& rng, sctx& ctx ); QSIM_METHOD static void random_direction_marsaglia(float3* dir, RNG& rng, sctx& ctx ); QSIM_METHOD void rayleigh_scatter(RNG& rng, sctx& ctx ); - QSIM_METHOD int propagate_to_boundary( unsigned& flag, RNG& rng, sctx& ctx ); -#endif + QSIM_METHOD FlowAction propagate_to_boundary(unsigned& flag, RNG& rng, sctx& ctx); -#if defined(__CUDACC__) || defined(__CUDABE__) || defined( MOCK_CURAND ) || defined(MOCK_CUDA) - QSIM_METHOD int propagate_at_boundary( unsigned& flag, RNG& rng, sctx& ctx, float theTransmittance=-1.f ) const ; - QSIM_METHOD int propagate_at_boundary_with_T( unsigned& flag, RNG& rng, sctx& ctx, float theTransmittance ) const ; + QSIM_METHOD FlowAction propagate_at_boundary(unsigned& flag, RNG& rng, sctx& ctx, float theTransmittance = -1.f) const; + QSIM_METHOD FlowAction propagate_at_boundary_with_T(unsigned& flag, RNG& rng, sctx& ctx, float theTransmittance) const; #endif #if defined(__CUDACC__) || defined(__CUDABE__) - QSIM_METHOD int propagate_at_surface_MultiFilm(unsigned& flag, RNG& rng, sctx& ctx ); + QSIM_METHOD FlowAction propagate_at_surface_MultiFilm(unsigned& flag, RNG& rng, sctx& ctx); #endif #if defined(__CUDACC__) || defined(__CUDABE__) || defined( MOCK_CURAND ) || defined(MOCK_CUDA) - QSIM_METHOD int propagate_at_surface( unsigned& flag, RNG& rng, sctx& ctx ); - QSIM_METHOD int propagate_at_surface_Detect( unsigned& flag, RNG& rng, sctx& ctx ) const ; + QSIM_METHOD FlowAction propagate_at_surface(unsigned& flag, RNG& rng, sctx& ctx); + QSIM_METHOD FlowAction propagate_at_surface_Detect(unsigned& flag, RNG& rng, sctx& ctx) const; #endif #if defined(__CUDACC__) || defined(__CUDABE__) || defined( MOCK_CURAND ) || defined(MOCK_CUDA) @@ -127,7 +125,7 @@ struct qsim QSIM_METHOD void reflect_specular( RNG& rng, sctx& ctx ); QSIM_METHOD void fake_propagate( sphoton& p, const quad2* mock_prd, RNG& rng, unsigned long long idx ); - QSIM_FORCEINLINE_METHOD int propagate(const int bounce, RNG& rng, sctx& ctx); + QSIM_FORCEINLINE_METHOD FlowAction propagate(const int bounce, RNG& rng, sctx& ctx); QSIM_METHOD void hemisphere_polarized( unsigned polz, bool inwards, RNG& rng, sctx& ctx ); QSIM_METHOD void generate_photon_simtrace( quad4& p, RNG& rng, const quad6& gs, unsigned long long photon_id, unsigned genstep_id ) const ; @@ -711,9 +709,7 @@ qsim::propagate_to_boundary **/ - - -inline QSIM_METHOD int qsim::propagate_to_boundary(unsigned& flag, RNG& rng, sctx& ctx) +inline QSIM_METHOD FlowAction qsim::propagate_to_boundary(unsigned& flag, RNG& rng, sctx& ctx) { sphoton& p = ctx.p ; const sstate& s = ctx.s ; @@ -806,7 +802,7 @@ inline QSIM_METHOD int qsim::propagate_to_boundary(unsigned& flag, RNG& rng, sct { // Failed energy conservation after 100 attempts — absorb without re-emission flag = BULK_ABSORB; - return BREAK; + return FlowAction::Break; } p.wavelength = new_wavelength; @@ -846,7 +842,7 @@ inline QSIM_METHOD int qsim::propagate_to_boundary(unsigned& flag, RNG& rng, sct } flag = BULK_REEMIT; - return CONTINUE; + return FlowAction::Continue; } else if (absorption_distance <= scattering_distance) { @@ -879,7 +875,7 @@ inline QSIM_METHOD int qsim::propagate_to_boundary(unsigned& flag, RNG& rng, sct if (scint == nullptr) { flag = BULK_ABSORB; - return BREAK; + return FlowAction::Break; } float u_re_wavelength = curand_uniform(&rng); @@ -901,12 +897,12 @@ inline QSIM_METHOD int qsim::propagate_to_boundary(unsigned& flag, RNG& rng, sct #endif flag = BULK_REEMIT ; - return CONTINUE; + return FlowAction::Continue; } else { flag = BULK_ABSORB ; - return BREAK; + return FlowAction::Break; } } // otherwise sail to boundary @@ -922,7 +918,7 @@ inline QSIM_METHOD int qsim::propagate_to_boundary(unsigned& flag, RNG& rng, sct flag = BULK_SCATTER; - return CONTINUE; + return FlowAction::Continue; } // otherwise sail to boundary } // if scattering_distance < absorption_distance @@ -938,7 +934,7 @@ inline QSIM_METHOD int qsim::propagate_to_boundary(unsigned& flag, RNG& rng, sct ctx.pidx, p.pos.x, p.pos.y, p.pos.z, p.time, sail_time_delta ); #endif - return BOUNDARY ; + return FlowAction::Boundary; } #endif #if defined(__CUDACC__) || defined(__CUDABE__) || defined( MOCK_CURAND ) || defined(MOCK_CUDA) @@ -1073,7 +1069,7 @@ incidence. **/ -inline QSIM_METHOD int qsim::propagate_at_boundary(unsigned& flag, RNG& rng, sctx& ctx, float theTransmittance ) const +inline QSIM_METHOD FlowAction qsim::propagate_at_boundary(unsigned& flag, RNG& rng, sctx& ctx, float theTransmittance) const { #if !defined(PRODUCTION) && defined(DEBUG_PIDX) if(ctx.pidx == base->pidx) @@ -1281,7 +1277,7 @@ inline QSIM_METHOD int qsim::propagate_at_boundary(unsigned& flag, RNG& rng, sct } #endif - return CONTINUE ; + return FlowAction::Continue; } /** qsim::propagate_at_boundary_with_T @@ -1300,7 +1296,7 @@ and leave this just for testing. **/ -inline QSIM_METHOD int qsim::propagate_at_boundary_with_T(unsigned& flag, RNG& rng, sctx& ctx, float theTransmittance ) const +inline QSIM_METHOD FlowAction qsim::propagate_at_boundary_with_T(unsigned& flag, RNG& rng, sctx& ctx, float theTransmittance) const { sphoton& p = ctx.p ; const sstate& s = ctx.s ; @@ -1374,8 +1370,7 @@ inline QSIM_METHOD int qsim::propagate_at_boundary_with_T(unsigned& flag, RNG& r flag = reflect ? BOUNDARY_REFLECT : BOUNDARY_TRANSMIT ; - - return CONTINUE ; + return FlowAction::Continue; } #endif @@ -1614,7 +1609,7 @@ Tp: p-component reflect probability */ #if defined(__CUDACC__) || defined(__CUDABE__) -inline QSIM_METHOD int qsim::propagate_at_surface_MultiFilm(unsigned& flag, RNG& rng, sctx& ctx ) +inline QSIM_METHOD FlowAction qsim::propagate_at_surface_MultiFilm(unsigned& flag, RNG& rng, sctx& ctx) { const float one = 1.0f; @@ -1665,9 +1660,9 @@ inline QSIM_METHOD int qsim::propagate_at_surface_MultiFilm(unsigned& flag, RNG& const float& theEfficiency = _qe/An; float u_theAbsorption = curand_uniform(&rng); - int action = u_theAbsorption < theAbsorption ? BREAK : CONTINUE ; + FlowAction action = u_theAbsorption < theAbsorption ? FlowAction::Break : FlowAction::Continue; - if( action == BREAK ) + if (action == FlowAction::Break) { float u_theEfficiency = curand_uniform(&rng) ; flag = u_theEfficiency < theEfficiency ? SURFACE_DETECT : SURFACE_ABSORB ; @@ -1753,7 +1748,7 @@ The s.surface float4 is filled by qbnd::fill_state via:: **/ -inline QSIM_METHOD int qsim::propagate_at_surface(unsigned& flag, RNG& rng, sctx& ctx) +inline QSIM_METHOD FlowAction qsim::propagate_at_surface(unsigned& flag, RNG& rng, sctx& ctx) { const sstate& s = ctx.s ; const float& detect = s.surface.x ; @@ -1770,10 +1765,9 @@ inline QSIM_METHOD int qsim::propagate_at_surface(unsigned& flag, RNG& rng, sctx tagr.add( stag_sf_burn, u_surface_burn); #endif + FlowAction action = u_surface < absorb + detect ? FlowAction::Break : FlowAction::Continue; - int action = u_surface < absorb + detect ? BREAK : CONTINUE ; - - if( action == BREAK ) + if (action == FlowAction::Break) { flag = u_surface < absorb ? SURFACE_ABSORB @@ -1802,12 +1796,11 @@ inline QSIM_METHOD int qsim::propagate_at_surface(unsigned& flag, RNG& rng, sctx return action ; } - -inline QSIM_METHOD int qsim::propagate_at_surface_Detect(unsigned& flag, RNG& rng, sctx& ctx) const +inline QSIM_METHOD FlowAction qsim::propagate_at_surface_Detect(unsigned& flag, RNG& rng, sctx& ctx) const { float u_surface_burn = curand_uniform(&rng); // for random alignment flag = SURFACE_DETECT ; - return BREAK ; + return FlowAction::Break; } #endif @@ -2046,7 +2039,7 @@ inline QSIM_METHOD void qsim::fake_propagate( sphoton& p, const quad2* mock_prd, ctx.evt = evt ; ctx.pidx = idx ; - int command = START ; + FlowAction command = FlowAction::Start; int bounce = 0 ; #ifndef PRODUCTION ctx.point(bounce); @@ -2070,7 +2063,8 @@ inline QSIM_METHOD void qsim::fake_propagate( sphoton& p, const quad2* mock_prd, #ifndef PRODUCTION ctx.point(bounce); #endif - if(command == BREAK) break ; + if (command == FlowAction::Break) + break; } #ifndef PRODUCTION ctx.end(); @@ -2123,7 +2117,7 @@ Prior to supporting special surfaces, within the command == BOUNDARY used:: **/ -QSIM_FORCEINLINE_METHOD int qsim::propagate(const int bounce, RNG& rng, sctx& ctx) +QSIM_FORCEINLINE_METHOD FlowAction qsim::propagate(const int bounce, RNG& rng, sctx& ctx) { const unsigned boundary = ctx.prd->boundary() ; const unsigned identity = ctx.prd->identity() ; // sensor_identifier+1, 0:not-a-sensor @@ -2171,7 +2165,7 @@ QSIM_FORCEINLINE_METHOD int qsim::propagate(const int bounce, RNG& rng, sctx& ct unsigned flag = 0 ; - int command = propagate_to_boundary( flag, rng, ctx ); + FlowAction command = propagate_to_boundary(flag, rng, ctx); /** command possibilities: @@ -2187,7 +2181,7 @@ QSIM_FORCEINLINE_METHOD int qsim::propagate(const int bounce, RNG& rng, sctx& ct ctx.pidx, bounce, command, flag, ctx.s.optical.x, ctx.s.optical.y ); #endif - if( command == BOUNDARY ) + if (command == FlowAction::Boundary) { const int& ems = ctx.s.optical.y ; diff --git a/qudarap/tests/QSim_MockTest.cc b/qudarap/tests/QSim_MockTest.cc index 478a9cdbcb..2837119a5b 100644 --- a/qudarap/tests/QSim_MockTest.cc +++ b/qudarap/tests/QSim_MockTest.cc @@ -33,17 +33,17 @@ Standalone compile and run with:: #include "NPFold.h" -#include "ssys.h" -#include "spath.h" +#include "FlowAction.h" +#include "scerenkov.h" #include "scuda.h" -#include "smath.h" // includes s_mock_erfinvf.h when MOCK_CUDA is defined +#include "smath.h" // includes s_mock_erfinvf.h when MOCK_CUDA is defined +#include "spath.h" +#include "sphoton.h" #include "squad.h" #include "srec.h" -#include "stag.h" -#include "sflow.h" -#include "sphoton.h" #include "sstate.h" -#include "scerenkov.h" +#include "ssys.h" +#include "stag.h" #include "srngcpu.h" using RNG = srngcpu ; @@ -275,27 +275,26 @@ inline int QSim_MockTest::propagate_at_boundary_manual() std::vector pp(N*2) ; for(int i=0 ; i < N ; i++) - { - float frac_twopi = float(i)/float(N) ; + { + float frac_twopi = float(i) / float(N); - p.mom = mom ; - p.set_polarization(frac_twopi) ; + p.mom = mom; + p.set_polarization(frac_twopi); - sphoton p0(p) ; - int ctrl = sim->propagate_at_boundary(flag, rng, ctx) ; + sphoton p0(p); + FlowAction ctrl = sim->propagate_at_boundary(flag, rng, ctx); - pp[i*2+0 ] = p0 ; - pp[i*2+1 ] = p ; + pp[i * 2 + 0] = p0; + pp[i * 2 + 1] = p; - std::cout - << " flag " << OpticksPhoton::Flag(flag) - << " ctrl " << sflow::desc(ctrl) + std::cout + << " flag " << OpticksPhoton::Flag(flag) + << " ctrl " << flow_action_name(ctrl) << std::endl << " p0 " << p0.descDir() << std::endl - << " p " << p.descDir() - << std::endl - ; + << " p " << p.descDir() + << std::endl; } NP* a = NP::Make(N,2,4,4) ; @@ -328,15 +327,13 @@ inline int QSim_MockTest::propagate_at_boundary() sim->bnd->fill_state(ctx.s, boundary, ctx.p.wavelength, cosTheta, ctx.idx ); + unsigned flag = 0; + FlowAction ctrl = sim->propagate_at_boundary(flag, rng, ctx); - unsigned flag = 0 ; - int ctrl = sim->propagate_at_boundary(flag, rng, ctx) ; - - std::cout - << " flag " << OpticksPhoton::Flag(flag) - << " ctrl " << sflow::desc(ctrl) - << std::endl - ; + std::cout + << " flag " << OpticksPhoton::Flag(flag) + << " ctrl " << flow_action_name(ctrl) + << std::endl; std::cout << "p1 " << p << std::endl ; diff --git a/sysrap/CMakeLists.txt b/sysrap/CMakeLists.txt index 5b2dffabe2..1f69906ddb 100644 --- a/sysrap/CMakeLists.txt +++ b/sysrap/CMakeLists.txt @@ -297,7 +297,7 @@ set(HEADERS SGenerate.h - sflow.h + FlowAction.h sqat4.h saabb.h stran.h diff --git a/sysrap/FlowAction.h b/sysrap/FlowAction.h new file mode 100644 index 0000000000..2d0123df3e --- /dev/null +++ b/sysrap/FlowAction.h @@ -0,0 +1,58 @@ +#pragma once +#include + +/** + * Flow-control actions shared by CSG traversal and photon propagation. + * + * FlowAction is returned by routines that advance either a CSG tree traversal + * or a photon propagation step. The caller uses the action to decide whether + * to continue the current loop, terminate it, or process a reached geometry + * boundary. Keeping the action as a scoped enum prevents it from being mixed + * accidentally with unrelated integer flags, indices, or error codes. + * + * The explicit unsigned underlying type preserves the compact representation + * previously used by the unscoped enum. flow_action_name is a constexpr, + * allocation-free formatter intended for diagnostics; it returns "UNKNOWN" + * for values outside the defined enumeration. + */ +enum class FlowAction : unsigned +{ + Undefined, ///< No action has been selected. + Break, ///< Stop the current traversal or propagation loop. + Continue, ///< Continue the current traversal or propagation loop. + Boundary, ///< A geometry boundary was reached and needs handling. + Pass, ///< Pass control to the next processing stage. + Start, ///< Initial action before the first processing step. + Return, ///< Return control to the caller. + Last ///< Sentinel one past the final actionable value. +}; + +/** + * Returns the stable diagnostic name of a flow-control action. + * + * @param action Flow-control action to format. + * @return Uppercase enumerator name, or "UNKNOWN" for an invalid value. + */ +constexpr std::string_view flow_action_name(FlowAction action) noexcept +{ + switch (action) + { + case FlowAction::Undefined: + return "UNDEFINED"; + case FlowAction::Break: + return "BREAK"; + case FlowAction::Continue: + return "CONTINUE"; + case FlowAction::Boundary: + return "BOUNDARY"; + case FlowAction::Pass: + return "PASS"; + case FlowAction::Start: + return "START"; + case FlowAction::Return: + return "RETURN"; + case FlowAction::Last: + return "LAST"; + } + return "UNKNOWN"; +} diff --git a/sysrap/sflow.h b/sysrap/sflow.h deleted file mode 100644 index 08e3cd6675..0000000000 --- a/sysrap/sflow.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once -/** -sflow.h -========== - -Flow control enum and CPU only presentation - -**/ - -enum { - UNDEFINED, - BREAK, - CONTINUE, - BOUNDARY, - PASS, - START, - RETURN, - LAST - }; - - -#if defined(__CUDACC__) || defined(__CUDABE__) -#else -struct sflow -{ - static const char* desc( unsigned ctrl ); - static constexpr const char* UNDEFINED_ = "UNDEFINED"; - static constexpr const char* BREAK_ = "BREAK"; - static constexpr const char* CONTINUE_ = "CONTINUE" ; - static constexpr const char* BOUNDARY_ = "BOUNDARY" ; - static constexpr const char* PASS_ = "PASS" ; - static constexpr const char* START_ = "START" ; - static constexpr const char* RETURN_ = "RETURN" ; - static constexpr const char* LAST_ = "LAST" ; -}; -inline const char* sflow::desc(unsigned ctrl ) -{ - const char* d = nullptr ; - switch(ctrl) - { - case UNDEFINED: d = UNDEFINED_ ; break ; - case BREAK : d = BREAK_ ; break ; - case CONTINUE : d = CONTINUE_ ; break ; - case BOUNDARY : d = BOUNDARY_ ; break ; - case PASS: d = PASS_ ; break ; - case START: d = START_ ; break ; - case RETURN: d = RETURN_ ; break ; - case LAST: d = LAST_ ; break ; - } - return d ; -} -#endif - diff --git a/sysrap/tests/sflowTest.cc b/sysrap/tests/sflowTest.cc deleted file mode 100644 index 78f0c576ba..0000000000 --- a/sysrap/tests/sflowTest.cc +++ /dev/null @@ -1,14 +0,0 @@ -// name=sflowTest ; gcc $name.cc -std=c++11 -lstdc++ -I.. -o /tmp/$name && /tmp/$name - -#include -#include - -#include "sflow.h" - -int main() -{ - for(unsigned i=0 ; i <= LAST ; i++ ) std::cout << std::setw(3) << i << " " << sflow::desc(i) << std::endl ; - return 0 ; -} - -