From 66b97e2caf266c413fa6c835665e4b47e2c0ccd4 Mon Sep 17 00:00:00 2001 From: hrocho Date: Wed, 2 Aug 2017 12:17:34 +0200 Subject: [PATCH] python module --- catima.pyx | 43 ++++++++++++++++++++++++++++++------------ tests/test_storage.cpp | 17 ++++++----------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/catima.pyx b/catima.pyx index b9311d8..32fde78 100644 --- a/catima.pyx +++ b/catima.pyx @@ -242,6 +242,17 @@ cdef class MultiResult: if(isinstance(key,str) and key in self.total): return self.total[key] return None + + def getJSON(self): + res = {} + res["total"] = self.total + res["partial"] = [] + for r in self.results: + #print(r) + #temp = Result() + #temp.setc(r) + res["partial"].append(r) + return res class z_eff_type(IntEnum): none = 0, @@ -287,7 +298,15 @@ cdef class Config: default_config = Config() -def calculate(Projectile projectile, Material material, energy = None, Config config = default_config): +def calculate(Projectile projectile, material, energy = None, config=default_config): + if(not energy is None): + projectile.T(energy) + if(isinstance(material,Material)): + return calculate_material(projectile, material, config = config) + if(isinstance(material,Layers)): + return calculate_layers(projectile, material, config = config) + +def calculate_material(Projectile projectile, Material material, energy = None, Config config = default_config): if(not energy is None): projectile.T(energy) cdef catimac.Result cres = catimac.calculate(projectile.cbase,material.cbase,config.cbase) @@ -295,6 +314,17 @@ def calculate(Projectile projectile, Material material, energy = None, Config co res.setc(cres) return res +def calculate_layers(Projectile projectile, Layers layers, energy = None, Config config = default_config): + cdef catimac.Layers clayers + clayers = catimac.Layers() + clayers = get_clayers(layers) + if(not energy is None): + projectile.T(energy) + cdef catimac.MultiResult cres = catimac.calculate(projectile.cbase, clayers, config.cbase) + res = MultiResult() + res.setc(cres) + return res + cdef catimac.Layers get_clayers(Layers layers): cdef catimac.Layers res cdef catimac.Material m @@ -308,17 +338,6 @@ cdef catimac.Material get_cmaterial(Material material): res = material.cbase return res -def calculate_layers(Projectile projectile, Layers layers, energy = None, Config config = default_config): - cdef catimac.Layers clayers - clayers = catimac.Layers() - clayers = get_clayers(layers) - if(not energy is None): - projectile.T(energy) - cdef catimac.MultiResult cres = catimac.calculate(projectile.cbase, clayers, config.cbase) - res = MultiResult() - res.setc(cres) - return res - def range(Projectile projectile, Material material, energy = None, Config config = default_config): if(isinstance(energy,numpy.ndarray)): res = numpy.empty(energy.size) diff --git a/tests/test_storage.cpp b/tests/test_storage.cpp index bfbcf16..fe0ded7 100644 --- a/tests/test_storage.cpp +++ b/tests/test_storage.cpp @@ -4,17 +4,7 @@ using namespace std; #include "catima/catima.h" #include "catima/storage.h" - -bool rcompare(double a, double b,double eps){ - if(fabs((a-b)/fabs(b))