From 77a53d41a63f06089ecc83c84fccd7c4a42a89af Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Wed, 13 Mar 2019 15:36:46 -0700 Subject: switch to WarpXParser --- Source/Parser/wp_parser_c.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Source/Parser/wp_parser_c.h (limited to 'Source/Parser/wp_parser_c.h') diff --git a/Source/Parser/wp_parser_c.h b/Source/Parser/wp_parser_c.h new file mode 100644 index 000000000..1c55433a0 --- /dev/null +++ b/Source/Parser/wp_parser_c.h @@ -0,0 +1,16 @@ +#ifndef WP_PARSER_C_H_ +#define WP_PARSER_C_H_ + +#include "wp_parser_y.h" + +#ifdef __cplusplus +extern "C" { +#endif + + struct wp_parser* wp_c_parser_new (char const* function_body); + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.2.3 From 109e41ccb6edb85016b899943d64212430634208 Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Sat, 16 Mar 2019 17:24:47 -0700 Subject: inline parser's eval function --- Source/Parser/wp_parser_c.h | 76 +++++++++++++++++++++ Source/Parser/wp_parser_y.c | 156 +++++++++++++------------------------------- Source/Parser/wp_parser_y.h | 4 +- 3 files changed, 123 insertions(+), 113 deletions(-) (limited to 'Source/Parser/wp_parser_c.h') diff --git a/Source/Parser/wp_parser_c.h b/Source/Parser/wp_parser_c.h index 1c55433a0..91833b3b8 100644 --- a/Source/Parser/wp_parser_c.h +++ b/Source/Parser/wp_parser_c.h @@ -13,4 +13,80 @@ extern "C" { } #endif +#ifdef __cplusplus + +inline +double +wp_ast_eval (struct wp_node* node) +{ + double result; + + switch (node->type) + { + case WP_NUMBER: + result = ((struct wp_number*)node)->value; + break; + case WP_SYMBOL: + result = *(((struct wp_symbol*)node)->pointer); + break; + case WP_ADD: + result = wp_ast_eval(node->l) + wp_ast_eval(node->r); + break; + case WP_SUB: + result = wp_ast_eval(node->l) - wp_ast_eval(node->r); + break; + case WP_MUL: + result = wp_ast_eval(node->l) * wp_ast_eval(node->r); + break; + case WP_DIV: + result = wp_ast_eval(node->l) / wp_ast_eval(node->r); + break; + case WP_NEG: + result = -wp_ast_eval(node->l); + break; + case WP_F1: + result = wp_call_f1(((struct wp_f1*)node)->ftype, + wp_ast_eval(((struct wp_f1*)node)->l)); + break; + case WP_F2: + result = wp_call_f2(((struct wp_f2*)node)->ftype, + wp_ast_eval(((struct wp_f2*)node)->l), + wp_ast_eval(((struct wp_f2*)node)->r)); + break; + case WP_ADD_VP: + result = node->lvp.v + *(node->rp); + break; + case WP_ADD_PP: + result = *(node->lvp.p) + *(node->rp); + break; + case WP_SUB_VP: + result = node->lvp.v - *(node->rp); + break; + case WP_SUB_PP: + result = *(node->lvp.p) - *(node->rp); + break; + case WP_MUL_VP: + result = node->lvp.v * *(node->rp); + break; + case WP_MUL_PP: + result = *(node->lvp.p) * *(node->rp); + break; + case WP_DIV_VP: + result = node->lvp.v / *(node->rp); + break; + case WP_DIV_PP: + result = *(node->lvp.p) / *(node->rp); + break; + case WP_NEG_P: + result = -*(node->lvp.p); + break; + default: + yyerror("wp_ast_eval: unknown node type %d\n", node->type); + } + + return result; +} + +#endif + #endif diff --git a/Source/Parser/wp_parser_y.c b/Source/Parser/wp_parser_y.c index 21b6cc96e..ac752a948 100644 --- a/Source/Parser/wp_parser_y.c +++ b/Source/Parser/wp_parser_y.c @@ -145,12 +145,10 @@ wp_parser_dup (struct wp_parser* source) return dest; } -static double -wp_call_f1 (struct wp_f1* f1) +wp_call_f1 (enum wp_f1_t type, double a) { - double a = wp_ast_eval(f1->l); - switch (f1->ftype) { + switch (type) { case WP_SQRT: return sqrt(a); case WP_EXP: return exp(a); case WP_LOG: return log(a); @@ -172,18 +170,15 @@ wp_call_f1 (struct wp_f1* f1) case WP_POW_P2: return a*a; case WP_POW_P3: return a*a*a; default: - yyerror("wp_call_f1: Unknow function %d", f1->ftype); + yyerror("wp_call_f1: Unknow function %d", type); return 0.0; } } -static double -wp_call_f2 (struct wp_f2* f2) +wp_call_f2 (enum wp_f2_t type, double a, double b) { - double a = wp_ast_eval(f2->l); - double b = wp_ast_eval(f2->r); - switch (f2->ftype) { + switch (type) { case WP_POW: return pow(a,b); case WP_GT: @@ -197,79 +192,11 @@ wp_call_f2 (struct wp_f2* f2) case WP_MAX: return (a > b) ? a : b; default: - yyerror("wp_call_f2: Unknow function %d", f2->ftype); + yyerror("wp_call_f2: Unknow function %d", type); return 0.0; } } -double -wp_ast_eval (struct wp_node* node) -{ - double result; - - switch (node->type) - { - case WP_NUMBER: - result = ((struct wp_number*)node)->value; - break; - case WP_SYMBOL: - result = *(((struct wp_symbol*)node)->pointer); - break; - case WP_ADD: - result = wp_ast_eval(node->l) + wp_ast_eval(node->r); - break; - case WP_SUB: - result = wp_ast_eval(node->l) - wp_ast_eval(node->r); - break; - case WP_MUL: - result = wp_ast_eval(node->l) * wp_ast_eval(node->r); - break; - case WP_DIV: - result = wp_ast_eval(node->l) / wp_ast_eval(node->r); - break; - case WP_NEG: - result = -wp_ast_eval(node->l); - break; - case WP_F1: - result = wp_call_f1((struct wp_f1*)node); - break; - case WP_F2: - result = wp_call_f2((struct wp_f2*)node); - break; - case WP_ADD_VP: - result = node->lvp.v + *(node->rp); - break; - case WP_ADD_PP: - result = *(node->lvp.p) + *(node->rp); - break; - case WP_SUB_VP: - result = node->lvp.v - *(node->rp); - break; - case WP_SUB_PP: - result = *(node->lvp.p) - *(node->rp); - break; - case WP_MUL_VP: - result = node->lvp.v * *(node->rp); - break; - case WP_MUL_PP: - result = *(node->lvp.p) * *(node->rp); - break; - case WP_DIV_VP: - result = node->lvp.v / *(node->rp); - break; - case WP_DIV_PP: - result = *(node->lvp.p) / *(node->rp); - break; - case WP_NEG_P: - result = -*(node->lvp.p); - break; - default: - yyerror("wp_ast_eval: unknown node type %d\n", node->type); - } - - return result; -} - size_t wp_ast_size (struct wp_node* node) { @@ -451,14 +378,14 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_SYMBOL) { - node->lvp.v = wp_ast_eval(node->l); + node->lvp.v = ((struct wp_number*)(node->l))->value; node->rp = ((struct wp_symbol*)(node->r))->pointer; node->type = WP_ADD_VP; } else if (node->l->type == WP_SYMBOL && node->r->type == WP_NUMBER) { - node->lvp.v = wp_ast_eval(node->r); + node->lvp.v = ((struct wp_number*)(node->r))->value; node->rp = ((struct wp_symbol*)(node->l))->pointer; node->r = node->l; node->type = WP_ADD_VP; @@ -473,28 +400,28 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_ADD_VP) { - double v = wp_ast_eval(node->l) + WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value + WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_ADD_VP; } else if (node->l->type == WP_NUMBER && node->r->type == WP_SUB_VP) { - double v = wp_ast_eval(node->l) + WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value + WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_SUB_VP; } else if (node->l->type == WP_ADD_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) + wp_ast_eval(node->r); + double v = WP_EVAL_L(node) + ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_ADD_VP; } else if (node->l->type == WP_SUB_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) + wp_ast_eval(node->r); + double v = WP_EVAL_L(node) + ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_SUB_VP; } @@ -514,14 +441,14 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_SYMBOL) { - node->lvp.v = wp_ast_eval(node->l); + node->lvp.v = ((struct wp_number*)(node->l))->value; node->rp = ((struct wp_symbol*)(node->r))->pointer; node->type = WP_SUB_VP; } else if (node->l->type == WP_SYMBOL && node->r->type == WP_NUMBER) { - node->lvp.v = -wp_ast_eval(node->r); + node->lvp.v = -((struct wp_number*)(node->r))->value; node->rp = ((struct wp_symbol*)(node->l))->pointer; node->r = node->l; node->type = WP_ADD_VP; @@ -536,28 +463,28 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_ADD_VP) { - double v = wp_ast_eval(node->l) - WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value - WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_SUB_VP; } else if (node->l->type == WP_NUMBER && node->r->type == WP_SUB_VP) { - double v = wp_ast_eval(node->l) - WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value - WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_ADD_VP; } else if (node->l->type == WP_ADD_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) - wp_ast_eval(node->r); + double v = WP_EVAL_L(node) - ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_ADD_VP; } else if (node->l->type == WP_SUB_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) - wp_ast_eval(node->r); + double v = WP_EVAL_L(node) - ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_SUB_VP; } @@ -577,14 +504,14 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_SYMBOL) { - node->lvp.v = wp_ast_eval(node->l); + node->lvp.v = ((struct wp_number*)(node->l))->value; node->rp = ((struct wp_symbol*)(node->r))->pointer; node->type = WP_MUL_VP; } else if (node->l->type == WP_SYMBOL && node->r->type == WP_NUMBER) { - node->lvp.v = wp_ast_eval(node->r); + node->lvp.v = ((struct wp_number*)(node->r))->value; node->rp = ((struct wp_symbol*)(node->l))->pointer; node->r = node->l; node->type = WP_MUL_VP; @@ -599,28 +526,28 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_MUL_VP) { - double v = wp_ast_eval(node->l) * WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value * WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_MUL_VP; } else if (node->l->type == WP_NUMBER && node->r->type == WP_DIV_VP) { - double v = wp_ast_eval(node->l) * WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value * WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_DIV_VP; } else if (node->l->type == WP_MUL_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) * wp_ast_eval(node->r); + double v = WP_EVAL_L(node) * ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_MUL_VP; } else if (node->l->type == WP_DIV_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) * wp_ast_eval(node->r); + double v = WP_EVAL_L(node) * ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_DIV_VP; } @@ -640,14 +567,14 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_SYMBOL) { - node->lvp.v = wp_ast_eval(node->l); + node->lvp.v = ((struct wp_number*)(node->l))->value; node->rp = ((struct wp_symbol*)(node->r))->pointer; node->type = WP_DIV_VP; } else if (node->l->type == WP_SYMBOL && node->r->type == WP_NUMBER) { - node->lvp.v = 1./wp_ast_eval(node->r); + node->lvp.v = 1./((struct wp_number*)(node->r))->value; node->rp = ((struct wp_symbol*)(node->l))->pointer; node->r = node->l; node->type = WP_MUL_VP; @@ -662,28 +589,28 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_MUL_VP) { - double v = wp_ast_eval(node->l) / WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value / WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_DIV_VP; } else if (node->l->type == WP_NUMBER && node->r->type == WP_DIV_VP) { - double v = wp_ast_eval(node->l) / WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value / WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_MUL_VP; } else if (node->l->type == WP_MUL_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) / wp_ast_eval(node->r); + double v = WP_EVAL_L(node) / ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_MUL_VP; } else if (node->l->type == WP_DIV_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) / wp_ast_eval(node->r); + double v = WP_EVAL_L(node) / ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_DIV_VP; } @@ -726,7 +653,9 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->l); if (node->l->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = wp_call_f1 + (((struct wp_f1*)node)->ftype, + ((struct wp_number*)(((struct wp_f1*)node)->l))->value); ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -737,14 +666,17 @@ wp_ast_optimize (struct wp_node* node) if (node->l->type == WP_NUMBER && node->r->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = wp_call_f2 + (((struct wp_f2*)node)->ftype, + ((struct wp_number*)(((struct wp_f2*)node)->l))->value, + ((struct wp_number*)(((struct wp_f2*)node)->r))->value); ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } else if (node->r->type == WP_NUMBER && ((struct wp_f2*)node)->ftype == WP_POW) { struct wp_node* n = node->l; - double v = wp_ast_eval(node->r); + double v = ((struct wp_number*)(node->r))->value; if (-3.0 == v) { ((struct wp_f1*)node)->type = WP_F1; ((struct wp_f1*)node)->l = n; @@ -779,7 +711,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->r); if (node->r->type == WP_NUMBER) { - double v = node->lvp.v + wp_ast_eval(node->r); + double v = node->lvp.v + ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -788,7 +720,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->r); if (node->r->type == WP_NUMBER) { - double v = node->lvp.v - wp_ast_eval(node->r); + double v = node->lvp.v - ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -797,7 +729,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->r); if (node->r->type == WP_NUMBER) { - double v = node->lvp.v * wp_ast_eval(node->r); + double v = node->lvp.v * ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -806,7 +738,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->r); if (node->r->type == WP_NUMBER) { - double v = node->lvp.v / wp_ast_eval(node->r); + double v = node->lvp.v / ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -815,7 +747,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->l); if (node->l->type == WP_NUMBER) { - double v = -wp_ast_eval(node->l); + double v = -((struct wp_number*)(node->l))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -893,7 +825,7 @@ wp_ast_print (struct wp_node* node) switch (node->type) { case WP_NUMBER: - printf("NUMBER: %.17g\n", wp_ast_eval(node)); + printf("NUMBER: %.17g\n", ((struct wp_number*)node)->value); break; case WP_SYMBOL: printf("VARIABLE: %s\n", ((struct wp_symbol*)node)->name); diff --git a/Source/Parser/wp_parser_y.h b/Source/Parser/wp_parser_y.h index 9c1a0d448..c583d1a33 100644 --- a/Source/Parser/wp_parser_y.h +++ b/Source/Parser/wp_parser_y.h @@ -143,13 +143,15 @@ void wp_parser_regvar (struct wp_parser* parser, char const* name, double* p); void wp_parser_setconst (struct wp_parser* parser, char const* name, double c); /* We need to walk the tree in these functions */ -double wp_ast_eval (struct wp_node* node); void wp_ast_optimize (struct wp_node* node); size_t wp_ast_size (struct wp_node* node); void wp_ast_print (struct wp_node* node); void wp_ast_regvar (struct wp_node* node, char const* name, double* p); void wp_ast_setconst (struct wp_node* node, char const* name, double c); +double wp_call_f1 (enum wp_f1_t type, double a); +double wp_call_f2 (enum wp_f2_t type, double a, double b); + #ifdef __cplusplus } #endif -- cgit v1.2.3 From 26ae866f50576380a75e9876bc836ab1161ad584 Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Thu, 21 Mar 2019 21:04:55 -0700 Subject: use parser's setConstant function instead of using string replacement --- .../laser_acceleration/inputs.2d.boost | 8 ++-- .../plasma_acceleration/inputs.2d.boost | 6 +-- .../plasma_acceleration/inputs.3d.boost | 6 +-- .../Physics_applications/plasma_mirror/inputs.2d | 9 ++-- Examples/Tests/Langmuir/inputs.multi.2d.rt | 6 +-- Examples/Tests/Langmuir/inputs.multi.rt | 6 +-- Source/Initialization/PlasmaInjector.cpp | 51 ++++++++++++++++++---- Source/Laser/LaserParticleContainer.cpp | 30 ++++++++----- Source/Parser/WarpXParser.H | 4 +- Source/Parser/WarpXParser.cpp | 12 +++++ Source/Parser/wp_parser_c.h | 47 ++++++++++++++++++++ 11 files changed, 147 insertions(+), 38 deletions(-) (limited to 'Source/Parser/wp_parser_c.h') diff --git a/Examples/Physics_applications/laser_acceleration/inputs.2d.boost b/Examples/Physics_applications/laser_acceleration/inputs.2d.boost index 57213e067..e7a79652b 100644 --- a/Examples/Physics_applications/laser_acceleration/inputs.2d.boost +++ b/Examples/Physics_applications/laser_acceleration/inputs.2d.boost @@ -48,9 +48,11 @@ warpx.dt_snapshots_lab = 1.6678204759907604e-12 ################################# ############ PLASMA ############# ################################# -constants.use_my_constants = 1 -constants.constant_names = zmax rc rmax n0 kp -constants.constant_values = 30.e-2 50.e-6 110.e-6 3.5e24 353352. +my_constants.zmax = 30.e-2 +my_constants.rc = 50.e-6 +my_constants.rmax = 110.e-6 +my_constants.n0 = 3.5e24 +my_constants.kp = 353352. particles.nspecies = 3 particles.species_names = electrons ions beam diff --git a/Examples/Physics_applications/plasma_acceleration/inputs.2d.boost b/Examples/Physics_applications/plasma_acceleration/inputs.2d.boost index 39995f6f9..adeedaba9 100644 --- a/Examples/Physics_applications/plasma_acceleration/inputs.2d.boost +++ b/Examples/Physics_applications/plasma_acceleration/inputs.2d.boost @@ -31,9 +31,9 @@ warpx.cfl = .99 warpx.do_moving_window = 1 warpx.moving_window_dir = z warpx.moving_window_v = 1. # in units of the speed of light -constants.use_my_constants = 1 -constants.constant_names = lramp pi dens -constants.constant_values = 8.e-3 3.141592653589793 1e+23 +my_constants.lramp = 8.e-3 +my_constants.pi = 3.141592653589793 +my_constants.dens = 1e+23 interpolation.nox = 3 interpolation.noy = 3 interpolation.noz = 3 diff --git a/Examples/Physics_applications/plasma_acceleration/inputs.3d.boost b/Examples/Physics_applications/plasma_acceleration/inputs.3d.boost index cc4a2cce2..5f1ef1a2e 100644 --- a/Examples/Physics_applications/plasma_acceleration/inputs.3d.boost +++ b/Examples/Physics_applications/plasma_acceleration/inputs.3d.boost @@ -30,9 +30,9 @@ warpx.cfl = .99 warpx.do_moving_window = 1 warpx.moving_window_dir = z warpx.moving_window_v = 1. # in units of the speed of light -constants.use_my_constants = 1 -constants.constant_names = lramp pi dens -constants.constant_values = 8.e-3 3.141592653589793 1e+23 +my_constants.lramp = 8.e-3 +my_constants.pi = 3.141592653589793 +my_constants.dens = 1e+23 interpolation.nox = 3 interpolation.noy = 3 interpolation.noz = 3 diff --git a/Examples/Physics_applications/plasma_mirror/inputs.2d b/Examples/Physics_applications/plasma_mirror/inputs.2d index 40e38da0c..2a1fb454a 100644 --- a/Examples/Physics_applications/plasma_mirror/inputs.2d +++ b/Examples/Physics_applications/plasma_mirror/inputs.2d @@ -25,9 +25,12 @@ algo.particle_pusher = 0 interpolation.nox = 3 interpolation.noy = 3 interpolation.noz = 3 -constants.use_my_constants = 1 -constants.constant_names = zc zp lgrad nc zp2 zc2 -constants.constant_values = 20.e-6 20.05545177444479562e-6 .08e-6 1.74e27 24.e-6 24.05545177444479562e-6 +my_constants.zc = 20.e-6 +my_constants.zp = 20.05545177444479562e-6 +my_constants.lgrad = .08e-6 +my_constants.nc = 1.74e27 +my_constants.zp2 = 24.e-6 +my_constants.zc2 = 24.05545177444479562e-6 warpx.cfl = 1.0 warpx.do_dynamic_scheduling = 0 warpx.load_balance_int = 66 diff --git a/Examples/Tests/Langmuir/inputs.multi.2d.rt b/Examples/Tests/Langmuir/inputs.multi.2d.rt index ce82fd1c7..79bc2b383 100644 --- a/Examples/Tests/Langmuir/inputs.multi.2d.rt +++ b/Examples/Tests/Langmuir/inputs.multi.2d.rt @@ -39,9 +39,9 @@ interpolation.noz = 1 warpx.cfl = 1.0 # Parameters for the plasma wave -constants.use_my_constants = 1 -constants.constant_names = epsilon kp k -constants.constant_values = 0.01 376357.71524190728 314159.2653589793 +my_constants.epsilon = 0.01 +my_constants.kp = 376357.71524190728 +my_constants.k = 314159.2653589793 # Note: kp is calculated in SI for a density of 4e24 (i.e. 2e24 electrons + 2e24 positrons) # k is calculated so as to have 2 periods within the 40e-6 wide box. diff --git a/Examples/Tests/Langmuir/inputs.multi.rt b/Examples/Tests/Langmuir/inputs.multi.rt index d2b648159..185051412 100644 --- a/Examples/Tests/Langmuir/inputs.multi.rt +++ b/Examples/Tests/Langmuir/inputs.multi.rt @@ -39,9 +39,9 @@ interpolation.noz = 1 warpx.cfl = 1.0 # Parameters for the plasma wave -constants.use_my_constants = 1 -constants.constant_names = epsilon kp k -constants.constant_values = 0.01 376357.71524190728 314159.2653589793 +my_constants.epsilon = 0.01 +my_constants.kp = 376357.71524190728 +my_constants.k = 314159.2653589793 # Note: kp is calculated in SI for a density of 4e24 (i.e. 2e24 electrons + 2e24 positrons) # k is calculated so as to have 2 periods within the 40e-6 wide box. diff --git a/Source/Initialization/PlasmaInjector.cpp b/Source/Initialization/PlasmaInjector.cpp index 58211fb56..3d3edf715 100644 --- a/Source/Initialization/PlasmaInjector.cpp +++ b/Source/Initialization/PlasmaInjector.cpp @@ -1,6 +1,7 @@ #include "PlasmaInjector.H" #include +#include #include #include @@ -72,10 +73,26 @@ CustomDensityProfile::CustomDensityProfile(const std::string& species_name) ParseDensityProfile::ParseDensityProfile(std::string parse_density_function) : _parse_density_function(parse_density_function) { - my_constants.ReadParameters(); - parse_density_function = my_constants.replaceStringValue(parse_density_function); parser_density.define(parse_density_function); parser_density.registerVariables({"x","y","z"}); + + ParmParse pp("my_constants"); + std::set symbols = parser_density.symbols(); + symbols.erase("x"); + symbols.erase("y"); + symbols.erase("z"); // after removing variables, we are left with constants + for (auto it = symbols.begin(); it != symbols.end(); ) { + Real v; + if (pp.query(it->c_str(), v)) { + parser_density.setConstant(*it, v); + it = symbols.erase(it); + } else { + ++it; + } + } + for (auto const& s : symbols) { // make sure there no unknown symbols + amrex::Abort("ParseDensityProfile: Unknown symbol "+s); + } } Real ParseDensityProfile::getDensity(Real x, Real y, Real z) const @@ -137,16 +154,32 @@ ParseMomentumFunction::ParseMomentumFunction(std::string parse_momentum_function _parse_momentum_function_uy(parse_momentum_function_uy), _parse_momentum_function_uz(parse_momentum_function_uz) { - my_constants.ReadParameters(); - parse_momentum_function_ux = my_constants.replaceStringValue(parse_momentum_function_ux); - parse_momentum_function_uy = my_constants.replaceStringValue(parse_momentum_function_uy); - parse_momentum_function_uz = my_constants.replaceStringValue(parse_momentum_function_uz); parser_ux.define(parse_momentum_function_ux); parser_uy.define(parse_momentum_function_uy); parser_uz.define(parse_momentum_function_uz); - parser_ux.registerVariables({"x","y","z"}); - parser_uy.registerVariables({"x","y","z"}); - parser_uz.registerVariables({"x","y","z"}); + + amrex::Array,3> parsers{parser_ux, parser_uy, parser_uz}; + ParmParse pp("my_constants"); + for (auto& p : parsers) { + auto& parser = p.get(); + parser.registerVariables({"x","y","z"}); + std::set symbols = parser.symbols(); + symbols.erase("x"); + symbols.erase("y"); + symbols.erase("z"); // after removing variables, we are left with constants + for (auto it = symbols.begin(); it != symbols.end(); ) { + Real v; + if (pp.query(it->c_str(), v)) { + parser.setConstant(*it, v); + it = symbols.erase(it); + } else { + ++it; + } + } + for (auto const& s : symbols) { // make sure there no unknown symbols + amrex::Abort("ParseMomentumFunction: Unknown symbol "+s); + } + } } void ParseMomentumFunction::getMomentum(vec3& u, Real x, Real y, Real z) diff --git a/Source/Laser/LaserParticleContainer.cpp b/Source/Laser/LaserParticleContainer.cpp index ec6e86be1..3ef1be154 100644 --- a/Source/Laser/LaserParticleContainer.cpp +++ b/Source/Laser/LaserParticleContainer.cpp @@ -74,10 +74,26 @@ LaserParticleContainer::LaserParticleContainer (AmrCore* amr_core, int ispecies) if ( profile == laser_t::parse_field_function ) { // Parse the properties of the parse_field_function profile pp.get("field_function(X,Y,t)", field_function); - // User-defined constants: replace names by value - my_constants.ReadParameters(); - field_function = my_constants.replaceStringValue(field_function); parser.define(field_function); + parser.registerVariables({"X","Y","t"}); + + ParmParse pp("my_constants"); + std::set symbols = parser.symbols(); + symbols.erase("X"); + symbols.erase("Y"); + symbols.erase("t"); // after removing variables, we are left with constants + for (auto it = symbols.begin(); it != symbols.end(); ) { + Real v; + if (pp.query(it->c_str(), v)) { + parser.setConstant(*it, v); + it = symbols.erase(it); + } else { + ++it; + } + } + for (auto const& s : symbols) { // make sure there no unknown symbols + amrex::Abort("Laser Profile: Unknown symbol "+s); + } } // Plane normal @@ -393,14 +409,8 @@ LaserParticleContainer::Evolve (int lev, } if (profile == laser_t::parse_field_function) { - Real parser_X, parser_Y, parser_t; - parser.registerVariable("X", parser_X); - parser.registerVariable("Y", parser_Y); - parser.registerVariable("t", t); for (int i = 0; i < np; ++i) { - parser_X = plane_Xp[i]; - parser_Y = plane_Yp[i]; - amplitude_E[i] = parser.eval(); + amplitude_E[i] = parser.eval(plane_Xp[i], plane_Yp[i], t); } } // Calculate the corresponding momentum and position for the particles diff --git a/Source/Parser/WarpXParser.H b/Source/Parser/WarpXParser.H index 17e87a165..046491e29 100644 --- a/Source/Parser/WarpXParser.H +++ b/Source/Parser/WarpXParser.H @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include "wp_parser_c.h" #include "wp_parser_y.h" @@ -44,6 +44,8 @@ public: std::string const& expr () const; + std::set symbols () const; + private: void clear (); diff --git a/Source/Parser/WarpXParser.cpp b/Source/Parser/WarpXParser.cpp index ea15759c2..8c800215f 100644 --- a/Source/Parser/WarpXParser.cpp +++ b/Source/Parser/WarpXParser.cpp @@ -136,3 +136,15 @@ WarpXParser::expr () const { return m_expression; } + +std::set +WarpXParser::symbols () const +{ + std::set results; +#ifdef _OPENMP + wp_ast_get_symbols(m_parser[omp_get_thread_num()]->ast, results); +#else + wp_ast_get_symbols(m_parser->ast, results); +#endif + return results; +} diff --git a/Source/Parser/wp_parser_c.h b/Source/Parser/wp_parser_c.h index 91833b3b8..d810bd685 100644 --- a/Source/Parser/wp_parser_c.h +++ b/Source/Parser/wp_parser_c.h @@ -15,6 +15,9 @@ extern "C" { #ifdef __cplusplus +#include +#include + inline double wp_ast_eval (struct wp_node* node) @@ -87,6 +90,50 @@ wp_ast_eval (struct wp_node* node) return result; } +inline +void +wp_ast_get_symbols (struct wp_node* node, std::set& symbols) +{ + switch (node->type) + { + case WP_NUMBER: + break; + case WP_SYMBOL: + symbols.emplace(((struct wp_symbol*)node)->name); + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + wp_ast_get_symbols(node->l, symbols); + wp_ast_get_symbols(node->r, symbols); + break; + case WP_NEG: + case WP_NEG_P: + wp_ast_get_symbols(node->l, symbols); + break; + case WP_F1: + wp_ast_get_symbols(((struct wp_f1*)node)->l, symbols); + break; + case WP_F2: + wp_ast_get_symbols(((struct wp_f2*)node)->l, symbols); + wp_ast_get_symbols(((struct wp_f2*)node)->r, symbols); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + wp_ast_get_symbols(node->r, symbols); + break; + default: + yyerror("wp_ast_get_symbols: unknown node type %d\n", node->type); + } +} + #endif #endif -- cgit v1.2.3