G4GenericMessenger command to set material density

I’m trying to use the G4GenericMessenger to set the density of a material in my application. Ideally I would use it as:

/myCommands/density 0.1 g/cm3

I’d need to understand if it’s feasible (I have some doubts about the way of defining the units) and how to define the unit category in the command definition. I naively tried with:

genericMessenger.AddProperty("density", dens, "Custom density").SetUnitCategory("Density");

but that didn’t work since the Density category is not defined. So I tried with a composite unit category Mass/Volume but that didn’t work either.

So how do I set a density parameter with G4GenericMessenger? Is it maybe not feasible and thus I must handle the setting in a function where I manually handle the given?
Thanks in advance for any help.

I solved my problem very easily by creating a custom unit for density:

new G4UnitDefinition("g/cm3", "g/cm3", "Density", CLHEP::g / CLHEP::cm3);

so that the AddProperty command in my first post works as expected.

Don’t ask me why, but the units category for density is “Volumic Mass” (see <G4>/source/global/management/src/G4UnitsTable.cc). The units categories do not appear to be tabulated anywhere in the documentation.

1 Like

@mkelsey thanks for your reply. Actually I found a web page with a list of unit categories, but I searched for “Density” and didn’t find anything. So I supposed that this category didn’t exist, even judging it very strange given its very basic nature.

So it’s my fault, I overlooked that, but honeslty I could never imagine to search for “Volumic Mass”…

I only found it by scrolling through the G4UnitsTable.cc and looking for “g/cm3” :-/ That Web page doesn’t exactly match what’s in 10.06.p03; I wonder when it was created…

Do you mean that “Volumic Mass” is not present in 10.06.p03? I’m on 10.04.p02 and it works.

“Volumic Mass” is there, just as in the Web page you showed. But in the 10.06 series, it now has three units entries, where the Web page only has the first two:

//Volumic Mass
new G4UnitDefinition( "g/cm3", "g/cm3","Volumic Mass", g/cm3);
new G4UnitDefinition("mg/cm3","mg/cm3","Volumic Mass",mg/cm3);
new G4UnitDefinition("kg/m3", "kg/m3", "Volumic Mass",kg/m3);

If I fire up my own simulation and type /units/list, it shows all three of those entries.

Ok, I got the same units with 10.04.p02. Evidently at some point the kg/m3 has been added. On the funny side I see that there’s also a “Mass/Surface” category: too bad my “Mass/Volume” attempt failed :laughing:

Probably because the French term for density is «masse volumique» … and French speakers were quite influential in Geant4’s history.

1 Like