Quantum Phenomena Explorer 7.9.8
Interactive quantum-phenomena Explorer Tutor — source documentation
Loading...
Searching...
No Matches
diracspinor2d.cpp
Go to the documentation of this file.
1/**
2 * @file diracspinor2d.cpp
3 * @brief Implements storage, normalization, observables, and utilities for two-component Dirac spinor fields.
4 * @ingroup qpe_quantum
5 *
6 * Source: @c core/quantum/diracspinor2d.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 "diracspinor2d.h"
13
14#include <algorithm>
15#include <cmath>
16
17namespace qpe {
18
19DiracSpinor2D::DiracSpinor2D(std::size_t width, std::size_t height, float dx, float dy)
20{
22}
23
24void DiracSpinor2D::resize(std::size_t width, std::size_t height, float dx, float dy)
25{
28}
29
30void DiracSpinor2D::clear() noexcept
31{
32 upper_.clear();
33 lower_.clear();
34}
35
37 const ComplexField2D::Value &lowerValue)
38{
39 upper_.fill(upperValue);
40 lower_.fill(lowerValue);
41}
42
43bool DiracSpinor2D::empty() const noexcept
44{
45 return upper_.empty() || lower_.empty();
46}
47
48bool DiracSpinor2D::matchesGrid(const ComplexField2D &field) const noexcept
49{
50 return !empty() && !field.empty() &&
51 width() == field.width() && height() == field.height() &&
52 dx() == field.dx() && dy() == field.dy();
53}
54
55double DiracSpinor2D::norm2() const noexcept
56{
57 return empty() ? 0.0 : upper_.norm2() + lower_.norm2();
58}
59
60double DiracSpinor2D::norm() const noexcept
61{
62 return std::sqrt(norm2());
63}
64
66{
67 const double n = norm();
68 if (!(n > 0.0) || !std::isfinite(n)) return false;
69 const float s = static_cast<float>(1.0 / n);
70 for (std::size_t i = 0; i < upper_.size(); ++i) {
71 upper_.data()[i] *= s;
72 lower_.data()[i] *= s;
73 }
74 return true;
75}
76
77double DiracSpinor2D::upperPopulation() const noexcept
78{
79 const double n = norm2();
80 return n > 0.0 ? upper_.norm2() / n : 0.0;
81}
82
83double DiracSpinor2D::lowerPopulation() const noexcept
84{
85 const double n = norm2();
86 return n > 0.0 ? lower_.norm2() / n : 0.0;
87}
88
90{
91 if (empty()) {
92 out.clear();
93 return;
94 }
95 if (!matchesGrid(out)) out.resize(width(), height(), dx(), dy());
96 for (std::size_t i = 0; i < upper_.size(); ++i) {
97 const double rho = std::norm(upper_.data()[i]) + std::norm(lower_.data()[i]);
98 out.data()[i] = ComplexField2D::Value(static_cast<float>(std::sqrt(std::max(0.0, rho))), 0.0f);
99 }
100}
101
102} // namespace qpe
Owning Cartesian grid of complex scalar samples.
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.
bool empty() const noexcept
Returns whether the field contains no samples.
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.
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::complex< float > Value
double upperPopulation() const noexcept
Carries out upper population.
void writeDensityProxy(ComplexField2D &out) const
Writes density proxy.
std::size_t height() const noexcept
Returns height.
double norm2() const noexcept
Returns norm2.
ComplexField2D upper_
void fill(const ComplexField2D::Value &upperValue, const ComplexField2D::Value &lowerValue)
Assigns one value to all stored samples.
bool normalize() noexcept
Returns normalize.
float dx() const noexcept
Returns the X-axis grid spacing.
void clear() noexcept
Clears stored state and returns the component to its empty/default state.
void resize(std::size_t width, std::size_t height, float dx=1.0f, float dy=1.0f)
Resizes storage and updates the associated grid geometry.
double lowerPopulation() const noexcept
Carries out lower population.
float dy() const noexcept
Returns the Y-axis grid spacing.
double norm() const noexcept
Returns norm.
bool matchesGrid(const ComplexField2D &field) const noexcept
Carries out matches grid.
std::size_t width() const noexcept
Returns width.
bool empty() const noexcept
Returns whether the object contains no data.
DiracSpinor2D()=default
Constructs a Dirac Spinor 2D instance.
ComplexField2D lower_
Declares the two-component Dirac spinor field container used by QPE.