1 cimport camns_interface
2 from camns_interface cimport *
6 # some SO for cython & numpy
7 # http://stackoverflow.com/questions/3046305/simple-wrapping-of-c-code-with-cython
8 # http://stackoverflow.com/questions/4495420/passing-numpy-arrays-to-c-code-wrapped-with-cython
9 # http://stackoverflow.com/questions/8366095/have-pointer-to-data-need-numpy-array-in-fortran-order-want-to-use-cython
12 # """Class for interfacing to the AMNS library"""
15 # """AMNS handle (opaque)"""
17 def __init__(self, version_string=None, version_number=None, version_backend=None, version_user=None):
18 """Use to initialize the Amns class"""
20 self._setup(version_string=version_string, version_number=version_number, version_backend=version_backend, version_user=version_user)
22 cdef void* handle(self):
25 def _setup(self, version_string=None, version_number=None, version_backend=None, version_user=None):
26 """Perform the actual call to the C interface to the original primary Fortran SETUP routine"""
30 if (version_string == None) & ( version_number == None) & (version_backend == None) & (version_user == None):
34 if version_string != None:
version.string = version_string
35 if version_number != None:
version.number = version_number
36 if version_backend != None:
version.backend = version_backend
37 if version_user != None:
version.user = version_user
41 raise AmnsException(error_status.string)
50 raise AmnsException(error_status.string)
53 def
query(self, queryString):
54 """Provide the interface to ITM_AMNS_QUERY"""
61 raise AmnsException(error_status.string)
66 """Query for version of the AMNS data"""
67 cdef camns_interface.amns_c_answer_type
answer
68 answer =
self.lquery(
"version")
71 def set(
self, setString):
72 """Provide the interface to ITM_AMNS_SET"""
73 cdef camns_interface.amns_c_set_type set
75 set.string = setString
76 camns_interface.ITM_AMNS_CC_SET(
self._handle, &set, &
error_status);
81 """Provide the interface to ITM_AMNS_FINISH"""
83 camns_interface.ITM_AMNS_CC_FINISH(&
self._handle, &
error_status)
88 """Provide the interface to the Table class used for tables"""
93 # """Class for interfacing to Tables in the the AMNS library"""
98 def __init__(
self, reactionString, Reactants reactants, Amns parentAmns,
isotope_resolved=None):
99 """Create a new table"""
103 self._parentAmns = parentAmns
104 reaction.string = reactionString
108 reactants.handle(), &self._handle,
127 """Provide the interface to ITM_AMNS_QUERY_TABLE"""
133 if error_status.
flag:
134 raise AmnsException(error_status.
string)
135 return answer.
string, answer.number
137 def set(self, setString):
138 """Provide the interface to ITM_AMNS_SET_TABLE"""
141 set.
string = setString
143 if error_status.
flag:
144 raise AmnsException(error_status.
string)
147 """Provide the interface to ITM_AMNS_FINISH_TABLE"""
150 if error_status.
flag:
151 raise AmnsException(error_status.
string)
154 def no_of_reactants(self):
155 """Query for no_of_reactants associated with the AMNS table"""
157 answer = self.lquery("no_of_reactants")
162 """Query for
ndim associated with the AMNS table"""
164 answer = self.lquery("
ndim")
169 """Query for source associated with the AMNS table"""
171 answer = self.lquery("source")
176 """Query for filled status associated with the AMNS table"""
178 answer = self.lquery("filled")
182 def reaction_type(self):
183 """Query for reaction_type associated with the AMNS table"""
185 answer = self.lquery("reaction_type")
190 """Query for reactants associated with the AMNS table"""
192 answer = self.lquery("reactants")
197 """Query for version associated with the AMNS table"""
199 answer = self.lquery("version")
200 return answer.
string, answer.number
203 def state_label(self):
204 """Query for state_label associated with the AMNS table"""
206 answer = self.lquery("state_label")
210 def result_unit(self):
211 """Query for result_unit associated with the AMNS table"""
213 answer = self.lquery("result_unit")
217 def result_label(self):
218 """Query for result_label associated with the AMNS table"""
220 answer = self.lquery("result_label")
224 def interp_fun(self):
225 """Query for interp_fun associated with the AMNS table"""
227 answer = self.lquery("interp_fun")
230 def data(self,
p1,
p2 = None,
p3 = None):
231 """Return the AMNS data based on the arguments"""
236 raise AmnsException("Number of parameters does not match table dimensions (
ndim="
237 + str(self.
ndim) + ")")
240 if
p2 is not None and
p2.shape != refShape:
241 raise AmnsException("Shape of parameter 2 inconsistent to shape of parameter 1")
242 if
p3 is not None and
p3.shape != refShape:
243 raise AmnsException("Shape of parameter 3 inconsistent to shape of parameter 1")
245 if
len(refShape) == 1:
246 res = self._data_1d(p1,
p2,
p3)
247 elif
len(refShape) == 2:
248 res = self._data_2d(p1,p2,
p3)
249 elif
len(refShape) == 3:
250 res = self._data_3d(p1,p2,
p3)
252 raise AmnsException("Unsupported rank of input arguments: %s" % (
len(refShape),))
256 def _data_1d(self, np.ndarray[np.double_t,
ndim=1] p1,
257 np.ndarray[np.double_t,
ndim=1] p2 = None,
258 np.ndarray[np.double_t,
ndim=1]
p3 = None):
260 cdef np.ndarray[np.double_t,
ndim=1] res
263 if p2 is not None:
nargs += 1
266 res = np.empty_like(p1, order='F')
271 <
double*> p1.data, &error_status);
276 <
double*> p2.data, &error_status);
282 <
double*>
p3.data, &error_status);
285 raise AmnsException(error_status.
string)
288 def _data_2d(self, np.ndarray[np.double_t,
ndim=2] p1,
289 np.ndarray[np.double_t,
ndim=2] p2 = None,
290 np.ndarray[np.double_t,
ndim=2]
p3 = None):
293 cdef np.ndarray[np.double_t,
ndim=2] res
296 if p2 is not None:
nargs += 1
299 res = np.empty_like(p1, order='F')
304 <
double*> p1.data, &error_status);
309 <
double*> p2.data, &error_status);
315 <
double*>
p3.data, &error_status);
317 if error_status.
flag:
318 raise AmnsException(error_status.
string)
321 def _data_3d(self, np.ndarray[np.double_t,
ndim=3] p1,
322 np.ndarray[np.double_t,
ndim=3] p2 = None,
323 np.ndarray[np.double_t,
ndim=3]
p3 = None):
325 cdef np.ndarray[np.double_t,
ndim=3] res
328 if p2 is not None:
nargs += 1
331 res = np.empty_like(p1, order='F')
334 camns_interface.
ITM_AMNS_CC_RX_3_A(self._handle, p1.shape[0], p1.shape[1], p1.shape[2],
336 <
double*> p1.data, &error_status);
338 camns_interface.
ITM_AMNS_CC_RX_3_B(self._handle, p1.shape[0], p1.shape[1], p1.shape[2],
341 <
double*> p2.data, &error_status);
343 camns_interface.
ITM_AMNS_CC_RX_3_C(self._handle, p1.shape[0], p1.shape[1], p1.shape[2],
347 <
double*>
p3.data, &error_status);
349 if error_status.
flag:
350 raise AmnsException(error_status.
string)
354 cdef class Reactants:
355 # """Class providing the machanism for indicating reactants and products"""
367 cdef void* handle(self):
369 if self._handle == NULL:
371 for i, reactant in enumerate(
self._reactants):
375 def _invalidate_handle(
self):
376 if self._handle != NULL:
380 def add(self, zn, za, mi, lr=None, real_specifier=None, int_specifier=None):
381 """Add to the list of reactants/products"""
384 self._invalidate_handle()
389 if real_specifier: r.real_specifier = real_specifier
390 if int_specifier: r.int_specifier = int_specifier
391 self._reactants.append(r)
394 """Return the number of reactants/products"""
395 return
len(self._reactants)
398 """Return a string version of all the reactants/products"""
399 return str(self._reactants)
403 handle = self.handle()
406 """Return all of the reactants/products"""
407 return self._reactants
410 class AmnsException(Exception):
if error_status queryString
void ITM_AMNS_CC_SET_TABLE(void *handle_rx_in, amns_c_set_type *set, amns_c_error_type *error_status)
void ITM_AMNS_CC_RX_1_B(void *handle_rx_in, int nx, double *out, double *arg1, double *arg2, amns_c_error_type *error_status)
amns_c_reaction_type get_default_amns_c_reaction_type(void)
prototype
void ITM_AMNS_CC_RX_3_B(void *handle_rx_in, int nx, int ny, int nz, double *out, double *arg1, double *arg2, amns_c_error_type *error_status)
void ITM_AMNS_CC_RX_3_C(void *handle_rx_in, int nx, int ny, int nz, double *out, double *arg1, double *arg2, double *arg3, amns_c_error_type *error_status)
Type for error returns from the AMNS interface ("C" version)
Type for querying parameters in the AMNS package ("C" version)
amns_c_version_type get_default_amns_c_version_type(void)
prototype
amns_c_reactant_type get_default_amns_c_reactant_type(void)
prototype
if error_status np ndarray[np.double_t, ndim=2] np ndarray[np.double_t, ndim=2] np ndarray[np.double_t, ndim=2] p3
if error_status np ndarray[np.double_t, ndim=2] p1
Type for answers from queries in the AMNS package ("C" version)
void ITM_AMNS_CC_FINISH_TABLE(void **handle_rx_inout, amns_c_error_type *error_status)
Type used for specifying reactions when using the AMNS interface ("C" version)
void ITM_AMNS_CC_FINISH_REACTANTS(void **reactants_handle_inout)
from libcpp cimport bool cdef from amns_interface ZA
if error_status answer number def version(self) if error_status &error_status if error_status reactionString
void ITM_AMNS_CC_RX_1_C(void *handle_rx_in, int nx, double *out, double *arg1, double *arg2, double *arg3, amns_c_error_type *error_status)
Type for setting parameters in the AMNS package ("C" version)
if error_status np ndarray[np.double_t, ndim=2] np ndarray[np.double_t, ndim=2] np ndarray[np.double_t, ndim=2] ndim
void ITM_AMNS_CC_QUERY_TABLE(void *handle_rx_in, amns_c_query_type *query, amns_c_answer_type *answer, amns_c_error_type *error_status)
Type for specifying the AMNS version ("C" version)
if error_status answer number def version(self) if error_status &error_status if error_status Reactants isotope_resolved
if error_status answer number def set(self, setString) if error_status &error_status if error_status answer number def state_label(self) elif nargs
void ITM_AMNS_CC_RX_2_C(void *handle_rx_in, int nx, int ny, double *out, double *arg1, double *arg2, double *arg3, amns_c_error_type *error_status)
void ITM_AMNS_CC_SETUP_REACTANTS(void **reactants_handle_out, char *string_in, int index_in, int n_reactants)
void ITM_AMNS_CC_SET_REACTANT(void *reactants_handle_in, int reactant_index, amns_c_reactant_type *reactant_in)
if error_status np ndarray[np.double_t, ndim=2] np ndarray[np.double_t, ndim=2] p2
void ITM_AMNS_CC_QUERY(void *handle_in, amns_c_query_type *query, amns_c_answer_type *answer, amns_c_error_type *error_status)
void ITM_AMNS_CC_RX_2_B(void *handle_rx_in, int nx, int ny, double *out, double *arg1, double *arg2, amns_c_error_type *error_status)
Type for indicating a single reactant or product when using the AMNS interface.
if error_status answer number def version(self) if error_status &error_status if error_status Reactants reactants
if error_status & error_status
void ITM_AMNS_CC_RX_3_A(void *handle_rx_in, int nx, int ny, int nz, double *out, double *arg1, amns_c_error_type *error_status)
if error_status len(self._reactants))
void ITM_AMNS_CC_SETUP_TABLE(void *handle_in, amns_c_reaction_type *reaction_type, void *reactant_handle_in, void **handle_rx_out, amns_c_error_type *error_status)
void ITM_AMNS_CC_RX_1_A(void *handle_rx_in, int nx, double *out, double *arg1, amns_c_error_type *error_status)
void ITM_AMNS_CC_RX_2_A(void *handle_rx_in, int nx, int ny, double *out, double *arg1, amns_c_error_type *error_status)
void ITM_AMNS_CC_SETUP(void **handle_out, amns_c_error_type *error_status)
void ITM_AMNS_CC_SETUP_VERSION(void **handle_in, amns_c_version_type *version, amns_c_error_type *error_status)