Implements Additional Logs and class Player Tests
This commit is contained in:
@@ -9,7 +9,7 @@ from diceplayer.shared.environment.system import System
|
||||
from diceplayer.shared.config.step_dto import StepDTO
|
||||
from diceplayer.shared.config.dice_dto import DiceDTO
|
||||
from diceplayer.shared.environment.atom import Atom
|
||||
from diceplayer.shared.utils.ptable import atommass
|
||||
from diceplayer import logger
|
||||
|
||||
from dataclasses import fields
|
||||
from pathlib import Path
|
||||
@@ -24,13 +24,6 @@ ENV = ["OMP_STACKSIZE"]
|
||||
|
||||
|
||||
class Player:
|
||||
__slots__ = [
|
||||
'config',
|
||||
'system',
|
||||
'dice',
|
||||
'gaussian',
|
||||
]
|
||||
|
||||
def __init__(self, infile: str):
|
||||
config_data = self.read_keywords(infile)
|
||||
|
||||
@@ -43,16 +36,16 @@ class Player:
|
||||
self.gaussian = GaussianInterface(config_data.get("gaussian"))
|
||||
self.dice = DiceInterface(config_data.get("dice"))
|
||||
|
||||
def start(self):
|
||||
def start(self, initial_cycle: int = 1):
|
||||
self.print_keywords()
|
||||
|
||||
self.create_simulation_dir()
|
||||
|
||||
self.read_potentials()
|
||||
# self.print_potentials()
|
||||
self.print_potentials()
|
||||
|
||||
self.dice_start(1)
|
||||
self.dice_start(2)
|
||||
for cycle in range(initial_cycle, self.config.maxcyc + 1):
|
||||
self.dice_start(cycle)
|
||||
|
||||
def create_simulation_dir(self):
|
||||
simulation_dir_path = Path(self.config.simulation_dir)
|
||||
@@ -61,12 +54,7 @@ class Player:
|
||||
f"Error: a file or a directory {self.config.simulation_dir} already exists,"
|
||||
f" move or delete the simfiles directory to continue."
|
||||
)
|
||||
try:
|
||||
simulation_dir_path.mkdir()
|
||||
except FileExistsError:
|
||||
OSError(
|
||||
f"Error: cannot make directory {self.config.simulation_dir}"
|
||||
)
|
||||
simulation_dir_path.mkdir()
|
||||
|
||||
def print_keywords(self) -> None:
|
||||
|
||||
@@ -75,38 +63,38 @@ class Player:
|
||||
if getattr(config, key) is not None:
|
||||
if isinstance(getattr(config, key), list):
|
||||
string = " ".join(str(x) for x in getattr(config, key))
|
||||
logging.info(f"{key} = [ {string} ]")
|
||||
logger.info(f"{key} = [ {string} ]")
|
||||
else:
|
||||
logging.info(f"{key} = {getattr(config, key)}")
|
||||
logger.info(f"{key} = {getattr(config, key)}")
|
||||
|
||||
logging.info(
|
||||
logger.info(
|
||||
"##########################################################################################\n"
|
||||
"############# Welcome to DICEPLAYER version 1.0 #############\n"
|
||||
"##########################################################################################\n"
|
||||
"\n"
|
||||
)
|
||||
logging.info("Your python version is {}\n".format(sys.version))
|
||||
logging.info("\n")
|
||||
logging.info("Program started on {}\n".format(weekday_date_time()))
|
||||
logging.info("\n")
|
||||
logging.info("Environment variables:\n")
|
||||
logger.info("Your python version is {}\n".format(sys.version))
|
||||
logger.info("\n")
|
||||
logger.info("Program started on {}\n".format(weekday_date_time()))
|
||||
logger.info("\n")
|
||||
logger.info("Environment variables:\n")
|
||||
for var in ENV:
|
||||
logging.info(
|
||||
logger.info(
|
||||
"{} = {}\n".format(
|
||||
var, (os.environ[var] if var in os.environ else "Not set")
|
||||
)
|
||||
)
|
||||
|
||||
logging.info(
|
||||
logger.info(
|
||||
"\n==========================================================================================\n"
|
||||
" CONTROL variables being used in this run:\n"
|
||||
"------------------------------------------------------------------------------------------\n"
|
||||
"\n"
|
||||
)
|
||||
|
||||
logging.info("\n")
|
||||
logger.info("\n")
|
||||
|
||||
logging.info(
|
||||
logger.info(
|
||||
"------------------------------------------------------------------------------------------\n"
|
||||
" DICE variables being used in this run:\n"
|
||||
"------------------------------------------------------------------------------------------\n"
|
||||
@@ -115,9 +103,9 @@ class Player:
|
||||
|
||||
log_keywords(self.dice.config, DiceDTO)
|
||||
|
||||
logging.info("\n")
|
||||
logger.info("\n")
|
||||
|
||||
logging.info(
|
||||
logger.info(
|
||||
"------------------------------------------------------------------------------------------\n"
|
||||
" GAUSSIAN variables being used in this run:\n"
|
||||
"------------------------------------------------------------------------------------------\n"
|
||||
@@ -126,18 +114,19 @@ class Player:
|
||||
|
||||
log_keywords(self.gaussian.config, GaussianDTO)
|
||||
|
||||
logging.info("\n")
|
||||
logger.info("\n")
|
||||
|
||||
def read_potentials(self):
|
||||
try:
|
||||
ljname_path = Path(self.dice.config.ljname)
|
||||
if ljname_path.exists():
|
||||
with open(self.dice.config.ljname) as file:
|
||||
ljdata = file.readlines()
|
||||
except FileNotFoundError:
|
||||
ljc_data = file.readlines()
|
||||
else:
|
||||
raise RuntimeError(
|
||||
f"Potential file {self.dice.config.ljname} not found."
|
||||
)
|
||||
|
||||
combrule = ljdata.pop(0).split()[0]
|
||||
combrule = ljc_data.pop(0).split()[0]
|
||||
if combrule not in ("*", "+"):
|
||||
sys.exit(
|
||||
"Error: expected a '*' or a '+' sign in 1st line of file {}".format(
|
||||
@@ -146,7 +135,7 @@ class Player:
|
||||
)
|
||||
self.dice.config.combrule = combrule
|
||||
|
||||
ntypes = ljdata.pop(0).split()[0]
|
||||
ntypes = ljc_data.pop(0).split()[0]
|
||||
if not ntypes.isdigit():
|
||||
sys.exit(
|
||||
"Error: expected an integer in the 2nd line of file {}".format(
|
||||
@@ -157,22 +146,22 @@ class Player:
|
||||
|
||||
if ntypes != len(self.dice.config.nmol):
|
||||
sys.exit(
|
||||
f"Error: number of molecule types in file {self.dice.config.ljname}"
|
||||
f"Error: number of molecule types in file {self.dice.config.ljname} "
|
||||
f"must match that of 'nmol' keyword in config file"
|
||||
)
|
||||
|
||||
for i in range(ntypes):
|
||||
|
||||
nsites, molname = ljdata.pop(0).split()[:2]
|
||||
try:
|
||||
nsites, molname = ljc_data.pop(0).split()[:2]
|
||||
except ValueError:
|
||||
raise ValueError(
|
||||
f"Error: expected nsites and molname for the molecule type {i+1}"
|
||||
)
|
||||
|
||||
if not nsites.isdigit():
|
||||
raise ValueError(
|
||||
f"Error: expected nsites to be an integer for molecule type {i}"
|
||||
)
|
||||
|
||||
if molname is None:
|
||||
raise ValueError(
|
||||
f"Error: expected molecule name for molecule type {i}"
|
||||
f"Error: expected nsites to be an integer for molecule type {i+1}"
|
||||
)
|
||||
|
||||
nsites = int(nsites)
|
||||
@@ -182,12 +171,68 @@ class Player:
|
||||
for j in range(nsites):
|
||||
new_atom = dict(zip(
|
||||
atom_fields,
|
||||
ljdata.pop(0).split()
|
||||
ljc_data.pop(0).split()
|
||||
))
|
||||
self.system.molecule[i].add_atom(
|
||||
Atom(**self.validate_atom_dict(i, j, new_atom))
|
||||
)
|
||||
|
||||
def print_potentials(self) -> None:
|
||||
|
||||
formatstr = "{:<3d} {:>3d} {:>10.5f} {:>10.5f} {:>10.5f} {:>10.6f} {:>9.5f} {:>7.4f} {:>9.4f}"
|
||||
logger.info(
|
||||
"==========================================================================================\n"
|
||||
)
|
||||
logger.info(
|
||||
f" Potential parameters from file {self.dice.config.ljname}:"
|
||||
)
|
||||
logger.info(
|
||||
"------------------------------------------------------------------------------------------"
|
||||
"\n"
|
||||
)
|
||||
|
||||
logger.info(f"Combination rule: {self.dice.config.combrule}")
|
||||
logger.info(
|
||||
f"Types of molecules: {len(self.system.molecule)}\n"
|
||||
)
|
||||
|
||||
i = 0
|
||||
for mol in self.system.molecule:
|
||||
i += 1
|
||||
logger.info(
|
||||
"{} atoms in molecule type {}:".format(len(mol.atom), i)
|
||||
)
|
||||
logger.info(
|
||||
"---------------------------------------------------------------------------------"
|
||||
)
|
||||
logger.info(
|
||||
"Lbl AN X Y Z Charge Epsilon Sigma Mass"
|
||||
)
|
||||
logger.info(
|
||||
"---------------------------------------------------------------------------------"
|
||||
)
|
||||
|
||||
for atom in mol.atom:
|
||||
logger.info(
|
||||
formatstr.format(
|
||||
atom.lbl,
|
||||
atom.na,
|
||||
atom.rx,
|
||||
atom.ry,
|
||||
atom.rz,
|
||||
atom.chg,
|
||||
atom.eps,
|
||||
atom.sig,
|
||||
atom.mass,
|
||||
)
|
||||
)
|
||||
|
||||
logger.info("\n")
|
||||
|
||||
logger.info(
|
||||
"=========================================================================================="
|
||||
)
|
||||
|
||||
def dice_start(self, cycle: int):
|
||||
self.dice.configure(
|
||||
StepDTO(
|
||||
@@ -204,8 +249,8 @@ class Player:
|
||||
|
||||
self.dice.reset()
|
||||
|
||||
def gaussian_start(self):
|
||||
self.gaussian.start()
|
||||
def gaussian_start(self, cycle: int):
|
||||
self.gaussian.start(cycle)
|
||||
|
||||
@staticmethod
|
||||
def validate_atom_dict(molecule_type, molecule_site, atom_dict: dict) -> dict:
|
||||
@@ -219,63 +264,63 @@ class Player:
|
||||
|
||||
try:
|
||||
atom_dict['lbl'] = int(atom_dict['lbl'])
|
||||
except ValueError:
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f'Invalid lbl fields for site {molecule_site} for molecule type {molecule_type}.'
|
||||
)
|
||||
|
||||
try:
|
||||
atom_dict['na'] = int(atom_dict['na'])
|
||||
except ValueError:
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f'Invalid na fields for site {molecule_site} for molecule type {molecule_type}.'
|
||||
)
|
||||
|
||||
try:
|
||||
atom_dict['rx'] = float(atom_dict['rx'])
|
||||
except ValueError:
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f'Invalid rx fields for site {molecule_site} for molecule type {molecule_type}.'
|
||||
f'Invalid rx fields for site {molecule_site} for molecule type {molecule_type}. '
|
||||
f'Value must be a float.'
|
||||
)
|
||||
|
||||
try:
|
||||
atom_dict['ry'] = float(atom_dict['ry'])
|
||||
except ValueError:
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f'Invalid ry fields for site {molecule_site} for molecule type {molecule_type}.'
|
||||
f'Invalid ry fields for site {molecule_site} for molecule type {molecule_type}. '
|
||||
f'Value must be a float.'
|
||||
)
|
||||
|
||||
try:
|
||||
atom_dict['rz'] = float(atom_dict['rx'])
|
||||
except ValueError:
|
||||
atom_dict['rz'] = float(atom_dict['rz'])
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f'Invalid rz fields for site {molecule_site} for molecule type {molecule_type}.'
|
||||
f'Invalid rz fields for site {molecule_site} for molecule type {molecule_type}. '
|
||||
f'Value must be a float.'
|
||||
)
|
||||
|
||||
try:
|
||||
atom_dict['chg'] = float(atom_dict['chg'])
|
||||
except ValueError:
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f'Invalid chg fields for site {molecule_site} for molecule type {molecule_type}.'
|
||||
f'Invalid chg fields for site {molecule_site} for molecule type {molecule_type}. '
|
||||
f'Value must be a float.'
|
||||
)
|
||||
|
||||
try:
|
||||
atom_dict['eps'] = float(atom_dict['eps'])
|
||||
except ValueError:
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f'Invalid eps fields for site {molecule_site} for molecule type {molecule_type}.'
|
||||
f'Invalid eps fields for site {molecule_site} for molecule type {molecule_type}. '
|
||||
f'Value must be a float.'
|
||||
)
|
||||
|
||||
try:
|
||||
atom_dict['sig'] = float(atom_dict['sig'])
|
||||
except ValueError:
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f'Invalid sig fields for site {molecule_site} for molecule type {molecule_type}.'
|
||||
f'Invalid sig fields for site {molecule_site} for molecule type {molecule_type}. '
|
||||
f'Value must be a float.'
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user