RealConstant.cpp 3.56 KB
Newer Older
gsell's avatar
gsell committed
1
//
2 3
// Class RealConstant
//   The REAL CONSTANT definition.
gsell's avatar
gsell committed
4
//
5 6
// Copyright (c) 2000 - 2021, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
gsell's avatar
gsell committed
7
//
8 9 10 11 12 13 14 15 16
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
gsell's avatar
gsell committed
17 18
//
#include "ValueDefinitions/RealConstant.h"
19

gsell's avatar
gsell committed
20 21
#include "AbstractObjects/OpalData.h"
#include "Attributes/Attributes.h"
22
#include "OPALconfig.h"
23 24
#include "Physics/Physics.h"
#include "Utilities/Options.h"
kraus's avatar
kraus committed
25 26
#include "Utility/IpplInfo.h"

gsell's avatar
gsell committed
27 28 29 30 31 32 33 34 35 36 37
#include <cmath>
#include <iostream>


RealConstant::RealConstant():
    ValueDefinition(1, "REAL_CONSTANT",
                    "The \"REAL CONSTANT\" statement defines a global "
                    "real constant:\n"
                    "\tREAL CONSTANT <name> = <real-expression>;\n") {
    itsAttr[0] = Attributes::makeReal("VALUE", "The constant value", 0.0);

38 39
    registerOwnership(AttributeHandler::STATEMENT);

gsell's avatar
gsell committed
40
    // Define the standard constants.
41 42 43
    OpalData *opal = OpalData::getInstance();
    opal->create(new RealConstant("PI",     this, Physics::pi));
    opal->create(new RealConstant("TWOPI",  this, Physics::two_pi));
44 45
    opal->create(new RealConstant("RADDEG", this, Physics::rad2deg));
    opal->create(new RealConstant("DEGRAD", this, Physics::deg2rad));
46 47
    opal->create(new RealConstant("E",      this, Physics::e));

48 49 50 51 52 53 54 55 56 57
    opal->create(new RealConstant("MUMASS",    this, Physics::m_mu));
    opal->create(new RealConstant("EMASS",     this, Physics::m_e));
    opal->create(new RealConstant("PMASS",     this, Physics::m_p));
    opal->create(new RealConstant("HMMASS",    this, Physics::m_hm));
    opal->create(new RealConstant("H2PMASS",   this, Physics::m_h2p));
    opal->create(new RealConstant("DMASS",     this, Physics::m_d));
    opal->create(new RealConstant("ALPHAMASS", this, Physics::m_alpha));
    opal->create(new RealConstant("CMASS",     this, Physics::m_c));
    opal->create(new RealConstant("XEMASS",    this, Physics::m_xe));
    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));
adelmann's avatar
adelmann committed
61

62
    opal->create(new RealConstant("OPALVERSION", this, OPAL_VERSION_MAJOR * 10000
kraus's avatar
kraus committed
63 64
                                  + OPAL_VERSION_MINOR * 100
                                  + OPAL_VERSION_PATCH));
65
    opal->create(new RealConstant("RANK", this, Ippl::myNode()));
gsell's avatar
gsell committed
66 67 68
}


69
RealConstant::RealConstant(const std::string &name, RealConstant *parent):
gsell's avatar
gsell committed
70 71 72 73
    ValueDefinition(name, parent)
{}


74
RealConstant::RealConstant(const std::string &name, RealConstant *parent,
gsell's avatar
gsell committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
                           double value):
    ValueDefinition(name, parent) {
    Attributes::setReal(itsAttr[0], value);
    itsAttr[0].setReadOnly(true);
    builtin = true;
}


RealConstant::~RealConstant()
{}


bool RealConstant::canReplaceBy(Object *) {
    return false;
}


92
RealConstant *RealConstant::clone(const std::string &name) {
gsell's avatar
gsell committed
93 94 95 96 97 98 99 100 101 102 103 104
    return new RealConstant(name, this);
}


double RealConstant::getReal() const {
    return Attributes::getReal(itsAttr[0]);
}


void RealConstant::print(std::ostream &os) const {
    os << "REAL CONST " << getOpalName() << '=' << itsAttr[0] << ';';
    os << std::endl;
105 106 107 108
}

void RealConstant::printValue(std::ostream &os) const {
    os << itsAttr[0];
109
}