Geant 4 程序中,输入数据时必须指定其单位,这是所有Geant 4 代码都要遵循的规范,这使得代码与用户选择的单位系统无关。如果没有指定,这些数据将被认为使用G4系统内部的隐含单位,这将导致代码的移植性变差。
声明:除部分代码外,本文基本翻译自G4官方文件 Book for Application Developers. pdf
一、基本单位
Geant 4内核在内部使用一个统一的单位集合,其基于HepSystemOfUnits:
millimeter (mm)
nanosecond (ns)
Mega electron Volt (MeV)
positron charge (eplus)
degree Kelvin (kelvin)
the amount of substance (mole)
luminous intensity (candela)
radian (radian)
steradian (steradian)
所有其他的单位都是有这些基本单位导出的。例如:
millimeter = mm = 1;
meter = m = 1000*mm;
...
m3 = m*m*m;
...
基本单位定义:source/global/management/include/SystemOfUnits.h,这个文件是CLHEP的一部分。此外,用户可以自由的改变内核所使用的单位系统。
二、输入数据
1. 如果数据来自于一个数组或外部文件,强烈推荐在读取数据的时候设置他们的单位。例如:
for (int j=0, j<jmax, j++) CrossSection[j] *= millibarn;
...
my calculations
...
2. 交互式命令:一些用户接口的内建命令也要求指定单位。例如:
/gun/energy 15.2 keV
/gun/position 3 2 -7 meter
三、输出数据
1. 以G4内部隐含单位输出数据:
G4out << stepSize << "mm";
2. 以用户规定的单位输出数据:
G4out << stepSize/cm << "cm";
3. 以最合适的单位输出数据:需要调用单位列表所在的头文件
#include "G4UnitsTable.hh"
G4out << G4BestUnit(stepSize, "Length");// Length is the category.
四、引入新的单位
1. 调用头文件,定义新的单位:
#include "SystemOfUnits.h"
static const G4double inch = 2.54*cm;
注:因为为将其加入到单位列表,所以需要定义静态全局常量。
2. 用户可以实例化一个 G4UnitDefinition 类的对象,这些对象在构造时由全局 G4UnitsTable 拥有,并且不能由用户删除。
#include "G4UnitsTable.hh"
#include "SystemOfUnits.h"
new G4UnitDefinition ( name, symbol, category, value );
例如:
#include "G4UnitsTable.hh"
#include "SystemOfUnits.h"
// add new units for dose
//
const G4double milligray = 1.e-3*gray;
new G4UnitDefinition("milligray", "milliGy" , "Dose", milligray);
// let G4 choose the most appropriate units
//
G4cout << G4BestUnit(dose,"Dose");
也可以定义复合单位,如速度:
new G4UnitDefinition ( "km/hour" , "km/h", "Speed", km/(3600*s) );
new G4UnitDefinition ( "meter/ns", "m/ns", "Speed", m/ns );
在 G4UnitsTable 中,"Speed" 类的单位缺省不存在,但是它将自动被建立。
G4UnitDefinition 类所在目录:source/global/management。
五、输出单位列表
用户可以使用静态函数 G4UnitDefinition::PrintUnitsTable() 输出单位列表,或者使用交互式命令, /units/list 。