Skip to content

Commit fbe6efd

Browse files
authored
Merge pull request #9 from dragos-ana/fix-function-evaluation
Fix function evaluation
2 parents b068c65 + 5383653 commit fbe6efd

File tree

4 files changed

+27
-40
lines changed

4 files changed

+27
-40
lines changed

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ numpy
1111
# include fourcipp from github
1212
git+https://github.com/4C-multiphysics/fourcipp.git
1313
lnmmeshio>=5.6.3
14+
numexpr
1415

1516
# development
1617
pytest

requirements.txt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
aiohappyeyeballs==2.6.1
88
# via aiohttp
9-
aiohttp==3.11.18
9+
aiohttp==3.12.2
1010
# via wslink
1111
aiosignal==1.3.2
1212
# via aiohttp
@@ -34,15 +34,15 @@ distlib==0.3.9
3434
# via virtualenv
3535
filelock==3.18.0
3636
# via virtualenv
37-
fonttools==4.57.0
37+
fonttools==4.58.0
3838
# via matplotlib
3939
fourcipp @ git+https://github.com/4C-multiphysics/fourcipp.git
4040
# via -r requirements.in
4141
frozenlist==1.6.0
4242
# via
4343
# aiohttp
4444
# aiosignal
45-
identify==2.6.10
45+
identify==2.6.12
4646
# via pre-commit
4747
idna==3.10
4848
# via
@@ -68,7 +68,7 @@ loguru==0.7.3
6868
# lnmmeshio
6969
markdown-it-py==3.0.0
7070
# via rich
71-
matplotlib==3.10.1
71+
matplotlib==3.10.3
7272
# via
7373
# pyvista
7474
# vtk
@@ -80,14 +80,16 @@ more-itertools==10.7.0
8080
# via trame-server
8181
msgpack==1.1.0
8282
# via wslink
83-
multidict==6.4.3
83+
multidict==6.4.4
8484
# via
8585
# aiohttp
8686
# yarl
87-
narwhals==1.38.0
87+
narwhals==1.41.0
8888
# via plotly
8989
nodeenv==1.9.1
9090
# via pre-commit
91+
numexpr==2.10.2
92+
# via -r requirements.in
9193
numpy==2.2.5
9294
# via
9395
# -r requirements.in
@@ -96,6 +98,7 @@ numpy==2.2.5
9698
# lnmmeshio
9799
# matplotlib
98100
# meshio
101+
# numexpr
99102
# pandas
100103
# pyvista
101104
packaging==25.0
@@ -116,7 +119,7 @@ platformdirs==4.3.8
116119
# via
117120
# pooch
118121
# virtualenv
119-
plotly==6.0.1
122+
plotly==6.1.2
120123
# via
121124
# -r requirements.in
122125
# trame-plotly
@@ -144,15 +147,14 @@ python-utils==3.9.1
144147
# via lnmmeshio
145148
pytz==2025.2
146149
# via pandas
147-
pyvista==0.45.0
150+
pyvista==0.45.2
148151
# via -r requirements.in
149152
pyyaml==6.0.2
150-
# via trame
151-
rapidyaml==0.9.0
152153
# via
153154
# lnmmeshio
154155
# pre-commit
155156
# trame
157+
rapidyaml==0.9.0
156158
# via fourcipp
157159
referencing==0.36.2
158160
# via
@@ -180,16 +182,16 @@ six==1.17.0
180182
# via python-dateutil
181183
tqdm==4.66.5
182184
# via lnmmeshio
183-
trame==3.9.0
185+
trame==3.9.1
184186
# via -r requirements.in
185-
trame-client==3.8.2
187+
trame-client==3.9.0
186188
# via
187189
# trame
188190
# trame-components
189191
# trame-plotly
190192
# trame-vtk
191193
# trame-vuetify
192-
trame-common==0.2.0
194+
trame-common==1.0.0
193195
# via
194196
# trame
195197
# trame-client
@@ -219,7 +221,7 @@ vtk==9.4.2
219221
# via
220222
# -r requirements.in
221223
# pyvista
222-
wslink==2.3.3
224+
wslink==2.3.4
223225
# via
224226
# trame
225227
# trame-server

src/fourc_webviewer/input_file_utils/fourc_yaml_file_visualization.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from pathlib import Path
66

77
import lnmmeshio
8+
import numexpr as ne
89
import numpy as np
910
import plotly.express as px
1011

1112
from fourc_webviewer.input_file_utils.io_utils import (
1213
add_fourc_yaml_file_data_to_dis,
13-
safely_parse_string,
1414
)
1515

1616

@@ -161,14 +161,20 @@ def funct_using_eval(x, y, z, t):
161161
# replace the used power sign
162162
funct_string_copy = funct_string_copy.replace("^", "**")
163163

164+
# replace variables
165+
funct_string_copy = (
166+
funct_string_copy.replace("x", str(x))
167+
.replace("y", str(y))
168+
.replace("z", str(z))
169+
.replace("t", str(t))
170+
)
171+
164172
# for heaviside: np.heaviside takes two arguments -> second argument denotes the function value at the first argument -> we set it by default to 0
165173
funct_string_copy = re.sub(
166174
r"heaviside\((.*?)\)", r"heaviside(\1,0)", funct_string_copy
167175
) # usage of raw strings, (.*?) is a non greedy capturing, and \1 replaces the captured value
168176

169-
return safely_parse_string(
170-
funct_string_copy
171-
) # this parses string in as a function
177+
return ne.evaluate(funct_string_copy) # this parses string in as a function
172178

173179
return np.frompyfunc(funct_using_eval, 4, 1)
174180

src/fourc_webviewer/input_file_utils/io_utils.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -398,25 +398,3 @@ def get_master_and_linked_material_indices(materials_section):
398398
"master_mat_indices": master_mat_indices,
399399
"linked_mat_indices": linked_mat_indices,
400400
}
401-
402-
403-
def safely_parse_string(string):
404-
"""Safely evaluate a string containing a Python literal expression.
405-
406-
This function attempts to parse the input string using ast.literal_eval,
407-
which safely evaluates strings containing Python literals such as strings,
408-
numbers, tuples, lists, dicts, booleans, and None.
409-
410-
Parameters:
411-
string (str): The string to evaluate.
412-
413-
Returns:
414-
The evaluated Python object.
415-
416-
Raises:
417-
ValueError: If the input string is not a valid Python literal.
418-
"""
419-
try:
420-
return ast.literal_eval(string)
421-
except (ValueError, SyntaxError):
422-
raise ValueError("Invalid input for literal_eval")

0 commit comments

Comments
 (0)