Streamer fluid modeling - An overview of ARCoS  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
main.c
Go to the documentation of this file.
1 
4 #include <stdlib.h>
5 #include <time.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <errno.h>
9 //#include <getopt.h>
10 #include <math.h>
11 
12 #include "cdr.h"
13 #include "cstream.h"
14 #include "grid.h"
15 #include "parameters.h"
16 #include "proto.h"
17 #include "species.h"
18 
19 int main (int argc, char *argv[]);
20 void start_process (cdr_grid_t *cdr);
21 void output_tree (cdr_grid_t *cdr, int step, double t);
22 void check_params (void);
23 
25 int
26 main (int argc, char *argv[])
27 {
28  // param_t **p;
29  // char *s;
30  // struct option *opt;
31  // struct option *long_options;
32  // int c;
33  cdr_grid_t *cdr;
34  int count,count_user;
35  static const char *output_file;
36  config_t cfg,cfg_user;
37  config_setting_t *setting,*setting_user;
38  const char *prog_name,*f_defname,*f_username;
39 
40  f_defname = "input/default.cfg";
41  f_username = "input/user_init.cfg";
42  output_file = "input/user_continue.cfg";
43 
44  /* Open configuration cfg */
45  config_init(&cfg);
46 
47  /* Read the default file.
48  * The default file might be a result of a previous run, containing values
49  * for all global variables.
50  * If there is an error, report it and exit. */
51  if(! config_read_file(&cfg, f_defname))
52  {
53  fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
55  config_destroy(&cfg);
56  return(EXIT_FAILURE);
57  }
58 
59  /* Get the program name. */
60  if(config_lookup_string(&cfg, "name", &prog_name))
61  printf("Example : %s\n\n", prog_name);
62  else
63  fprintf(stderr, "No ' program name' setting in configuration file.\n");
64 
65  /* Output a list of all parameters in the file. */
66  setting = config_lookup(&cfg, "param");
67  if(setting != NULL)
68  {
69  int i;
70  count = config_setting_length(setting);
71 
72  fprintf(stdout, "# params: count=%i\n",count);
73 
74  for(i = 0; i < count; ++i)
75  {
76  if (read_parameter(setting,setting,i,count,FALSE))
77  continue;
78  }
79 
80  /* Write out the updated configuration. */
81  if(! config_write_file(&cfg, output_file))
82  {
83  fprintf(stderr, "Error while writing file.\n");
84  config_destroy(&cfg);
85  return(EXIT_FAILURE);
86  }
87  putchar('\n');
88  }
89  fprintf(stdout, "Default configuration successfully written to: %s\n",
90  output_file);
91 
92 /* ========================================================================== */
93 
94  /* Open configuration cfg_user */
95  config_init(&cfg_user);
96 
97  /* Read the file made by the user, with adapted values.
98  * This file does not have to contain entries for variables which remain
99  * unchanged.
100  * If there is an error, report it and exit. */
101  if(! config_read_file(&cfg_user, f_username))
102  {
103  fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg_user),
104  config_error_line(&cfg_user), config_error_text(&cfg_user));
105  config_destroy(&cfg_user);
106  return(EXIT_FAILURE);
107  }
108 
109  /* Output a list of all parameters in the file. */
110  setting_user = config_lookup(&cfg_user, "param");
111  if(setting_user)
112  {
113  int i;
114  count_user = config_setting_length(setting_user);
115 
116  fprintf(stdout, "# params: count_user=%i\n",count_user);
117 
118  for(i = 0; i < count_user; ++i)
119  {
120  if (read_parameter(setting,setting_user,i,count,TRUE))
121  continue;
122  }
123 
124  /* Write out the updated configuration. */
125  if(! config_write_file(&cfg, output_file))
126  {
127  fprintf(stderr, "Error while writing file.\n");
128  config_destroy(&cfg);
129  return(EXIT_FAILURE);
130  } else {
131  fprintf(stdout, "Updated configuration successfully written to: %s\n",
132  output_file);
133  }
134  putchar('\n');
135  }
136 
137  check_params ();
138 
139  cstream_init ();
140 
141  if (restart) {
142  if (0 == strcmp (load_file, "")) {
143  printf("restart file is empty\n");
144  return 1;
145  } else
146  cdr = cdr_load_tree_r (output_dir, load_file, NULL);
147  } else
148  cdr = cdr_scratch_init ();
149 
150  start_process(cdr);
151 
152  /* Destroy both configurations */
153  config_destroy(&cfg_user);
154  config_destroy(&cfg);
155 
156  return 0;
157 }
158 
160 void
162 {
163  int i, i0;
164  double t, dt;
165  double t0, t1;
166  struct timeval tv;
167  FILE *fp;
168 
169  i0 = (int) (start_t / output_dt);
170 
171  debug(1,"before loop :start_t= %e; t = %e; dt = %e\n",start_t,t, dt);
172  /* This is the main loop of the code. */
173  for (i = i0, t = start_t; t < end_t; t += dt)
174  {
175  debug(1,"inside loop: i, t, dt : %d %e %e\n",i,t,dt);
176  /* Set the field and the fixed charge when we have time-dependent
177  external fields (controled by rise_time) */
179  dt = cdr_rk2 (cdr, attempt_dt, t);
180  cdr_update_refined (&cdr);
181  printf("t = %lf\tdt = %lf\tnext output at: %lf\n",t,dt,i * output_dt);
182  if (output_dt <= 0.0 || t >= output_dt * i)
183  {
184  output_tree (cdr, i, t);
185  if (cdr->ntheta > 1)
186  {
187  dft_out_weights (cdr, output_dir, t);
188  }
189  i++;
190  }
191  }
192  cdr_dump (cdr, output_dir, "C");
193  cdr_free_r (cdr);
194  cstream_end ();
195 }
196 
201 void
202 output_tree (cdr_grid_t *cdr, int step, double t)
203 {
204  char *gname;
205  pois_grid_t **pois_modes;
206  FILE *fp;
207  char logfile[100];
208 
209  asprintf (&gname, "C%.3d", step);
210  cdr_set_ext_bnd_r (cdr);
211 
212  /* !!!!!!!!!!!! */
213  if (1 || t > start_t || 0 == strcmp (load_file, "")) {
214  cdr_dump_r (cdr, output_dir, gname, NULL, t);
215  cdr_dump_frames (cdr, output_dir, gname);
216 
217  printf ("CDR grid %s saved\n", gname);
218  }
219 
220  free (gname);
221 
222  if (pois_output) {
223  int mode;
224  pois_modes = cdr_calc_field_r (cdr, TRUE);
225 
226  for (mode = 0; mode < max_ntheta; mode++) {
227  asprintf (&gname, "P%.3d%.2d", step, mode);
228  pois_dump_r (pois_modes[mode], output_dir, gname);
229  pois_free_r (pois_modes[mode]);
230  printf ("Poisson grid %s saved\n", gname);
231  free (gname);
232  }
233 
234  free (pois_modes);
235  }
236 }
237 
241 void
243 {
244  if ((gridpoints_z % 4) != 0 && pois_inhom == 1) {
245  fatal ("To use inhomogeneous fields, gridpoints_z must be a"
246  " multiple of 4\n");
247  }
248 }