https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | List of all members
UnitsConversionEvaler Class Reference

#include <Parser.h>

Inheritance diagram for UnitsConversionEvaler:
[legend]

Public Member Functions

virtual std::string eval (hit::Field *n, const std::list< std::string > &args, hit::BraceExpander &exp)
 

Detailed Description

Definition at line 29 of file Parser.h.

Member Function Documentation

◆ eval()

std::string UnitsConversionEvaler::eval ( hit::Field *  n,
const std::list< std::string > &  args,
hit::BraceExpander &  exp 
)
virtual

Definition at line 83 of file Parser.C.

86{
87 std::vector<std::string> argv;
88 argv.insert(argv.begin(), args.begin(), args.end());
89
90 // no conversion, the expression currently only documents the units and passes through the value
91 if (argv.size() == 2)
92 {
93 n->setVal(n->val(), hit::Field::Kind::Float);
94 return argv[0];
95 }
96
97 // conversion
98 if (argv.size() != 4 || (argv.size() >= 3 && argv[2] != "->"))
99 {
100 exp.errors.emplace_back("units error: Expected 4 arguments ${units number from_unit -> "
101 "to_unit} or 2 arguments ${units number unit} in '" +
102 n->fullpath() + "'",
103 n);
104 return n->val();
105 }
106
107 // get and check units
108 auto from_unit = MooseUnits(argv[1]);
109 auto to_unit = MooseUnits(argv[3]);
110 if (!from_unit.conformsTo(to_unit))
111 {
112 std::ostringstream err;
113 err << "units error: " << argv[1] << " (" << from_unit << ") does not convert to " << argv[3]
114 << " (" << to_unit << ") in '" << n->fullpath() << "'";
115 exp.errors.emplace_back(err.str(), n);
116 return n->val();
117 }
118
119 // parse number
120 Real num = MooseUtils::convert<Real>(argv[0]);
121
122 // convert units
123 std::stringstream ss;
124 ss << std::setprecision(17) << to_unit.convert(num, from_unit);
125
126#ifndef NDEBUG
127 mooseInfoRepeated(n->filename() + ':' + Moose::stringify(n->line()) + ':' +
128 Moose::stringify(n->column()) + ": Unit conversion ",
129 num,
130 ' ',
131 argv[1],
132 " -> ",
133 ss.str(),
134 ' ',
135 argv[3]);
136#endif
137
138 // change kind only (not val)
139 n->setVal(n->val(), hit::Field::Kind::Float);
140 return ss.str();
141}
void mooseInfoRepeated(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition MooseError.h:409
Physical unit management class with runtime unit string parsing, unit checking, unit conversion,...
Definition Units.h:33
auto exp(const T &)
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64
OStreamProxy err(std::cerr)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

The documentation for this class was generated from the following files: