Skip to content

Commit e88ea0a

Browse files
ichiniiJonasTrenkler
authored andcommitted
write element names to h5
1 parent c448662 commit e88ea0a

File tree

9 files changed

+45
-19
lines changed

9 files changed

+45
-19
lines changed

Intern/rayx-core/src/Shader/DynamicElements.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void dynamicElements(const int gid, const InvState& inv, OutputEvents& outputEve
5858
case BehaveType::ImagePlane:
5959
ray = behaveImagePlane(ray);
6060
break;
61-
case BehaveType::Foil:
61+
case BehaveType::Foil:
6262
ray = behaveFoil(ray, behaviour, col, element.m_material, inv.materialIndices, inv.materialTables);
6363
break;
6464
}

Intern/rayx-core/src/Shader/Utils.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
namespace RAYX {
66

77
RAYX_FN_ACC
8-
double RAYX_API energyToWaveLength(double x) {
9-
return INV_NM_TO_EVOLT / x;
10-
}
8+
double RAYX_API energyToWaveLength(double x) { return INV_NM_TO_EVOLT / x; }
119

1210
RAYX_FN_ACC
1311
double waveLengthToEnergy(const double waveLength) { return INV_NM_TO_EVOLT / waveLength; }

Intern/rayx-core/src/Tracer/DeviceConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ struct RAYX_API DeviceConfig {
5252
DeviceType m_fetchedDeviceType;
5353
};
5454

55-
} // namespace RAYX
55+
} // namespace RAYX

Intern/rayx-core/src/Writer/H5Writer.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,23 @@ BundleHistory readH5BundleHistory(const std::filesystem::path& filepath) {
9292
return raySoAToBundleHistory(rays);
9393
}
9494

95-
void writeH5RaySoA(const std::filesystem::path& filepath, const RaySoA& rays, const RayAttrFlag attr) {
95+
std::vector<std::string> readH5ElementNames(const std::filesystem::path& filepath) {
96+
RAYX_VERB << "reading element names from '" << filepath;
97+
98+
auto element_names = std::vector<std::string>();
99+
100+
try {
101+
auto file = HighFive::File(filepath.string(), HighFive::File::ReadOnly);
102+
103+
file.getDataSet("/rayx/element_names").read(element_names);
104+
} catch (const std::exception& e) {
105+
RAYX_EXIT << "exception caught while attempting to read h5 file: " << e.what();
106+
}
107+
108+
return element_names;
109+
}
110+
111+
void writeH5RaySoA(const std::filesystem::path& filepath, const std::vector<std::string>& element_names, const RaySoA& rays, const RayAttrFlag attr) {
96112
RAYX_PROFILE_FUNCTION_STDOUT();
97113
RAYX_VERB << "writing rays to '" << filepath << "' with attribute flags: "
98114
<< std::bitset<static_cast<RayAttrFlagType>(RayAttrFlag::RayAttrFlagCount)>(static_cast<RayAttrFlagType>(attr));
@@ -108,14 +124,16 @@ void writeH5RaySoA(const std::filesystem::path& filepath, const RaySoA& rays, co
108124
#undef X
109125

110126
file.createDataSet("rayx/num_paths", rays.num_paths);
127+
file.createDataSet("rayx/element_names", element_names);
111128
} catch (const std::exception& e) {
112129
RAYX_EXIT << "exception caught while attempting to write h5 file: " << e.what();
113130
}
114131
}
115132

116-
void writeH5BundleHistory(const std::filesystem::path& filepath, const BundleHistory& bundle, const RayAttrFlag attr) {
133+
void writeH5BundleHistory(const std::filesystem::path& filepath, const std::vector<std::string>& element_names, const BundleHistory& bundle,
134+
const RayAttrFlag attr) {
117135
const auto rays = bundleHistoryToRaySoA(bundle);
118-
writeH5RaySoA(filepath, rays, attr);
136+
writeH5RaySoA(filepath, element_names, rays, attr);
119137
}
120138

121139
} // namespace RAYX

Intern/rayx-core/src/Writer/H5Writer.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
namespace RAYX {
88

99
#ifndef NO_H5
10-
RAYX_API RaySoA readH5RaySoA(const std::filesystem::path& filename, const RayAttrFlag attr = RayAttrFlag::All);
11-
RAYX_API BundleHistory readH5BundleHistory(const std::filesystem::path& filename);
10+
RAYX_API RaySoA readH5RaySoA(const std::filesystem::path& filepath, const RayAttrFlag attr = RayAttrFlag::All);
11+
RAYX_API BundleHistory readH5BundleHistory(const std::filesystem::path& filepath);
12+
RAYX_API std::vector<std::string> readH5ElementNames(const std::filesystem::path& filepath);
1213

13-
RAYX_API void writeH5RaySoA(const std::filesystem::path& filename, const RaySoA& rays, const RayAttrFlag attr = RayAttrFlag::All);
14-
RAYX_API void writeH5BundleHistory(const std::filesystem::path& filename, const BundleHistory& bundle, const RayAttrFlag attr = RayAttrFlag::All);
14+
RAYX_API void writeH5RaySoA(const std::filesystem::path& filepath, const std::vector<std::string>& element_names, const RaySoA& rays,
15+
const RayAttrFlag attr = RayAttrFlag::All);
16+
RAYX_API void writeH5BundleHistory(const std::filesystem::path& filepath, const std::vector<std::string>& element_names, const BundleHistory& bundle,
17+
const RayAttrFlag attr = RayAttrFlag::All);
1518
#endif
1619

1720
} // namespace RAYX

Intern/rayx-core/tests/testSources.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ TEST_F(TestSuite, testH5Writer) {
214214
const auto beamlineFilename = "METRIX_U41_G1_H1_318eV_PS_MLearn_v114";
215215
const auto rayOriginal = traceRML(beamlineFilename);
216216
const auto rayOriginalSoA = bundleHistoryToRaySoA(rayOriginal);
217+
const auto elementNamesOriginal = loadBeamline(beamlineFilename).getElementNames();
217218

218219
// test conversion between BundleHistory and RaySoA
219220
{
@@ -225,9 +226,12 @@ TEST_F(TestSuite, testH5Writer) {
225226

226227
// test if write and read of BundleHistory work without altering the contents
227228
{
228-
writeH5BundleHistory(h5Filepath, rayOriginal);
229+
writeH5BundleHistory(h5Filepath, elementNamesOriginal, rayOriginal);
229230
const auto bundle = readH5BundleHistory(h5Filepath);
230231
CHECK_EQ(rayOriginal, bundle);
232+
233+
const auto elementNames = readH5ElementNames(h5Filepath);
234+
if (elementNamesOriginal != elementNames) ADD_FAILURE();
231235
}
232236

233237
// test if write and read of partial BundleHistory work without altering the contents
@@ -241,7 +245,7 @@ TEST_F(TestSuite, testH5Writer) {
241245

242246
// write only some attributes
243247
const auto attr = RayAttrFlag::Energy | RayAttrFlag::Position;
244-
writeH5BundleHistory(h5Filepath, rayOriginal, attr);
248+
writeH5BundleHistory(h5Filepath, elementNamesOriginal, rayOriginal, attr);
245249
// read only some attributes
246250
const auto raySoA = readH5RaySoA(h5Filepath, attr);
247251

Intern/rayx-ui/src/Simulator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void Simulator::runSimulation() {
4444

4545
path += ".h5";
4646
#ifndef NO_H5
47-
RAYX::writeH5RaySoA(path, rays);
47+
RAYX::writeH5RaySoA(path, m_Beamline.getElementNames(), rays);
4848
#else
4949
RAYX::writeCsv(bundleHist, path);
5050
#endif

Intern/rayx/src/TerminalApp.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ void TerminalApp::tracePath(const std::filesystem::path& path) {
203203
RAYX_EXIT << "Output directory '" << parent.string() << "' does not exist. Create it first or use a different -o path.";
204204
}
205205

206-
auto file = exportRays(rays, isCSV, outputPath, attr);
206+
auto element_names = m_Beamline->getElementNames();
207+
auto file = exportRays(rays, element_names, isCSV, outputPath, attr);
207208

208209
// Plot
209210
if (m_CommandParser->m_args.m_plotFlag) {
@@ -303,7 +304,8 @@ void TerminalApp::run() {
303304
tracePath(m_CommandParser->m_args.m_providedFile);
304305
}
305306

306-
std::filesystem::path TerminalApp::exportRays(const RAYX::RaySoA& rays, bool isCSV, const std::filesystem::path& path, const RAYX::RayAttrFlag attr) {
307+
std::filesystem::path TerminalApp::exportRays(const RAYX::RaySoA& rays, const std::vector<std::string>& element_names, bool isCSV,
308+
const std::filesystem::path& path, const RAYX::RayAttrFlag attr) {
307309
RAYX_PROFILE_FUNCTION_STDOUT();
308310

309311
if (isCSV) {
@@ -312,7 +314,7 @@ std::filesystem::path TerminalApp::exportRays(const RAYX::RaySoA& rays, bool isC
312314
#ifdef NO_H5
313315
RAYX_EXIT << "writeH5 called during NO_H5 (HDF5 disabled during build)";
314316
#else
315-
writeH5RaySoA(path, rays, attr);
317+
writeH5RaySoA(path, element_names, rays, attr);
316318
#endif
317319
}
318320

Intern/rayx/src/TerminalApp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class TerminalApp {
3232
/// children of that directory.
3333
void tracePath(const std::filesystem::path& path);
3434
// returns the output filename (either .csv or .h5)
35-
std::filesystem::path exportRays(const RAYX::RaySoA& rays, bool isCSV, const std::filesystem::path&, const RAYX::RayAttrFlag attr);
35+
std::filesystem::path exportRays(const RAYX::RaySoA& rays, const std::vector<std::string>& element_names, bool isCSV,
36+
const std::filesystem::path&, const RAYX::RayAttrFlag attr);
3637

3738
std::string providedFile;
3839
std::unique_ptr<CommandParser> m_CommandParser;

0 commit comments

Comments
 (0)