Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output temperatures in Kelvin (revert to the old behavior) #70

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion abics/sampling/mc_mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ class ParallelMC(object):
"MPI size"
kTs: list[float]
"Temperatures"
T2E: float
"Temperature to energy conversion factor"
E2T: float
"Energy to temperature conversion factor"
nreplicas: int
"The number of replicas"
model: Model
Expand All @@ -230,6 +234,7 @@ def __init__(
configs,
kTs,
write_node=True,
T2E=1.0,
):
"""

Expand All @@ -245,14 +250,18 @@ def __init__(
Configurations
kTs: list
Temperature list
T2E: float
Temperature to energy conversion factor
"""
self.comm = comm
self.rank = self.comm.Get_rank()
self.procs = self.comm.Get_size()
self.kTs = kTs
self.model = model
self.nreplicas = len(configs)
self.write_node = write_node
self.T2E = T2E
self.E2T = 1.0 / T2E
self.kTs = [T2E * T for T in kTs]

## mycalc.kT and mycalc.config should be set later
myconfig = configs[0]
Expand Down
20 changes: 12 additions & 8 deletions abics/sampling/pamc.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def from_dict(cls, d):
kTend = d["kTend"]
kTnum = d["kTnum"]
params.kTs = np.linspace(kTstart, kTend, kTnum)

if "nsteps_between_anneal" in d:
params.nsteps = d["nsteps_between_anneal"] * kTnum
if "nsteps" in d:
Expand Down Expand Up @@ -157,7 +158,8 @@ def __init__(
model: Model,
configs,
kTs: np.ndarray,
write_node=True,
write_node: bool=True,
T2E: float=1.0,
):
"""

Expand All @@ -179,8 +181,8 @@ def __init__(
if kTs[0] < kTs[-1]:
logger.warning("Warning: kTs[0] < kTs[-1]. abICS reverses kTs.")
kTs = kTs[::-1]
super().__init__(comm, MCalgo, model, configs, kTs, write_node=write_node)
self.mycalc.kT = kTs[0]
super().__init__(comm, MCalgo, model, configs, kTs, write_node=write_node, T2E=T2E)
self.mycalc.kT = self.kTs[0]
self.mycalc.config = configs[self.rank]
self.betas = 1.0 / np.array(kTs)
self.obs_save = []
Expand Down Expand Up @@ -360,7 +362,8 @@ def run(
self.anneal(self.mycalc.energy)
logger.info(
"--Anneal from T={} to {}".format(
self.kTs[self.Tindex - 1], self.kTs[self.Tindex]
self.E2T*self.kTs[self.Tindex - 1],
self.E2T*self.kTs[self.Tindex]
)
)
if self.Tindex % resample_frequency == 0:
Expand Down Expand Up @@ -389,7 +392,7 @@ def run(
self.acceptance_ratios.append(naccepted / ntrials)
with open("acceptance_ratio.dat", "w") as f:
for T, ar in zip(self.kTs, self.acceptance_ratios):
f.write(f"{T} {ar}\n")
f.write(f"{self.E2T*T} {ar}\n")
self.Tindex += 1
self.isamples_offsets[self.Tindex] = nsample
output.close()
Expand Down Expand Up @@ -421,7 +424,7 @@ def postproc(self):
isamples_offsets = self.isamples_offsets
comm = self.comm
postproc(
kTs, obs_save, dlogz, logweight_history, obsnames, isamples_offsets, comm
kTs, obs_save, dlogz, logweight_history, obsnames, isamples_offsets, comm, E2T=self.E2T
)


Expand All @@ -433,6 +436,7 @@ def postproc(
obsnames,
isamples_offsets,
comm,
E2T:float=1.0,
):
procs = comm.Get_size()
rank = comm.Get_rank()
Expand Down Expand Up @@ -505,7 +509,7 @@ def postproc(
f.write(f"# $7: ERROR of <{oname}^2> - <{oname}>^2\n")

for iT in range(nT):
f.write(f"{kTs[iT]}")
f.write(f"{E2T*kTs[iT]}")
for j in range(3):
f.write(f" {o_mean[iT, 3*iobs+j]} {o_err[iT, 3*iobs+j]}")
f.write("\n")
Expand All @@ -523,5 +527,5 @@ def postproc(
for i, (dlz, dlz_e) in enumerate(zip(dlogZ, dlogZ_err)):
F += dlz
dF += dlz_e
f.write(f"{kTs[i]} {F} {dF} {dlz} {dlz_e}\n")
f.write(f"{E2T*kTs[i]} {F} {dF} {dlz} {dlz_e}\n")
comm.Barrier()
29 changes: 16 additions & 13 deletions abics/sampling/rxmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def __init__(
model: Model,
configs,
kTs: list[float],
write_node=True,
write_node: bool=True,
T2E: float=1.0,
):
"""

Expand All @@ -154,10 +155,10 @@ def __init__(
subdirs: boolean
If true, working directory for this rank is made
"""
super().__init__(comm, MCalgo, model, configs, kTs, write_node=write_node)
self.mycalc.kT = kTs[self.rank]
super().__init__(comm, MCalgo, model, configs, kTs, write_node=write_node, T2E=T2E)
self.mycalc.kT = self.kTs[self.rank]
self.mycalc.config = configs[self.rank]
self.betas = 1.0 / np.array(kTs)
self.betas = 1.0 / np.array(self.kTs)
self.rank_to_T = np.arange(0, self.procs, 1, dtype=np.int64)
self.float_buffer = np.array(0.0, dtype=np.float64)
self.int_buffer = np.array(0, dtype=np.int64)
Expand Down Expand Up @@ -360,9 +361,9 @@ def run(
with open("acceptance_ratio.dat", "w") as f:
for T, acc, trial in zip(self.kTs, self.naccepted, self.ntrials):
if trial > 0:
f.write(f"{T} {acc/trial}\n")
f.write(f"{self.E2T*T} {acc/trial}\n")
else:
f.write(f"{T} {acc}/{trial}\n")
f.write(f"{self.E2T*T} {acc}/{trial}\n")

if nsample != 0:
obs_buffer = np.empty(obs.shape)
Expand Down Expand Up @@ -406,7 +407,7 @@ def postproc(self, throw_out: int | float):
kTs = self.kTs
comm = self.comm
obsnames = self.obsnames
postproc(obs_save, Trank_hist, kT_hist, kTs, comm, obsnames, throw_out)
postproc(obs_save, Trank_hist, kT_hist, kTs, comm, obsnames, throw_out, E2T=self.E2T)

def __merge_obs(self):
if hasattr(self, "obs_save0"):
Expand All @@ -427,7 +428,9 @@ def jackknife(X: np.ndarray) -> np.ndarray:


def postproc(obs_save, Trank_hist, kT_hist, kTs, comm,
obsnames, throw_out: int | float):
obsnames, throw_out: int | float,
E2T: float = 1.0,
):
assert throw_out >= 0
rank = comm.Get_rank()
nT = comm.Get_size()
Expand Down Expand Up @@ -513,7 +516,7 @@ def postproc(obs_save, Trank_hist, kT_hist, kTs, comm,
f.write(f"# $6: <{oname}^2> - <{oname}>^2\n")
f.write(f"# $7: ERROR of <{oname}^2> - <{oname}>^2\n")
for iT in range(nT):
f.write(f"{kTs[iT]}")
f.write(f"{E2T*kTs[iT]}")
for itype in range(ntype):
f.write(f" {obs_all[iT, itype, iobs]}")
f.write("\n")
Expand All @@ -527,17 +530,17 @@ def postproc(obs_save, Trank_hist, kT_hist, kTs, comm,
F = 0.0
dF = 0.0
if kTs[0] <= kTs[-1]:
f.write(f"{kTs[-1]} {F} {dF} {0.0} {0.0}\n")
f.write(f"{E2T*kTs[-1]} {F} {dF} {0.0} {0.0}\n")
for iT in np.arange(1, nT)[::-1]:
dlz, dlz_e = dlogz[iT]
F += dlz
dF += dlz_e
f.write(f"{kTs[iT-1]} {F} {dF} {dlz} {dlz_e}\n")
f.write(f"{E2T*kTs[iT-1]} {F} {dF} {dlz} {dlz_e}\n")
else:
f.write(f"{kTs[0]} {F} {dF} {0.0} {0.0}\n")
f.write(f"{E2T*kTs[0]} {F} {dF} {0.0} {0.0}\n")
for iT in np.arange(0, nT - 1):
dlz, dlz_e = dlogz[iT]
F += dlz
dF += dlz_e
f.write(f"{kTs[iT+1]} {F} {dF} {dlz} {dlz_e}\n")
f.write(f"{E2T*kTs[iT+1]} {F} {dF} {dlz} {dlz_e}\n")
comm.Barrier()
23 changes: 14 additions & 9 deletions abics/sampling/simple_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def __init__(
model: Model,
configs,
kTs=None,
write_node=True,
write_node:bool=True,
T2E:float=1.0,
):
"""

Expand All @@ -234,7 +235,9 @@ def __init__(
kTs = [0] * self.procs
if isinstance(kTs, (int, float)):
kTs = [kTs] * self.procs
self.kTs = kTs
self.T2E = T2E
self.E2T = 1.0 / T2E
self.kTs = [T2E * T for T in kTs]
self.model = model
self.nreplicas = len(configs)
self.write_node = write_node
Expand All @@ -248,7 +251,7 @@ def __init__(
sys.exit(1)

myconfig = configs[self.rank]
mytemp = kTs[self.rank]
mytemp = self.kTs[self.rank]
self.mycalc = MCalgo(model, mytemp, myconfig)
self.obs_save: list[np.ndarray] = []
self.kT_hist: list[float] = []
Expand Down Expand Up @@ -381,11 +384,11 @@ def run(


def postproc(self, throw_out):
postproc(obs_save=np.array(self.obs_save), kTs=self.kTs, comm=self.comm, obsnames=self.obsnames, throw_out=throw_out)
postproc(obs_save=np.array(self.obs_save), kTs=self.kTs, comm=self.comm, obsnames=self.obsnames, throw_out=throw_out, E2T=self.E2T)

class RandomSampling_MPI(ParallelMC):
def __init__(
self, comm, MCalgo: type[MCAlgorithm], model: Model, configs, write_node=True
self, comm, MCalgo: type[MCAlgorithm], model: Model, configs, write_node=True, T2E:float=1.0,
):
"""

Expand All @@ -401,8 +404,8 @@ def __init__(
Configuration
"""

super().__init__(comm, MCalgo, model, configs, kTs, write_node=write_node)
self.mycalc.kT = kTs[self.rank]
super().__init__(comm, MCalgo, model, configs, kTs, write_node=write_node, T2E=T2E)
self.mycalc.kT = self.kTs[self.rank]
self.mycalc.config = configs[self.rank]
self.betas = 1.0 / np.array(kTs)
self.rank_to_T = np.arange(0, self.procs, 1, dtype=np.int64)
Expand All @@ -415,7 +418,9 @@ def __init__(


def postproc(obs_save, kTs, comm,
obsnames, throw_out: int | float):
obsnames, throw_out: int | float,
E2T: float = 1.0,
):
assert throw_out >= 0
rank = comm.Get_rank()
nT = comm.Get_size()
Expand Down Expand Up @@ -456,7 +461,7 @@ def postproc(obs_save, kTs, comm,
f.write(f"# $6: <{oname}^2> - <{oname}>^2\n")
f.write(f"# $7: ERROR of <{oname}^2> - <{oname}>^2\n")
for iT in range(nT):
f.write(f"{kTs[iT]}")
f.write(f"{E2T*kTs[iT]}")
for itype in range(ntype):
f.write(f" {obs_all[iT, itype, iobs]}")
f.write("\n")
Expand Down
12 changes: 6 additions & 6 deletions abics/scripts/main_dft_latgas.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def main_dft_latgas(params_root: MutableMapping):

# RXMC parameters
# specify temperatures for each replica, number of steps, etc.
kTs = kB * rxparams.kTs
kTs = rxparams.kTs
kTstart = rxparams.kTs[0]
kTend = rxparams.kTs[-1]

Expand Down Expand Up @@ -122,7 +122,7 @@ def main_dft_latgas(params_root: MutableMapping):
kTend = pamcparams.kTs[-1]
if kTstart < kTend:
kTstart, kTend = kTend, kTstart
kTs = kB * pamcparams.kTs
kTs = pamcparams.kTs

# Set Lreload to True when restarting
Lreload = pamcparams.reload
Expand Down Expand Up @@ -168,7 +168,7 @@ def main_dft_latgas(params_root: MutableMapping):
# specify temperatures for each replica, number of steps, etc.
kTstart = rxparams.kTs[0]
kTend = rxparams.kTs[-1]
kTs = kB * rxparams.kTs
kTs = rxparams.kTs

# Set Lreload to True when restarting
Lreload = rxparams.reload
Expand Down Expand Up @@ -350,7 +350,7 @@ def main_dft_latgas(params_root: MutableMapping):
if sampler_type == "RXMC":
# RXMC calculation
RXcalc = TemperatureRX_MPI(
comm, mc_class, model, configs, kTs, write_node=write_node
comm, mc_class, model, configs, kTs, write_node=write_node, T2E=kB,
)
if Lreload:
logger.info("-Reloading from previous calculation")
Expand All @@ -371,7 +371,7 @@ def main_dft_latgas(params_root: MutableMapping):
elif sampler_type == "PAMC":
# PAMC calculation
PAcalc = PopulationAnnealing(
comm, mc_class, model, configs, kTs, write_node=write_node
comm, mc_class, model, configs, kTs, write_node=write_node, T2E=kB,
)
if Lreload:
logger.info("-Reloading from previous calculation")
Expand Down Expand Up @@ -404,7 +404,7 @@ def main_dft_latgas(params_root: MutableMapping):

elif sampler_type == "parallelMC":
calc = EmbarrassinglyParallelSampling(
comm, mc_class, model, configs, kTs, write_node=write_node
comm, mc_class, model, configs, kTs, write_node=write_node, T2E=kB
)
if Lreload:
calc.reload()
Expand Down
4 changes: 4 additions & 0 deletions abics/scripts/postproc_dft_latgas.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def postproc_dft_latgas(params_root: MutableMapping):
comm=comm,
obsnames=obsnames,
throw_out=throw_out,
E2T=1.0/kB,
)

elif sampler_type == "PAMC":
Expand All @@ -410,6 +411,7 @@ def postproc_dft_latgas(params_root: MutableMapping):
obsnames=obsnames,
isamples_offsets=isamples_offsets,
comm=comm,
E2T=1.0/kB,
)

elif sampler_type == "parallelRand":
Expand All @@ -420,6 +422,7 @@ def postproc_dft_latgas(params_root: MutableMapping):
comm=comm,
obsnames=obsnames,
throw_out=throw_out,
E2T=1.0/kB,
)

elif sampler_type == "parallelMC":
Expand All @@ -430,6 +433,7 @@ def postproc_dft_latgas(params_root: MutableMapping):
comm=comm,
obsnames=obsnames,
throw_out=throw_out,
E2T=1.0/kB,
)
logger.info("--Sampling completed sucessfully.")
logger.info("Exiting normally on {}\n".format(datetime.datetime.now()))
3 changes: 3 additions & 0 deletions tests/integration/active_learn/input.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[sampling]
sampler = "RXMC"
# sampler = "PAMC"
# kTnum = 10
nreplicas = 2
nprocs_per_replica = 1
kTstart = 1200.0
Expand Down