Skip to content

Commit

Permalink
load energy dat file earlier and catch faulty file, rename Folder Dat…
Browse files Browse the repository at this point in the history
…a->Rml
  • Loading branch information
F Zotter committed Jan 6, 2025
1 parent fcb1482 commit 11de4ef
Show file tree
Hide file tree
Showing 39 changed files with 87 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,53 @@

namespace RAYX {
bool DatFile::load(const std::filesystem::path& filename, DatFile* out) {
std::ifstream s(filename);
RAYX_VERB << filename;
std::string line;

// line 1
std::getline(s, out->m_title);

// line 2
std::getline(s, line);
#if defined(WIN32)
if (sscanf_s(line.c_str(), "%u %le %le %le", &out->m_lineCount, &out->m_start, &out->m_end, &out->m_step) != 4) {
#else
if (sscanf(line.c_str(), "%u %le %le %le", &out->m_lineCount, &out->m_start, &out->m_end, &out->m_step) != 4) {
#endif
RAYX_EXIT << "Failed to parse DatFile \"" << filename << "\", at line 2: \"" << line << "\"";
return false;
}
out->m_Lines.reserve(out->m_lineCount);

// line 3..EOF
out->m_weightSum = 0;
for (uint32_t lineidx = 3; std::getline(s, line); lineidx++) {
if (line.empty()) {
continue;
try
{
std::ifstream s(filename);
RAYX_VERB << filename;
std::string line;

// line 1
std::getline(s, out->m_title);

// line 2
std::getline(s, line);
#if defined(WIN32)
if (sscanf_s(line.c_str(), "%u %le %le %le", &out->m_lineCount, &out->m_start, &out->m_end, &out->m_step) != 4) {
#else
if (sscanf(line.c_str(), "%u %le %le %le", &out->m_lineCount, &out->m_start, &out->m_end, &out->m_step) != 4) {
#endif
RAYX_D_ERR << "Failed to parse DatFile \"" << filename << "\", at line 2: \"" << line << "\"";
return false;
}
out->m_Lines.reserve(out->m_lineCount);

DatEntry e{};
#if defined(WIN32)
if (sscanf_s(line.c_str(), "%le %le", &e.m_energy, &e.m_weight) != 2) {
#else
if (sscanf(line.c_str(), "%le %le", &e.m_energy, &e.m_weight) != 2) {
#endif
RAYX_EXIT << "Failed to parse DatFile \"" << filename << "\", at line " << lineidx << ": \"" << line << "\"";
return false;
// line 3..EOF
out->m_weightSum = 0;
for (uint32_t lineidx = 3; std::getline(s, line); lineidx++) {
if (line.empty()) {
continue;
}

DatEntry e{};
#if defined(WIN32)
if (sscanf_s(line.c_str(), "%le %le", &e.m_energy, &e.m_weight) != 2) {
#else
if (sscanf(line.c_str(), "%le %le", &e.m_energy, &e.m_weight) != 2) {
#endif
RAYX_D_ERR << "Failed to parse DatFile \"" << filename << "\", at line " << lineidx << ": \"" << line << "\"";
return false;
}
out->m_Lines.push_back(e);
out->m_weightSum += e.m_weight;
}
out->m_Lines.push_back(e);
out->m_weightSum += e.m_weight;
}

return true;
return true;

} catch (const std::exception& e) {
RAYX_D_ERR << "Exception caught while loading DatFile \"" << filename << "\": " << e.what();
return false;
}
}

[[maybe_unused]] std::string DatFile::dump() {
Expand All @@ -64,12 +71,7 @@ bool DatFile::load(const std::filesystem::path& filename, DatFile* out) {

double DatFile::selectEnergy() const {
// runs either continuous Energydistribution from DataFile or just the specific energies
// provisionally set to true because EnergyDistibution ended support for this choice
// TODO: Fanny find a way to get a choise for DataFile Distribution back
if (m_continuous) {
if (m_Lines.size() == 1) { // weird edge case, which would crash the code below
return m_Lines[0].m_energy;
}
// find the index `idx`, s.t.
// we will return an energy between lines[idx].energy and
// lines[idx+1].energy
Expand All @@ -79,7 +81,7 @@ double DatFile::selectEnergy() const {
double counter = 0;
uint32_t idx = 0;

for (; idx < m_Lines.size() - 2; idx++) {
for (; idx <= m_Lines.size() - 1; idx++) {
counter += (m_Lines[idx].m_weight + m_Lines[idx + 1].m_weight) / 2;
if (counter >= w) {
break;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Beamline/EnergyDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <variant>

#include "Core.h"
#include "Data/DatFile.h"
#include "DatFile.h"

namespace RAYX {

Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-core/src/Beamline/LightSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <vector>

#include "Core.h"
#include "Data/Strings.h"
#include "Data/xml.h"
#include "Element/Strings.h"
#include "Rml/xml.h"
#include "EnergyDistribution.h"
#include "Shader/Ray.h"

Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Beamline/Objects/CircleSource.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include "CircleSource.h"

#include "Data/xml.h"
#include "Rml/xml.h"
#include "Debug/Debug.h"
#include "Debug/Instrumentor.h"
#include "Design/DesignSource.h"
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 @@ -2,7 +2,7 @@

#include <fstream>

#include "Data/xml.h"
#include "Rml/xml.h"
#include "Debug/Debug.h"
#include "Debug/Instrumentor.h"
#include "Design/DesignSource.h"
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
@@ -1,6 +1,6 @@
#include "MatrixSource.h"

#include "Data/xml.h"
#include "Rml/xml.h"
#include "Debug/Debug.h"
#include "Debug/Instrumentor.h"
#include "Design/DesignSource.h"
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
@@ -1,6 +1,6 @@
#include "PixelSource.h"

#include "Data/xml.h"
#include "Rml/xml.h"
#include "Debug/Debug.h"
#include "Debug/Instrumentor.h"
#include "Design/DesignElement.h"
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Beamline/Objects/PointSource.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "Beamline/LightSource.h"
#include "Data/xml.h"
#include "Rml/xml.h"

namespace RAYX {
struct DesignSource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "SimpleUndulatorSource.h"

#include "Data/xml.h"
#include "Rml/xml.h"
#include "Debug/Debug.h"
#include "Debug/Instrumentor.h"
#include "Design/DesignSource.h"
Expand Down
5 changes: 3 additions & 2 deletions Intern/rayx-core/src/Design/DesignSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ void DesignSource::setEnergySpreadUnit(EnergySpreadUnit value) { m_elementParame
EnergySpreadUnit DesignSource::getEnergySpreadUnit() const { return m_elementParameters["energySpreadUnit"].as_energySpreadUnit(); }

void DesignSource::setEnergyDistributionType(EnergyDistributionType value) { m_elementParameters["energyDistributionType"] = value; }
EnergyDistributionType DesignSource::getEnergyDistributionType() const { return m_elementParameters["energyDistributionType"].as_energyDistributionType(); }

void DesignSource::setEnergyDistributionFile(std::string value) { m_elementParameters["photonEnergyDistributionFile"] = value; }

void DesignSource::setEnergySpreadType(SpreadType value) { m_elementParameters["energyDistribution"] = value; }
Expand All @@ -207,12 +209,11 @@ double DesignSource::getPhotonFlux() const { return m_elementParameters["photonF
EnergyDistribution DesignSource::getEnergyDistribution() const {
EnergyDistribution en;
SpreadType spreadType = m_elementParameters["energyDistribution"].as_energySpreadType();
EnergyDistributionType energyDistributionType = m_elementParameters["energyDistributionType"].as_energyDistType();
EnergyDistributionType energyDistributionType = m_elementParameters["energyDistributionType"].as_energyDistributionType();

if (energyDistributionType == EnergyDistributionType::File) {
std::string filename = m_elementParameters["photonEnergyDistributionFile"].as_string();

std::cout << std::filesystem::current_path() << std::endl;
DatFile df;
DatFile::load(filename, &df);

Expand Down
2 changes: 2 additions & 0 deletions Intern/rayx-core/src/Design/DesignSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ struct RAYX_API DesignSource {
SpreadType getEnergySpreadType() const;

void setEnergyDistributionType(EnergyDistributionType value);
EnergyDistributionType getEnergyDistributionType() const;

void setEnergyDistributionFile(std::string value);

void setEnergySpreadUnit(EnergySpreadUnit value);
Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-core/src/Design/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ class DesignMap {
return *x;
}

inline EnergyDistributionType as_energyDistType() const {
inline EnergyDistributionType as_energyDistributionType() const {
auto* x = std::get_if<EnergyDistributionType>(&m_variant);
if (!x) throw std::runtime_error("as_energyDistType() called on non-energyDistType!");
if (!x) throw std::runtime_error("as_energyDistributionType() called on non-energyDistributionType!");
return *x;
}

Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Element/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Behaviour.h"
#include "Core.h"
#include "Cutout.h"
#include "Data/xml.h"
#include "Rml/xml.h"
#include "Shader/SlopeError.h"
#include "Surface.h"

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Material/NffTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <algorithm>
#include <fstream>

#include "Data/Locate.h"
#include "Rml/Locate.h"
#include "Debug/Debug.h"

namespace RAYX {
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Material/PalikTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <algorithm>
#include <fstream>

#include "Data/Locate.h"
#include "Rml/Locate.h"
#include "Debug/Debug.h"

namespace RAYX {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ void setAllMandatory(xml::Parser parser, DesignSource* ds) {
}

void setDefaultEnergy(xml::Parser parser, DesignSource* ds) {
ds->setEnergy(parser.parsePhotonEnergy());
ds->setEnergyDistributionFile(parser.parseEnergyDistributionFile().generic_string());
ds->setEnergyDistributionType(parser.parseEnergyDistributionType());
ds->setEnergySpread(parser.parseEnergySpread());
ds->setEnergySpreadType(parser.parseEnergySpreadType());

if (ds->getEnergyDistributionType() == EnergyDistributionType::File) {
ds->setEnergyDistributionFile(parser.parseEnergyDistributionFile().generic_string());
} else {
ds->setEnergy(parser.parsePhotonEnergy());
ds->setEnergySpread(parser.parseEnergySpread());
}
}

void setDefaultOrientation(xml::Parser parser, DesignSource* ds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

#include "Beamline/Beamline.h"
#include "Beamline/Objects/Objects.h"
#include "Data/xml.h"
#include "Rml/xml.h"
#include "Debug/Debug.h"
#include "Debug/Instrumentor.h"
#include "Design/DesignElement.h"
#include "DesignElementWriter.h"
#include "DesignSourceWriter.h"
#include "Strings.h"
#include "Element/Strings.h"

using RAYX::ElementType;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "Beamline/LightSource.h"
#include "Debug/Debug.h"
#include "Shader/Constants.h"
#include "Strings.h"
#include "Element/Strings.h"

namespace RAYX::xml {

Expand Down
File renamed without changes.
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 @@ -136,4 +136,4 @@ Ray behaveImagePlane(Ray r, [[maybe_unused]] int id, [[maybe_unused]] Collision
return r;
}

} // namespace RAYX
} // namespace RAYX
Binary file added Intern/rayx-core/tests/input/loadDatFile.h5
Binary file not shown.
2 changes: 1 addition & 1 deletion Intern/rayx-core/tests/input/loadDatFile.rml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<param id="rotationXerror" enabled="F">0</param>
<param id="rotationYerror" enabled="F">0</param>
<param id="energyDistributionType" comment="File" enabled="T">0</param>
<param id="photonEnergyDistributionFile" relative="loadDatFile.DAT" enabled="T">loadDatFile.DAT</param>
<param id="photonEnergyDistributionFile" relative="loadDatFile.DAT" enabled="T">loadDatFie.DAT</param>
<param id="photonEnergy" enabled="F">100</param>
<param id="energySpreadType" comment="white band" enabled="F">0</param>
<param id="energySpreadUnit" comment="eV" enabled="F">0</param>
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 @@ -8,7 +8,7 @@
#include "Beamline/Objects/Objects.h"
#include "CanonicalizePath.h"
#include "Core.h"
#include "Data/Importer.h"
#include "Rml/Importer.h"
#include "Debug/Debug.h"
#include "Design/DesignElement.h"
#include "Design/DesignSource.h"
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "CanonicalizePath.h"
#include "Colors.h"
#include "Data/Importer.h"
#include "Rml/Importer.h"
#include "Debug/Debug.h"
#include "Debug/Instrumentor.h"
#include "FrameInfo.h"
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/GraphicsCore/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <utility>

#include "Buffer.h"
#include "Data/Locate.h"
#include "Rml/Locate.h"
#include "Debug/Instrumentor.h"

Texture::Texture(const Device& device)
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/RenderSystem/GridRenderSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "GridRenderSystem.h"

#include "Data/Locate.h"
#include "Rml/Locate.h"

GridRenderSystem::GridRenderSystem(Device& device, VkRenderPass renderPass, const std::vector<VkDescriptorSetLayout>& setLayouts)
: RenderSystem(device, fillInput(renderPass), setLayouts) {}
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/RenderSystem/ObjectRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <filesystem>

#include "Data/Locate.h"
#include "Rml/Locate.h"
#include "Vertex.h"

ObjectRenderSystem::ObjectRenderSystem(Device& device, VkRenderPass renderPass, const std::vector<VkDescriptorSetLayout>& setLayouts)
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/RenderSystem/RayRenderSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "RayRenderSystem.h"

#include "Data/Locate.h"
#include "Rml/Locate.h"
#include "RenderObject.h"
#include "Vertex.h"

Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-ui/src/UserInterface/BeamlineDesignHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <unordered_set>

#include "Beamline/StringConversion.h"
#include "Data/Strings.cpp"
#include "Element/Strings.cpp"
#include "Debug/Instrumentor.h"

void BeamlineDesignHandler::showBeamlineDesignWindow(UIBeamlineInfo& uiBeamlineInfo) {
Expand Down Expand Up @@ -321,7 +321,7 @@ void BeamlineDesignHandler::createInputField(const std::string& key, RAYX::Desig
break;
}
case RAYX::ValueType::EnergyDistributionType: {
auto currentValue = element.as_energyDistType();
auto currentValue = element.as_energyDistributionType();
if (ImGui::BeginCombo("##energydisttype", RAYX::EnergyDistributionTypeToString.at(currentValue).c_str())) {
for (const auto& [value, name] : RAYX::EnergyDistributionTypeToString) {
bool isSelected = (currentValue == value);
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/UserInterface/BeamlineOutliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <fstream>

#include "Data/Strings.h"
#include "Element/Strings.h"

BeamlineOutliner::BeamlineOutliner(/* args */) {}

Expand Down
Loading

0 comments on commit 11de4ef

Please sign in to comment.