www.mooseframework.org
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 88 of file Parser.C.

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