Skip to content

Commit

Permalink
Merge pull request #138 from mir-group/feature/steven/hp_merge
Browse files Browse the repository at this point in the history
WIP: Merge development since merge onto branch
  • Loading branch information
nw13slx authored Feb 12, 2020
2 parents b25f5d4 + dcf39f9 commit a5ab9b3
Show file tree
Hide file tree
Showing 20 changed files with 554 additions and 138 deletions.
2 changes: 1 addition & 1 deletion flare/dft_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
from flare.dft_interface import vasp_util
dft_software["vasp"]=vasp_util
except ImportError:
pass
pass
20 changes: 10 additions & 10 deletions flare/dft_interface/qe_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

name = "QE"

def run_dft_par(dft_input, structure, dft_loc, ncpus=1, dft_out='pwscf.out',
def run_dft_par(dft_input, structure, dft_loc, n_cpus=1, dft_out='pwscf.out',
npool=None, mpi="mpi", **dft_kwargs):
"""run DFT calculation with given input template
and atomic configurations. if ncpus == 1, it executes serial run.
and atomic configurations. if n_cpus == 1, it executes serial run.
:param dft_input: input template file name
:param structure: atomic configuration
:param dft_loc: relative/absolute executable of the DFT code
:param ncpus: # of CPU for mpi
:param n_cpus: # of CPU for mpi
:param dft_out: output file name
:param npool: not used
:param mpi: not used
Expand All @@ -43,28 +43,28 @@ def run_dft_par(dft_input, structure, dft_loc, ncpus=1, dft_out='pwscf.out',
dft_command = \
f'{dft_loc} -nk {npool} -i {newfilename} > {dft_out}'

if (ncpus > 1):
if (n_cpus > 1):
if (mpi == "mpi"):
dft_command = f'mpirun -np {ncpus} {dft_command}'
dft_command = f'mpirun -np {n_cpus} {dft_command}'
else:
dft_command = f'srun -n {ncpus} --mpi=pmi2 {dft_command}'
dft_command = f'srun -n {n_cpus} --mpi=pmi2 {dft_command}'

call(dft_command, shell=True)
os.remove(newfilename)

return parse_dft_forces(dft_out)


def run_dft_en_par(dft_input, structure, dft_loc, ncpus):
def run_dft_en_par(dft_input, structure, dft_loc, n_cpus):
"""run DFT calculation with given input template
and atomic configurations. This function is not used atm
if ncpus == 1, it executes serial run.
if n_cpus == 1, it executes serial run.
:param dft_input: input template file name
:param structure: atomic configuration
:param dft_loc: relative/absolute executable of the DFT code
:param ncpus: # of CPU for mpi
:param n_cpus: # of CPU for mpi
:param dft_out: output file name
:param npool: not used
:param mpi: not used
Expand All @@ -75,7 +75,7 @@ def run_dft_en_par(dft_input, structure, dft_loc, ncpus):
run_qe_path = dft_input
edit_dft_input_positions(run_qe_path, structure)
qe_command = \
'mpirun -np {ncpus} {dft_loc} < {run_qe_path} > pwscf.out'
'mpirun -np {n_cpus} {dft_loc} < {run_qe_path} > pwscf.out'
call(qe_command, shell=True)

forces, energy = parse_dft_forces_and_energy('pwscf.out')
Expand Down
6 changes: 3 additions & 3 deletions flare/dft_interface/vasp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def run_dft(calc_dir: str, dft_loc: str,


def run_dft_par(dft_input: str, structure: Structure,
dft_command:str= None, ncpus=1,
dft_command:str= None, n_cpus=1,
dft_out="vasprun.xml",
parallel_prefix="mpi",
mpi = None, npool = None,
Expand All @@ -112,9 +112,9 @@ def run_dft_par(dft_input: str, structure: Structure,
("Warning: No VASP Command passed, or stored in "
"environment as VASP_COMMAND. ")

if ncpus > 1:
if n_cpus > 1:
if parallel_prefix == "mpi":
dft_command = f'mpirun -np {ncpus} {dft_command}'
dft_command = f'mpirun -np {n_cpus} {dft_command}'
else:
serial_prefix = dft_kwargs.get('serial_prefix', '')
dft_command = f'{serial_prefix} {dft_command}'
Expand Down
57 changes: 42 additions & 15 deletions flare/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class GaussianProcess:
hyperparameter optimization algorithm. Defaults to 10.
par (bool, optional): If True, the covariance matrix K of the GP is
computed in parallel. Defaults to False.
ncpus (int, optional): Number of cpus used for parallel
n_cpus (int, optional): Number of cpus used for parallel
calculations. Defaults to 1.
output (Output, optional): Output object used to dump hyperparameters
during optimization. Defaults to None.
Expand All @@ -66,7 +66,7 @@ def __init__(self, kernel: Callable,
opt_algorithm: str = 'L-BFGS-B',
maxiter: int = 10, par: bool = False,
per_atom_par: bool = True,
ncpus: int = None, nsample: int = 100,
n_cpus: int = 1, nsample: int = 100,
output: Output = None,
multihyps: bool = False, hyps_mask: dict = None):
"""Initialize GP parameters and training data."""
Expand All @@ -89,7 +89,7 @@ def __init__(self, kernel: Callable,
self.maxiter = maxiter
self.par = par
self.per_atom_par = per_atom_par
self.ncpus = ncpus
self.n_cpus = n_cpus
self.nsample = nsample
self.output = output

Expand Down Expand Up @@ -245,7 +245,7 @@ def train(self, output=None, custom_bounds=None,
args = (self.training_data, self.training_labels_np,
self.kernel_grad, output,
self.cutoffs, self.hyps_mask,
self.ncpus, self.nsample)
self.n_cpus, self.nsample)
objective_func = get_neg_like_grad
res = None

Expand Down Expand Up @@ -323,16 +323,16 @@ def predict(self, x_t: AtomicEnvironment, d: int) -> [float, float]:

# Kernel vector allows for evaluation of At. Env.
if (self.par and not self.per_atom_par):
ncpus = self.ncpus
n_cpus = self.n_cpus
else:
ncpus = 1
n_cpus = 1

k_v = get_kernel_vector_par(self.training_data, self.kernel,
x_t, d,
self.hyps,
cutoffs=self.cutoffs,
hyps_mask=self.hyps_mask,
ncpus=self.ncpus,
n_cpus=self.n_cpus,
nsample=self.nsample)

# Guarantee that alpha is up to date with training set
Expand Down Expand Up @@ -479,7 +479,7 @@ def set_L_alpha(self):
self.kernel,
cutoffs=self.cutoffs,
hyps_mask=self.hyps_mask,
ncpus=self.ncpus,
n_cpus=self.n_cpus,
nsample=self.nsample)

l_mat = np.linalg.cholesky(ky_mat)
Expand All @@ -506,16 +506,16 @@ def update_L_alpha(self):
return

if (self.par and not self.per_atom_par):
ncpus=self.ncpus
n_cpus=self.n_cpus
else:
ncpus=1
n_cpus=1

ky_mat = get_ky_mat_update_par(self.ky_mat, self.hyps,
self.training_data,
self.kernel,
cutoffs=self.cutoffs,
hyps_mask=self.hyps_mask,
ncpus=ncpus,
n_cpus=n_cpus,
nsample=self.nsample)

l_mat = np.linalg.cholesky(ky_mat)
Expand Down Expand Up @@ -585,7 +585,7 @@ def from_dict(dictionary):
"""Create GP object from dictionary representation."""

if 'mc' in dictionary['kernel_name']:
if (dictionary['multihyps'] is False):
if (dictionary.get('multihyps',False) is False):
force_kernel, grad = \
str_to_mc_kernel(dictionary['kernel_name'], include_grad=True)
else:
Expand Down Expand Up @@ -615,11 +615,13 @@ def from_dict(dictionary):
hyps=np.array(dictionary['hyps']),
hyp_labels=dictionary['hyp_labels'],
par=dictionary['par'],
ncpus=dictionary['ncpus'],
per_atom_par=dictionary.get('per_atom_par',True),
n_cpus=dictionary.get('n_cpus') or dictionary.get('no_cpus'),
maxiter=dictionary['maxiter'],
opt_algorithm=dictionary['algo'],
multihyps=dictionary['multihyps'],
hyps_mask=dictionary['hyps_mask'])
multihyps=dictionary.get('multihyps',False),
hyps_mask=dictionary.get('hyps_mask',None)
)

new_gp.training_data = [AtomicEnvironment.from_dict(env) for env in
dictionary['training_data']]
Expand Down Expand Up @@ -688,3 +690,28 @@ def write_model(self, name: str, format: str = 'json'):
if (len(self.training_data)>5000):
self.ky_mat = np.load(f"{name}_ky_mat.npy")
self.compute_matrices()


@staticmethod
def from_file(filename: str, format: str=''):
"""
One-line convenience method to load a GP from a file stored using
write_file
Args:
filename (str): path to GP model
format (str): json or pickle if format is not in filename
:return:
"""

if '.json' in filename or 'json' in format:
with open(filename, 'r') as f:
return GaussianProcess.from_dict(json.loads(f.readline()))

elif '.pickle' in filename or 'pickle' in format:
with open(filename, 'rb') as f:
return pickle.load(f)

else:
raise ValueError("Warning: Format unspecified or file is not "
".json or .pickle format.")
Loading

0 comments on commit a5ab9b3

Please sign in to comment.