1
1
"""Contains the definition of the simulation functions given to libEnsemble."""
2
+ from contextlib import redirect_stderr , redirect_stdout
2
3
3
4
import jinja2
4
5
import numpy as np
@@ -122,6 +123,7 @@ def run_function(H, persis_info, sim_specs, libE_info):
122
123
else :
123
124
user_specs = sim_specs ["user" ]
124
125
evaluation_func = user_specs ["evaluation_func" ]
126
+ redirect_logs_to_file = user_specs ["redirect_logs_to_file" ]
125
127
126
128
# Prepare the array that is returned to libE
127
129
libE_output = np .zeros (1 , dtype = sim_specs ["out" ])
@@ -130,7 +132,16 @@ def run_function(H, persis_info, sim_specs, libE_info):
130
132
libE_output [name ].fill (np .nan )
131
133
132
134
# Run evaluation.
133
- evaluation_func (input_values , libE_output [0 ])
135
+ if redirect_logs_to_file :
136
+ with (
137
+ open ("log.out" , "w" ) as stdout_file ,
138
+ open ("log.err" , "w" ) as stderr_file ,
139
+ redirect_stdout (stdout_file ),
140
+ redirect_stderr (stderr_file ),
141
+ ):
142
+ evaluation_func (input_values , libE_output [0 ])
143
+ else :
144
+ evaluation_func (input_values , libE_output [0 ])
134
145
calc_status = WORKER_DONE
135
146
136
147
# If required, fail when the objectives are NaN.
0 commit comments