refactor: update dice handling and optimization flow to return structured results
This commit is contained in:
@@ -4,7 +4,7 @@ from diceplayer.dice.dice_input import (
|
||||
NVTEqConfig,
|
||||
NVTTerConfig,
|
||||
)
|
||||
from diceplayer.dice.dice_wrapper import DiceWrapper
|
||||
from diceplayer.dice.dice_wrapper import DiceWrapper, DiceEnvironment
|
||||
from diceplayer.logger import logger
|
||||
from diceplayer.state.state_model import StateModel
|
||||
|
||||
@@ -17,7 +17,7 @@ class DiceHandler:
|
||||
def __init__(self, step_directory: Path):
|
||||
self.dice_directory = step_directory / "dice"
|
||||
|
||||
def run(self, state: StateModel, cycle: int) -> StateModel:
|
||||
def run(self, state: StateModel, cycle: int) -> list[DiceEnvironment]:
|
||||
if self.dice_directory.exists():
|
||||
logger.info(
|
||||
f"Found dice directory: {self.dice_directory}, this directory will be purged for a clean state"
|
||||
@@ -25,13 +25,9 @@ class DiceHandler:
|
||||
shutil.rmtree(self.dice_directory)
|
||||
self.dice_directory.mkdir(parents=True)
|
||||
|
||||
simulation_results = self.run_simulations(state, cycle)
|
||||
return self.run_simulations(state, cycle)
|
||||
|
||||
result = self.aggregate_results(simulation_results)
|
||||
|
||||
return self.commit_simulation_state(state, result)
|
||||
|
||||
def run_simulations(self, state: StateModel, cycle: int) -> list[dict]:
|
||||
def run_simulations(self, state: StateModel, cycle: int) -> list[DiceEnvironment]:
|
||||
results = []
|
||||
|
||||
threads = []
|
||||
@@ -48,15 +44,12 @@ class DiceHandler:
|
||||
f"Expected {state.config.dice.nprocs} simulation results, but got {len(results)}"
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
def aggregate_results(self, simulation_results: list[dict]) -> dict: ...
|
||||
|
||||
def commit_simulation_state(self, state: StateModel, result: dict) -> StateModel:
|
||||
return state
|
||||
return [
|
||||
i for i in [r for r in results]
|
||||
]
|
||||
|
||||
def _simulation_process(
|
||||
self, state: StateModel, cycle: int, proc: int, results: list[dict]
|
||||
self, state: StateModel, cycle: int, proc: int, results: list[list[DiceEnvironment]]
|
||||
) -> None:
|
||||
proc_directory = self.dice_directory / f"{proc:02d}"
|
||||
if proc_directory.exists():
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
import diceplayer.dice.dice_input as dice_input
|
||||
from diceplayer.config import DiceConfig
|
||||
from diceplayer.environment import System
|
||||
@@ -7,6 +9,10 @@ from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
|
||||
type DiceEnvironment = tuple[str, int, int, int]
|
||||
DiceEnvironmentAdapter = TypeAdapter(DiceEnvironment)
|
||||
|
||||
|
||||
DICE_FLAG_LINE: Final[int] = -2
|
||||
DICE_END_FLAG: Final[str] = "End of simulation"
|
||||
|
||||
@@ -35,9 +41,22 @@ class DiceWrapper:
|
||||
|
||||
raise RuntimeError(f"Dice simulation failed with exit status {exit_status}")
|
||||
|
||||
def parse_results(self, system: System) -> dict:
|
||||
results = {}
|
||||
def parse_results(self, system: System) -> list[DiceEnvironment]:
|
||||
NUMBER_OF_HEADER_LINES = 2
|
||||
NUMBER_OF_PRIMARY_ATOMS = len(system.molecule[0].atom)
|
||||
|
||||
results = []
|
||||
for output_file in sorted(self.working_directory.glob("phb*.xyz")):
|
||||
...
|
||||
with open(output_file, "r") as f:
|
||||
for _ in range(NUMBER_OF_HEADER_LINES + NUMBER_OF_PRIMARY_ATOMS):
|
||||
next(f, None)
|
||||
|
||||
for line in f:
|
||||
if line.strip() == "":
|
||||
break
|
||||
|
||||
results.append(
|
||||
DiceEnvironmentAdapter.validate_python(line.split())
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user