Message: Re: Any way to use complex derived units (c.f. CLHEP::Evaluator)? SOLVED Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

Ok Re: Any way to use complex derived units (c.f. CLHEP::Evaluator)? SOLVED 

Forum: Geometry
Re: Question Any way to use complex derived units (c.f. CLHEP::Evaluator)? (Michael H. Kelsey)
Re: None Re: Any way to use complex derived units (c.f. CLHEP::Evaluator)? (michel maire)
Re: None Re: Any way to use complex derived units (c.f. CLHEP::Evaluator)? (Michael H. Kelsey)
Date: 24 Mar, 2018
From: Michael H. Kelsey <Michael H. Kelsey>

I have successfully implemented this, following the example code fragment in Evaluator.h itself. Here is my unit handling function:

  G4double GetUnitValue(const G4String& unit) const {
    if (unit.empty()) return 1.;			// Units may be optional

    if (G4UnitDefinition::IsUnitDefined(unit))	// Pre-defined unit name
      return G4UnitDefinition::GetValueOf(unit);

    HepTool::Evaluator eval;		// Set up parser for expressions
    eval.setStdMath();
    eval.setSystemOfUnits(meter, kg, second, ampere, kelvin, mole, candela);

    double scale = eval.evaluate(unit);
    if (eval.status() != HepTool::Evaluator::OK) eval.print_error();

    return scale;
  }

One shortcoming of this is that the "setSystemOfUnits()" defines only a subset of the unit name strings which G4UnitsTable.cc itself defines (and certainly doesn't take into account additional units defined in user code).

I am not sure how expensive the Evaluator object is to create and destroy; it only has a single data member, a void*; everything else seems to be done within function code. This could probably be made more performant by moving the Evaluator to a class data member and setting up just once.

Since Evaluator is distributed with the G4 toolkit (externals/CLHEP/Evaluator/), it might be worth considering adding something like this to G4UnitDefinition::GetValueOf() itself, perhaps with registration of the full set of G4UnitsTable name strings.

  -- Michael Kelsey

 Add Message Add Message
to: "Re: Any way to use complex derived units (c.f. CLHEP::Evaluator)? SOLVED"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]