OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 "Utilities/Options.h"
25 #include "Utility/IpplInfo.h"
26 
27 #include <cmath>
28 #include <iostream>
29 
30 
32  ValueDefinition(1, "REAL_CONSTANT",
33  "The \"REAL CONSTANT\" statement defines a global "
34  "real constant:\n"
35  "\tREAL CONSTANT <name> = <real-expression>;\n") {
36  itsAttr[0] = Attributes::makeReal("VALUE", "The constant value", 0.0);
37 
39 
40  // Define the standard constants.
42  opal->create(new RealConstant("PI", this, Physics::pi));
43  opal->create(new RealConstant("TWOPI", this, Physics::two_pi));
44  opal->create(new RealConstant("RADDEG", this, Physics::rad2deg));
45  opal->create(new RealConstant("DEGRAD", this, Physics::deg2rad));
46  opal->create(new RealConstant("E", this, Physics::e));
47 
48  opal->create(new RealConstant("MUMASS", this, Physics::m_mu));
49  opal->create(new RealConstant("EMASS", this, Physics::m_e));
50  opal->create(new RealConstant("PMASS", this, Physics::m_p));
51  opal->create(new RealConstant("HMMASS", this, Physics::m_hm));
52  opal->create(new RealConstant("H2PMASS", this, Physics::m_h2p));
53  opal->create(new RealConstant("DMASS", this, Physics::m_d));
54  opal->create(new RealConstant("ALPHAMASS", this, Physics::m_alpha));
55  opal->create(new RealConstant("CMASS", this, Physics::m_c));
56  opal->create(new RealConstant("XEMASS", this, Physics::m_xe));
57  opal->create(new RealConstant("UMASS", this, Physics::m_u));
58 
59  opal->create(new RealConstant("CLIGHT", this, Physics::c));
60  opal->create(new RealConstant("AMU", this, Physics::amu));
61 
62  opal->create(new RealConstant("OPALVERSION", this, OPAL_VERSION_MAJOR * 10000
63  + OPAL_VERSION_MINOR * 100
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) {
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 }
#define OPAL_VERSION_PATCH
Definition: OPALconfig.h:8
#define OPAL_VERSION_MINOR
Definition: OPALconfig.h:7
#define OPAL_VERSION_MAJOR
Definition: OPALconfig.h:6
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
const std::string name
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:252
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:240
void setReal(Attribute &attr, double val)
Set real value.
Definition: Attributes.cpp:271
constexpr double m_xe
The xenon rest mass in GeV.
Definition: Physics.h:126
constexpr double m_alpha
The alpha particle rest mass in GeV.
Definition: Physics.h:129
constexpr double m_u
The uranium rest mass in GeV.
Definition: Physics.h:117
constexpr double amu
The atomic mass unit energy equivalent in GeV.
Definition: Physics.h:81
constexpr double two_pi
The value of.
Definition: Physics.h:33
constexpr double deg2rad
The conversion factor from degrees to radians.
Definition: Physics.h:48
constexpr double m_p
The proton rest mass in GeV.
Definition: Physics.h:96
constexpr double m_h2p
The H2+ rest mass in GeV.
Definition: Physics.h:135
constexpr double m_e
The electron rest mass in GeV.
Definition: Physics.h:84
constexpr double m_hm
The H- rest mass in GeV.
Definition: Physics.h:114
constexpr double m_mu
The muon rest mass in GeV.
Definition: Physics.h:120
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:51
constexpr double m_c
The carbon rest mass in GeV.
Definition: Physics.h:111
constexpr double m_d
The deuteron rest mass in GeV.
Definition: Physics.h:123
constexpr double pi
The value of.
Definition: Physics.h:30
constexpr double rad2deg
The conversion factor from radians to degrees.
Definition: Physics.h:45
The base class for all OPAL objects.
Definition: Object.h:48
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:281
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
bool builtin
Built-in flag.
Definition: Object.h:233
The global OPAL structure.
Definition: OpalData.h:49
void create(Object *newObject)
Create new object.
Definition: OpalData.cpp:475
static OpalData * getInstance()
Definition: OpalData.cpp:195
The base class for all OPAL value definitions.
Attribute & value()
Return the attribute representing the value of the definition.
virtual void print(std::ostream &) const
Print the constant.
virtual ~RealConstant()
RealConstant()
Exemplar constructor.
virtual double getReal() const
Return value.
virtual void printValue(std::ostream &os) const
Print its value.
virtual bool canReplaceBy(Object *object)
Test if object can be replaced.
virtual RealConstant * clone(const std::string &name)
Make clone.
static int myNode()
Definition: IpplInfo.cpp:691