ITM AMNS: User Interface  \$Id: Doxyfile 502 2015-10-15 12:23:45Z dpc $
amns.pyx
Go to the documentation of this file.
1 cimport camns_interface
2 from camns_interface cimport *
3 import numpy as np
4 cimport numpy as np
5 
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
10 
11 cdef class Amns:
12 # """Class for interfacing to the AMNS library"""
13 
14  cdef void* _handle
15 # """AMNS handle (opaque)"""
16 
17  def __init__(self, version_string=None, version_number=None, version_backend=None, version_user=None):
18  """Use to initialize the Amns class"""
19  self._handle = NULL
20  self._setup(version_string=version_string, version_number=version_number, version_backend=version_backend, version_user=version_user)
21 
22  cdef void* handle(self):
23  return self._handle
24 
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"""
27  cdef camns_interface.amns_c_error_type error_status
28  cdef camns_interface.amns_c_version_type version
29  version = camns_interface.get_default_amns_c_version_type()
30  if (version_string == None) & ( version_number == None) & (version_backend == None) & (version_user == None):
31  camns_interface.ITM_AMNS_CC_SETUP(&self._handle, &error_status)
32  else:
33  print version
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
38  print version
39  camns_interface.ITM_AMNS_CC_SETUP_VERSION(&self._handle, &version, &error_status)
40  if error_status.flag:
41  raise AmnsException(error_status.string)
42 
43  cdef camns_interface.amns_c_answer_type lquery(self, queryString):
44  cdef camns_interface.amns_c_query_type query
45  cdef camns_interface.amns_c_answer_type answer
46  cdef camns_interface.amns_c_error_type error_status
47  query.string = queryString
48  camns_interface.ITM_AMNS_CC_QUERY(self._handle, &query, &answer, &error_status);
49  if error_status.flag:
50  raise AmnsException(error_status.string)
51  return answer
52 
53  def query(self, queryString):
54  """Provide the interface to ITM_AMNS_QUERY"""
55  cdef camns_interface.amns_c_query_type query
56  cdef camns_interface.amns_c_answer_type answer
57  cdef camns_interface.amns_c_error_type error_status
58  query.string = queryString
59  camns_interface.ITM_AMNS_CC_QUERY(self._handle, &query, &answer, &error_status);
60  if error_status.flag:
61  raise AmnsException(error_status.string)
62  return answer.string, answer.number
63 
64  @property
65  def version(self):
66  """Query for version of the AMNS data"""
67  cdef camns_interface.amns_c_answer_type answer
68  answer = self.lquery("version")
69  return answer.number, answer.string
70 
71  def set(self, setString):
72  """Provide the interface to ITM_AMNS_SET"""
73  cdef camns_interface.amns_c_set_type set
74  cdef camns_interface.amns_c_error_type error_status
75  set.string = setString
76  camns_interface.ITM_AMNS_CC_SET(self._handle, &set, &error_status);
77  if error_status.flag:
78  raise AmnsException(error_status.string)
79 
80  def finalize(self):
81  """Provide the interface to ITM_AMNS_FINISH"""
82  cdef camns_interface.amns_c_error_type error_status
83  camns_interface.ITM_AMNS_CC_FINISH(&self._handle, &error_status)
84  if error_status.flag:
85  raise AmnsException(error_status.string)
86 
87  def get_table(self, reactionString, Reactants reactants, isotope_resolved=None):
88  """Provide the interface to the Table class used for tables"""
89  return Table(reactionString, reactants, self, isotope_resolved)
90 
91 
92 cdef class Table:
93 # """Class for interfacing to Tables in the the AMNS library"""
94 
95  cdef void* _handle
96  cdef Amns _parentAmns
97 
98  def __init__(self, reactionString, Reactants reactants, Amns parentAmns, isotope_resolved=None):
99  """Create a new table"""
100  cdef camns_interface.amns_c_error_type error_status
101  cdef camns_interface.amns_c_reaction_type reaction
102  reaction = camns_interface.get_default_amns_c_reaction_type()
103  self._parentAmns = parentAmns
104  reaction.string = reactionString
106  camns_interface.ITM_AMNS_CC_SETUP_TABLE(self._parentAmns.handle(),
107  &reaction,
108  reactants.handle(), &self._handle,
109  &error_status)
110  if error_status.flag:
111  raise AmnsException(error_status.string)
112 
113  self.set("nowarn")
114 
115 
116  cdef camns_interface.amns_c_answer_type lquery(self, queryString):
117  cdef camns_interface.amns_c_query_type query
118  cdef camns_interface.amns_c_answer_type answer
119  cdef camns_interface.amns_c_error_type error_status
120  query.string = queryString
121  camns_interface.ITM_AMNS_CC_QUERY_TABLE(self._handle, &query, &answer, &error_status);
122  if error_status.flag:
123  raise AmnsException(error_status.string)
124  return answer
125 
126  def query(self, queryString):
127  """Provide the interface to ITM_AMNS_QUERY_TABLE"""
128  cdef camns_interface.amns_c_query_type query
129  cdef camns_interface.amns_c_answer_type answer
130  cdef camns_interface.amns_c_error_type error_status
131  query.string = queryString
132  camns_interface.ITM_AMNS_CC_QUERY_TABLE(self._handle, &query, &answer, &error_status);
133  if error_status.flag:
134  raise AmnsException(error_status.string)
135  return answer.string, answer.number
136 
137  def set(self, setString):
138  """Provide the interface to ITM_AMNS_SET_TABLE"""
139  cdef camns_interface.amns_c_set_type set
140  cdef camns_interface.amns_c_error_type error_status
141  set.string = setString
142  camns_interface.ITM_AMNS_CC_SET_TABLE(self._handle, &set, &error_status);
143  if error_status.flag:
144  raise AmnsException(error_status.string)
145 
146  def finalize(self):
147  """Provide the interface to ITM_AMNS_FINISH_TABLE"""
148  cdef camns_interface.amns_c_error_type error_status
149  camns_interface.ITM_AMNS_CC_FINISH_TABLE(&self._handle, &error_status)
150  if error_status.flag:
151  raise AmnsException(error_status.string)
152 
153  @property
154  def no_of_reactants(self):
155  """Query for no_of_reactants associated with the AMNS table"""
156  cdef camns_interface.amns_c_answer_type answer
157  answer = self.lquery("no_of_reactants")
158  return answer.number
159 
160  @property
161  def ndim(self):
162  """Query for ndim associated with the AMNS table"""
163  cdef camns_interface.amns_c_answer_type answer
164  answer = self.lquery("ndim")
165  return answer.number
166 
167  @property
168  def source(self):
169  """Query for source associated with the AMNS table"""
170  cdef camns_interface.amns_c_answer_type answer
171  answer = self.lquery("source")
172  return answer.string
173 
174  @property
175  def filled(self):
176  """Query for filled status associated with the AMNS table"""
177  cdef camns_interface.amns_c_answer_type answer
178  answer = self.lquery("filled")
179  return answer.string
180 
181  @property
182  def reaction_type(self):
183  """Query for reaction_type associated with the AMNS table"""
184  cdef camns_interface.amns_c_answer_type answer
185  answer = self.lquery("reaction_type")
186  return answer.string
187 
188  @property
189  def reactants(self):
190  """Query for reactants associated with the AMNS table"""
191  cdef camns_interface.amns_c_answer_type answer
192  answer = self.lquery("reactants")
193  return answer.string
194 
195  @property
196  def version(self):
197  """Query for version associated with the AMNS table"""
198  cdef camns_interface.amns_c_answer_type answer
199  answer = self.lquery("version")
200  return answer.string, answer.number
201 
202  @property
203  def state_label(self):
204  """Query for state_label associated with the AMNS table"""
205  cdef camns_interface.amns_c_answer_type answer
206  answer = self.lquery("state_label")
207  return answer.string
208 
209  @property
210  def result_unit(self):
211  """Query for result_unit associated with the AMNS table"""
212  cdef camns_interface.amns_c_answer_type answer
213  answer = self.lquery("result_unit")
214  return answer.string
215 
216  @property
217  def result_label(self):
218  """Query for result_label associated with the AMNS table"""
219  cdef camns_interface.amns_c_answer_type answer
220  answer = self.lquery("result_label")
221  return answer.string
222 
223  @property
224  def interp_fun(self):
225  """Query for interp_fun associated with the AMNS table"""
226  cdef camns_interface.amns_c_answer_type answer
227  answer = self.lquery("interp_fun")
228  return answer.number
229 
230  def data(self, p1, p2 = None, p3 = None):
231  """Return the AMNS data based on the arguments"""
232  nargs = 1
233  if p2 is not None: nargs += 1
234  if p3 is not None: nargs += 1
235  if nargs != self.ndim:
236  raise AmnsException("Number of parameters does not match table dimensions (ndim="
237  + str(self.ndim) + ")")
238 
239  refShape = p1.shape
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")
244 
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)
251  else:
252  raise AmnsException("Unsupported rank of input arguments: %s" % (len(refShape),))
253 
254  return res
255 
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):
259  cdef camns_interface.amns_c_error_type error_status
260  cdef np.ndarray[np.double_t, ndim=1] res
261 
262  nargs = 1
263  if p2 is not None: nargs += 1
264  if p3 is not None: nargs += 1
265 
266  res = np.empty_like(p1, order='F')
267 
268  if nargs == 1:
269  camns_interface.ITM_AMNS_CC_RX_1_A(self._handle, p1.shape[0],
270  <double*> res.data,
271  <double*> p1.data, &error_status);
272  elif nargs == 2:
273  camns_interface.ITM_AMNS_CC_RX_1_B(self._handle, p1.shape[0],
274  <double*> res.data,
275  <double*> p1.data,
276  <double*> p2.data, &error_status);
277  elif nargs == 3:
278  camns_interface.ITM_AMNS_CC_RX_1_C(self._handle, p1.shape[0],
279  <double*> res.data,
280  <double*> p1.data,
281  <double*> p2.data,
282  <double*> p3.data, &error_status);
284  if error_status.flag:
285  raise AmnsException(error_status.string)
286  return res
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):
291 
292  cdef camns_interface.amns_c_error_type error_status
293  cdef np.ndarray[np.double_t, ndim=2] res
294 
295  nargs = 1
296  if p2 is not None: nargs += 1
297  if p3 is not None: nargs += 1
298 
299  res = np.empty_like(p1, order='F')
300 
301  if nargs == 1:
302  camns_interface.ITM_AMNS_CC_RX_2_A(self._handle, p1.shape[0], p1.shape[1],
303  <double*> res.data,
304  <double*> p1.data, &error_status);
305  elif nargs == 2:
306  camns_interface.ITM_AMNS_CC_RX_2_B(self._handle, p1.shape[0], p1.shape[1],
307  <double*> res.data,
308  <double*> p1.data,
309  <double*> p2.data, &error_status);
310  elif nargs == 3:
311  camns_interface.ITM_AMNS_CC_RX_2_C(self._handle, p1.shape[0], p1.shape[1],
312  <double*> res.data,
313  <double*> p1.data,
314  <double*> p2.data,
315  <double*> p3.data, &error_status);
316 
317  if error_status.flag:
318  raise AmnsException(error_status.string)
319  return res
320 
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):
324  cdef camns_interface.amns_c_error_type error_status
325  cdef np.ndarray[np.double_t, ndim=3] res
326 
327  nargs = 1
328  if p2 is not None: nargs += 1
329  if p3 is not None: nargs += 1
330 
331  res = np.empty_like(p1, order='F')
332 
333  if nargs == 1:
334  camns_interface.ITM_AMNS_CC_RX_3_A(self._handle, p1.shape[0], p1.shape[1], p1.shape[2],
335  <double*> res.data,
336  <double*> p1.data, &error_status);
337  elif nargs == 2:
338  camns_interface.ITM_AMNS_CC_RX_3_B(self._handle, p1.shape[0], p1.shape[1], p1.shape[2],
339  <double*> res.data,
340  <double*> p1.data,
341  <double*> p2.data, &error_status);
342  elif nargs == 3:
343  camns_interface.ITM_AMNS_CC_RX_3_C(self._handle, p1.shape[0], p1.shape[1], p1.shape[2],
344  <double*> res.data,
345  <double*> p1.data,
346  <double*> p2.data,
347  <double*> p3.data, &error_status);
348 
349  if error_status.flag:
350  raise AmnsException(error_status.string)
351  return res
352 
353 
354 cdef class Reactants:
355 # """Class providing the machanism for indicating reactants and products"""
356  cdef void* _handle
357  cdef _reactants
358 
359  def __cinit__(self):
360  pass
361 
362  def __init__(self):
363  self._handle = NULL
364  self._reactants= []
365  pass
366 
367  cdef void* handle(self):
368  cdef camns_interface.amns_c_reactant_type reactant
369  if self._handle == NULL:
370  camns_interface.ITM_AMNS_CC_SETUP_REACTANTS(&self._handle, "", 0, len(self._reactants));
371  for i, reactant in enumerate(self._reactants):
372  camns_interface.ITM_AMNS_CC_SET_REACTANT(self._handle, i+1, &reactant);
373  return self._handle
374 
375  def _invalidate_handle(self):
376  if self._handle != NULL:
377  camns_interface.ITM_AMNS_CC_FINISH_REACTANTS(&self._handle)
378  self._handle = NULL
379 
380  def add(self, zn, za, mi, lr=None, real_specifier=None, int_specifier=None):
381  """Add to the list of reactants/products"""
382  cdef camns_interface.amns_c_reactant_type r
383  r = camns_interface.get_default_amns_c_reactant_type()
384  self._invalidate_handle()
385  r.ZN = zn
386  r.ZA = za
387  r.MI = mi
388  if lr: r.LR = lr
389  if real_specifier: r.real_specifier = real_specifier
390  if int_specifier: r.int_specifier = int_specifier
391  self._reactants.append(r)
392 
393  def __len__(self):
394  """Return the number of reactants/products"""
395  return len(self._reactants)
396 
397  def __str__(self):
398  """Return a string version of all the reactants/products"""
399  return str(self._reactants)
400 
401  def test(self):
402  cdef void* handle
403  handle = self.handle()
404 
405  def value(self):
406  """Return all of the reactants/products"""
407  return self._reactants
408 
409 
410 class AmnsException(Exception):
411  pass
if error_status queryString
Definition: amns.pyx:11
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 (&quot;C&quot; version)
Type for querying parameters in the AMNS package (&quot;C&quot; version)
if error_status & query
Definition: amns.pyx:11
for i
Definition: amns.pyx:365
amns_c_version_type get_default_amns_c_version_type(void)
prototype
if error_status flag
Definition: amns.pyx:11
if error_status & answer
Definition: amns.pyx:11
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
Definition: amns.pyx:284
if error_status np ndarray[np.double_t, ndim=2] p1
Definition: amns.pyx:282
Type for answers from queries in the AMNS package (&quot;C&quot; 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 (&quot;C&quot; 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
Definition: amns.pyx:59
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 (&quot;C&quot; 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
Definition: amns.pyx:287
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 (&quot;C&quot; version)
if error_status answer number def version(self) if error_status &error_status if error_status Reactants isotope_resolved
Definition: amns.pyx:81
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
Definition: amns.pyx:266
version
Definition: setup.py:45
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
Definition: amns.pyx:283
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
Definition: amns.pyx:59
if error_status & error_status
Definition: amns.pyx:11
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)