Skip to content

Commit

Permalink
Merge pull request #103 from ICAMS/clean_code
Browse files Browse the repository at this point in the history
Clean code
  • Loading branch information
srmnitc authored Jan 19, 2024
2 parents a450ac4 + e704d83 commit 92c259f
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 1,310 deletions.
1 change: 0 additions & 1 deletion calphy/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import yaml

from calphy.integrators import *
import calphy.lattice as pl
import calphy.helpers as ph
import calphy.phase as cph

Expand Down
170 changes: 1 addition & 169 deletions calphy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from lammps import lammps
from ase.io import read, write

import calphy.lattice as pl
import pyscal3.core as pc
from pyscal3.trajectory import Trajectory

Expand Down Expand Up @@ -154,46 +153,10 @@ def set_potential(lmp, options, ghost_elements=0):

return lmp


def read_dump(lmp, file, species=1):
# Read atoms positions, velocities and box parameters.
lmp.command("lattice fcc 4.0")
lmp.command("region box block 0 2 0 2 0 2")
lmp.command("create_box %d box" % species)
lmp.command(
"read_dump %s 0 x y z vx vy vz scaled no box yes add keep" % file
)
lmp.command("change_box all triclinic")
return lmp


def read_data(lmp, file):
lmp.command(f"read_data {file}")
return lmp


def convert_to_data_file(inputfile, outputfile, ghost_elements=0):
atoms = read(inputfile, format="lammps-dump-text")
write(outputfile, atoms, format="lammps-data")

if ghost_elements > 0:
lines = []
with open(outputfile, "r") as fin:
for line in fin:
raw = line.strip().split()
if (len(raw) == 3) and (raw[2] == "types"):
raw[0] = "%d" % ghost_elements
raw.append("\n")
rline = " ".join(raw)
lines.append(rline)
else:
lines.append(line)

with open(outputfile, "w") as fout:
for line in lines:
fout.write(line)


def get_structures(file, species, index=None):
traj = Trajectory(file)
if index is None:
Expand All @@ -202,94 +165,6 @@ def get_structures(file, species, index=None):
aseobjs = traj[index].to_ase(species=species)
return aseobjs


def set_hybrid_potential(lmp, options, eps, ghost_elements=0):
pc = options.pair_coeff[0]
pcraw = pc.split()
pcnew = " ".join(
[
*pcraw[:2],
*[
options._pair_style_names[0],
],
*pcraw[2:],
]
)

lmp.command(
"pair_style hybrid/overlay %s ufm 7.5"
% options._pair_style_with_options[0]
)
lmp.command("pair_coeff %s" % pcnew)
lmp.command("pair_coeff * * ufm %f 1.5" % eps)

lmp = set_mass(lmp, options, ghost_elements=ghost_elements)

return lmp


def set_double_hybrid_potential(lmp, options, ghost_elements=0):

pc1 = options.pair_coeff[0]
pcraw1 = pc1.split()

pc2 = options.pair_coeff[1]
pcraw2 = pc2.split()

if options.pair_style[0] == options.pair_style[1]:
pcnew1 = " ".join(
[
*pcraw1[:2],
*[
options._pair_style_names[0],
],
"1",
*pcraw1[2:],
]
)
pcnew2 = " ".join(
[
*pcraw2[:2],
*[
options._pair_style_names[1],
],
"2",
*pcraw2[2:],
]
)
else:
pcnew1 = " ".join(
[
*pcraw1[:2],
*[
options._pair_style_names[0],
],
*pcraw1[2:],
]
)
pcnew2 = " ".join(
[
*pcraw2[:2],
*[
options._pair_style_names[1],
],
*pcraw2[2:],
]
)

lmp.command(
"pair_style hybrid/overlay %s %s"
% (options._pair_style_with_options[0], options._pair_style_with_options[1])
)

lmp.command("pair_coeff %s" % pcnew1)
lmp.command("pair_coeff %s" % pcnew2)

lmp = set_mass(lmp, options, ghost_elements=ghost_elements)

return lmp


def remap_box(lmp, x, y, z):
lmp.command("run 0")
lmp.command(
Expand Down Expand Up @@ -338,7 +213,7 @@ def find_solid_fraction(file):
sys.find.neighbors(
method="cutoff", cutoff=5.0
) # Maybe add value as convergence param?
sys.find.solids()
sys.find.solids(cluster=False)
solids = np.sum(sys.atoms.solid)
return solids

Expand All @@ -347,49 +222,6 @@ def write_data(lmp, file):
lmp.command(f"write_data {file}")
return lmp


def reset_timestep(conf, file="current.data", init_commands=None):
lmp = create_object(
cores=1,
directory=os.path.dirname(file),
timestep=0,
cmdargs=None,
init_commands=init_commands,
)
lmp = read_data(lmp, file)
lmp = write_data(lmp, conf)
return lmp

# with open(file, "r") as f:
# with open(conf, "w") as c:
# zero = False
# for l in f:
# if zero:
# c.write("0\n")
# zero = False
# continue
# elif l.startswith("ITEM: TIMESTEP"):
# zero = True
# c.write(l)

# lmp = create_object(
# cores=1,
# directory = os.path.dirname(file),
# timestep=0,
# cmdargs=None,
# )
# lmp.command("dump 2 all custom 1 %s id type mass x y z vx vy vz"%(file))+
# lmp.command("reset_timestep 0")
# lmp.command("run 0")
# lmp.command("undump 2")


"""
NOrmal helper routines
---------------------------------------------------------------------
"""


def prepare_log(file, screen=False):
logger = logging.getLogger(__name__)
handler = logging.FileHandler(file)
Expand Down
47 changes: 19 additions & 28 deletions calphy/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,27 @@ def _check_equal(val):
return False
return True


def to_list(v: Any) -> List[Any]:
return np.atleast_1d(v)

def _to_str(val):
if np.isscalar(val):
return str(val)
else:
return [str(x) for x in val]

def _to_int(val):
if np.isscalar(val):
return int(val)
else:
return [int(x) for x in val]

def _to_float(val):
if np.isscalar(val):
return float(val)
else:
return [float(x) for x in val]

class CompositionScaling(BaseModel, title='Composition scaling input options'):
_input_chemical_composition: PrivateAttr(default=None)
output_chemical_composition: Annotated[dict, Field(default=None, required=False)]
Expand Down Expand Up @@ -517,13 +534,6 @@ def load_job(filename):
job = np.load(filename, allow_pickle=True).flatten()[0]
return job

def check_dict(indict, key, retval=None):
if key in indict.items():
return indict[key]
else:
return retval


def read_inputfile(file):
if not os.path.exists(file):
raise FileNotFoundError(f'Input file {file} not found.')
Expand Down Expand Up @@ -645,23 +655,4 @@ def _convert_legacy_inputfile(file, return_calcs=False):
warnings.warn(f'Old style input file calphy < v2 found. Converted input in {outfile}. Please check!')
with open(outfile, 'w') as fout:
yaml.safe_dump(newdata, fout)
return outfile


def _to_str(val):
if np.isscalar(val):
return str(val)
else:
return [str(x) for x in val]

def _to_int(val):
if np.isscalar(val):
return int(val)
else:
return [int(x) for x in val]

def _to_float(val):
if np.isscalar(val):
return float(val)
else:
return [float(x) for x in val]
return outfile
Loading

0 comments on commit 92c259f

Please sign in to comment.