from diceplayer.config import PlayerConfig from diceplayer.environment import Atom, Molecule, System from pathlib import Path def read_system_from_phb(config: PlayerConfig) -> System: phb_file = Path(config.dice.ljname) if not phb_file.exists(): raise FileNotFoundError ljc_data = phb_file.read_text(encoding="utf-8").splitlines() combrule = ljc_data.pop(0).strip() if combrule != config.dice.combrule: raise ValueError( f"Invalid combrule defined in {phb_file}. Expected the same value configured in the config file" ) ntypes = ljc_data.pop(0).strip() if not ntypes.isdigit(): raise ValueError(f"Invalid ntypes defined in {phb_file}") nmol = int(ntypes) if nmol != len(config.dice.nmol): raise ValueError(f"Invalid nmol defined in {phb_file}") sys = System() for i in range(nmol): nsites, molname = ljc_data.pop(0).split() if not nsites.isdigit(): raise ValueError(f"Invalid nsites defined in {phb_file}") nsites = int(nsites) mol = Molecule(molname) for j in range(nsites): _fields = ljc_data.pop(0).split() mol.add_atom(Atom(*_fields)) sys.add_type(mol) return sys # def write_phb(phb_file: Path, state: StateModel) -> None: # if phb_file.exists(): # raise RuntimeError(f"File {phb_file} already exists") # # with open(phb_file, "w") as f: # f.write(f"{state.config.dice.combrule}\n") # f.write(f"{len(state.system.nmols)}\n") # f.write(f"{state.config.dice.nmol}\n")