Streamer fluid modeling - An overview of ARCoS  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
react_table.c
Go to the documentation of this file.
1 
10 /* Structure of the reaction table file between -----
11 
12 ------
13 Optional comments, maximum line length of 100 chars!
14 START
15 is_log
16 e_min
17 e_step
18 points
19 underflow
20 overflow
21 value(0)
22 value(1)
23 ...
24 value(points-1)
25 -----
26 
27 Anything before 'START' is ignored. 'START' denotes the start of the actual data
28 and is case-sensitive. 'is_log' equals one if the electric field values are spaced
29 logarithmically. 'e_min' denotes the lowest value of the electric field for
30 which a data-point exists. 'e_step' is the difference in fieldstrength between
31 consecutive data-points and 'points' is the number of data-points. 'value(x)' are
32 the actual data-points. Note that anything after 'points' points is ignored.
33 
34 Underflow and overflow are reaction rates that are returned when the supplied field
35 strength is out of the bounds of the table.
36 
37 IMPORTANT: electric field values are 10-logarithmic. So instead of 'e_min' actually
38 denotes log10(e_min)!
39 IMPORTANT: electric field values must be (logarithmically) equi-distant! */
40 
41 /* Example file
42 
43 -----
44 Sample reaction table. k(E) = E. 4 points are defined at E = 10^i, i \in {-2,-1,0,1}.
45 So the 'e_min' value is -2, 'e_step' is 1 and 'points' is 4. Overflow (100) and
46 underflow (0) values are added in case E-input values fall outside the bounds, in
47 this case E < 10^-2 or E > 10^1.
48 START
49 -2.0
50 1
51 4
52 0.0
53 100.0
54 0.01
55 0.1
56 1.0
57 10.0
58 This bit is ignored by the program, so can be used for comments. Though if i had anything
59 useful to say, it would've probably been better placed at the start of the file!
60 -----
61 */
62 
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <math.h>
66 
67 #include "react_table.h"
68 
72 void
74 {
75  FILE *fp;
76  char *comments, buffer[101];
77  double e_min, e_step, underflow, overflow;
78 
79  int cnt;
80 
81  double values[MAX_TABLE_SIZE];
82 
83  fp = fopen(filename,"r");
84  if (fp == NULL)
85  {
86  fprintf(stderr,"Unable to load reaction table in file: %s -- Shutting down\n",filename);
87  exit(1);
88  }
89 
90  while (strncmp(buffer,"START",5) != 0)
91  fgets(buffer,100,fp);
92 
93  fgets(buffer,100,fp);
94  r->e_min = atof(buffer);
95  fgets(buffer,100,fp);
96  r->e_step = atof(buffer);
97  fgets(buffer,100,fp);
98  r->steps = atoi(buffer) - 1;
99  fgets(buffer,100,fp);
100  r->underflow = atof(buffer);
101  fgets(buffer,100,fp);
102  r->overflow = atof(buffer);
103 
104  for (cnt = 0; cnt <= r->steps; cnt++)
105  {
106  fgets(buffer,100,fp);
107  r->values[cnt] = atof(buffer);
108  }
109 
110  fclose(fp);
111 }
112 
119 void
120 react_table_lookup(react_table *r, double e, double *ra)
121 {
122  int pos;
123  double e1, e2, val1, val2, log_e, res;
124 
125  // e == 0 is possible at initialization.
126  log_e = (e > 0) ? log10(e) : -1000.0;
127 
128  /* If the supplied fieldstrength falls outside the boundaries of the table,
129  return predetermined under-/overflow values. In most cases the underflow(overflow)
130  will be equal to the lowest(highest) of the specified values in the table. */
131  if (log_e < r->e_min) { *ra = r->underflow; return; }
132  if (log_e > r->e_min + r->e_step * r->steps) { *ra = r->overflow; return; }
133 
134  pos = floor((log_e - r->e_min) / r->e_step);
135  val1 = r->values[pos];
136  val2 = r->values[pos+1];
137  e1 = pow(10, r->e_min + r->e_step * (double) pos);
138  e2 = e1 * pow(10, r->e_step);
139 
140  /* Linear interpolation of the 2 table values closest to e.
141  Note that the field-values in the table are logarithmic! */
142 
143  res = val1 + ((val2 - val1) / (e2 - e1)) * (e - e1);
144 
145  /* Dirty tricks with pointers to get the return value right. The normal 'return'
146  statement would give a completely different value on the "other side".
147 
148  It's not pretty, but it works, so who cares? :) */
149  *ra = res;
150 }