Skip to content

Commit

Permalink
add enum EventType
Browse files Browse the repository at this point in the history
  • Loading branch information
ichinii committed Oct 15, 2024
1 parent 4651f9a commit d1ac3b9
Show file tree
Hide file tree
Showing 27 changed files with 108 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Beamline/Objects/CircleSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ std::vector<Ray> CircleSource::getRays([[maybe_unused]] int thread_count) const

const auto field = stokesToElectricField(m_stokes, direction);

Ray r = {position, ETYPE_UNINIT, direction, en, field, 0.0, 0.0, -1.0, -1.0};
Ray r = {position, EventType::Uninit, direction, en, field, 0.0, 0.0, -1.0, -1.0};

rayList.push_back(r);
}
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Beamline/Objects/DipoleSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ std::vector<Ray> DipoleSource::getRays(int thread_count) const {

const auto field = stokesToElectricField(psiandstokes.stokes, direction);

Ray r = {position, ETYPE_UNINIT, direction, en, field, 0.0, 0.0, -1.0, -1.0};
Ray r = {position, EventType::Uninit, direction, en, field, 0.0, 0.0, -1.0, -1.0};
#if defined(DIPOLE_OMP)
#pragma omp critical // thread-safety for writing rayList
{ rayList.push_back(r); }
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Beamline/Objects/MatrixSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ std::vector<Ray> MatrixSource::getRays([[maybe_unused]] int thread_count) const

const auto field = stokesToElectricField(m_pol, direction);

Ray r = {position, ETYPE_UNINIT, direction, en, field, 0.0, 0.0, -1.0, -1.0};
Ray r = {position, EventType::Uninit, direction, en, field, 0.0, 0.0, -1.0, -1.0};

returnList.push_back(r);
}
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Beamline/Objects/PixelSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ std::vector<Ray> PixelSource::getRays([[maybe_unused]] int thread_count) const {
const auto rotation = glm::dmat3(m_orientation);
const auto field = stokesToElectricField(m_pol, rotation);

Ray r = {position, ETYPE_UNINIT, direction, en, field, 0.0, 0.0, -1.0, -1.0};
Ray r = {position, EventType::Uninit, direction, en, field, 0.0, 0.0, -1.0, -1.0};

rayList.push_back(r);
}
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Beamline/Objects/PointSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std::vector<Ray> PointSource::getRays(int thread_count) const {

const auto field = stokesToElectricField(m_pol, direction);

Ray r = {position, ETYPE_UNINIT, direction, en, field, 0.0, 0.0, -1.0, -1.0};
Ray r = {position, EventType::Uninit, direction, en, field, 0.0, 0.0, -1.0, -1.0};
#if defined(DIPOLE_OMP)
#pragma omp critical
{ rayList.push_back(r); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ std::vector<Ray> SimpleUndulatorSource::getRays([[maybe_unused]] int thread_coun

const auto field = stokesToElectricField(m_pol, direction);

Ray r = {position, ETYPE_UNINIT, direction, en, field, 0.0, 0.0, -1.0, -1.0};
Ray r = {position, EventType::Uninit, direction, en, field, 0.0, 0.0, -1.0, -1.0};

rayList.push_back(r);
}
Expand Down
3 changes: 2 additions & 1 deletion Intern/rayx-core/src/Debug/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ inline std::vector<double> formatAsVec(double arg) { return {arg}; }
inline std::vector<double> formatAsVec(complex::Complex comp) { return {comp.real(), comp.imag()}; }

inline std::vector<double> formatAsVec(const Ray arg) {
const auto eventType = static_cast<double>(arg.m_eventType);
return {
arg.m_position.x, arg.m_position.y, arg.m_position.z, arg.m_eventType, arg.m_direction.x, arg.m_direction.y,
arg.m_position.x, arg.m_position.y, arg.m_position.z, eventType, arg.m_direction.x, arg.m_direction.y,
arg.m_direction.z, arg.m_energy, arg.m_field.x.real(), arg.m_field.x.imag(), arg.m_field.y.real(), arg.m_field.y.imag(),
arg.m_field.z.real(), arg.m_field.z.imag(), arg.m_pathLength, arg.m_order, arg.m_lastElement, arg.m_sourceID,
};
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Shader/Behave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Ray behaveSlit(Ray r, int id, [[maybe_unused]] Collision col, InvState& inv) {
bool withinBeamstop = inCutout(beamstopCutout, r.m_position.x, r.m_position.z);

if (!withinOpening || withinBeamstop) {
recordFinalEvent(r, ETYPE_ABSORBED, inv);
recordFinalEvent(r, EventType::Absorbed, inv);
return r;
}

Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Shader/Behave.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace RAYX {
/// The `behave*` functions, will
/// - change the rays direction, typically by reflecting using the normal
/// - change the rays stokes vector
/// - potentially absorb the ray (by calling `recordFinalEvent(_, ETYPE_ABSORBED)`)
/// - potentially absorb the ray (by calling `recordFinalEvent(_, EventType::Absorbed)`)

RAYX_FN_ACC Ray behaveSlit(Ray r, int id, Collision col, InvState& inv);
RAYX_FN_ACC Ray behaveRZP(Ray r, int id, Collision col, InvState& inv);
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Shader/DynamicElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void dynamicElements(int gid, InvState& inv) {
break;
}

recordEvent(ray, ETYPE_JUST_HIT_ELEM, inv);
recordEvent(ray, EventType::HitElement, inv);

// transform back to WORLD coordinates
ray = rayMatrixMult(ray, nextElement.m_outTrans);
Expand Down
64 changes: 32 additions & 32 deletions Intern/rayx-core/src/Shader/EventType.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@

namespace RAYX {

// The meaning of the `m_eventType` field of a `Ray`:
////////////////////////////////////////////////////

// This Ray has just hit `m_lastElement`.
// And will continue tracing afterwards.
// Ray is in element coordinates of the hit element.
constexpr double ETYPE_JUST_HIT_ELEM = 1;

// If the storage space for the events is insufficient for the amount of events that were recorded in a shader call.
constexpr double ETYPE_TOO_MANY_EVENTS = 2;

// This Ray was absorbed by `m_lastElement`.
// Ray is in element coordinates, relative to `m_lastElement`.
constexpr double ETYPE_ABSORBED = 3;

// This is a yet uninitialized ray from outputData.
// This is the initial weight within outputData, and if less events than `maxEvents` are taken,
// the remaining weights in outputData will stay ETYPE_UNINIT even when returned to the CPU.
constexpr double ETYPE_UNINIT = 4;

// This is an error code.
// Functions like refrac2D can error due to "ray beyond horizon", see Utils.h.
// In that case this is returned as final event.
constexpr double ETYPE_BEYOND_HORIZON = 5;

// This is a general error code that means some assertion failed in the shader.
// This error code is typically generated using `_throw`.
constexpr double ETYPE_FATAL_ERROR = 6;

// These rays have just been emitted and not had any other events
// If there are no other elements the ray has this eventtype
constexpr double ETYPE_EMITTED = 7;
// The meaning of the `m_eventType` field of a `Ray`
enum class EventType {
// This is a yet uninitialized ray from outputData.
// This is the initial weight within outputData, and if less events than `maxEvents` are taken,
// the remaining weights in outputData will stay EventType::Uninit even when returned to the CPU.
Uninit = 0,

// This Ray has just hit `m_lastElement`.
// And will continue tracing afterwards.
// Ray is in element coordinates of the hit element.
HitElement = 1,

// If the storage space for the events is insufficient for the amount of events that were recorded in a shader call.
TooManyEvents = 2,

// This Ray was absorbed by `m_lastElement`.
// Ray is in element coordinates, relative to `m_lastElement`.
Absorbed = 3,

// This is an error code.
// Functions like refrac2D can error due to "ray beyond horizon", see Utils.h.
// In that case this is returned as final event.
BeyondHorizon = 5,

// This is a general error code that means some assertion failed in the shader.
// This error code is typically generated using `_throw`.
FatalError = 6,

// These rays have just been emitted and not had any other events
// If there are no other elements the ray has this eventtype
Emitted = 7,
};

} // namespace RAYX
14 changes: 7 additions & 7 deletions Intern/rayx-core/src/Shader/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ void init(InvState& inv) {
inv.finalized = false;

// TODO(Sven): dont waste time with initializing
// sets all output rays controlled by this shader call to ETYPE_UNINIT.
// sets all output rays controlled by this shader call to EventType::Uninit.
for (uint32_t i = uint32_t(0); i < inv.pushConstants.maxEvents; i++) {
inv.outputRays[output_index(i, inv)].m_eventType = ETYPE_UNINIT;
inv.outputRays[output_index(i, inv)].m_eventType = EventType::Uninit;
}
inv.nextEventIndex = 0;

Expand All @@ -38,13 +38,13 @@ uint32_t output_index(uint32_t i, InvState& inv) {
// record an event and store it in the next free spot in outputRays.
// `r` will typically be ray, or some related ray.
RAYX_FN_ACC
void recordEvent(Ray r, double w, InvState& inv) {
void recordEvent(Ray r, EventType w, InvState& inv) {
if (inv.finalized) {
return;
}

// recording of event type ETYPE_UINIT is forbidden.
if (w == ETYPE_UNINIT) {
if (w == EventType::Uninit) {
_throw("recordEvent failed: weight UNINIT is invalid in recordEvent");

return;
Expand All @@ -54,9 +54,9 @@ void recordEvent(Ray r, double w, InvState& inv) {
if (inv.nextEventIndex >= inv.pushConstants.maxEvents) {
inv.finalized = true;

// change the last event to "ETYPE_TOO_MANY_EVENTS".
// change the last event to "EventType::TooManyEvents".
uint32_t idx = output_index(uint32_t(inv.pushConstants.maxEvents - 1), inv);
inv.outputRays[idx].m_eventType = ETYPE_TOO_MANY_EVENTS;
inv.outputRays[idx].m_eventType = EventType::TooManyEvents;

_throw("recordEvent failed: too many events!");

Expand All @@ -74,7 +74,7 @@ void recordEvent(Ray r, double w, InvState& inv) {
// Like `recordEvent` above, but it will prevent recording more events after this.
// Is used for events terminating the path of the ray.
RAYX_FN_ACC
void recordFinalEvent(Ray r, double w, InvState& inv) {
void recordFinalEvent(Ray r, EventType w, InvState& inv) {
recordEvent(r, w, inv);
inv.finalized = true;
}
Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-core/src/Shader/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace RAYX {
RAYX_FN_ACC void init(InvState& inv);
RAYX_FN_ACC uint64_t rayId(InvState& inv);
RAYX_FN_ACC uint32_t output_index(uint32_t i, InvState& inv);
RAYX_FN_ACC void recordEvent(Ray r, double w, InvState& inv);
RAYX_FN_ACC void recordFinalEvent(Ray r, double w, InvState& inv);
RAYX_FN_ACC void recordEvent(Ray r, EventType w, InvState& inv);
RAYX_FN_ACC void recordFinalEvent(Ray r, EventType w, InvState& inv);

} // namespace RAYX
4 changes: 2 additions & 2 deletions Intern/rayx-core/src/Shader/Ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ struct RAYX_API Ray {
glm::dvec3 m_position;

/// The m_eventType expresses what is currently happening to the ray.
/// During tracing the eventType will be uninitialized (ETYPE_UNINIT).
/// During tracing the eventType will be uninitialized (EventType::Uninit).
/// Only when an event will be recorded, the m_eventType will be set accordingly.
/// See the potential values of `m_eventType` in `EventType.h`.
double m_eventType;
EventType m_eventType;

/// The direction of the ray.
/// The direction is normalized, so its L2 norm (aka length) is one.
Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-core/src/Shader/Refrac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ calculates refracted ray
normal: normal at intersection point of ray and element
az: linedensity in z direction varied spacing for different collision angles is already considered
ax: linedensity in x direction
@returns: refracted ray (position unchanged, direction changed), weight = ETYPE_BEYOND_HORIZON if
@returns: refracted ray (position unchanged, direction changed), weight = EventType::BeyondHorizon if
"ray beyond horizon"
*/
RAYX_FN_ACC
Expand Down Expand Up @@ -41,7 +41,7 @@ Ray refrac2D(Ray r, glm::dvec3 normal, double density_z, double density_x, InvSt
r.m_direction.z = z1;
r.m_direction = inv_rot * r.m_direction;
} else { // beyond horizon - when divergence too large
recordFinalEvent(r, ETYPE_BEYOND_HORIZON, inv);
recordFinalEvent(r, EventType::BeyondHorizon, inv);
}
return r;
}
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Shader/Refrac.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ calculates refracted ray
@params: r: ray
normal: normal at intersection point of ray and element -> for planes normal is always the same (0,1,0) -> no need to rotate but we do
anyways. az: linedensity in z direction ax: linedensity in x direction
@returns: refracted ray (position unchanged, direction changed), weight = ETYPE_BEYOND_HORIZON if
@returns: refracted ray (position unchanged, direction changed), weight = EventType::BeyondHorizon if
"ray beyond horizon"
*/
RAYX_FN_ACC Ray refrac2D(Ray r, glm::dvec3 normal, double az, double ax, InvState& inv);
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Shader/Throw.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// throws an error, and termiantes the program
// TODO(Sven): rethink error handling. just instantly terminate with RAYX_EXIT or use recordFinalEvent?
// #define _throw(string) recordFinalEvent(_ray, ETYPE_FATAL_ERROR)
// #define _throw(string) recordFinalEvent(_ray, EventType::FatalError)
// #define _throw(string) RAYX_ERR << string
#define _throw(string) \
printf("Error occurred while executing shader: %s\n", string); \
Expand Down
23 changes: 15 additions & 8 deletions Intern/rayx-core/src/Writer/CSVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,26 @@ RAYX::BundleHistory loadCSV(const std::string& filename) {
RAYX_EXIT << "CSV line has incorrect length: " << d.size();
}

const auto direction = glm::dvec3(d[4], d[5], d[6]);
const auto position = glm::dvec3{d[0], d[1], d[2]};
const auto eventType = static_cast<RAYX::EventType>(d[3]);
const auto direction = glm::dvec3(d[4], d[5], d[6]);
const auto energy = d[7];
const auto field = RAYX::ElectricField({d[8], d[9]}, {d[10], d[11]}, {d[12], d[13]});
const auto pathLength = d[14];
const auto order = d[15];
const auto lastElement = d[16];
const auto sourceID = d[17];

// create the Ray from the loaded doubles from this line.
RAYX::Ray ray = {.m_position = {d[0], d[1], d[2]},
.m_eventType = d[3],
RAYX::Ray ray = {.m_position = position,
.m_eventType = eventType,
.m_direction = direction,
.m_energy = d[7],
.m_energy = energy,
.m_field = field,
.m_pathLength = d[14],
.m_order = d[15],
.m_lastElement = d[16],
.m_sourceID = d[17]};
.m_pathLength = pathLength,
.m_order = order,
.m_lastElement = lastElement,
.m_sourceID = sourceID};
// This checks whether `ray_id` is from a "new ray" that didn't yet come up in the BundleHistory.
// If so, we need to make place for it.
if (out.size() <= ray_id) {
Expand Down
34 changes: 20 additions & 14 deletions Intern/rayx-core/src/Writer/H5Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,27 @@ RAYX::BundleHistory fromDoubles(const std::vector<double>& doubles, const Format
currentRayID = rayID;
}

const auto* d = rayData + 2;
const auto position = glm::dvec3{d[0], d[1], d[2]};
const auto eventType = static_cast<RAYX::EventType>(d[3]);
const auto direction = glm::dvec3(d[4], d[5], d[6]);
const auto energy = d[7];
const auto field = RAYX::ElectricField({d[8], d[9]}, {d[10], d[11]}, {d[12], d[13]});
const auto pathLength = d[14];
const auto order = d[15];
const auto lastElement = d[16];
const auto sourceID = d[17];

const auto ray = RAYX::Ray{
.m_position = {rayData[2], rayData[3], rayData[4]}, // origin
.m_eventType = rayData[5], // eventType
.m_direction = {rayData[6], rayData[7], rayData[8]}, // direction
.m_energy = rayData[9], // energy
.m_field =
{
{rayData[10], rayData[11]},
{rayData[12], rayData[13]},
{rayData[14], rayData[15]},
},
.m_pathLength = rayData[16], // pathLength
.m_order = rayData[17], // order
.m_lastElement = rayData[18], // lastElement
.m_sourceID = rayData[19] // sourceID
.m_position = position,
.m_eventType = eventType,
.m_direction = direction,
.m_energy = energy,
.m_field = field,
.m_pathLength = pathLength,
.m_order = order,
.m_lastElement = lastElement,
.m_sourceID = sourceID,
};

rayHist.push_back(ray);
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Writer/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static Format FULL_FORMAT = {
},
FormatComponent{
.name = "Event-type",
.get_double = [](uint32_t, uint32_t, RAYX::Ray ray) { return ray.m_eventType; },
.get_double = [](uint32_t, uint32_t, RAYX::Ray ray) { return static_cast<double>(ray.m_eventType); },
},
FormatComponent{
.name = "X-direction",
Expand Down
10 changes: 5 additions & 5 deletions Intern/rayx-core/tests/setupTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ RAYX::Ray parseCSVline(std::string line) {

// otherwise uninitialized:
ray.m_sourceID = -1;
ray.m_eventType = -1;
ray.m_eventType = EventType::Uninit;
ray.m_lastElement = -1;
ray.m_order = -1;

Expand Down Expand Up @@ -84,13 +84,13 @@ std::vector<RAYX::Ray> extractLastHit(const RAYX::BundleHistory& hist) {
std::vector<RAYX::Ray> outs;
for (auto rr : hist) {
Ray out;
out.m_eventType = ETYPE_UNINIT;
out.m_eventType = EventType::Uninit;
for (auto r : rr) {
if (r.m_eventType == ETYPE_JUST_HIT_ELEM) {
if (r.m_eventType == EventType::HitElement) {
out = r;
}
}
if (out.m_eventType != ETYPE_UNINIT) {
if (out.m_eventType != EventType::Uninit) {
outs.push_back(out);
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ std::optional<RAYX::Ray> lastSequentialHit(RayHistory ray_hist, uint32_t beamlin
if (ray_hist[i].m_lastElement != i) {
return {};
}
if (ray_hist[i].m_eventType != ETYPE_JUST_HIT_ELEM) {
if (ray_hist[i].m_eventType != EventType::HitElement) {
return {};
}
}
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/tests/setupTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void writeToOutputCSV(const RAYX::BundleHistory& hist, std::string filename);
/// Returns all traced rays
RAYX::BundleHistory traceRML(std::string filename);

// extracts the last ETYPE_JUST_HIT_ELEM for each ray.
// extracts the last EventType::HitElement for each ray.
std::vector<RAYX::Ray> extractLastHit(const RAYX::BundleHistory&);

/// will look at Tests/input/<filename>.csv
Expand Down
Loading

0 comments on commit d1ac3b9

Please sign in to comment.