1
0
Fork 0
mirror of https://github.com/gwm17/catima.git synced 2024-11-26 12:08:52 -05:00

Merge pull request #78 from hrosiak/py

Py
This commit is contained in:
Andrej Prochazka 2020-11-30 12:08:09 +01:00 committed by GitHub
commit 9c17a07324
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 178 additions and 326 deletions

176
docs/pycatima.md Normal file
View File

@ -0,0 +1,176 @@
Usage
=====
Pojectile
---------
Projectile is defined by __pycatima.Projectile__ class. It is initialized using:
**Projectile(A, Z, Q=Z, T=0)**
where __A__ is mass in u units, __Z__ is proton number, __Q__ is charge state
and __T__ is energy in Mev/u units.
```python
import pycatima
p = pycatima.Projectile(238.00032 ,92) # 238U92+ at 0 Mev/u
p = pycatima.Projectile(238.00702 ,92,90,1000) # 238U90+ at 1000 Mev/u
# following methods are defined:
mass = p.A() # get mass of the nucleus us u units
z = p.Z() # get proton number
q = p.Q() # get charge state
energy = p.T() # get energy
p.T(1000) # set energy
```
Material
--------
Material is defined by __pycatima.Material__ class. The recommended way of
initialization is usign the following init signature:
**Material(elements, density, thickness, i_potential, mass)**
* elements - list of elements, where element is defined as list of [A, Z, STN]
A is atomic mass of the element, if 0 natural abundance atomic mass is taken,
Z is the proton number of the element, STN is the stoichiometric if >=1.0 or
weight fraction if < 1.0
* density - optional, defaults to 0
* thickness - optional, if not defined 0
* i_potential - optional, if <=0 it will be calulated using Bragg rule from elemental ionization potentials
* mass - optional, if <=0 it will be calculated from elements masses and STN number.
__Material__ class has following methods:
* **add_element(a, z, stn)** - adds another element to the material, see initialization comments for details about, a,z, stn
* **ncomponents()** - returns number of elements in the material
* **density()** - returns density
* **density(value)** - set density
* **thickness()** - get thickness in g/cm^2
* **thickness(value)** - set thickness in g/cm^2
* **thickness_cm(value)** - set thickness in cm
* **I()** - get mean ionization potential
* **I(value)** - set mean ionization potential
### Default Materials
The material with predefined density and atomic weights can be obtained for elemental targets and some compounds using:
**pycatima.get_material(id)**
id is integer which identifies material, For elements use 1-99 for elemental targets and >200 for compounds. See available pre-defined compounds in __predefined material__ section in manual./
### Example
```python
import pycatima
#H2O with natural atomic masses and Ipot=78eV
h2o = pycatima.Material([[0,1,2],[0,8,1]],density=1.0, i_potential=78)
h2o.thickness_cm(1.0) # 1cm of water
# C target
c_mat = pycatima.get_material(6)
c_mat.thickness(0.1) # set to 0.1g/cm2
```
Layers
------
Layers are sequential layers of __Material__ which __Projectile__ passes.
The __pycatima.Layers__ class is defined using following signature:
**Layers()**
This creates empty layers which needs to be filled by __Material__ class.
__Layers__ clas has following methods:
* **add(material)** - add material to the layers, material must be __Material__ class
* **add_layers(other)** - add all material from __Layers__ class __other__
* **num()** - returns number of layers
* **__get_item__(i)** returns i-th Material class
* **__get(i)** returns i-th Material class
### Example
```python
import pycatima
layers = pycatima.Layers()
# define some materials
graphite = pycatima.get_material(6)
graphite.thickness(0.2)
p10 = pycatima.get_material(pycatima.material.P10)
p10.thickness_cm(2.0)
air = pycatima.get_material(pycatima.material.Air)
air.thickness_cm(2.0)
# now add materials to layers
layers.add(graphite)
layers.add(air)
graphite.thickness(0.1) # change thickness for next layer
layers.add(graphite)
layers.add(p10)
```
Calculation
-----------
Calculations are done using mainly via functions:
* **calculate(Projectile, Material)**
* **calculate_layers(Projectile, Layers)**
The __calculate__ function returns __Result__ class and __calculate_layers__ returns __MultiResult_class__
### Example
```python
import pycatima
water = catima.get_material(catima.material.Water)
water.thickness(1.0)
p = catima.Projectile(1,1)
p.T(1000) # set projectile energy to 1000MeV/u
res = catima.calculate(p,water) # now res contains results
d = res.get_dict() # get results as dictionary
```
```python
p = catima.Projectile(12,6)
water = catima.get_material(catima.material.Water)
water.thickness(10.0)
graphite = catima.get_material(6)
graphite.thickness(1.0)
graphite.density(2.0)
mat = catima.Layers()
mat.add(water)
mat.add(graphite)
# now calculate results for projectile at 1000MeV/u
res = catima.calculate_layers(p(1000),mat)
```
Results
-------
__Results__ class stores results for 1 layer of __Material__. It has following variables:
* Ein - Energy at entrance of the material in MeV/u
* Eloss - Energy loss in material in MeV
* Eout - Energy at the end of material in MeV/i
* dEdxi - Stopping power at entrance
* dEdxo - Stopping power at exit
* range - range in the material in g/cm^2
* sigma_E - Energy straggling in MeV/u
* sigma_a - Angular straggling in MeV/u
* sigma_r - range straggling in g/cm^2
* sigma_x - position straggling in cm
* sp - non-reaction probability
* tof - time of flight through the material
__MultiResults__ class stores the results for multiple layers. It consists of following variables:
* total_result - __Result__ class with total results for projectile passing all layers.
* results - list of __Result__ classes, one for each layer in __Layers__
Config
------
TODO

View File

@ -1,17 +0,0 @@
Usage
=====
Pojectile
---------
Material
--------
Layers
------
Calculation
-----------
Config
------

View File

@ -100,6 +100,7 @@ py::dict get_result_dict(const Result& r){
d["sigma_E"] = r.sigma_E; d["sigma_E"] = r.sigma_E;
d["sigma_r"] = r.sigma_r; d["sigma_r"] = r.sigma_r;
d["sigma_a"] = r.sigma_a; d["sigma_a"] = r.sigma_a;
d["sigma_x"] = r.sigma_x;
d["tof"] = r.tof; d["tof"] = r.tof;
d["sp"] = r.sp; d["sp"] = r.sp;
return d; return d;
@ -171,6 +172,7 @@ PYBIND11_MODULE(pycatima,m){
.def_readwrite("sigma_E", &Result::sigma_E) .def_readwrite("sigma_E", &Result::sigma_E)
.def_readwrite("sigma_a", &Result::sigma_a) .def_readwrite("sigma_a", &Result::sigma_a)
.def_readwrite("sigma_r", &Result::sigma_r) .def_readwrite("sigma_r", &Result::sigma_r)
.def_readwrite("sigma_x", &Result::sigma_x)
.def_readwrite("tof", &Result::tof) .def_readwrite("tof", &Result::tof)
.def_readwrite("sp", &Result::sp) .def_readwrite("sp", &Result::sp)
.def("get_dict",&get_result_dict); .def("get_dict",&get_result_dict);

View File

@ -1,309 +0,0 @@
import sys
sys.path.insert(0,"../build")
import unittest
import pycatima as catima
import math
class TestStructures(unittest.TestCase):
def test_Projectile(self):
print(catima.storage_info())
p = catima.Projectile(238,92)
self.assertEqual(p.A(),238)
self.assertEqual(p.Z(),92)
self.assertEqual(p.Q(),92)
p = catima.Projectile(238,92,90)
self.assertEqual(p.A(),238)
self.assertEqual(p.Z(),92)
self.assertEqual(p.Q(),90)
p.T(1000)
self.assertEqual(p.T(),1000)
p(500)
self.assertEqual(p.T(),500)
p = catima.Projectile(238,92,90, T=100)
self.assertEqual(p.T(),100)
def test_Material(self):
mat = catima.Material()
mat.add_element(12,6,1)
self.assertEqual(mat.ncomponents(),1)
mat.add_element(1,1,2)
self.assertEqual(mat.ncomponents(),2)
mat2 = catima.Material([[12.01,6,1]])
self.assertEqual(mat2.ncomponents(),1)
self.assertEqual(mat2.molar_mass(),12.01)
mat3 = catima.Material([[12,6,1]])
self.assertEqual(mat3.ncomponents(),1)
self.assertEqual(mat3.molar_mass(),12)
Water = catima.Material([[1,1,2],[16,8,1]])
self.assertEqual(Water.molar_mass(),18)
mat2 = catima.Material([[0,6,1]])
self.assertEqual(mat2.ncomponents(),1)
self.assertAlmostEqual(mat2.molar_mass(),12,1)
mat5 = catima.Material([[0,6,1]],density=1.9, thickness=0.5)
self.assertEqual(mat5.ncomponents(),1)
self.assertEqual(mat5.thickness(),0.5)
self.assertEqual(mat5.density(),1.9)
mat6 = catima.Material([[0,6,1]],density=1.9, thickness=0.5,i_potential=80.0)
self.assertEqual(mat6.ncomponents(),1)
self.assertEqual(mat6.thickness(),0.5)
self.assertEqual(mat6.density(),1.9)
self.assertEqual(mat6.I(),80.0)
# copy
mat3.density(1.8)
matc = mat3.copy()
self.assertEqual(matc.ncomponents(),1)
self.assertEqual(matc.molar_mass(),12)
self.assertEqual(matc.density(),1.8)
mat3.density(2.0)
self.assertEqual(matc.density(),1.8)
self.assertEqual(mat3.density(),2.0)
def test_default_material(self):
m1 = catima.get_material(6);
self.assertAlmostEqual(m1.molar_mass(),12,1)
self.assertEqual(m1.ncomponents(),1)
self.assertAlmostEqual(m1.density(),2.0,1)
m2 = catima.get_material(catima.material.Water)
self.assertEqual(m2.ncomponents(),2)
self.assertAlmostEqual(m2.molar_mass(),18,1)
self.assertAlmostEqual(m2.density(),1.0,1)
m3 = catima.get_material(3001)
self.assertEqual(m3.ncomponents(),0)
self.assertAlmostEqual(m3.molar_mass(),0,1)
self.assertAlmostEqual(m3.density(),0.0,1)
def test_layers(self):
graphite = catima.get_material(6)
graphite.thickness(0.5)
p10 = catima.get_material(catima.material.P10)
p10.thickness(0.01)
n2 = catima.get_material(7)
n2.thickness(0.02)
mat= catima.Layers()
self.assertEqual(mat.num(),0)
mat.add(graphite)
self.assertEqual(mat.num(),1)
self.assertAlmostEqual(mat[0].molar_mass(),12,1)
self.assertAlmostEqual(mat[0].thickness(),0.5,1)
self.assertAlmostEqual(mat[0].density(),2.0,1)
mat.add(p10)
self.assertEqual(mat.num(),2)
graphite.thickness(1.0)
graphite.density(1.8)
mat.add(graphite)
self.assertEqual(mat.num(),3)
self.assertAlmostEqual(mat[2].molar_mass(),12,1)
self.assertAlmostEqual(mat[0].thickness(),0.5,1)
self.assertAlmostEqual(mat[0].density(),2.0,1)
self.assertAlmostEqual(mat[2].thickness(),1.0,1)
self.assertAlmostEqual(mat[2].density(),1.8,1)
mat[2].thickness(1.2)
mat[2].density(1.9)
self.assertAlmostEqual(mat.materials[2].thickness(),1.2,1)
self.assertAlmostEqual(mat.materials[2].density(),1.9,1)
#self.assertAlmostEqual(mat.materials[0].thickness(),0.5,1)
#self.assertAlmostEqual(mat.materials[0].density(),2.0,1)
self.assertEqual(mat[3],None)
self.assertEqual(mat["a"],None)
mat2 = catima.Layers()
mat2.add(n2)
self.assertEqual(mat2.num(),1)
mats = mat2 + mat
self.assertEqual(mats.num(),4)
self.assertAlmostEqual(mats[0].molar_mass(),14,1)
self.assertEqual(mats[0].thickness(),0.02)
self.assertAlmostEqual(mats[1].molar_mass(),12,1)
self.assertAlmostEqual(mats[3].molar_mass(),12,1)
n2.thickness(0.5)
mats = mats + n2
self.assertEqual(mats.num(),5)
self.assertAlmostEqual(mats[0].molar_mass(),14,1)
self.assertEqual(mats[0].thickness(),0.02)
self.assertAlmostEqual(mats[4].molar_mass(),14,1)
self.assertEqual(mats[4].thickness(),0.5)
def test_material_calculation(self):
Water = catima.get_material(catima.material.Water)
p = catima.Projectile(1,1)
p(1000)
res = catima.calculate(p,Water)
res2 = catima.dedx(p,Water)
self.assertAlmostEqual(res.dEdxi,2.23,1)
self.assertAlmostEqual(res["dEdxi"],2.23,1)
self.assertAlmostEqual(res.dEdxi,res2,3)
res = catima.calculate(p(500),Water)
res2 = catima.dedx(p,Water)
self.assertAlmostEqual(res.dEdxi,2.76,1)
self.assertAlmostEqual(res.dEdxi,res2,3)
res = catima.calculate(p(9),Water)
res2 = catima.dedx(p,Water)
self.assertAlmostEqual(res.dEdxi,51.17,1)
self.assertAlmostEqual(res.dEdxi,res2,3)
res = catima.calculate(p(9),Water)
res = catima.calculate(p(9),Water)
self.assertAlmostEqual(res.dEdxi,51.17,1)
p(900000)
res = catima.calculate(p,Water)
res2 = catima.dedx_from_range(p,Water)
self.assertAlmostEqual(res.dEdxi,res2,3)
def test_config(self):
Water = catima.get_material(catima.material.Water)
Water.density(1.0)
Water.thickness(1.0)
p = catima.Projectile(1,1)
conf = catima.Config()
conf.dedx_straggling = catima.omega_type.bohr
conf2 = catima.Config()
conf2.dedx_straggling = catima.omega_type.atima
p(1000)
res = catima.calculate(p,Water,config=conf)
res2 = catima.calculate(p,Water,config=conf2)
self.assertAlmostEqual(res.dEdxi,res2.dEdxi,delta=1e-6)
self.assertNotAlmostEqual(res.sigma_E,res2.sigma_E,delta=1e-4)
self.assertNotAlmostEqual(res.sigma_r,res2.sigma_r,delta=1e-4)
def test_eout(self):
graphite = catima.get_material(6)
graphite.thickness(0.5)
p = catima.Projectile(12,6)
res = catima.calculate(p(1000),graphite)
res2 = catima.energy_out(p(1000),graphite)
self.assertAlmostEqual(res.Eout,997.077,1)
self.assertAlmostEqual(res["Eout"],997.077,1)
self.assertAlmostEqual(res.Eout,res2,3)
def test_eout_list(self):
graphite = catima.get_material(6)
graphite.thickness(0.5)
p = catima.Projectile(12,6)
energies = [100,500,1000]
res = catima.calculate(p(1000),graphite)
self.assertAlmostEqual(res.Eout,997.077,1)
res2 = catima.energy_out(p,graphite,energy=energies)
self.assertEqual(len(res2),len(energies))
self.assertAlmostEqual(res2[2], 997.077,1)
self.assertAlmostEqual(res2[0], catima.calculate(p(energies[0]),graphite).Eout ,1)
self.assertAlmostEqual(res2[1], catima.calculate(p(energies[1]),graphite).Eout ,1)
def test_dedx_from_range_list(self):
graphite = catima.get_material(6)
graphite.thickness(0.5)
p = catima.Projectile(12,6)
energies = [100,500,1000]
res2 = catima.dedx_from_range(p,graphite,energy=energies)
self.assertEqual(len(res2),len(energies))
self.assertEqual(len(res2),3)
for i,e in enumerate(energies):
r = catima.dedx_from_range(p, graphite, energy=e)
print(r)
print(res2)
self.assertAlmostEqual(res2[i], r, 0.1)
def test_layer_calculation(self):
p = catima.Projectile(12,6)
Water = catima.get_material(catima.material.Water)
Water.thickness(10.0)
graphite = catima.get_material(6)
graphite.thickness(1.0)
graphite.density(2.0)
mat = catima.Layers()
mat.add(Water)
mat.add(graphite)
res = catima.calculate_layers(p(1000),mat)
self.assertEqual(len(res.results),2)
self.assertAlmostEqual(res.total_result.Eout,926.3,1)
self.assertAlmostEqual(res.total_result.sigma_a,0.00269,1)
self.assertAlmostEqual(res["Eout"],926.3,1)
self.assertAlmostEqual(res["sigma_a"],0.00269,4)
self.assertAlmostEqual(res["tof"],0.402,2)
self.assertAlmostEqual(res["Eloss"],884,0)
self.assertAlmostEqual(res[0]["Eout"],932.24,0)
self.assertAlmostEqual(res[1]["Eout"],926.3,0)
self.assertAlmostEqual(res[0]["sigma_a"],0.00258,4)
self.assertAlmostEqual(res[1]["sigma_a"],0.000774,4)
self.assertAlmostEqual(res[0]["range"],107.1,0)
self.assertAlmostEqual(res[1]["range"],111.3,0)
def test_energy_table(self):
table = catima.get_energy_table()
self.assertEqual(table[0],math.log(catima.logEmin))
#self.assertEqual(table[10],catima.energy_table(10))
self.assertEqual(len(table),catima.max_datapoints)
def test_storage(self):
p = catima.Projectile(12,6)
Water = catima.get_material(catima.material.Water)
Water.thickness(10.0)
graphite = catima.get_material(6)
graphite.thickness(1.0)
data = catima.get_data(p, Water)
print(data[0])
et = catima.get_energy_table()
self.assertEqual(len(data),3)
self.assertEqual(len(data[0]),len(et))
self.assertEqual(len(data[0]),catima.max_datapoints)
res = catima.calculate(p(et[10]),Water)
self.assertAlmostEqual(res.range,data[0][10],6)
self.assertAlmostEqual(catima.projectile_range(p,Water),data[0][10],6)
#self.assertAlmostEqual(catima.domega2de(p,Water),data[1][10],6)
res = catima.calculate(p(et[100]),Water)
self.assertAlmostEqual(res.range,data[0][100],6)
self.assertAlmostEqual(catima.projectile_range(p,Water),data[0][100],6)
#self.assertAlmostEqual(catima.domega2de(p,Water),data[1][100],6)
res = catima.calculate(p(et[200]),Water)
self.assertAlmostEqual(res.range,data[0][200],6)
self.assertAlmostEqual(catima.projectile_range(p,Water),data[0][200],6)
#self.assertAlmostEqual(catima.domega2de(p,Water),data[1][200],6)
res = catima.calculate(p(et[401]),Water)
self.assertAlmostEqual(res.range,data[0][401],6)
self.assertAlmostEqual(catima.projectile_range(p,Water),data[0][401],6)
#self.assertAlmostEqual(catima.domega2de(p,Water),data[1][401],6)
def test_python_storage_access(self):
p = catima.Projectile(12,6)
Water = catima.get_material(catima.material.Water)
Water.thickness(10.0)
graphite = catima.get_material(6)
graphite.thickness(1.0)
data = catima.get_data(p, Water)
self.assertEqual(catima.max_storage_data,100) # assuming 50, this has to be changed manually
r = catima.storage_info()
#self.assertAlmostEqual(catima.da2de(p,Water,et[100]),data[2][100],6)
#self.assertAlmostEqual(catima.da2de(p,Water,et[400]),data[2][400],6)
if __name__ == "__main__":
unittest.main()