|
3 | 3 | import types |
4 | 4 | import warnings |
5 | 5 | from collections.abc import Iterable |
| 6 | +from contextlib import nullcontext |
6 | 7 | from typing import Literal |
7 | 8 |
|
8 | 9 | import numpy as np |
@@ -433,32 +434,31 @@ def execute( |
433 | 434 | next_output = start_time + outputdt * sign_dt |
434 | 435 |
|
435 | 436 | time = start_time |
436 | | - while sign_dt * (time - end_time) < 0: |
437 | | - if next_output is not None: |
438 | | - f = min if sign_dt > 0 else max |
439 | | - next_time = f(next_output, end_time) |
440 | | - else: |
441 | | - next_time = end_time |
442 | | - |
443 | | - self._kernel.execute(self, endtime=next_time, dt=dt) |
444 | | - |
445 | | - if next_output is not None: |
446 | | - if np.abs(next_time - next_output) < 0.001: |
447 | | - if output_file: |
448 | | - output_file.write(self, next_output) |
449 | | - if np.isfinite(outputdt): |
450 | | - next_output += outputdt * sign_dt |
451 | 437 |
|
452 | | - if verbose_progress: |
453 | | - pbar.set_description_str( |
454 | | - "Integration time: " + str(float_to_datelike(time, self.fieldset.time_interval)) |
455 | | - ) |
456 | | - pbar.update(sign_dt * (next_time - time)) |
457 | | - |
458 | | - time = next_time |
459 | | - |
460 | | - if output_file is not None: |
461 | | - output_file.close() |
| 438 | + with output_file if output_file is not None else nullcontext(): |
| 439 | + while sign_dt * (time - end_time) < 0: |
| 440 | + if next_output is not None: |
| 441 | + f = min if sign_dt > 0 else max |
| 442 | + next_time = f(next_output, end_time) |
| 443 | + else: |
| 444 | + next_time = end_time |
| 445 | + |
| 446 | + self._kernel.execute(self, endtime=next_time, dt=dt) |
| 447 | + |
| 448 | + if next_output is not None: |
| 449 | + if np.abs(next_time - next_output) < 0.001: |
| 450 | + if output_file: |
| 451 | + output_file.write(self, next_output) |
| 452 | + if np.isfinite(outputdt): |
| 453 | + next_output += outputdt * sign_dt |
| 454 | + |
| 455 | + if verbose_progress: |
| 456 | + pbar.set_description_str( |
| 457 | + "Integration time: " + str(float_to_datelike(time, self.fieldset.time_interval)) |
| 458 | + ) |
| 459 | + pbar.update(sign_dt * (next_time - time)) |
| 460 | + |
| 461 | + time = next_time |
462 | 462 |
|
463 | 463 | if verbose_progress: |
464 | 464 | pbar.close() |
|
0 commit comments