Quantum Phenomena Explorer 7.9.8
Interactive quantum-phenomena Explorer Tutor — source documentation
Loading...
Searching...
No Matches
complexfield2d.cpp
Go to the documentation of this file.
1/**
2 * @file complexfield2d.cpp
3 * @brief Implements storage and numerical utilities for complex scalar wave-function fields.
4 * @ingroup qpe_math
5 *
6 * Source: @c core/math/complexfield2d.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
12#include "complexfield2d.h"
13
14#include <algorithm>
15#include <cmath>
16#include <stdexcept>
17
18namespace qpe {
19
21 std::size_t height,
22 float dx,
23 float dy)
24{
26}
27
28void ComplexField2D::resize(std::size_t width,
29 std::size_t height,
30 float dx,
31 float dy)
32{
33 if (!(dx > 0.0f) || !(dy > 0.0f)) {
34 throw std::invalid_argument("ComplexField2D: dx and dy must be positive");
35 }
36
37 width_ = width;
39 dx_ = dx;
40 dy_ = dy;
41 data_.assign(width_ * height_, Value(0.0f, 0.0f));
42}
43
44void ComplexField2D::clear() noexcept
45{
46 std::fill(data_.begin(), data_.end(), Value(0.0f, 0.0f));
47}
48
49void ComplexField2D::fill(const Value &value)
50{
51 std::fill(data_.begin(), data_.end(), value);
52}
53
54bool ComplexField2D::empty() const noexcept
55{
56 return data_.empty();
57}
58
59std::size_t ComplexField2D::width() const noexcept
60{
61 return width_;
62}
63
64std::size_t ComplexField2D::height() const noexcept
65{
66 return height_;
67}
68
69std::size_t ComplexField2D::size() const noexcept
70{
71 return data_.size();
72}
73
74float ComplexField2D::dx() const noexcept
75{
76 return dx_;
77}
78
79float ComplexField2D::dy() const noexcept
80{
81 return dy_;
82}
83
84ComplexField2D::Value &ComplexField2D::operator()(std::size_t x, std::size_t y) noexcept
85{
86 return data_[index(x, y)];
87}
88
89const ComplexField2D::Value &ComplexField2D::operator()(std::size_t x, std::size_t y) const noexcept
90{
91 return data_[index(x, y)];
92}
93
94ComplexField2D::Value &ComplexField2D::at(std::size_t x, std::size_t y)
95{
96 return data_[checkedIndex(x, y)];
97}
98
99const ComplexField2D::Value &ComplexField2D::at(std::size_t x, std::size_t y) const
100{
101 return data_[checkedIndex(x, y)];
102}
103
105{
106 return data_.data();
107}
108
110{
111 return data_.data();
112}
113
114double ComplexField2D::sumAbs2() const noexcept
115{
116 double sum = 0.0;
117 for (const Value &v : data_) {
118 const double re = static_cast<double>(v.real());
119 const double im = static_cast<double>(v.imag());
120 sum += re * re + im * im;
121 }
122 return sum;
123}
124
125double ComplexField2D::norm2() const noexcept
126{
127 return sumAbs2() * static_cast<double>(dx_) * static_cast<double>(dy_);
128}
129
130double ComplexField2D::norm() const noexcept
131{
132 return std::sqrt(norm2());
133}
134
136{
137 const double n = norm();
138 if (!(n > 0.0) || !std::isfinite(n)) {
139 return false;
140 }
141
142 const float scale = static_cast<float>(1.0 / n);
143 for (Value &v : data_) {
144 v *= scale;
145 }
146 return true;
147}
148
149std::size_t ComplexField2D::index(std::size_t x, std::size_t y) const noexcept
150{
151 return y * width_ + x;
152}
153
154std::size_t ComplexField2D::checkedIndex(std::size_t x, std::size_t y) const
155{
156 if (x >= width_ || y >= height_) {
157 throw std::out_of_range("ComplexField2D: coordinates outside field");
158 }
159 return index(x, y);
160}
161
162} // namespace qpe
std::size_t checkedIndex(std::size_t x, std::size_t y) const
Checks ed index.
std::size_t size() const noexcept
Returns the total number of complex samples.
double norm2() const noexcept
Returns the discrete integral of |psi|^2 including dx*dy.
ComplexField2D()=default
Constructs an empty field.
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.
double norm() const noexcept
Returns the square root of the discrete probability norm.
Value & at(std::size_t x, std::size_t y)
Returns a mutable sample with bounds checking.
void clear() noexcept
Releases all samples and returns the field to the empty state.
void fill(const Value &value)
Assigns one complex value to every grid sample.
double sumAbs2() const noexcept
Returns the raw sample sum sum(|psi|^2) without cell area.
bool normalize() noexcept
Scales the field to unit discrete probability norm.
Value * data() noexcept
Returns mutable contiguous row-major sample storage.
void resize(std::size_t width, std::size_t height, float dx=1.0f, float dy=1.0f)
Reallocates the grid and resets all samples to zero.
std::size_t index(std::size_t x, std::size_t y) const noexcept
Returns index.
std::vector< Value > data_
std::complex< float > Value
float dy() const noexcept
Returns the Y grid spacing.
Value & operator()(std::size_t x, std::size_t y) noexcept
Returns a mutable sample without bounds checking.
std::size_t width() const noexcept
Returns the number of samples along X.
Declares the complex scalar field container representing two-dimensional wave functions.