OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
RealConstant.cpp
Go to the documentation of this file.
1 //
2 // Class RealConstant
3 // The REAL CONSTANT definition.
4 //
5 // Copyright (c) 2000 - 2021, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // This file is part of OPAL.
9 //
10 // OPAL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17 //
19 
21 #include "Attributes/Attributes.h"
22 #include "OPALconfig.h"
23 #include "Physics/Physics.h"
24 #include "Physics/Units.h"
25 #include "Utilities/Options.h"
26 #include "Utility/IpplInfo.h"
27 
28 #include <cmath>
29 #include <iostream>
30 
31 
33  ValueDefinition(1, "REAL_CONSTANT",
34  "The \"REAL CONSTANT\" statement defines a global "
35  "real constant:\n"
36  "\tREAL CONSTANT <name> = <real-expression>;\n") {
37  itsAttr[0] = Attributes::makeReal("VALUE", "The constant value", 0.0);
38 
40 
41  // Define the standard constants.
43  opal->create(new RealConstant("PI", this, Physics::pi));
44  opal->create(new RealConstant("TWOPI", this, Physics::two_pi));
45  opal->create(new RealConstant("RADDEG", this, Units::rad2deg));
46  opal->create(new RealConstant("DEGRAD", this, Units::deg2rad));
47  opal->create(new RealConstant("E", this, Physics::e));
48 
49  opal->create(new RealConstant("MUMASS", this, Physics::m_mu));
50  opal->create(new RealConstant("EMASS", this, Physics::m_e));
51  opal->create(new RealConstant("PMASS", this, Physics::m_p));
52  opal->create(new RealConstant("HMMASS", this, Physics::m_hm));
53  opal->create(new RealConstant("H2PMASS", this, Physics::m_h2p));
54  opal->create(new RealConstant("DMASS", this, Physics::m_d));
55  opal->create(new RealConstant("ALPHAMASS", this, Physics::m_alpha));
56  opal->create(new RealConstant("CMASS", this, Physics::m_c));
57  opal->create(new RealConstant("XEMASS", this, Physics::m_xe));
58  opal->create(new RealConstant("UMASS", this, Physics::m_u));
59 
60  opal->create(new RealConstant("CLIGHT", this, Physics::c));
61  opal->create(new RealConstant("AMU", this, Physics::amu));
62 
63  opal->create(new RealConstant("OPALVERSION", this, OPAL_VERSION_MAJOR*10
65  opal->create(new RealConstant("RANK", this, Ippl::myNode()));
66 }
67 
68 
69 RealConstant::RealConstant(const std::string &name, RealConstant *parent):
70  ValueDefinition(name, parent)
71 {}
72 
73 
74 RealConstant::RealConstant(const std::string &name, RealConstant *parent,
75  double value):
76  ValueDefinition(name, parent) {
77  Attributes::setReal(itsAttr[0], value);
78  itsAttr[0].setReadOnly(true);
79  builtin = true;
80 }
81 
82 
84 {}
85 
86 
88  return false;
89 }
90 
91 
92 RealConstant *RealConstant::clone(const std::string &name) {
93  return new RealConstant(name, this);
94 }
95 
96 
97 double RealConstant::getReal() const {
98  return Attributes::getReal(itsAttr[0]);
99 }
100 
101 
102 void RealConstant::print(std::ostream &os) const {
103  os << "REAL CONST " << getOpalName() << '=' << itsAttr[0] << ';';
104  os << std::endl;
105 }
106 
107 void RealConstant::printValue(std::ostream &os) const {
108  os << itsAttr[0];
109 }
The global OPAL structure.
Definition: OpalData.h:49
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:240
static OpalData * getInstance()
Definition: OpalData.cpp:196
bool builtin
Built-in flag.
Definition: Object.h:233
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
virtual void print(std::ostream &) const
Print the constant.
virtual void printValue(std::ostream &os) const
Print its value.
The base class for all OPAL objects.
Definition: Object.h:48
void setReal(Attribute &attr, double val)
Set real value.
Definition: Attributes.cpp:271
constexpr double two_pi
The value of .
Definition: Physics.h:33
constexpr double m_h2p
The H2+ rest mass in GeV.
Definition: Physics.h:129
constexpr double m_xe
The xenon-129 (fully-stripped) rest mass in GeV.
Definition: Physics.h:120
static int myNode()
Definition: IpplInfo.cpp:691
constexpr double m_d
The deuteron rest mass in GeV.
Definition: Physics.h:117
constexpr double m_mu
The muon rest mass in GeV.
Definition: Physics.h:114
#define OPAL_VERSION_MAJOR
Definition: config.h.in:6
constexpr double pi
The value of .
Definition: Physics.h:30
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
void create(Object *newObject)
Create new object.
Definition: OpalData.cpp:476
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
constexpr double deg2rad
Definition: Units.h:143
#define OPAL_VERSION_MINOR
Definition: config.h.in:7
constexpr double amu
The atomic mass unit energy equivalent in GeV.
Definition: Physics.h:75
constexpr double m_c
The carbon-12 (fully-stripped) rest mass in GeV.
Definition: Physics.h:105
constexpr double m_u
The uranium-238 (fully-stripped) rest mass in GeV.
Definition: Physics.h:111
RealConstant()
Exemplar constructor.
virtual double getReal() const
Return value.
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:310
constexpr double rad2deg
Definition: Units.h:146
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
The base class for all OPAL value definitions.
constexpr double m_e
The electron rest mass in GeV.
Definition: Physics.h:78
const std::string name
constexpr double m_alpha
The alpha particle rest mass in GeV.
Definition: Physics.h:123
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:252
constexpr double m_p
The proton rest mass in GeV.
Definition: Physics.h:90
virtual RealConstant * clone(const std::string &name)
Make clone.
constexpr double e
The value of .
Definition: Physics.h:39
virtual bool canReplaceBy(Object *object)
Test if object can be replaced.
constexpr double m_hm
The H- rest mass in GeV.
Definition: Physics.h:108
virtual ~RealConstant()