EIRAM
atomic and molecular data in form of polynomial fits
eiram_octave.cpp
Go to the documentation of this file.
1 
7 // Copyright (c) 2016 Forschungszentrum Juelich GmbH
8 // Markus Brenneis, Vladislav Kotov
9 //
10 // This file is part of EIRAM.
11 //
12 // EIRAM is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // EIRAM is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with EIRAM. If not, see <http://www.gnu.org/licenses/>.
24 
25 #include <octave/oct.h>
26 #include <cstring>
27 #include <string>
28 #include "eiram_octave.hpp"
29 
30 DEFUN_DLD (eiram, args, nargout,
31 "\n\
32  Calculate reaction rates and cross-sections described by polynomial fit formulas \n\
33  (interface to fortran module EIRAM) \n\
34 \n\
35  [Y1 Y2 .. ] = eiram(X1, [X2,] name, type, index, [path]) \n\
36 \n\
37  Y1, Y2 ... : vectors or matrices with the results, \n\
38  each Y1, Y2 ..., corresponds to one reaction defined in INDEX, \n\
39  each column in Y1, Y2 ... corresponds to one value of X1 \n\
40  X1 : row vector, first argument of the fit \n\
41  X2 : optional, column vector, second argument of the fit \n\
42  name : string, name of the file with the data-set ('HYDHEL', 'AMJUEL' etc.) \n\
43  type : string, type of the reaction ('H.1','H.3','H.4' etc.) \n\
44  index : array of strings, index of the reaction ('3.1.8', '2.1.5' etc.) \n\
45  path : optional, string, complete path to the file defined by NAME, \n\
46  if PATH is omitted than it is assumed that the data-set is in the current folder \n\
47 \n\
48  Units : energy and temperature in [eV], density in [cm^-3], \n\
49  cross sections in cm^2, collision rates in cm^3/s, \n\
50  energy loss rates in cm^3/s*eV \n\
51 \n\
52  Examples: \n\
53  % Charge-exchange H + p, Ebeam=10 eV \n\
54  svcx = eiram(10,[1:100],'HYDHEL','H.3','3.1.8'); \n\
55 \n\
56  % Momentum transfer cross-sections for elastic collisions p+H, p+He, p+H2 \n\
57  [sel_D sel_He sel_D2] = eiram([0.1:10],'AMJUEL','H.1',['0.1D'; '0.2D'; '0.3D'],'.../eiram/data/'); \n\
58 \n\
59  % Ionization rate of H atoms, electron density 1e12 cm-3 and 1e14 cm-3 \n\
60  svi = eiram([1e12 1e14],[0.1:0.1:10 10+1:1:100],'AMJUEL','H.4','2.1.5');"){
61  octave_value_list retval;
62  int nargin = args.length();
63 
64  if(nargin < 4) { error("At least 4 input arguments required"); return retval;}
65 
66  if(!args(0).is_numeric_type()) { error("1st argument must be of numeric type"); return retval;}
67 
68  int numberOfArgs;
69 
70  if(args(1).is_numeric_type()) { numberOfArgs = 2; }
71  else { numberOfArgs = 1; }
72 
73  if ( (nargin-numberOfArgs) < 3 ) { error("Too few input arguments"); }
74 
75  NDArray Arg1s = args(0).array_value();
76  double* Arg1s_v = Arg1s.fortran_vec();
77 
78  if(error_state) {return retval;}
79  octave_idx_type n_Arg1s = Arg1s.numel();
80 
81  std::string fileName = args(numberOfArgs).char_matrix_value().row_as_string(0);
82  std::string reactionType = args(numberOfArgs+1).char_matrix_value().row_as_string(0);
83  charMatrix reactionIndex = args(numberOfArgs+2).char_matrix_value();
84  if(error_state) {return retval;}
85 
86  std::string filePath;
87  if( (nargin-numberOfArgs) > 3 ) {filePath = args(numberOfArgs+3).char_matrix_value().row_as_string(0);}
88 
89  int err = 0;
90 
91  if(error_state) {return retval;}
92 
93  F77_XFCN(eiram_load_wrapper, FORTSUB, (filePath.c_str(), fileName.c_str(), err, filePath.length(), fileName.length()));
94 
95  if(err != 0) {
96  char str[1024];
97  switch(err){
98  case 310 :
99  sprintf(str,"Input file %s%s is not found",filePath.c_str(),fileName.c_str());
100  error(str);
101  break;
102  case 300 :
103  sprintf(str,"Cannot read input file %s%s",filePath.c_str(),fileName.c_str());
104  error(str);
105  break;
106  default :
107  error("error during initialization of EIRAM");
108  }
109  F77_XFCN(eiram_deallocate_wrapper, FORTSUB, (err));
110  return retval;
111  }
112 
113  octave_idx_type n_reactions = reactionIndex.rows();
114 
115  for(int i=0;i<n_reactions;i++) {
116  std::string index=reactionIndex.row_as_string(i);
117 
118  if( numberOfArgs == 1) {
119  Matrix Ys(n_Arg1s, 1);
120 
121  F77_XFCN(eiram_matlab_calc1, FORTSUB,
122  (Ys.fortran_vec(),
123  Arg1s_v, n_Arg1s,
124  fileName.c_str(),reactionType.c_str(),
125  index.c_str(),err
126  F77_CHAR_ARG_LEN(fileName.length())
127  F77_CHAR_ARG_LEN(reactionType.length())
128  F77_CHAR_ARG_LEN(index.length())
129  ));
130  if(err == 0) {retval(i) = Ys;}
131 
132  }
133  else if( numberOfArgs == 2) {
134  NDArray Arg2s=args(1).array_value();
135  double* Arg2s_v = Arg2s.fortran_vec();
136  octave_idx_type n_Arg2s = Arg2s.numel();
137  Matrix Ys(n_Arg2s, n_Arg1s);
138 
139  F77_XFCN(eiram_matlab_calc2, FORTSUB,
140  (Ys.fortran_vec(),
141  Arg1s_v, Arg2s_v,n_Arg1s,n_Arg2s,
142  fileName.c_str(),reactionType.c_str(),
143  index.c_str(),err
144  F77_CHAR_ARG_LEN(fileName.length())
145  F77_CHAR_ARG_LEN(reactionType.length())
146  F77_CHAR_ARG_LEN(index.length())
147  ));
148  if(err == 0) {retval(i) = Ys;}
149  }
150 
151  switch(err){
152  case 11 :
153  error("error : negative argument");
154  break;
155  case 12 :
156  error("error : negative 1st argument");
157  break;
158  case 13 :
159  error("error : negative 2nd argument");
160  break;
161  case 110 :
162  char str[128];
163  sprintf(str,"error: requested reaction %s %s %s is not found",fileName.c_str(),reactionType.c_str(),index.c_str());
164  error(str);
165  break;
166  case 120 :
167  error("error : double fit is requested for reaction described by a single fit");
168  break;
169  case 130 :
170  error("error : single fit is requested for reaction described by a double fit");
171  break;
172  default :
173  if(err != 0) {error("error during execution of EIRAM");}
174  }
175  if(err != 0) {
176  F77_XFCN(eiram_deallocate_wrapper, FORTSUB, (err));
177  return retval; }
178  } //for(int i=0;i<n_reactions;i++)
179 
180  F77_XFCN(eiram_deallocate_wrapper, FORTSUB, (err));
181  return retval;
182 }
183 
184 
DEFUN_DLD(eiram, args, nargout,"\n\ Calculate reaction rates and cross-sections described by polynomial fit formulas \n\ (interface to fortran module EIRAM) \n\ \n\ [Y1 Y2 .. ] = eiram(X1, [X2,] name, type, index, [path]) \n\ \n\ Y1, Y2 ... : vectors or matrices with the results, \n\ each Y1, Y2 ..., corresponds to one reaction defined in INDEX, \n\ each column in Y1, Y2 ... corresponds to one value of X1 \n\ X1 : row vector, first argument of the fit \n\ X2 : optional, column vector, second argument of the fit \n\ name : string, name of the file with the data-set ('HYDHEL', 'AMJUEL' etc.) \n\ type : string, type of the reaction ('H.1','H.3','H.4' etc.) \n\ index : array of strings, index of the reaction ('3.1.8', '2.1.5' etc.) \n\ path : optional, string, complete path to the file defined by NAME, \n\ if PATH is omitted than it is assumed that the data-set is in the current folder \n\ \n\ Units : energy and temperature in [eV], density in [cm^-3], \n\ cross sections in cm^2, collision rates in cm^3/s, \n\ energy loss rates in cm^3/s*eV \n\ \n\ Examples: \n\ % Charge-exchange H + p, Ebeam=10 eV \n\ svcx = eiram(10,[1:100],'HYDHEL','H.3','3.1.8'); \n\ \n\ % Momentum transfer cross-sections for elastic collisions p+H, p+He, p+H2 \n\ [sel_D sel_He sel_D2] = eiram([0.1:10],'AMJUEL','H.1',['0.1D'; '0.2D'; '0.3D'],'.../eiram/data/'); \n\ \n\ % Ionization rate of H atoms, electron density 1e12 cm-3 and 1e14 cm-3 \n\ svi = eiram([1e12 1e14],[0.1:0.1:10 10+1:1:100],'AMJUEL','H.4','2.1.5');")
integer, parameter n_reactions
Definition: eiram_test.f90:46
subroutine eiram_deallocate_wrapper(err)
module-less wrapper for eiram_deallocate
subroutine eiram_matlab_calc2(Y, X1, X2, M, N, fileName, reactionType, reactionIndex, err)
Binding for eiram_calc2 which has to be called from Matlab/Octave interface.
subroutine eiram_load_wrapper(filePath, fileName, err)
Wrappers are required for subroutines defined in a fortran module. See eiram_octave.hpp, eiram_octave.cpp as an example of how to call those subroutines in a C program.
Definition: eiram.f90:96
subroutine eiram_matlab_calc1(Y, X, N, fileName, reactionType, reactionIndex, err)
Binding for eiram_calc1 which has to be called from Matlab/Octave interface.