Skip to content

Commit

Permalink
Add a basic experiment class
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeilstenedmands committed Jan 17, 2025
1 parent 4318508 commit 3898dc0
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 2 deletions.
1 change: 0 additions & 1 deletion include/dx2/beam.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,4 @@ json MonoElectronBeam::to_json() const {
return MonochromaticBeam::to_json("electron");
}


#endif //DX2_MODEL_BEAM_H
35 changes: 34 additions & 1 deletion include/dx2/detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,37 @@ std::array<double, 2> Panel::px_to_mm(double x, double y) const {
return std::array<double, 2>{c1,c2};
}

#endif // DX2_MODEL_DETECTOR_H
// Define a simple detector, for now is just a vector of panels without any hierarchy.
class Detector {
public:
Detector()=default;
Detector(json detector_data);
json to_json() const;
std::vector<Panel> panels() const;

protected:
std::vector<Panel> _panels{};
};

Detector::Detector(json detector_data){
json panel_data = detector_data["panels"];
for (json::iterator it = panel_data.begin(); it !=panel_data.end(); ++it) {
_panels.push_back(Panel(*it));
}
}

json Detector::to_json() const {
json detector_data;
std::vector<json> panels_array;
for (auto p = _panels.begin(); p != _panels.end(); ++p){
panels_array.push_back(p->to_json());
}
detector_data["panels"] = panels_array;
return detector_data;
}

std::vector<Panel> Detector::panels() const {
return _panels;
}

#endif // DX2_MODEL_DETECTOR_H
122 changes: 122 additions & 0 deletions include/dx2/experiment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#ifndef DX2_MODEL_EXPERIMENT_H
#define DX2_MODEL_EXPERIMENT_H
#include <Eigen/Dense>
#include <nlohmann/json.hpp>
#include <dx2/detector.h>
#include <dx2/goniometer.h>
#include <dx2/scan.h>
#include <dx2/beam.h>
#include <dx2/crystal.h>

using Eigen::Vector3d;
using json = nlohmann::json;

template<class BeamType>
class Experiment {
public:
Experiment()=default;
Experiment(json experiment_data);
json to_json() const;
Goniometer goniometer() const;
BeamType beam() const;
Scan scan() const;
Detector detector() const;
Crystal crystal() const;
void set_crystal(Crystal crystal);

protected:
BeamType _beam{};
Scan _scan{};
Goniometer _goniometer{};
Detector _detector{};
Crystal _crystal{};
};

template<class BeamType>
Experiment<BeamType>::Experiment(json experiment_data){
json beam_data = experiment_data["beam"][0];
BeamType beam = BeamType(beam_data);
json scan_data = experiment_data["scan"][0];
Scan scan(scan_data);
json gonio_data = experiment_data["goniometer"][0];
Goniometer gonio(gonio_data);
json detector_data = experiment_data["detector"][0];
Detector detector(detector_data);
this->_beam = beam;
this->_scan = scan;
this->_goniometer = gonio;
this->_detector = detector;
try { // We don't always have a crystal model e.g. before indexing.
json crystal_data = experiment_data["crystal"][0];
Crystal crystal(crystal_data);
this->_crystal = crystal;
}
catch (...) {
;
}
}

template<class BeamType>
json Experiment<BeamType>::to_json() const {
// save this experiment as an example experiment list
json elist_out; // a list of potentially multiple experiments
elist_out["__id__"] = "ExperimentList";
json expt_out; // our single experiment
// no imageset (for now?).
expt_out["__id__"] = "Experiment";
expt_out["identifier"] = "test";
expt_out["beam"] =
0; // the indices of the models that will correspond to our experiment
expt_out["detector"] = 0;
expt_out["goniometer"] = 0;
expt_out["scan"] = 0;

// add the the actual models
elist_out["scan"] = std::array<json, 1>{_scan.to_json()};
elist_out["goniometer"] = std::array<json, 1>{_goniometer.to_json()};
elist_out["beam"] = std::array<json, 1>{_beam.to_json()};
elist_out["detector"] = std::array<json, 1>{_detector.to_json()};

if (_crystal.get_U_matrix().determinant()){
expt_out["crystal"] = 0;
elist_out["crystal"] = std::array<json, 1>{_crystal.to_json()};
}
else {
elist_out["crystal"] = std::array<json, 0>{};
}

elist_out["experiment"] = std::array<json, 1>{expt_out};
return elist_out;
}

template<class BeamType>
Scan Experiment<BeamType>::scan() const {
return _scan;
}

template<class BeamType>
Goniometer Experiment<BeamType>::goniometer() const {
return _goniometer;
}

template<class BeamType>
Detector Experiment<BeamType>::detector() const {
return _detector;
}

template<class BeamType>
Crystal Experiment<BeamType>::crystal() const {
return _crystal;
}

template<class BeamType>
void Experiment<BeamType>::set_crystal(Crystal crystal) {
_crystal = crystal;
}

template<class BeamType>
BeamType Experiment<BeamType>::beam() const {
return _beam;
}

#endif //DX2_MODEL_EXPERIMENT_H

0 comments on commit 3898dc0

Please sign in to comment.