ISLEC  Version 4.2
sim_process.c
Go to the documentation of this file.
1 /* src/sim_process.c
2  *
3  * Copyright (C) 2011-2018 Dongdong Li
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundataion; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABLITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
31 /* do once equilibrium calculation */
32 static void do_equilibrium ()
33 {
34  gem_ipopt ();
35 
36  if (successed)
37  {
38  print_results ();
39  }
40 }
41 
42 /* do evaporation calculation */
43 static void do_evaporation (double *initial_components, double *moved_components, double *final_components)
44 {
45  int i;
46  for (i = 0; i < total_comp_num; i++)
47  {
48  total_components[i] = initial_components[i];
49  }
50 
51  bool Is_True = TRUE;
52  while (Is_True)
53  {
54  do_equilibrium ();
55 
56  int i;
57  for (i = 0; i < total_comp_num; i++)
58  {
59  total_components[i] -= moved_components[i];
60  }
61 
62  if (total_components[0] <= 0 || total_components[0] <= final_components[0]) {Is_True = FALSE; break;}
63  }
64 }
65 
66 /* do freezing calculation */
67 static void do_freezing (double iniT, double stepT, double finT)
68 {
69  system_T = iniT;
70 
71  if (system_T <= finT)
72  {
73  printf ("invalid temperature interval\n");
74  exit (0);
75  }
76 
77  while (system_T > finT)
78  {
79  do_equilibrium ();
80  system_T -= stepT;
81  }
82 }
83 
84 /* do heating calculation */
85 static void do_heating (double iniT, double stepT, double finT)
86 {
87  system_T = iniT;
88 
89  if (system_T >= finT)
90  {
91  printf ("invalid temperature interval\n");
92  exit (0);
93  }
94 
95  while (system_T < finT)
96  {
97  do_equilibrium ();
98  system_T += stepT;
99  }
100 }
101 
102 /* do mixing calculation */
103 static void do_mixing (double *total_components_1, double *total_components_2,
104  double system_charge_1, double system_charge_2)
105 {
106  int i;
107  for (i = 0; i < total_comp_num; i++)
108  {
109  total_components[i] = total_components_1[i] + total_components_2[i];
110  }
111 
112  system_charge = system_charge_1 + system_charge_2;
113 
114  do_equilibrium ();
115 }
116 
117 /* do simulation */
118 extern void simulate (char *file_name)
119 {
120  FILE *fp;
121  int i, j;
122  int rv;
123 
124  total_components = (double *)malloc(sizeof (double) * total_comp_num);
125 
126  if ((fp = fopen (file_name, "r")) == NULL)
127  {
128  printf ("Open Input File Error!!!\n");
129  exit (1);
130  }
131 
132  char process[64];
133 
134  if (fscanf (fp, "%s\n", process) != 1)
135  {
136  printf ("invalid input file\n");
137  exit (0);
138  }
139 
140  else
141  {
142  for (j = 0; j < 5; j ++)
143  {
144  if(strcmp(process, PROCESS[j]) == 0)
145  break;
146  }
147 
148  switch (j)
149  {
150  case 0: // EQUILIBRIUM
151  {
152  rv = fscanf (fp, "%lf\n", &system_T);
153 
154  if (rv != 1)
155  {
156  printf ("Load Input File Error!!!\n");
157  exit (1);
158  }
159 
160  rv = fscanf (fp, "%lf\n", &system_P);
161 
162  if (rv != 1)
163  {
164  printf ("Load Input File Error!!!\n");
165  exit (1);
166  }
167 
168  rv = fscanf (fp, "%lf\n", &system_charge);
169 
170  if (rv != 1)
171  {
172  printf ("Load Input File Error!!!\n");
173  exit (1);
174  }
175 
176  for (i = 0; i < total_comp_num; i++)
177  {
178  rv = fscanf (fp, "\t%lf", &total_components[i]);
179 
180  if (rv != 1)
181  {
182  printf ("Load Input File Error!!!\n");
183  exit (1);
184  }
185  }
186 
187  do_equilibrium ();
188 
189  break;
190  }
191 
192  case 1: // EVAPORATION
193  {
194  rv = fscanf (fp, "%lf\n", &system_T);
195 
196  if (rv != 1)
197  {
198  printf ("Load Input File Error!!!\n");
199  exit (1);
200  }
201 
202  rv = fscanf (fp, "%lf\n", &system_P);
203  if (rv != 1)
204  {
205  printf ("Load Input File Error!!!\n");
206  exit (1);
207  }
208 
209  rv = fscanf (fp, "%lf\n", &system_charge);
210 
211  if (rv != 1)
212  {
213  printf ("Load Input File Error!!!\n");
214  exit (1);
215  }
216 
217  double *initial_components = (double *)malloc(sizeof (double) * total_comp_num);
218  double *moved_components = (double *)malloc(sizeof (double) * total_comp_num);
219  double *final_components = (double *)malloc(sizeof (double) * total_comp_num);
220 
221  for (i = 0; i < total_comp_num; i++)
222  {
223  rv = fscanf (fp, "\t%lf", &initial_components[i]);
224 
225  if (rv != 1)
226  {
227  printf ("Load Input File Error!!!\n");
228  exit (1);
229  }
230  }
231 
232  for (i = 0; i < total_comp_num; i++)
233  {
234  rv = fscanf (fp, "\t%lf", &moved_components[i]);
235 
236  if (rv != 1)
237  {
238  printf ("Load Input File Error!!!\n");
239  exit (1);
240  }
241  }
242 
243  for (i = 0; i < total_comp_num; i++)
244  {
245  rv = fscanf (fp, "\t%lf", &final_components[i]);
246 
247  if (rv != 1)
248  {
249  printf ("Load Input File Error!!!\n");
250  exit (1);
251  }
252  }
253 
254  do_evaporation (initial_components, moved_components, final_components);
255 
256  break;
257  }
258 
259  case 2: // FREEZING
260  {
261  double initial_T, decreased_T, final_T;
262 
263  rv = fscanf (fp, "%lf\t%lf\t%lf\n", &initial_T, &decreased_T, &final_T);
264 
265  if (rv != 3)
266  {
267  printf ("Load Input File Error!!!\n");
268  exit (1);
269  }
270 
271  rv = fscanf (fp, "%lf\n", &system_P);
272 
273  if (rv != 1)
274  {
275  printf ("Load Input File Error!!!\n");
276  exit (1);
277  }
278 
279  rv = fscanf (fp, "%lf\n", &system_charge);
280 
281  if (rv != 1)
282  {
283  printf ("Load Input File Error!!!\n");
284  exit (1);
285  }
286 
287  for (i = 0; i < total_comp_num; i++)
288  {
289  rv = fscanf (fp, "\t%lf", &total_components[i]);
290 
291  if (rv != 1)
292  {
293  printf ("Load Input File Error!!!\n");
294  exit (1);
295  }
296  }
297 
298  do_freezing (initial_T, decreased_T, final_T);
299 
300  break;
301  }
302 
303  case 3: // HEATING
304  {
305  double initial_T, increased_T, final_T;
306 
307  rv = fscanf (fp, "%lf\t%lf\t%lf\n", &initial_T, &increased_T, &final_T);
308 
309  if (rv != 3)
310  {
311  printf ("Load Input File Error!!!\n");
312  exit (1);
313  }
314 
315  rv = fscanf (fp, "%lf\n", &system_P);
316 
317  if (rv != 1)
318  {
319  printf ("Load Input File Error!!!\n");
320  exit (1);
321  }
322 
323  rv = fscanf (fp, "%lf\n", &system_charge);
324 
325  if (rv != 1)
326  {
327  printf ("Load Input File Error!!!\n");
328  exit (1);
329  }
330 
331  for (i = 0; i < total_comp_num; i++)
332  {
333  rv = fscanf (fp, "\t%lf", &total_components[i]);
334 
335  if (rv != 1)
336  {
337  printf ("Load Input File Error!!!\n");
338  exit (1);
339  }
340  }
341 
342  do_heating (initial_T, increased_T, final_T);
343 
344  break;
345  }
346 
347  case 4: // MIXING
348  {
349  rv = fscanf (fp, "%lf\n", &system_T);
350 
351  if (rv != 1)
352  {
353  printf ("Load Input File Error!!!\n");
354  exit (1);
355  }
356 
357  rv = fscanf (fp, "%lf\n", &system_P);
358  if (rv != 1)
359  {
360  printf ("Load Input File Error!!!\n");
361  exit (1);
362  }
363 
364  double system_charge_1;
365  double system_charge_2;
366  double *total_components_1 = (double *)malloc(sizeof (double) * total_comp_num);
367  double *total_components_2 = (double *)malloc(sizeof (double) * total_comp_num);
368 
369  rv = fscanf (fp, "%lf\n", &system_charge_1);
370 
371  if (rv != 1)
372  {
373  printf ("Load Input File Error!!!\n");
374  exit (1);
375  }
376 
377  for (i = 0; i < total_comp_num; i++)
378  {
379  rv = fscanf (fp, "\t%lf", &total_components_1[i]);
380 
381  if (rv != 1)
382  {
383  printf ("Load Input File Error!!!\n");
384  exit (1);
385  }
386  }
387 
388  rv = fscanf (fp, "%lf\n", &system_charge_2);
389 
390  if (rv != 1)
391  {
392  printf ("Load Input File Error!!!\n");
393  exit (1);
394  }
395 
396  for (i = 0; i < total_comp_num; i++)
397  {
398  rv = fscanf (fp, "\t%lf", &total_components_2[i]);
399 
400  if (rv != 1)
401  {
402  printf ("Load Input File Error!!!\n");
403  exit (1);
404  }
405  }
406 
407  do_mixing (total_components_1, total_components_2, system_charge_1, system_charge_2);
408 
409  break;
410  }
411  }
412  }
413 }
void simulate(char *file_name)
Definition: sim_process.c:118
double * total_components
Definition: islec.h:213
bool successed
Definition: islec.h:217
double system_T
Definition: islec.h:210
int gem_ipopt()
Perform Gibbs energy minimization (GEM) using the IPOPT algrithium.
Definition: gem_ipopt.c:180
double system_P
Definition: islec.h:211
char PROCESS[5][64]
Definition: islec.h:57
int total_comp_num
Definition: islec.h:194
double system_charge
Definition: islec.h:212