Skip to content

Commit

Permalink
corrected more
Browse files Browse the repository at this point in the history
  • Loading branch information
yomichi committed Oct 23, 2024
1 parent 4e8e865 commit 08d7c1b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
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
15 changes: 9 additions & 6 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 Down
17 changes: 9 additions & 8 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 Down Expand Up @@ -515,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 Down
17 changes: 10 additions & 7 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 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
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

0 comments on commit 08d7c1b

Please sign in to comment.