Skip to content

Commit

Permalink
Merge pull request #80 from ICAMS/min_mode
Browse files Browse the repository at this point in the history
Min mode
  • Loading branch information
srmnitc authored Sep 7, 2023
2 parents 0a881ce + 3c2b5b9 commit 2705bbf
Show file tree
Hide file tree
Showing 13 changed files with 638 additions and 153 deletions.
3 changes: 1 addition & 2 deletions calphy/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ def run_averaging(self):
self.check_if_melted(lmp, "traj.equilibration_stage2.dat")

#close object and process traj
lmp = ph.write_data(lmp, "current.data")
lmp = ph.write_data(lmp, "conf.equilibration.data")
lmp.close()
self.process_traj("traj.equilibration_stage2.dat", "conf.equilibration.data")



Expand Down
92 changes: 92 additions & 0 deletions calphy/clitools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import os
import numpy as np
import shutil
import argparse as ap
import subprocess
import yaml
import time
import datetime

from calphy.input import read_inputfile, load_job, save_job
from calphy.liquid import Liquid
from calphy.solid import Solid
from calphy.alchemy import Alchemy

def _generate_job(calc, simfolder):
if calc.mode == "alchemy" or calc.mode == "composition_scaling":
job = Alchemy(calculation=calc, simfolder=simfolder)
return job
else:
if calc.reference_phase == "liquid":
job = Liquid(calculation=calc, simfolder=simfolder)
return job
else:
job = Solid(calculation=calc, simfolder=simfolder)
return job


def run_averaging():
arg = ap.ArgumentParser()
arg.add_argument("-i", "--input", required=True, type=str,
help="name of the input file")
arg.add_argument("-k", "--kernel", required=True, type=int,
help="kernel number of the calculation to be run.")
args = vars(arg.parse_args())
kernel = args["kernel"]
calculations = read_inputfile(args["input"])
calc = calculations[kernel]

simfolder = calc.create_folders()
job = _generate_job(calc, simfolder)
os.chdir(simfolder)


job.run_averaging()
save_job(job)


def process_averaging():
arg = ap.ArgumentParser()
arg.add_argument("-i", "--input", required=True, type=str,
help="name of the input file")
arg.add_argument("-k", "--kernel", required=True, type=int,
help="kernel number of the calculation to be run.")
args = vars(arg.parse_args())
kernel = args["kernel"]
calculations = read_inputfile(args["input"])
calc = calculations[kernel]

job = load_job(calc.savefile)
job.process_averaging_results()
save_job(job)

def run_integration():
arg = ap.ArgumentParser()
arg.add_argument("-i", "--input", required=True, type=str,
help="name of the input file")
arg.add_argument("-k", "--kernel", required=True, type=int,
help="kernel number of the calculation to be run.")
args = vars(arg.parse_args())
kernel = args["kernel"]
calculations = read_inputfile(args["input"])
calc = calculations[kernel]

job = load_job(calc.savefile)
job.run_integration()
save_job(job)

def process_integration():
arg = ap.ArgumentParser()
arg.add_argument("-i", "--input", required=True, type=str,
help="name of the input file")
arg.add_argument("-k", "--kernel", required=True, type=int,
help="kernel number of the calculation to be run.")
args = vars(arg.parse_args())
kernel = args["kernel"]
calculations = read_inputfile(args["input"])
calc = calculations[kernel]

job = load_job(calc.savefile)
job.thermodynamic_integration()
job.submit_report()
save_job(job)
28 changes: 22 additions & 6 deletions calphy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,20 @@
from ase.io import read, write
from pyscal.trajectory import Trajectory

class LammpsScript:
def __init__(self):
self.script = []

def command(self, command_str):
self.script.append(command_str)

def write(self, infile):
with open(infile, 'w') as fout:
for line in self.script:
fout.write(f'{line}\n')

def create_object(cores, directory, timestep, cmdargs=None,
init_commands=None):
init_commands=None, script_mode=False):
"""
Create LAMMPS object
Expand All @@ -53,9 +64,12 @@ def create_object(cores, directory, timestep, cmdargs=None,
-------
lmp : LammpsLibrary object
"""
lmp = LammpsLibrary(
cores=cores, working_directory=directory, cmdargs=cmdargs
)
if script_mode:
lmp = LammpsScript()
else:
lmp = LammpsLibrary(
cores=cores, working_directory=directory, cmdargs=cmdargs
)

commands = [["units", "metal"],
["boundary", "p p p"],
Expand Down Expand Up @@ -150,8 +164,10 @@ def set_potential(lmp, options, ghost_elements=0):
-------
lmp : LammpsLibrary object
"""
lmp.pair_style(options.pair_style_with_options[0])
lmp.pair_coeff(options.pair_coeff[0])
#lmp.pair_style(options.pair_style_with_options[0])
#lmp.pair_coeff(options.pair_coeff[0])
lmp.command(f'pair_style {options.pair_style_with_options[0]}')
lmp.command(f'pair_coeff {options.pair_coeff[0]}')

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

Expand Down
94 changes: 72 additions & 22 deletions calphy/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ def __init__(self):
self._reference_phase = None
self._lattice_constant = 0
self._repeat = [1, 1, 1]
self._script_mode = False
self._lammps_executable = None
self._mpi_executable = None
self._npt = True
self._n_equilibration_steps = 25000
self._n_switching_steps = 50000
Expand Down Expand Up @@ -242,20 +245,13 @@ def to_string(self, val):
else:
return str(val)

def to_bool(self, val):
if val == "True":
val = True
elif val == "False":
val = False
elif val == 1:
val = True
elif val == 0:
val = False
elif val == "1":
val = True
elif val == "0":
val = False
return val
def to_bool(self, val):
if val in ["True", "true", 1, "1", True]:
return True
elif val in ["False", "false", 0, "0", False]:
return False
else:
raise ValueError(f'Unknown bool input of type {type(val)} with value {val}')

@property
def element(self):
Expand Down Expand Up @@ -531,7 +527,39 @@ def npt(self):

@npt.setter
def npt(self, val):
self._npt = val
val = self.to_bool(val)
if isinstance(val, bool):
self._npt = val
else:
raise TypeError("NPT should be either True/False")

@property
def script_mode(self):
return self._script_mode

@script_mode.setter
def script_mode(self, val):
val = self.to_bool(val)
if isinstance(val, bool):
self._script_mode = val
else:
raise TypeError("script mode should be either True/False")

@property
def lammps_executable(self):
return self._lammps_executable

@lammps_executable.setter
def lammps_executable(self, val):
self._lammps_executable = val

@property
def mpi_executable(self):
return self._mpi_executable

@mpi_executable.setter
def mpi_executable(self, val):
self._mpi_executable = val

@property
def n_equilibration_steps(self):
Expand Down Expand Up @@ -664,7 +692,12 @@ def create_identifier(self):
identistring = "-".join([self.folder_prefix, prefix, l, str(ts), str(ps)])
return identistring

def create_folders(self, prefix=None):
def get_folder_name(self):
identistring = self.create_identifier()
simfolder = os.path.join(os.getcwd(), identistring)
return simfolder

def create_folders(self):
"""
Create the necessary folder for calculation
Expand All @@ -678,11 +711,7 @@ def create_folders(self, prefix=None):
folder : string
create folder
"""
identistring = self.create_identifier()
if prefix is None:
simfolder = os.path.join(os.getcwd(), identistring)
else:
simfolder = os.path.join(prefix, identistring)
simfolder = self.get_folder_name()

#if folder exists, delete it -> then create
try:
Expand All @@ -706,6 +735,10 @@ def generate(cls, indata):
calc = cls()
calc.element = indata["element"]
calc.mass = indata["mass"]
calc.script_mode = indata["script_mode"]
calc.lammps_executable = indata["lammps_executable"]
calc.mpi_executable = indata["mpi_executable"]

if "md" in indata.keys():
calc.md.add_from_dict(indata["md"])
if "queue" in indata.keys():
Expand Down Expand Up @@ -735,6 +768,12 @@ def read_file(file):
else:
raise FileNotFoundError('%s input file not found'% file)
return indata

@property
def savefile(self):
simfolder = self.get_folder_name()
return os.path.join(simfolder, 'job.npy')


def read_inputfile(file):
"""
Expand Down Expand Up @@ -793,7 +832,8 @@ def read_inputfile(file):
#create calculations
for combo in combos:
calc = Calculation.generate(indata)
calc.add_from_dict(ci, keys=["mode", "pair_style", "pair_coeff", "pair_style_options", "npt", "repeat", "n_equilibration_steps",
calc.add_from_dict(ci, keys=["mode", "pair_style", "pair_coeff", "pair_style_options", "npt",
"repeat", "n_equilibration_steps",
"n_switching_steps", "n_print_steps", "n_iterations", "potential_file", "spring_constants",
"melting_cycle", "equilibration_control", "folder_prefix", "temperature_high"])
calc.lattice = combo[0]["lattice"]
Expand All @@ -803,3 +843,13 @@ def read_inputfile(file):
calc.temperature = combo[2]
calculations.append(calc)
return calculations


def save_job(job):
filename = os.path.join(job.simfolder, 'job.npy')
np.save(filename, job)

def load_job(filename):
job = np.load(filename, allow_pickle=True).flatten()[0]
return job

Loading

0 comments on commit 2705bbf

Please sign in to comment.