File indexing completed on 2026-08-12 08:27:13
0001 #include <cstdio>
0002 #include <cstdlib>
0003
0004 #include "scerenkov.h"
0005 #include "scuda.h"
0006 #include "smath.h"
0007 #include "srec.h"
0008 #include "srngcpu.h"
0009 using RNG = srngcpu;
0010
0011 #include "stexture.h"
0012
0013 #include "OpticksPhoton.h"
0014 #include "qsim.h"
0015
0016 namespace
0017 {
0018 void require(bool condition, const char* expression, int line)
0019 {
0020 if (condition)
0021 return;
0022 std::fprintf(stderr, "%s:%d requirement failed: %s\n", __FILE__, line, expression);
0023 std::exit(EXIT_FAILURE);
0024 }
0025
0026 #define REQUIRE(expression) require((expression), #expression, __LINE__)
0027
0028 constexpr unsigned BOUNDARY_INDEX = 2u;
0029 constexpr unsigned OUTER_MATERIAL_LINE =
0030 BOUNDARY_INDEX * _BOUNDARY_NUM_MATSUR + OMAT;
0031 constexpr unsigned INNER_MATERIAL_LINE =
0032 BOUNDARY_INDEX * _BOUNDARY_NUM_MATSUR + IMAT;
0033
0034 void initialize(sctx& ctx, quad2& prd)
0035 {
0036 prd.zero();
0037 prd.q0.f.z = 1.f;
0038 prd.q1.u.w = BOUNDARY_INDEX;
0039
0040 ctx = {};
0041 ctx.prd = &prd;
0042 ctx.pidx = 0u;
0043 ctx.p.zero();
0044 ctx.p.mom = make_float3(0.f, 0.f, -1.f);
0045 ctx.p.pol = make_float3(1.f, 0.f, 0.f);
0046 ctx.p.wavelength = 420.f;
0047
0048 ctx.s.material1 = make_float4(1.f, 0.f, 0.f, 0.f);
0049 ctx.s.material2 = make_float4(1.f, 0.f, 0.f, 0.f);
0050 ctx.s.m1group2 = make_float4(100.f, 200.f, 0.f, 0.f);
0051 ctx.s.index = make_uint4(10u, 20u, 0u, 0u);
0052 }
0053
0054 void test_transmission_updates_carried_material(const qsim& sim, RNG& rng)
0055 {
0056 sctx ctx = {};
0057 quad2 prd = {};
0058 initialize(ctx, prd);
0059 ctx.current_matline = 99u;
0060
0061 unsigned flag = 0u;
0062 const FlowAction action = sim.propagate_at_boundary(flag, rng, ctx, 1.f);
0063
0064 REQUIRE(action == FlowAction::Continue);
0065 REQUIRE(flag == BOUNDARY_TRANSMIT);
0066 REQUIRE(ctx.current_matline == INNER_MATERIAL_LINE);
0067 REQUIRE(ctx.current_material_index == 20u);
0068 REQUIRE(ctx.current_group_velocity == 200.f);
0069 }
0070
0071 void test_reflection_preserves_carried_material(const qsim& sim, RNG& rng)
0072 {
0073 sctx ctx = {};
0074 quad2 prd = {};
0075 initialize(ctx, prd);
0076 ctx.current_matline = 99u;
0077
0078 unsigned flag = 0u;
0079 const FlowAction action = sim.propagate_at_boundary(flag, rng, ctx, 0.f);
0080
0081 REQUIRE(action == FlowAction::Continue);
0082 REQUIRE(flag == BOUNDARY_REFLECT);
0083 REQUIRE(ctx.current_matline == 99u);
0084 REQUIRE(ctx.current_material_index == 10u);
0085 REQUIRE(ctx.current_group_velocity == 100.f);
0086 }
0087
0088 void test_outward_transmission_selects_outer_material(const qsim& sim, RNG& rng)
0089 {
0090 sctx ctx = {};
0091 quad2 prd = {};
0092 initialize(ctx, prd);
0093 ctx.p.mom = make_float3(0.f, 0.f, 1.f);
0094
0095 unsigned flag = 0u;
0096 const FlowAction action = sim.propagate_at_boundary(flag, rng, ctx, 1.f);
0097
0098 REQUIRE(action == FlowAction::Continue);
0099 REQUIRE(flag == BOUNDARY_TRANSMIT);
0100 REQUIRE(ctx.current_matline == OUTER_MATERIAL_LINE);
0101 }
0102 }
0103
0104 int main()
0105 {
0106 qbase base = {};
0107 base.pidx = ~0ull;
0108
0109 qsim sim;
0110 sim.base = &base;
0111
0112 RNG rng;
0113 rng.set_fake(0.5);
0114
0115 test_transmission_updates_carried_material(sim, rng);
0116 test_reflection_preserves_carried_material(sim, rng);
0117 test_outward_transmission_selects_outer_material(sim, rng);
0118
0119 std::puts("QSim_MaterialCarryTest: PASS");
0120 return 0;
0121 }