1
/*============================================================================
3
*============================================================================*/
6
This file is part of Code_Saturne, a general-purpose CFD tool.
8
Copyright (C) 1998-2011 EDF S.A.
10
This program is free software; you can redistribute it and/or modify it under
11
the terms of the GNU General Public License as published by the Free Software
12
Foundation; either version 2 of the License, or (at your option) any later
15
This program is distributed in the hope that it will be useful, but WITHOUT
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20
You should have received a copy of the GNU General Public License along with
21
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
22
Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
/*----------------------------------------------------------------------------*/
27
/*----------------------------------------------------------------------------
28
* Standard C library headers
29
*----------------------------------------------------------------------------*/
35
/*----------------------------------------------------------------------------
37
*----------------------------------------------------------------------------*/
39
#include <bft_mem_usage.h>
41
#include <bft_error.h>
43
/*----------------------------------------------------------------------------
45
*----------------------------------------------------------------------------*/
47
#include "mei_evaluate.h"
49
/*============================================================================
50
* External function prototype
51
*============================================================================*/
53
extern int graphik(mei_node_t*);
55
/*============================================================================
56
* Private function definitions
57
*============================================================================*/
59
/*----------------------------------------------------------------------------
61
*----------------------------------------------------------------------------*/
64
_base_error_handler(const char *file_name,
73
fprintf(stderr, "\n");
75
if (sys_err_code != 0)
76
fprintf(stderr, "\nSystem error: %s\n", strerror(sys_err_code));
78
fprintf(stderr, "\n%s:%d: Fatal error.\n\n", file_name, line_num);
80
vfprintf(stderr, format, arg_ptr);
82
fprintf(stderr, "\n\n");
87
/*----------------------------------------------------------------------------
88
* Initialize memory handling
89
*----------------------------------------------------------------------------*/
94
char *log_name = NULL;
96
/* Initialization of memory usage count */
100
/* Initialization of memory management */
102
log_name = getenv("CS_FIC_MEM");
104
bft_mem_init(log_name);
107
/*----------------------------------------------------------------------------
108
* Finalize memory handling
109
*----------------------------------------------------------------------------*/
112
_base_mem_finalize(void)
117
int ind_val[2] = {1, 1};
118
char unit[] = {'k', 'm', 'g', 't', 'p'};
120
const char * type_bil[] = {"Total measured memory usage: ",
121
"Theoretical instrumented dynamic memory:"};
125
printf("\nMemory usage summary:\n\n");
127
valreal[0] = (double) bft_mem_usage_max_pr_size();
128
valreal[1] = (double) bft_mem_size_max();
130
/* We will ignore non-consistent measures */
132
if (valreal[2] < valreal[1] || valreal[2] < valreal[3])
135
for (ind_bil = 0; ind_bil < 2; ind_bil++) {
136
if (valreal[ind_bil] < 1.0)
137
ind_val[ind_bil] = 0;
140
/* Similar handling for several instrumentation methods */
142
for (ind_bil = 0; ind_bil < 2; ind_bil++) {
144
/* If an instrumentation method returns an apparently consistent
147
if (ind_val[ind_bil] == 1) {
150
valreal[ind_bil] > 1024. && unit[itot] != 'p';
152
valreal[ind_bil] /= 1024.;
156
printf (" %s %12.3f %co\n",
157
type_bil[ind_bil], valreal[ind_bil], unit[itot]);
162
/* Finalize memory handling */
166
/* Finalize memory usage count */
171
/*============================================================================
173
*============================================================================*/
178
const char *v[] = {"X", "Y", "Z"};
179
const char *w[] = {"yy", "zz"};
185
/* Initialize memory handling */
188
bft_error_handler_set(_base_error_handler);
190
/* Check functionality */
191
/*---------------------*/
193
printf("\n------------------------------------------------------------------\n");
195
/* return two empty interpreter */
197
e1 = mei_tree_new("x=(+2.5); y=8.7; zz=y+x; yy = zz+0.8; \n");
198
e2 = mei_tree_new("y = cos(-pi) ; tt = K+y ; abs(tt);");
201
/* try to build the two interpreter */
203
printf("\nBuilding interpreter for: %s\n", "x=(+2.5); y=8.7; zz=y+x; yy = zz+0.8; \n");
204
if (mei_tree_builder(e1)) {
205
mei_tree_destroy(e1);
206
printf("failed...\n");
210
printf("\n------------------------------------------------------------------\n");
212
printf("\nBuilding interpreter for: %s\n", e2->string);
213
if (mei_tree_builder(e2)) {
214
mei_tree_destroy(e2);
215
printf("failed...\n");
219
printf("\n------------------------------------------------------------------\n");
221
/* complete the symbol table with required */
223
/* return an empty interpreter */
224
e2 = mei_tree_new("y = cos(-pi) ; tt = K+y ; abs(tt);");
226
mei_tree_insert(e2, "K", -5.3e2);
228
/* try to re-build the interpreter --> memory problem */
230
printf("\nBuilding interpreter for: %s\n", e2->string);
231
if (mei_tree_builder(e2)) {
232
mei_tree_destroy(e2);
233
printf("failed...\n");
237
printf("\n------------------------------------------------------------------\n");
239
printf("\nFind symbols in: %s\n", e1->string);
241
printf("Try to find %s\n", "toto");
243
if (mei_tree_find_symbol(e1, "toto"))
244
printf("not found...\n");
248
printf("Try to find %s\n", "yy");
250
if (mei_tree_find_symbol(e1, "yy"))
251
printf("not found...\n");
255
printf("\nFind symbols in: %s\n", e1->string);
257
printf("Try to find X Y Z\n");
259
if (mei_tree_find_symbols(e1, 3, v))
260
printf("not found...\n");
264
printf("Try to find yy zz\n");
266
if (mei_tree_find_symbols(e1, 2, w))
267
printf("not found...\n");
271
printf("\n------------------------------------------------------------------\n");
273
printf("\nInterpret expression: \n%s\n", e1->string);
275
printf("Evaluate: %s = %f\n", "zz", mei_tree_lookup(e1, "zz"));
276
printf("Evaluate: %s = %f\n", "yy", mei_tree_lookup(e1, "yy"));
278
printf("\nInterpret expression: \n%s\n", e2->string);
279
printf("Evaluate: [%s] = %f\n", e2->string, mei_evaluate(e2));
281
mei_tree_destroy(e1);
282
mei_tree_destroy(e2);
284
printf("\n------------------------------------------------------------------\n");
286
printf("\nBuild a shared table of symbols: \n");
288
sym = mei_table_symbols_new();
289
mei_symbol_table_insert(sym, "x", 3.2);
290
e1 = mei_tree_new_with_shared_symbols("y= 0.8; x+y;", sym);
292
if (!mei_tree_builder(e1)) {
293
/* graphik(e1->node); */
294
printf("\nExpression: \n%s\n", e1->string);
295
printf("Evaluate: %f\n", mei_evaluate(e1));
298
mei_tree_insert(e1, "x", 1.2);
299
e2 = mei_tree_new_with_shared_symbols("z=0.6; k = x*2; k+z;", sym);
301
if (!mei_tree_builder(e2)) {
303
printf("\nExpression: \n%s\n", e2->string);
304
printf("Evaluate: %f\n", mei_evaluate(e2));
307
mei_tree_destroy(e1);
308
mei_tree_destroy(e2);
310
printf("\n------------------------------------------------------------------\n");
312
e1 = mei_tree_new("abc = -1.1; \nwhile (abc < 3) {\n print abc;\n abc = abc + 1;\n};");
313
if (!mei_tree_builder(e1)) {
315
printf("\nExpression: \n%s\n", e1->string);
316
printf("Evaluate: %f\n", mei_evaluate(e1));
318
mei_tree_destroy(e1);
321
printf("\n------------------------------------------------------------------\n");
323
/* Check error detection */
324
/*-----------------------*/
326
e1 = mei_tree_new("x = A+3; A=5;");
327
printf("\nExpression: \n%s\n", e1->string);
328
iok = mei_tree_builder(e1);
329
mei_tree_destroy(e1);
331
printf("\n------------------------------------------------------------------\n");
333
e1 = mei_tree_new("cst = 4; # toto \n u = cos(cst);");
334
printf("\nExpression: \n%s\n", e1->string);
335
if (!mei_tree_builder(e1)) {
337
printf("Evaluate: u = %f\n", mei_tree_lookup(e1, "u"));
339
mei_tree_destroy(e1);
341
printf("\n------------------------------------------------------------------\n");
343
e1 = mei_tree_new("%u = cos(pi);");
344
printf("\nInterpret expression: \n%s\n", e1->string);
345
if (!mei_tree_builder(e1)) {
347
printf("Evaluate: u = %f\n", mei_tree_lookup(e1, "u"));
349
mei_tree_destroy(e1);
351
printf("\n------------------------------------------------------------------\n");
353
e1 = mei_tree_new("v = max(pi,+3);");
354
printf("\nInterpret expression: \n%s\n", e1->string);
355
if (!mei_tree_builder(e1)) {
358
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
360
mei_tree_destroy(e1);
362
printf("\n------------------------------------------------------------------\n");
364
e1 = mei_tree_new("v = 1+2*+3;");
365
printf("\nInterpret expression: \n%s\n", e1->string);
366
if (!mei_tree_builder(e1)) {
369
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
371
mei_tree_destroy(e1);
373
printf("\n------------------------------------------------------------------\n");
375
e1 = mei_tree_new("v = +1000.9 -( 0.049245 +0.0041579*Temp.C)*Temp.C;");
376
mei_tree_insert(e1, "Temp.C", 18.0);
378
printf("\nInterpret expression: \n%s\n", e1->string);
379
if (!mei_tree_builder(e1)) {
382
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
384
mei_tree_destroy(e1);
386
printf("\n------------------------------------------------------------------\n");
388
e1 = mei_tree_new("v = 1.5652e-3 + (-3.3003e-5 + 2.5135e-7*Temp-C)*Temp-C;");
389
mei_tree_insert(e1, "Temp-C", 18.0);
391
printf("\nInterpret expression: \n%s\n", e1->string);
392
if (!mei_tree_builder(e1)) {
395
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
397
mei_tree_destroy(e1);
399
printf("\n------------------------------------------------------------------\n");
401
e1 = mei_tree_new("v = 1.3806e-4 + 3.1768e-7*Temp_C;");
402
mei_tree_insert(e1, "Temp_C", 18.0);
404
printf("\nInterpret expression: \n%s\n", e1->string);
405
if (!mei_tree_builder(e1)) {
408
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
410
mei_tree_destroy(e1);
413
printf("\n------------------------------------------------------------------\n");
415
/* http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html */
417
e1 = mei_tree_new("v = ! Something && Another;");
418
mei_tree_insert(e1, "Something", 1);
419
mei_tree_insert(e1, "Another", 0);
421
printf("\nInterpret expression: \n%s\n", e1->string);
422
if (!mei_tree_builder(e1)) {
425
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
426
if (mei_tree_lookup(e1, "v")) printf("Test failed");
428
mei_tree_destroy(e1);
430
printf("\n------------------------------------------------------------------\n");
432
/* http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html */
434
e1 = mei_tree_new("v = ! (Something && Another);");
435
mei_tree_insert(e1, "Something", 1);
436
mei_tree_insert(e1, "Another", 0);
438
printf("\nInterpret expression: \n%s\n", e1->string);
439
if (!mei_tree_builder(e1)) {
442
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
443
if (!mei_tree_lookup(e1, "v")) printf("Test failed");
445
mei_tree_destroy(e1);
447
printf("\n------------------------------------------------------------------\n");
449
/* http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html */
451
e1 = mei_tree_new("v = ! a || ! b && c;");
452
mei_tree_insert(e1, "a", 1);
453
mei_tree_insert(e1, "b", 1);
454
mei_tree_insert(e1, "c", 0);
456
printf("\nInterpret expression: \n%s\n", e1->string);
457
if (!mei_tree_builder(e1)) {
460
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
461
if (mei_tree_lookup(e1, "v")) printf("Test failed");
463
mei_tree_destroy(e1);
465
printf("\n------------------------------------------------------------------\n");
467
/* http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html */
469
e1 = mei_tree_new("v = n^2 + 1 > 10 && ! n < 3;");
470
mei_tree_insert(e1, "n", 4);
472
printf("\nInterpret expression: \n%s\n", e1->string);
473
if (!mei_tree_builder(e1)) {
476
printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
477
if (!mei_tree_lookup(e1, "v")) printf("Test failed");
479
mei_tree_destroy(e1);
481
printf("\n------------------------------------------------------------------\n");
483
printf("\n--------- WARNING: this test must be the last one ----------------\n");
484
printf("\n--------- because it is corrupted the parser. ----------------\n");
486
e1 = mei_tree_new("cst = (2*(pi)/2.0); u = coos(cst); c=uu;");
487
printf("\nExpression: \n%s\n", e1->string);
488
iok = mei_tree_builder(e1);
489
mei_tree_destroy(e1);
491
printf("\n------------------------------------------------------------------\n");
493
/* Finalization of memory management */
494
_base_mem_finalize();