OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
SinusoidalTimeDependence.cpp
Go to the documentation of this file.
1//
2// Class SinusoidalTimeDependence
3// A time dependence class that generates sine waves
4//
5// Copyright (c) 2025, Jon Thompson, STFC Rutherford Appleton Laboratory, Didcot, UK
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General Public License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17
19#include <Physics/Physics.h>
21#include "Utility/Inform.h"
22
23
25 const std::vector<double>& f, const std::vector<double>& p, const std::vector<double>& a,
26 const std::vector<double>& o)
27 : f_m(f), p_m(p), a_m(a), o_m(o) {
28}
29
30double SinusoidalTimeDependence::getValue(const double time) {
31 double result{};
32 for (size_t i = 0; i < f_m.size(); i++) {
33 const auto f = f_m[i];
34 auto p = 0.0;
35 auto a = 1.0;
36 auto o = 0.0;
37 if (i < p_m.size()) {
38 p = p_m[i];
39 }
40 if (i < a_m.size()) {
41 a = a_m[i];
42 }
43 if (i < o_m.size()) {
44 o = o_m[i];
45 }
46 const auto angle = 2.0 * Physics::pi * f * time + p;
47 result += a / 2.0 * std::sin(angle) + o;
48 }
49 return result;
50}
51
52double SinusoidalTimeDependence::getIntegral(const double time) {
53 double result{};
54 for (size_t i = 0; i < f_m.size(); i++) {
55 const auto f = f_m[i];
56 auto p = 0.0;
57 auto a = 1.0;
58 auto o = 0.0;
59 if (i < p_m.size()) {
60 p = p_m[i];
61 }
62 if (i < a_m.size()) {
63 a = a_m[i];
64 }
65 if (i < o_m.size()) {
66 o = o_m[i];
67 }
68 result += o * time + a / Physics::two_pi / f *
69 (std::cos(p) - std::cos(Physics::two_pi * f * time + p));
70 }
71 return result;
72}
73
75 return new SinusoidalTimeDependence(f_m, p_m, a_m, o_m);
76}
77
79 const Inform::FmtFlags_t ff = os.flags();
80 os << std::scientific;
81 os << "f=[";
82 for (size_t i = 0; i < this->f_m.size(); i++) {
83 if (i != 0) {
84 os << ", ";
85 }
86 os << this->f_m[i];
87 }
88 os << "], p=[";
89 for (size_t i = 0; i < this->p_m.size(); i++) {
90 if (i != 0) {
91 os << ", ";
92 }
93 os << this->p_m[i];
94 }
95 os << "], a=[";
96 for (size_t i = 0; i < this->a_m.size(); i++) {
97 if (i != 0) {
98 os << ", ";
99 }
100 os << this->a_m[i];
101 }
102 os << "], o=[";
103 for (size_t i = 0; i < this->o_m.size(); i++) {
104 if (i != 0) {
105 os << ", ";
106 }
107 os << this->o_m[i];
108 }
109 os << endl;
110 os.flags(ff);
111 return os;
112}
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
std::complex< double > a
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
constexpr double two_pi
The value of.
Definition: Physics.h:33
constexpr double pi
The value of.
Definition: Physics.h:30
Time dependence that follows sum of a set of sinusoids sigma_over_i(a[i] / 2 * sin(2 * pi * f[i] * t ...
SinusoidalTimeDependence * clone() override
Inheritable clone function.
Inform & print(Inform &os) const
Print the sinusoidals.
SinusoidalTimeDependence()=default
Default Constructor makes a 0 length polynomial.
double getIntegral(double time) override
Return the integral from 0 to time.
double getValue(double time) override
Return the sinusoidal value.
Definition: Inform.h:42
std::ios_base::fmtflags FmtFlags_t
Definition: Inform.h:99
FmtFlags_t flags() const
Definition: Inform.h:106