G4UnitDefinition not working as expected

I am trying to define new units to use in my simulation similar to what is in example B1. I instantiate a new G4UnitDefinition at the begging of RunAction, but when I try to reference the new unit later, I get an error saying “undeclared identifier”. If I use the /units/list command, the new units show up in the list with the proper value. I have UnitsTable and G4SystemOfUnits included in the script. Here is some sample code that throws the error:

const G4double Jupiter_radius = 71492.*km;
new G4UnitDefinition(“R_j”, “Rj”, “Length”, Jupiter_radius);
G4double x = 71492.*km;
G4cout << x/Rj << G4endl;

Which gives the error: “error C2065: ‘Rj’: undeclared identifier”. Any idea what I’m missing here?

1 Like

The G4UnitDefinition defines strings (here, the string literals “R_j” and “Rj”) which can be used by G4BestUnit for printing out length-based quantities, or can be found if you use one of the string literals as units for data input.

In your local code there, you’ve defined the numerical constant named Jupiter_radius, so in your calculation, you should be writing

G4cout << x/Jupiter_radius << G4endl;

If you want to define new units including nice constants, you should do that in a “MySystemOfUnits.hh” file. In our experiment, we do that, where that file itself has #include "G4SystemOfUnits.hh", and then it has a whole bunch of its own global static const G4double inch = 25.4*mm; (yes, really :frowning: ), and similar statements. Then, we have our own “MySystemOfUnits.cc” file where all of the G4UnitDefinition calls get made.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.