diff options
author | 2020-10-12 13:10:51 -0700 | |
---|---|---|
committer | 2020-10-12 13:10:51 -0700 | |
commit | 57eb81fa614dbf15de72a355fad9bf4f557a2439 (patch) | |
tree | 5d624d2247ea9983d8463b1ecd6c732335013b9c /Python/pywarpx/Constants.py | |
parent | bd0d79ffb8778a753b8825537510a49a0f977fca (diff) | |
download | WarpX-57eb81fa614dbf15de72a355fad9bf4f557a2439.tar.gz WarpX-57eb81fa614dbf15de72a355fad9bf4f557a2439.tar.zst WarpX-57eb81fa614dbf15de72a355fad9bf4f557a2439.zip |
picmi - do name mangling on expression variables to ensure their uniqueness (#1361)
Co-authored-by: Remi Lehe <remi.lehe@normalesup.org>
Diffstat (limited to 'Python/pywarpx/Constants.py')
-rw-r--r-- | Python/pywarpx/Constants.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Python/pywarpx/Constants.py b/Python/pywarpx/Constants.py index 93c45c92e..1ae1dda0d 100644 --- a/Python/pywarpx/Constants.py +++ b/Python/pywarpx/Constants.py @@ -4,6 +4,8 @@ # # License: BSD-3-Clause-LBNL +import re + from .Bucket import Bucket class Constants(Bucket): @@ -19,4 +21,33 @@ class Constants(Bucket): assert self.argvattrs[name] == value, Exception('Inconsistent values given for user defined constants') Bucket.__setattr__(self, name, value) + def add_keywords(self, kwdict): + mangle_dict = {} + for k,v in kwdict.items(): + # Check if keyword has already been defined + # WarpX has a single global dictionary of expression variables so each + # variable must be unique + if k in self.argvattrs: + # if so, mangle the name by appending a numerical suffix + mangle_number = 1 + k_mangled = f'{k}{mangle_number}' + while k_mangled in self.argvattrs: + # make sure that the mangled name has also not already been defined + mangle_number += 1 + k_mangled = f'{k}{mangle_number}' + mangle_dict[k] = k_mangled + k = k_mangled + setattr(self, k, v) + return mangle_dict + + def mangle_expression(self, expression, mangle_dict): + if expression is None: + return None + # For each key in mangle_dict, modify the expression replacing + # the key with its value, the mangled version of key + for k,v in mangle_dict.items(): + expression = re.sub(r'\b%s\b'%k, v, expression) + return expression + + my_constants = Constants() |