1
0
Fork 0
mirror of https://github.com/gwm17/catima.git synced 2024-11-29 21:48:51 -05:00

Merge pull request #7 from hrosiak/python

added few python functions
This commit is contained in:
Andrej Prochazka 2017-10-10 18:48:52 +02:00 committed by GitHub
commit 649377b4cf
3 changed files with 87 additions and 3 deletions

View File

@ -338,7 +338,7 @@ cdef catimac.Material get_cmaterial(Material material):
res = material.cbase res = material.cbase
return res return res
def range(Projectile projectile, Material material, energy = None, Config config = default_config): def projectile_range(Projectile projectile, Material material, energy = None, Config config = default_config):
if(isinstance(energy,numpy.ndarray)): if(isinstance(energy,numpy.ndarray)):
res = numpy.empty(energy.size) res = numpy.empty(energy.size)
for i,e in enumerate(energy): for i,e in enumerate(energy):
@ -403,3 +403,20 @@ def z_effective(Projectile p, Target t, Config c = default_config):
def z_eff_Pierce_Blann(double z, double beta): def z_eff_Pierce_Blann(double z, double beta):
return catimac.z_eff_Pierce_Blann(z,beta) return catimac.z_eff_Pierce_Blann(z,beta)
def get_data(Projectile projectile, Material material, Config config = default_config):
data = catimac.get_data(projectile.cbase, material.cbase, config.cbase)
return [data.range,data.range_straggling,data.angular_variance]
max_datapoints = catimac.max_datapoints
def energy_table(unsigned int i):
if(i<catimac.energy_table.num):
return catimac.energy_table(i)
else:
return -1.0
def get_energy_table():
r = [catimac.energy_table(x) for x in range(catimac.energy_table.num)]
return r

View File

@ -86,9 +86,25 @@ cdef extern from "catima/calculations.h" namespace "catima":
cdef double z_eff_Pierce_Blann(double z, double beta); cdef double z_eff_Pierce_Blann(double z, double beta);
cdef extern from "catima/constants.h" namespace "catima": cdef extern from "catima/constants.h" namespace "catima":
cdef int max_datapoints; int max_datapoints "catima::max_datapoints"
int logEmin "catima::logEmin"
int logEmax "catima::logEmax"
cdef extern from "catima/storage.h" namespace "catima": cdef extern from "catima/storage.h" namespace "catima":
cdef cppclass Interpolator: cdef cppclass Interpolator:
Interpolator(const double *x, const double *y, int num) except +
double eval(double) double eval(double)
double derivative(double) double derivative(double)
cdef cppclass DataPoint:
vector[double] range
vector[double] range_straggling
vector[double] angular_variance
cdef cppclass EnergyTableType "catima::EnergyTable[max_datapoints]":
size_t num;
double operator()(int i)
cdef EnergyTableType energy_table;
cdef DataPoint& get_data(const Projectile &p, const Material &t, Config c);

View File

@ -2,6 +2,7 @@ import sys
sys.path.insert(0,"../build") sys.path.insert(0,"../build")
import unittest import unittest
import catima import catima
import math
class TestStructures(unittest.TestCase): class TestStructures(unittest.TestCase):
@ -153,6 +154,11 @@ class TestStructures(unittest.TestCase):
res = catima.calculate(p(9),water) res = catima.calculate(p(9),water)
self.assertAlmostEqual(res.dEdxi,51.17,1) 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_eout(self): def test_eout(self):
graphite = catima.get_material(6) graphite = catima.get_material(6)
graphite.thickness(0.5) graphite.thickness(0.5)
@ -189,5 +195,50 @@ class TestStructures(unittest.TestCase):
self.assertAlmostEqual(res[0]["range"],107.1,0) self.assertAlmostEqual(res[0]["range"],107.1,0)
self.assertAlmostEqual(res[1]["range"],110.7,0) self.assertAlmostEqual(res[1]["range"],110.7,0)
def test_energy_table(self):
table = catima.get_energy_table()
self.assertEqual(table[0],catima.energy_table(0))
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[1])
et = catima.get_energy_table()
self.assertEqual(len(data),3)
self.assertEqual(len(data[0]),len(et))
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)
#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__": if __name__ == "__main__":
unittest.main() unittest.main()