Quantum Phenomena Explorer 7.9.8
Interactive quantum-phenomena Explorer Tutor — source documentation
Loading...
Searching...
No Matches
wavefunctioncrosssection2d.cpp
Go to the documentation of this file.
1/**
2 * @file wavefunctioncrosssection2d.cpp
3 * @brief Implements the wavefunctioncrosssection2d physics/numerical component used by QPE experiments.
4 * @ingroup qpe_quantum
5 *
6 * Source: @c core/quantum/wavefunctioncrosssection2d.cpp. This file is part of Quantum Phenomena Explorer (QPE),
7 * maintained by Officina Turini. See LICENSE_IT.txt / LICENSE.txt for the
8 * QPE freeware source-available terms and THIRD_PARTY_NOTICES.txt for
9 * third-party components.
10 */
11
13
15
16#include <algorithm>
17#include <cmath>
18
19namespace qpe {
20namespace {
21
22std::size_t nearestIndex(double coordinate,
23 std::size_t count,
24 double spacing)
25{
26 if (count == 0u || !(spacing > 0.0)) {
27 return 0u;
28 }
29
30 const double center = 0.5 * static_cast<double>(count - 1u);
31 const double floatingIndex = coordinate / spacing + center;
32 const double clamped = std::max(0.0,
33 std::min(static_cast<double>(count - 1u), floatingIndex));
34 return static_cast<std::size_t>(std::llround(clamped));
35}
36
37/**
38 * @brief Performs physical coordinate.
39 * @param index Zero-based index of the selected item.
40 * @param count The count value used by the operation.
41 * @param spacing The spacing value used by the operation.
42 */
43double physicalCoordinate(std::size_t index,
44 std::size_t count,
45 double spacing)
46{
47 return (static_cast<double>(index) -
48 0.5 * static_cast<double>(count - 1u)) * spacing;
49}
50
51/**
52 * @brief Performs append sample.
53 * @param value Value to assign or evaluate.
54 * @param coordinate The coordinate value used by the operation.
55 * @param out The out value used by the operation.
56 */
57void appendSample(const ComplexField2D::Value &value,
58 double coordinate,
60{
61 const double re = static_cast<double>(value.real());
62 const double im = static_cast<double>(value.imag());
63 out.coordinate.push_back(coordinate);
64 out.realPart.push_back(re);
65 out.imaginaryPart.push_back(im);
66 out.probability.push_back(re * re + im * im);
67 out.phase.push_back(std::atan2(im, re));
68}
69
70} // namespace
71
73 const ComplexField2D &field,
75 double position)
76{
78 out.axis = axis;
79 out.requestedPosition = position;
80
81 if (field.empty() || field.width() == 0u || field.height() == 0u) {
82 return out;
83 }
84
86 const std::size_t y = nearestIndex(position,
87 field.height(),
88 static_cast<double>(field.dy()));
89 out.actualPosition = physicalCoordinate(y,
90 field.height(),
91 static_cast<double>(field.dy()));
92 out.coordinate.reserve(field.width());
93 out.probability.reserve(field.width());
94 out.realPart.reserve(field.width());
95 out.imaginaryPart.reserve(field.width());
96 out.phase.reserve(field.width());
97 for (std::size_t x = 0u; x < field.width(); ++x) {
98 appendSample(field(x, y),
99 physicalCoordinate(x,
100 field.width(),
101 static_cast<double>(field.dx())),
102 out);
103 }
104 } else {
105 const std::size_t x = nearestIndex(position,
106 field.width(),
107 static_cast<double>(field.dx()));
108 out.actualPosition = physicalCoordinate(x,
109 field.width(),
110 static_cast<double>(field.dx()));
111 out.coordinate.reserve(field.height());
112 out.probability.reserve(field.height());
113 out.realPart.reserve(field.height());
114 out.imaginaryPart.reserve(field.height());
115 out.phase.reserve(field.height());
116 for (std::size_t y = 0u; y < field.height(); ++y) {
117 appendSample(field(x, y),
118 physicalCoordinate(y,
119 field.height(),
120 static_cast<double>(field.dy())),
121 out);
122 }
123 }
124
125 return out;
126}
127
128} // namespace qpe
Owning Cartesian grid of complex scalar samples.
bool empty() const noexcept
Returns whether the field contains no samples.
float dx() const noexcept
Returns the X grid spacing.
std::size_t height() const noexcept
Returns the number of samples along Y.
std::complex< float > Value
float dy() const noexcept
Returns the Y grid spacing.
std::size_t width() const noexcept
Returns the number of samples along X.
static WaveFunctionCrossSection2DData sample(const ComplexField2D &field, CrossSectionAxis axis, double position)
Performs sample.
Declares the complex scalar field container representing two-dimensional wave functions.
double physicalCoordinate(std::size_t index, std::size_t count, double spacing)
Performs physical coordinate.
std::size_t nearestIndex(double coordinate, std::size_t count, double spacing)
CrossSectionAxis
Enumerates the supported Cross Section Axis values.
Data structure used by the Wave Function Cross Section2D Data component.
Declares the wavefunctioncrosssection2d physics/numerical component used by QPE experiments.