Skip to content

Commit 46684a2

Browse files
committed
Avoid matching on variable values
The default values in the C++ version may change over time, so it is best to avoid matching on those to ensure that the Python version doesn't need fine-grain updates when the C++ changes.
1 parent 83c74dd commit 46684a2

1 file changed

Lines changed: 74 additions & 29 deletions

File tree

parametric_plasma_source/plasma.py

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,45 @@ def ion_density_pedistal(self, ion_density_pedistal):
249249
else:
250250
self._ion_density_pedistal = ion_density_pedistal
251251

252-
252+
def replace_variable_value(
253+
self, input_strings, variable_name, new_value, is_cpp=True
254+
):
255+
"""Replaces the value assigned to a variable with the provided value
256+
257+
param: input_strings: The list of strings to search for the variable.
258+
type input_strings: List[str]
259+
260+
param: variable_name: The name of the variable to search for.
261+
type variable_name: str
262+
263+
param: new_value: The value to set the variable to.
264+
type new_value: Union[str, float, int]
265+
266+
param is_cpp: Whether the variable is a C++ variable.
267+
Optional, by default True.
268+
type is_cpp: bool
269+
270+
raises ValueError: variable_name was not found in input_strings.
271+
"""
272+
if is_cpp:
273+
strs_to_find = ("const", variable_name, "=", ";")
274+
else:
275+
strs_to_find = (variable_name, "=")
276+
for idx, string in enumerate(input_strings):
277+
if all(str_to_find in string for str_to_find in strs_to_find):
278+
equals_idx = string.find("=")
279+
if equals_idx >= 0:
280+
if is_cpp:
281+
new_value = f"{new_value};"
282+
input_strings[idx] = string.replace(
283+
input_strings[idx][equals_idx:], f" = {new_value}"
284+
)
285+
break
286+
else:
287+
file_content = "\n".join(input_strings)
288+
raise ValueError(
289+
f"{variable_name} string not found in {file_content}"
290+
)
253291

254292
def export_plasma_source(self, output_filename):
255293
"""Writes and compiles custom plasma source for the reactor
@@ -267,45 +305,47 @@ def export_plasma_source(self, output_filename):
267305

268306
Path(output_filename).parent.mkdir(parents=True, exist_ok=True)
269307

270-
editted_plasma_make_file = self.plasma_make_file.replace('OPENMC_DIR = /opt/openmc', 'OPENMC_DIR = '+self.openmc_install_directory)
308+
plasma_make_file_lines = self.plasma_make_file.split("\n")
309+
self.replace_variable_value(
310+
plasma_make_file_lines, "OPENMC_DIR", self.openmc_install_directory, False
311+
)
312+
271313
with open(temp_folder/'Makefile', "w") as text_file:
272-
text_file.write(editted_plasma_make_file)
314+
text_file.write("\n".join(plasma_make_file_lines))
273315

274316
with open(temp_folder/'plasma_source.cpp', "w") as text_file:
275317
text_file.write(self.plasma_source_cpp_file)
276318

277319
with open(temp_folder/'plasma_source.hpp', "w") as text_file:
278320
text_file.write(self.plasma_source_hpp_file)
279321

280-
plasma_varibles = [
281-
('const double ion_density_pedistal = 1.09e+20', 'const double ion_density_pedistal = ' + str(self.ion_density_pedistal)),
282-
('const double ion_density_seperatrix = 3e+19', 'const double ion_density_seperatrix = ' + str(self.ion_density_seperatrix)),
283-
('const double ion_density_origin = 1.09e+20', 'const double ion_density_origin = ' + str(self.ion_density_origin)),
284-
('const double ion_temperature_pedistal = 6.09', 'const double ion_temperature_pedistal = ' + str(self.ion_temperature_pedistal)),
285-
('const double ion_temperature_seperatrix = 0.1','const double ion_temperature_seperatrix = ' + str(self.ion_temperature_seperatrix)),
286-
('const double ion_temperature_origin = 45.9', 'const double ion_temperature_origin = ' + str(self.ion_temperature_origin)),
287-
('const double pedistal_radius = 0.8', 'const double pedistal_radius = ' + str(self.pedistal_radius)),
288-
('const double ion_density_peaking_factor = 1', 'const double ion_density_peaking_factor = ' + str(self.ion_density_peaking_factor)),
289-
('const double ion_temperature_peaking_factor = 8.06', 'const double ion_temperature_peaking_factor = ' + str(self.ion_temperature_peaking_factor)),
290-
('const double minor_radius = 1.56', 'const double minor_radius = ' + str(self.minor_radius / 100.)),
291-
('const double major_radius = 2.5', 'const double major_radius = ' + str(self.major_radius / 100.)),
292-
('const double elongation = 2.0', 'const double elongation = ' + str(self.elongation)),
293-
('const double triangularity = 0.55', 'const double triangularity = ' + str(self.triangularity)),
294-
('const double shafranov_shift = 0.0', 'const double shafranov_shift = ' + str(self.shafranov_shift / 100.)),
295-
('const int number_of_bins = 100', 'const int number_of_bins = ' + str(self.number_of_bins)),
296-
('const int plasma_type = 1', 'const int plasma_type = ' + str(self.plasma_type))
322+
plasma_variables = {
323+
"ion_density_pedistal": self.ion_density_pedistal,
324+
"ion_density_seperatrix": self.ion_density_seperatrix,
325+
"ion_density_origin": self.ion_density_origin,
326+
"ion_temperature_pedistal": self.ion_temperature_pedistal,
327+
"ion_temperature_seperatrix": self.ion_temperature_seperatrix,
328+
"ion_temperature_origin": self.ion_temperature_origin,
329+
"pedistal_radius": self.pedistal_radius,
330+
"ion_density_peaking_factor": self.ion_density_peaking_factor,
331+
"ion_temperature_peaking_factor": self.ion_temperature_peaking_factor,
332+
"minor_radius": self.minor_radius,
333+
"major_radius": self.major_radius,
334+
"elongation": self.elongation,
335+
"triangularity": self.triangularity,
336+
"shafranov_shift": self.shafranov_shift,
337+
"number_of_bins": self.number_of_bins,
338+
"plasma_type": self.plasma_type,
339+
}
340+
341+
source_sampling_cpp_lines = self.source_sampling_cpp_file.split("\n")
342+
[
343+
self.replace_variable_value(source_sampling_cpp_lines, *variable)
344+
for variable in plasma_variables.items()
297345
]
298346

299-
editted_source_sampling_cpp_file = self.source_sampling_cpp_file
300-
for entry in plasma_varibles:
301-
if entry[0] in self.source_sampling_cpp_file:
302-
editted_source_sampling_cpp_file = editted_source_sampling_cpp_file.replace(entry[0], entry[1])
303-
else:
304-
raise ValueError(entry[0],' string not found in ', self.source_sampling_cpp_file)
305-
306-
307347
with open(temp_folder/'source_sampling.cpp', "w") as text_file:
308-
text_file.write(editted_source_sampling_cpp_file)
348+
text_file.write("\n".join(source_sampling_cpp_lines))
309349

310350
cwd = os.getcwd()
311351
os.chdir(Path(temp_folder))
@@ -319,3 +359,8 @@ def export_plasma_source(self, output_filename):
319359
shutil.rmtree(temp_folder)
320360

321361
return output_filename
362+
363+
364+
if __name__ == "__main__":
365+
p = Plasma()
366+
p.export_plasma_source("source_sampling.so")

0 commit comments

Comments
 (0)