Adds Extra Parameter to Logs and Updates Version Variable
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from importlib import metadata
|
||||
|
||||
VERSION = metadata.version("crystalpol")
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
from crystalpol import VERSION
|
||||
from crystalpol.polarization import Polarization
|
||||
from crystalpol.shared.config import Config
|
||||
from crystalpol.shared.utils.log import Log
|
||||
|
||||
from yaml.loader import SafeLoader
|
||||
from pathlib import Path
|
||||
import setproctitle
|
||||
import yaml
|
||||
from yaml.loader import SafeLoader
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import yaml
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
__VERSION = "v0.0.1"
|
||||
os.nice(+19)
|
||||
setproctitle.setproctitle("crystalpol-{}".format(__VERSION))
|
||||
setproctitle.setproctitle("crystalpol-{}".format(VERSION))
|
||||
|
||||
|
||||
def main():
|
||||
@@ -24,28 +24,31 @@ def main():
|
||||
|
||||
parser = argparse.ArgumentParser(prog="CrystalPol")
|
||||
parser.add_argument(
|
||||
"-v", "--version", action="version", version=f"crystalpol-{__VERSION}"
|
||||
"-v", "--version", action="version", version=f"crystalpol-{VERSION}"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c", "--config",
|
||||
"-c",
|
||||
"--config",
|
||||
dest="config",
|
||||
default="config.yml",
|
||||
metavar="INFILE",
|
||||
help="Config file of crystalpol [default = config.yml]"
|
||||
help="Config file of crystalpol [default = config.yml]",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-i", "--input",
|
||||
"-i",
|
||||
"--input",
|
||||
dest="infile",
|
||||
default="crystal.xyz",
|
||||
metavar="INFILE",
|
||||
help="Input file of crystalpol [default = crystal.xyz]"
|
||||
help="Input file of crystalpol [default = crystal.xyz]",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-o", "--output",
|
||||
"-o",
|
||||
"--output",
|
||||
dest="outfile",
|
||||
default="run.log",
|
||||
metavar="OUTFILE",
|
||||
help="Output file of crystalpol [default = run.log]"
|
||||
help="Output file of crystalpol [default = run.log]",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -53,20 +56,16 @@ def main():
|
||||
if log_file.exists():
|
||||
log_file.rename(log_file.with_suffix(".log.backup"))
|
||||
|
||||
logging.basicConfig(
|
||||
filename=args.outfile,
|
||||
format='%(message)s',
|
||||
level=logging.INFO
|
||||
)
|
||||
logging.basicConfig(filename=args.outfile, format="%(message)s", level=logging.INFO)
|
||||
|
||||
try:
|
||||
with open(args.config) as file:
|
||||
data = yaml.load(file, Loader=SafeLoader)
|
||||
config = Config(**data.get('crystal_pol'))
|
||||
config = Config(**data.get("crystal_pol"))
|
||||
except IOError:
|
||||
raise RuntimeError('Invalid or Missing Config File.')
|
||||
raise RuntimeError("Invalid or Missing Config File.")
|
||||
|
||||
Log.make_header(__VERSION, config.to_dict())
|
||||
Log.make_header(VERSION, config.to_dict())
|
||||
|
||||
pol = Polarization(args.infile, args.outfile, config)
|
||||
pol.run()
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
from crystalpol.shared.system.molecule import Molecule
|
||||
from crystalpol.shared.system.crystal import Crystal
|
||||
from crystalpol.shared.system.atom import Atom
|
||||
from crystalpol.shared.config import Config
|
||||
from crystalpol.gaussian import Gaussian
|
||||
|
||||
from typing import List, Tuple
|
||||
from crystalpol.shared.config import Config
|
||||
from crystalpol.shared.system.atom import Atom
|
||||
from crystalpol.shared.system.crystal import Crystal
|
||||
from crystalpol.shared.system.molecule import Molecule
|
||||
from crystalpol.shared.utils.log import Log
|
||||
|
||||
import sys
|
||||
|
||||
from crystalpol.shared.utils.log import Log
|
||||
from typing import List, Tuple
|
||||
|
||||
|
||||
class Polarization:
|
||||
__slots__ = ('geom_file', 'outfile', 'config', 'gaussian', 'crystal')
|
||||
__slots__ = ("geom_file", "outfile", "config", "gaussian", "crystal")
|
||||
|
||||
def __init__(self, geom_file: str, outfile: str, config: Config) -> None:
|
||||
self.crystal = None
|
||||
@@ -33,21 +31,22 @@ class Polarization:
|
||||
max_charge_diff = sys.float_info.max
|
||||
while max_charge_diff >= self.config.charge_tolerance:
|
||||
|
||||
max_charge_diff, charge_diff = self.update_crystal_charges(
|
||||
max_charge_diff, min_charge_diff, charge_diff = self.update_crystal_charges(
|
||||
self.gaussian.run(cycle, self.crystal),
|
||||
)
|
||||
|
||||
Log.make_run(
|
||||
cycle,
|
||||
max_charge_diff if cycle != 1 else 0,
|
||||
min_charge_diff if cycle != 1 else 0,
|
||||
charge_diff,
|
||||
self.crystal
|
||||
self.crystal,
|
||||
)
|
||||
|
||||
cycle += 1
|
||||
|
||||
def read_crystal(self) -> None:
|
||||
with open(self.geom_file, 'r') as geom_file:
|
||||
with open(self.geom_file, "r") as geom_file:
|
||||
lines = geom_file.readlines()
|
||||
|
||||
molecules = self._get_molecules_from_lines(lines)
|
||||
@@ -57,7 +56,7 @@ class Polarization:
|
||||
for molecule in molecules:
|
||||
self.crystal.add_cell([molecule])
|
||||
|
||||
def update_crystal_charges(self, charges: List[float]) -> Tuple[float, list]:
|
||||
def update_crystal_charges(self, charges: List[float]) -> Tuple[float, float, list]:
|
||||
|
||||
charge_diff = []
|
||||
|
||||
@@ -68,9 +67,12 @@ class Polarization:
|
||||
charge_diff.append(abs(atom.chg - charges[index]))
|
||||
atom.chg = charges[index]
|
||||
|
||||
max_charge_diff = abs(max(charge_diff, key=abs)) if charge_diff else sys.float_info.max
|
||||
max_charge_diff = (
|
||||
abs(max(charge_diff, key=abs)) if charge_diff else sys.float_info.max
|
||||
)
|
||||
min_charge_diff = abs(min(charge_diff, key=abs)) if charge_diff else 0
|
||||
|
||||
return max_charge_diff, charge_diff
|
||||
return max_charge_diff, min_charge_diff, charge_diff
|
||||
|
||||
def _get_molecules_from_lines(self, lines: List[str]) -> List[Molecule]:
|
||||
if (len(lines) % self.config.n_atoms) == 0:
|
||||
@@ -82,7 +84,9 @@ class Polarization:
|
||||
symbol, rx, ry, rz = tuple(atom_line.split())
|
||||
mol.add_atom(
|
||||
Atom(
|
||||
rx, ry, rz,
|
||||
rx,
|
||||
ry,
|
||||
rz,
|
||||
symbol=symbol.ljust(2),
|
||||
)
|
||||
)
|
||||
@@ -105,4 +109,4 @@ class Polarization:
|
||||
@staticmethod
|
||||
def split(array: List, partitions: int):
|
||||
for i in range(0, len(array), partitions):
|
||||
yield array[i: i + partitions]
|
||||
yield array[i : i + partitions]
|
||||
|
||||
@@ -19,31 +19,55 @@ class Log:
|
||||
logger.info(f"Your python version is {sys.version}\n")
|
||||
logger.info(f"Program started on {weekday_date_time()}\n")
|
||||
|
||||
logger.info("------------------------------------------------------------------------------------------")
|
||||
logger.info(" CRYSTALPOL variables being used in this run: ")
|
||||
logger.info("------------------------------------------------------------------------------------------\n")
|
||||
logger.info(
|
||||
"------------------------------------------------------------------------------------------"
|
||||
)
|
||||
logger.info(
|
||||
" CRYSTALPOL variables being used in this run: "
|
||||
)
|
||||
logger.info(
|
||||
"------------------------------------------------------------------------------------------\n"
|
||||
)
|
||||
for key, value in config_dict.items():
|
||||
logger.info(f"\t{key} = {(value if value else 'Not set')}")
|
||||
|
||||
logger.info("------------------------------------------------------------------------------------------")
|
||||
logger.info(f" RUN Results: ")
|
||||
logger.info("------------------------------------------------------------------------------------------\n")
|
||||
logger.info(
|
||||
"------------------------------------------------------------------------------------------"
|
||||
)
|
||||
logger.info(
|
||||
f" RUN Results: "
|
||||
)
|
||||
logger.info(
|
||||
"------------------------------------------------------------------------------------------\n"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def make_run(cycle, max_charge_diff, charge_diff, crystal, logger=None):
|
||||
def make_run(
|
||||
cycle, max_charge_diff, min_charge_diff, charge_diff, crystal, logger=None
|
||||
):
|
||||
|
||||
if logger is None:
|
||||
logger = logging.getLogger()
|
||||
|
||||
logger.info(f"cycle: {cycle}")
|
||||
logger.info(f"\nMax charge diff: {max_charge_diff:.5f}")
|
||||
logger.info(f"cycle: {cycle}\n")
|
||||
logger.info(f"Max charge diff: {max_charge_diff:.5f}")
|
||||
logger.info(f"Min charge diff: {min_charge_diff:.5f}")
|
||||
logger.info(f"Charge Diff: {charge_diff}\n")
|
||||
|
||||
logger.info(f"------------------------------------------------------------------------------------------")
|
||||
logger.info(f" S rx ry rz chg ")
|
||||
logger.info(f"------------------------------------------------------------------------------------------")
|
||||
logger.info(
|
||||
f"------------------------------------------------------------------------------------------"
|
||||
)
|
||||
logger.info(
|
||||
f" S rx ry rz chg "
|
||||
)
|
||||
logger.info(
|
||||
f"------------------------------------------------------------------------------------------"
|
||||
)
|
||||
for atom in crystal[0][0]:
|
||||
logger.info(
|
||||
f" {atom.symbol.rjust(2)} {float(atom.rx):.6f} {float(atom.ry):.6f} {float(atom.rz):.6f} {float(atom.chg):.6f} ")
|
||||
f" {atom.symbol.rjust(2)} {float(atom.rx):.6f} {float(atom.ry):.6f} {float(atom.rz):.6f} {float(atom.chg):.6f} "
|
||||
)
|
||||
|
||||
logger.info("\n------------------------------------------------------------------------------------------\n")
|
||||
logger.info(
|
||||
"\n------------------------------------------------------------------------------------------\n"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user