-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SpectrumBeam as derived class from Beam object #157
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -452,6 +452,145 @@ namespace dxtbx { namespace model { | |
scitbx::af::shared<vec3<double> > s0_at_scan_points_; | ||
}; | ||
|
||
/** A class to represent a beam with a known spectrum. */ | ||
class SpectrumBeam : public Beam { | ||
public: | ||
|
||
/** Default constructor: initialise all to zero */ | ||
SpectrumBeam() | ||
: Beam(), | ||
spectrum_energies_(), | ||
spectrum_weights_() {} | ||
|
||
/** Copy constructor */ | ||
SpectrumBeam(Beam b) | ||
: Beam(b), | ||
spectrum_energies_(), | ||
spectrum_weights_() {} | ||
|
||
/** | ||
* Initialise all the beam parameters. | ||
* @param direction The beam direction vector. | ||
*/ | ||
SpectrumBeam(vec3 <double> s0) | ||
: Beam(s0), | ||
spectrum_energies_(), | ||
spectrum_weights_() {} | ||
|
||
/** | ||
* Initialise all the beam parameters. Normalize the direction vector | ||
* and give it the length of 1.0 / wavelength | ||
* @param wavelength The wavelength of the beam | ||
* @param direction The beam direction vector. | ||
*/ | ||
SpectrumBeam(vec3 <double> direction, double wavelength) | ||
: Beam(direction, wavelength), | ||
spectrum_energies_(), | ||
spectrum_weights_() {} | ||
|
||
/** | ||
* Initialise all the beam parameters. | ||
* @param direction The beam direction vector. | ||
*/ | ||
SpectrumBeam(vec3 <double> s0, double divergence, double sigma_divergence) | ||
: Beam(s0, divergence, sigma_divergence), | ||
spectrum_energies_(), | ||
spectrum_weights_() {} | ||
|
||
/** | ||
* Initialise all the beam parameters. Normalize the direction vector | ||
* and give it the length of 1.0 / wavelength | ||
* @param wavelength The wavelength of the beam | ||
* @param direction The beam direction vector. | ||
*/ | ||
SpectrumBeam(vec3 <double> direction, double wavelength, | ||
double divergence, double sigma_divergence) | ||
: Beam(direction, wavelength, divergence, sigma_divergence), | ||
spectrum_energies_(), | ||
spectrum_weights_() {} | ||
|
||
SpectrumBeam(vec3 <double> direction, double wavelength, | ||
double divergence, double sigma_divergence, | ||
vec3<double> polarization_normal, | ||
double polarization_fraction, | ||
double flux, | ||
double transmission) | ||
: Beam(direction, wavelength, divergence, sigma_divergence, | ||
polarization_normal, polarization_fraction, flux, transmission), | ||
spectrum_energies_(), | ||
spectrum_weights_() {} | ||
|
||
SpectrumBeam(vec3 <double> direction, double wavelength, | ||
double divergence, double sigma_divergence, | ||
vec3<double> polarization_normal, | ||
double polarization_fraction, | ||
double flux, double transmission, | ||
scitbx::af::shared<double> spectrum_energies, scitbx::af::shared<double> spectrum_weights) | ||
: Beam(direction, wavelength, divergence, sigma_divergence, | ||
polarization_normal, polarization_fraction, flux, transmission), | ||
spectrum_energies_(spectrum_energies), spectrum_weights_(spectrum_weights){} | ||
|
||
/** | ||
* Set the spectrum. X is eV, Y is dimensionless | ||
*/ | ||
void set_spectrum(scitbx::af::shared<double> spectrum_energies, scitbx::af::shared<double> spectrum_weights) { | ||
DXTBX_ASSERT(spectrum_energies.size() == spectrum_weights.size()); | ||
spectrum_energies_ = spectrum_energies; | ||
spectrum_weights_ = spectrum_weights; | ||
} | ||
|
||
/** | ||
* Get the spectrum energies (eV) | ||
*/ | ||
scitbx::af::shared<double> get_spectrum_energies() const { | ||
return spectrum_energies_; | ||
} | ||
|
||
/** | ||
* Get the spectrum weights | ||
*/ | ||
scitbx::af::shared<double> get_spectrum_weights() const { | ||
return spectrum_weights_; | ||
} | ||
|
||
double get_weighted_wavelength() const { | ||
double weighted_sum = 0; | ||
double summed_weights = 0; | ||
for (size_t i = 0; i < spectrum_energies_.size(); i++) { | ||
weighted_sum += spectrum_energies_[i] * spectrum_weights_[i]; | ||
summed_weights += spectrum_weights_[i]; | ||
} | ||
DXTBX_ASSERT(weighted_sum > 0 && summed_weights > 0); | ||
return 12398.4187 / (weighted_sum / summed_weights); | ||
} | ||
|
||
/** | ||
* Check if two models are similar | ||
*/ | ||
bool is_similar_to(const BeamBase &rhs, | ||
double wavelength_tolerance, | ||
double direction_tolerance, | ||
double polarization_normal_tolerance, | ||
double polarization_fraction_tolerance, | ||
double spectrum_weighted_wavelength_tolerance) const { | ||
if (!dynamic_cast<const Beam*>(this)->is_similar_to(rhs, | ||
wavelength_tolerance, direction_tolerance, | ||
polarization_normal_tolerance, | ||
polarization_fraction_tolerance)) | ||
return false; | ||
const SpectrumBeam* other = dynamic_cast<const SpectrumBeam* >(&rhs); | ||
if (!other) return true; | ||
return (std::abs(get_weighted_wavelength() - other->get_weighted_wavelength()) <= | ||
spectrum_weighted_wavelength_tolerance); | ||
} | ||
|
||
friend std::ostream& operator<<(std::ostream &os, const SpectrumBeam &b); | ||
|
||
private: | ||
scitbx::af::shared<double> spectrum_energies_; | ||
scitbx::af::shared<double> spectrum_weights_; | ||
}; | ||
|
||
/** Print beam information */ | ||
inline std::ostream &operator<<(std::ostream &os, const Beam &b) { | ||
os << "Beam:\n"; | ||
|
@@ -466,6 +605,21 @@ namespace dxtbx { namespace model { | |
return os; | ||
} | ||
|
||
/** Print beam information */ | ||
inline | ||
std::ostream& operator<<(std::ostream &os, const SpectrumBeam &b) { | ||
os << "SpectrumBeam:\n"; | ||
os << " wavelength: " << b.get_wavelength() << "\n"; | ||
os << " sample to source direction : " | ||
<< b.get_sample_to_source_direction().const_ref() << "\n"; | ||
os << " divergence: " << b.get_divergence() << "\n"; | ||
os << " sigma divergence: " << b.get_sigma_divergence() << "\n"; | ||
os << " polarization normal: " << b.get_polarization_normal().const_ref() | ||
<< "\n"; | ||
os << " polarization fraction: " << b.get_polarization_fraction() << "\n"; | ||
os << " spectrum weighted wavelength: " << b.get_weighted_wavelength() << "\n"; | ||
return os; | ||
} | ||
Comment on lines
+608
to
+622
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to append to the existing output for |
||
}} // namespace dxtbx::model | ||
|
||
#endif // DXTBX_MODEL_BEAM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps put a comment here to note that this is the eV per Å conversion factor