Skip to content

Commit

Permalink
Move to tomli (#130)
Browse files Browse the repository at this point in the history
* Move to tomli, drop nested lists in config.toml

* Update test

* Update requirements

* Update conftest

* Update nesting for bonds, angles and chi
  • Loading branch information
Manuel Carrer authored Nov 16, 2021
1 parent edf9285 commit d3aff97
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 229 deletions.
38 changes: 19 additions & 19 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,33 @@ sigma = 0.5
# Interaction matrix, chi, ((atom name 1, atom name 2), (mixing energy in
# [kJ/mol])).
chi = [
[["C", "W"], [42.24]],
[["G", "C"], [10.47]],
[["N", "W"], [-3.77]],
[["G", "W"], [4.53]],
[["N", "P"], [-9.34]],
[["P", "G"], [8.04]],
[["N", "G"], [1.97]],
[["P", "C"], [14.72]],
[["P", "W"], [-1.51]],
[["N", "C"], [13.56]],
["C", "W", 42.24],
["G", "C", 10.47],
["N", "W", -3.77],
["G", "W", 4.53],
["N", "P", -9.34],
["P", "G", 8.04],
["N", "G", 1.97],
["P", "C", 14.72],
["P", "W", -1.51],
["N", "C", 13.56],
]

[bonds]
# Two-particle bonds, ((atom name 1, atom name 2), (equilibrium length in
# [nanometers], bond strenght in [kJ/mol])). Note the two
bonds = [
[["N", "P"], [0.47, 1250.0]],
[["P", "G"], [0.47, 1250.0]],
[["G", "G"], [0.37, 1250.0]],
[["G", "C"], [0.47, 1250.0]],
[["C", "C"], [0.47, 1250.0]],
["N", "P", 0.47, 1250.0],
["P", "G", 0.47, 1250.0],
["G", "G", 0.37, 1250.0],
["G", "C", 0.47, 1250.0],
["C", "C", 0.47, 1250.0],
]
# Three-particle angular bonds, ((atom name 1, atom name 2, atom name 3),
# (equilibrium angle in [degrees], bond strenght in [kJ/mol])).
angle_bonds = [
[["P", "G", "G"], [120.0, 25.0]],
[["P", "G", "C"], [180.0, 25.0]],
[["G", "C", "C"], [180.0, 25.0]],
[["C", "C", "C"], [180.0, 25.0]],
["P", "G", "G", 120.0, 25.0],
["P", "G", "C", 180.0, 25.0],
["G", "C", "C", 180.0, 25.0],
["C", "C", "C", 180.0, 25.0],
]
38 changes: 19 additions & 19 deletions examples/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,33 @@ sigma = 0.5
# Interaction matrix, chi, ((atom name 1, atom name 2), (mixing energy in
# [kJ/mol])).
chi = [
[["C", "W"], [42.24]],
[["G", "C"], [10.47]],
[["N", "W"], [-3.77]],
[["G", "W"], [4.53]],
[["N", "P"], [-9.34]],
[["P", "G"], [8.04]],
[["N", "G"], [1.97]],
[["P", "C"], [14.72]],
[["P", "W"], [-1.51]],
[["N", "C"], [13.56]],
["C", "W", 42.24],
["G", "C", 10.47],
["N", "W", -3.77],
["G", "W", 4.53],
["N", "P", -9.34],
["P", "G", 8.04],
["N", "G", 1.97],
["P", "C", 14.72],
["P", "W", -1.51],
["N", "C", 13.56],
]

[bonds]
# Two-particle bonds, ((atom name 1, atom name 2), (equilibrium length in
# [nanometers], bond strength in [kJ/mol])). Note the two
bonds = [
[["N", "P"], [0.47, 1250.0]],
[["P", "G"], [0.47, 1250.0]],
[["G", "G"], [0.37, 1250.0]],
[["G", "C"], [0.47, 1250.0]],
[["C", "C"], [0.47, 1250.0]],
["N", "P", 0.47, 1250.0],
["P", "G", 0.47, 1250.0],
["G", "G", 0.37, 1250.0],
["G", "C", 0.47, 1250.0],
["C", "C", 0.47, 1250.0],
]
# Three-particle angular bonds, ((atom name 1, atom name 2, atom name 3),
# (equilibrium angle in [degrees], bond strength in [kJ/mol])).
angle_bonds = [
[["P", "G", "G"], [120.0, 25.0]],
[["P", "G", "C"], [180.0, 25.0]],
[["G", "C", "C"], [180.0, 25.0]],
[["C", "C", "C"], [180.0, 25.0]],
["P", "G", "G", 120.0, 25.0],
["P", "G", "C", 180.0, 25.0],
["G", "C", "C", 180.0, 25.0],
["C", "C", "C", 180.0, 25.0],
]
40 changes: 21 additions & 19 deletions hymd/input_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import copy
import toml
import tomli
import datetime
import logging
import warnings
Expand Down Expand Up @@ -69,12 +69,14 @@ def __str__(self):
)
thermostat_coupling_groups_str = ""
if any(self.thermostat_coupling_groups):
thermostat_coupling_groups_str = "\tthermostat_coupling_groups:\n" + "".join(
[
"\t\t" + ", ".join(
[f"{n}" for n in ng]
) + "\n" for ng in self.thermostat_coupling_groups
]
thermostat_coupling_groups_str = (
"\tthermostat_coupling_groups:\n"
+ "".join(
[
"\t\t" + ", ".join([f"{n}" for n in ng]) + "\n"
for ng in self.thermostat_coupling_groups
]
)
)

ret_str = f'\n\n\tConfig: {self.file_name}\n\t{50 * "-"}\n'
Expand Down Expand Up @@ -200,7 +202,7 @@ def read_config_toml(file_path):


def parse_config_toml(toml_content, file_path=None, comm=MPI.COMM_WORLD):
parsed_toml = toml.loads(toml_content)
parsed_toml = tomli.loads(toml_content)
config_dict = {}

# Defaults = None
Expand Down Expand Up @@ -236,27 +238,27 @@ def parse_config_toml(toml_content, file_path=None, comm=MPI.COMM_WORLD):
config_dict["bonds"] = [None] * len(v)
for i, b in enumerate(v):
config_dict["bonds"][i] = Bond(
atom_1=b[0][0],
atom_2=b[0][1],
equilibrium=b[1][0],
strength=b[1][1],
atom_1=b[0],
atom_2=b[1],
equilibrium=b[2],
strength=b[3],
)
if k == "angle_bonds":
config_dict["angle_bonds"] = [None] * len(v)
for i, b in enumerate(v):
config_dict["angle_bonds"][i] = Angle(
atom_1=b[0][0],
atom_2=b[0][1],
atom_3=b[0][2],
equilibrium=b[1][0],
strength=b[1][1],
atom_1=b[0],
atom_2=b[1],
atom_3=b[2],
equilibrium=b[3],
strength=b[4],
)
if k == "chi":
config_dict["chi"] = [None] * len(v)
for i, c in enumerate(v):
c_ = sorted([c[0][0], c[0][1]])
c_ = sorted([c[0], c[1]])
config_dict["chi"][i] = Chi(
atom_1=c_[0], atom_2=c_[1], interaction_energy=c[1][0]
atom_1=c_[0], atom_2=c_[1], interaction_energy=c[2]
)

if file_path is not None:
Expand Down
11 changes: 6 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
networkx
cython
numpy
sympy
h5py
mpi4py
mpsort
networkx
numba
numpy
pfft-python
pmesh
h5py
numba
sympy
tomli
38 changes: 19 additions & 19 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,31 @@ def config_toml(mpi_file_name):
kappa = 0.05
sigma = 0.5
chi = [
[["C", "W"], [42.24]],
[["G", "C"], [10.47]],
[["N", "W"], [-3.77]],
[["G", "W"], [4.53]],
[["N", "P"], [-9.34]],
[["P", "G"], [8.04]],
[["N", "G"], [1.97]],
[["P", "C"], [14.72]],
[["P", "W"], [-1.51]],
[["N", "C"], [13.56]],
["C", "W", 42.24],
["G", "C", 10.47],
["N", "W", -3.77],
["G", "W", 4.53],
["N", "P", -9.34],
["P", "G", 8.04],
["N", "G", 1.97],
["P", "C", 14.72],
["P", "W", -1.51],
["N", "C", 13.56],
]
[bonds]
bonds = [
[["N", "P"], [0.47, 1250.0]],
[["P", "G"], [0.47, 1250.0]],
[["G", "G"], [0.37, 1250.0]],
[["G", "C"], [0.47, 1250.0]],
[["C", "C"], [0.47, 1250.0]],
["N", "P", 0.47, 1250.0],
["P", "G", 0.47, 1250.0],
["G", "G", 0.37, 1250.0],
["G", "C", 0.47, 1250.0],
["C", "C", 0.47, 1250.0],
]
angle_bonds = [
[["P", "G", "G"], [120.0, 25.0]],
[["P", "G", "C"], [180.0, 25.0]],
[["G", "C", "C"], [180.0, 25.0]],
[["C", "C", "C"], [180.0, 25.0]],
["P", "G", "G", 120.0, 25.0],
["P", "G", "C", 180.0, 25.0],
["G", "C", "C", 180.0, 25.0],
["C", "C", "C", 180.0, 25.0],
]
"""
if MPI.COMM_WORLD.Get_rank() == 0:
Expand Down
Loading

0 comments on commit d3aff97

Please sign in to comment.