OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 <cmath>
20#include "Physics/Physics.h"
22#include "Utility/Inform.h"
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
29double SinusoidalTimeDependence::getValue(const double time) {
30 double result{};
31 for (size_t i = 0; i < f_m.size(); i++) {
32 const auto f = f_m[i];
33 auto p = 0.0;
34 auto a = 1.0;
35 auto o = 0.0;
36 if (i < p_m.size()) {
37 p = p_m[i];
38 }
39 if (i < a_m.size()) {
40 a = a_m[i];
41 }
42 if (i < o_m.size()) {
43 o = o_m[i];
44 }
45 const auto angle = 2.0 * Physics::pi * f * time + p;
46 result += a / 2.0 * std::sin(angle) + o;
47 }
48 return result;
49}
50
51double SinusoidalTimeDependence::getIntegral(const double time) {
52 double result{};
53 for (size_t i = 0; i < f_m.size(); i++) {
54 const auto f = f_m[i];
55 auto p{0.0};
56 auto a{1.0};
57 auto o{0.0};
58 if (i < p_m.size()) {
59 p = p_m[i];
60 }
61 if (i < a_m.size()) {
62 a = a_m[i];
63 }
64 if (i < o_m.size()) {
65 o = o_m[i];
66 }
67 result += o * time
68 + a / Physics::two_pi / f
69 * (std::cos(p) - std::cos(Physics::two_pi * f * time + p));
70 }
71 return result;
72}
73
77
78Inform& SinusoidalTimeDependence::print(Inform& os) const {
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}
SinusoidalTimeDependence * clone() override
Inform & print(Inform &os) const
SinusoidalTimeDependence()=default
double getIntegral(double time) override
double getValue(double time) override
constexpr double two_pi
The value of.
Definition Physics.h:40
constexpr double pi
The value of.
Definition Physics.h:36