https://mooseframework.inl.gov
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 41 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 89 of file Parser.C.

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

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