Skip to content

Commit

Permalink
Merge pull request #61 from robinzyb/devel
Browse files Browse the repository at this point in the history
move print to logger
  • Loading branch information
robinzyb authored Jun 27, 2024
2 parents b8bcbb5 + f76e26c commit fd92982
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ pip install .
# Feature Request
Any advice is welcome. If you would like to request a new feature, please open an issue in github and upload example input and output files.


# Developer
set environment variable CP2KDATA_LOG_LEVEL to DEBUG to get the complete information.



Expand Down
35 changes: 20 additions & 15 deletions cp2kdata/dpdata_plugin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from .log import get_logger
import numpy as np

from dpdata.unit import EnergyConversion, LengthConversion, ForceConversion, PressureConversion
from dpdata.format import Format

from . import Cp2kOutput
from .block_parser.converge import parse_e_f_converge
import numpy as np
from cp2kdata.block_parser.md_xyz import parse_pos_xyz_from_wannier
import os
from .block_parser.md_xyz import parse_pos_xyz_from_wannier

logger = get_logger(__name__)


AU_TO_EV = EnergyConversion("hartree", "eV").value()
AU_TO_ANG = LengthConversion("bohr", "angstrom").value()
Expand All @@ -13,9 +18,9 @@

WRAPPER = "--- You are parsing data using package Cp2kData ---"
VIRIAL_WRN = (
"Virial Parsing using cp2kdata as plug in for dpdata\n"
"was not multiplied by volume before cp2kdata v0.6.4\n"
"please check the cp2kdata version and the virial.npy\n"
"Virial Parsing using cp2kdata as plug in for dpdata "
"was not multiplied by volume before cp2kdata v0.6.4 "
"please check the cp2kdata version and the virial.npy"
)


Expand All @@ -28,7 +33,7 @@ def from_labeled_system(self, file_name, **kwargs):
true_symbols = kwargs.get('true_symbols', False)

# -- start parsing --
print(WRAPPER)
logger.debug(WRAPPER)
converge_info = parse_e_f_converge(file_name)
if not converge_info.converge:
data = {
Expand Down Expand Up @@ -61,11 +66,11 @@ def from_labeled_system(self, file_name, **kwargs):
data['forces'] = cp2k_e_f.atomic_forces_list * AU_TO_EV/AU_TO_ANG
if cp2k_e_f.has_stress():
# note that virial = stress * volume
print(VIRIAL_WRN)
logger.warning(VIRIAL_WRN)
volume = np.linalg.det(data['cells'][0])
data['virials'] = cp2k_e_f.stress_tensor_list*volume/EV_ANG_m3_TO_GPa

print(WRAPPER)
logger.debug(WRAPPER)
return data


Expand All @@ -81,7 +86,7 @@ def from_labeled_system(self, file_name, restart: bool=None, **kwargs):
cp2k_output_name = kwargs.get('cp2k_output_name', None)

# -- start parsing --
print(WRAPPER)
logger.debug(WRAPPER)

cp2kmd = Cp2kOutput(output_file=cp2k_output_name,
run_type="MD",
Expand Down Expand Up @@ -130,26 +135,26 @@ def from_labeled_system(self, file_name, restart: bool=None, **kwargs):
data['forces'] = cp2kmd.atomic_forces_list * AU_TO_EV/AU_TO_ANG
if cp2kmd.has_stress():
# note that virial = stress * volume
print(VIRIAL_WRN)
logger.warning(VIRIAL_WRN)
# the shape of cells should be (num_frames, 3, 3)
# the np.linalg.det() function can handle this and return (num_frames,)
volumes = np.linalg.det(data['cells'])
volumes = volumes[:, np.newaxis, np.newaxis]
data['virials'] = cp2kmd.stress_tensor_list*volumes/EV_ANG_m3_TO_GPa

print(WRAPPER)
logger.debug(WRAPPER)
return data


def get_chemical_symbols_from_cp2kdata(cp2koutput, true_symbols):
if cp2koutput.atomic_kind is None:
print("Missing the atomic kind informations, atom names are true chemical symbols.")
logger.debug("Missing the atomic kind informations, atom names are true chemical symbols.")
chemical_symbols = cp2koutput.get_chemical_symbols()
elif true_symbols:
print("You have manually true_symbols=True, atom names are true chemical symbols.")
logger.debug("You have manually true_symbols=True, atom names are true chemical symbols.")
chemical_symbols = cp2koutput.get_chemical_symbols()
else:
print("Atom names are fake chemical symbols as you set in cp2k input.")
logger.debug("Atom names are fake chemical symbols as you set in cp2k input.")
chemical_symbols = cp2koutput.get_chemical_symbols_fake()
chemical_symbols = np.array(chemical_symbols)
return chemical_symbols
Expand Down
13 changes: 13 additions & 0 deletions cp2kdata/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logging
import os

level_name = os.environ.get('CP2KDATA_LOG_LEVEL', 'INFO')
level = logging._nameToLevel.get(level_name, logging.INFO)

# format to include timestamp and module
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(name)-40s: %(message)s', level=level)
# suppress transitions logging
# logging.getLogger('transitions.core').setLevel(logging.WARNING)

def get_logger(name=None):
return logging.getLogger(name)
10 changes: 7 additions & 3 deletions cp2kdata/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""
this script put misc function here.
"""
from ase.geometry.analysis import Analysis
from ase.io import read, write
#from cp2kdata import Cp2kOutput
import numpy as np
import os
import shutil

from ase.geometry.analysis import Analysis
from ase.io import read, write

from .log import get_logger

logger = get_logger(__name__)
# frequently used unit convertion
au2eV = 27.211386245988
au2A = 0.529177210903
Expand Down Expand Up @@ -75,7 +79,7 @@ def printtbox(arg):
print("--> CP2KDATA: {0}".format(arg))

def format_logger(info, filename):
print(f"Parsing {info:10} from {filename}")
logger.debug(f"Parsing {info:10} from {filename}")

def file_content(file, num):
# read a specific line of file or return the block
Expand Down
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

0 comments on commit fd92982

Please sign in to comment.