|
| 1 | +import sys |
| 2 | +import subprocess |
| 3 | + |
1 | 4 | from abc import ABC, abstractmethod |
2 | 5 |
|
3 | 6 | class Depcode(ABC): |
@@ -99,21 +102,37 @@ def read_depleted_materials(self, read_at_end=False): |
99 | 102 | :class:`Materialflow` object holding material composition and properties. |
100 | 103 |
|
101 | 104 | """ |
102 | | - |
103 | | - @abstractmethod |
104 | | - def run_depletion_step(self, mpi_args, threads): |
| 105 | + def run_depletion_step(self, mpi_args, args): |
105 | 106 | """Runs a depletion step as a subprocess with the given parameters. |
106 | 107 |
|
107 | 108 | Parameters |
108 | 109 | ---------- |
109 | 110 | mpi_args : list of str |
110 | 111 | Arguments for running simulations on supercomputers using |
111 | 112 | ``mpiexec`` or similar programs. |
112 | | - threads : int |
113 | | - Threads to use for shared-memory parallelism |
| 113 | + args : list of str |
| 114 | + Arguments for running depletion step. |
114 | 115 |
|
115 | 116 | """ |
116 | 117 |
|
| 118 | + print('Running %s' % (self.codename)) |
| 119 | + try: |
| 120 | + if mpi_args is None: |
| 121 | + stdout = sys.stdout |
| 122 | + else: |
| 123 | + stdout = None |
| 124 | + subprocess.run( |
| 125 | + args, |
| 126 | + check=True, |
| 127 | + cwd=self.output_path, |
| 128 | + stdout=stdout, |
| 129 | + stderr=subprocess.STDOUT) |
| 130 | + print(f'Finished {self.codename.upper()} Run') |
| 131 | + except subprocess.CalledProcessError as error: |
| 132 | + print(error.output.decode("utf-8")) |
| 133 | + raise RuntimeError('\n %s RUN FAILED\n see error message above' |
| 134 | + % (self.codename)) |
| 135 | + |
117 | 136 | @abstractmethod |
118 | 137 | def switch_to_next_geometry(self): |
119 | 138 | """Changes the geometry used in the depletion code simulation to the |
@@ -153,5 +172,76 @@ def update_depletable_materials(self, mats, dep_end_time): |
153 | 172 |
|
154 | 173 | """ |
155 | 174 |
|
| 175 | + def read_plaintext_file(self, file_path): |
| 176 | + """Reads the content of a plaintext file for use by other methods. |
| 177 | +
|
| 178 | + Parameters |
| 179 | + ---------- |
| 180 | + file_path : str |
| 181 | + Path to file. |
| 182 | +
|
| 183 | + Returns |
| 184 | + ------- |
| 185 | + file_lines : list of str |
| 186 | + File lines. |
| 187 | +
|
| 188 | + """ |
| 189 | + file_lines = [] |
| 190 | + with open(file_path, 'r') as file: |
| 191 | + file_lines = file.readlines() |
| 192 | + return file_lines |
| 193 | + |
| 194 | + @abstractmethod |
| 195 | + def convert_nuclide_code_to_name(self, nuc_code): |
| 196 | + """Converts depcode nuclide code to symbolic nuclide name. |
| 197 | +
|
| 198 | + Parameters |
| 199 | + ---------- |
| 200 | + nuc_code : str |
| 201 | + Nuclide code |
| 202 | +
|
| 203 | + Returns |
| 204 | + ------- |
| 205 | + nuc_name : str |
| 206 | + Symbolic nuclide name (`Am242m1`). |
| 207 | +
|
| 208 | + """ |
| 209 | + |
| 210 | + @abstractmethod |
| 211 | + def _convert_name_to_nuccode(self, nucname): |
| 212 | + """Converts depcode nuclide name to ZA nuclide code |
| 213 | +
|
| 214 | + Parameters |
| 215 | + ---------- |
| 216 | + nucname : str |
| 217 | + Nuclide namce |
| 218 | +
|
| 219 | + Returns |
| 220 | + ------- |
| 221 | + nuc_name : str |
| 222 | + ZA nuclide code |
| 223 | +
|
| 224 | + """ |
| 225 | + |
| 226 | + def preserve_simulation_files(self, step_idx): |
| 227 | + """Move simulation input and output files |
| 228 | + to unique a directory |
156 | 229 |
|
| 230 | + Parameters |
| 231 | + ---------- |
| 232 | + step_idx : int |
157 | 233 |
|
| 234 | + """ |
| 235 | + step_results_dir = self.output_path / f'step_{step_idx}_data' |
| 236 | + step_results_dir.mkdir(exist_ok=True) |
| 237 | + |
| 238 | + file_path = lambda file : self.output_path / file |
| 239 | + output_paths = list(map(file_path, self._OUTPUTFILE_NAMES)) |
| 240 | + input_paths = list(map(file_path, self._INPUTFILE_NAMES)) |
| 241 | + for file_path, fname in zip(output_paths, self._OUTPUTFILE_NAMES): |
| 242 | + file_path.rename(step_results_dir / fname) |
| 243 | + |
| 244 | + for file_path, fname in zip(input_paths, self._INPUTFILE_NAMES): |
| 245 | + lines = self.read_plaintext_file(file_path) |
| 246 | + with open(step_results_dir / fname, 'w') as out_file: |
| 247 | + out_file.writelines(lines) |
0 commit comments