mcmc_steps = 10000 minimization_steps = 50 burn_in_steps = minimization_steps particle_number = 10 max_delta = 1.0 cutoff = 0.98 * (upper - lower) / 2 potential = LennardJonesOpt(sigma=1., epsilon=3.0, cutoff=cutoff) p_group = ParticleGroup.random_square(number=particle_number, lower=lower, upper=upper, dimension=2, kind='p', use_cpp=False) p_group.attach_pairwise_potential(potential) propagator = Propagator( p_group, termo_properties=['potential_energy', 'trajectory']) ##### propagator.minimize(minimization_steps) propagator.propagate_mcmc_nvt_onep(steps=mcmc_steps, temperature=temperature, max_delta=max_delta) print(propagator.last_run_time) propagator.burn_in(burn_in_steps) #propagator.dump_to_xyz('test_lj.xyz') acceptances.append(propagator.get_acceptance_percentage()) print(f"Acceptance is {propagator.get_acceptance_percentage()}") propagator.clear_properties() pickle.dump((temperatures, acceptances), open('temperatures_acceptances.pkl', 'wb'))
import chem_mcmc import numpy as np import matplotlib.pyplot as plt from chem_mcmc.staging import Propagator, ParticleGroup from chem_mcmc.potentials import LogGaussianOpt from chem_mcmc.utils import get_running_mean, get_running_std, PDensity from chem_mcmc import potentials from chem_mcmc import plotting from chem_mcmc import constants # EXAMPLE 1 do this once temperature = 600 lower = 0. upper = 12. # ~ 80k is converged (10 s) , ~ 800k is very well converged mcmc_steps = 80000 #potential = LogGaussian(A=[2., 0.8], mu=[4, 8]) potential = LogGaussianOpt(A=[2., 0.8], mu=[4, 8]) p_group = ParticleGroup.random_square(number=1, lower=lower, upper=upper, dimension=1, kind='r') p_group.attach_external_potential(potential) propagator = Propagator(p_group, termo_properties=['potential_energy', 'trajectory']) propagator.propagate_mcmc_nvt(steps=mcmc_steps, temperature=temperature, max_delta=1.0)
# Simulation setup # temperature = 10000 pressure = 2 # units should be kcal/(mol ang^2) # an approximate idea given by the ideal gas law is that T*N * R * 4.184/upper^2 # \approx P lower = 0. upper = 12. mcmc_steps = 50000 potential = HardSpheresStep(sigma1=0.5, sigma2=1.0, epsilon=2) p_group = ParticleGroup.random_square(number=20, lower=lower, upper=upper, dimension=2, kind='p') p_group.attach_pairwise_potential(potential) propagator = Propagator( p_group, termo_properties=['potential_energy', 'trajectory', 'bound_sizes']) propagator.minimize(50) propagator.propagate_mcmc_npt(steps=mcmc_steps, temperature=temperature, pressure=pressure, max_delta_coord=1.0) propagator.burn_in(50) propagator.dump_to_xyz('npt_low_fixed.xyz') #bound_sizes = propagator.get_bound_sizes() #np.set_printoptions(threshold=3000) #print(bound_sizes) #####
from chem_mcmc.potentials import LogGaussian from chem_mcmc.utils import get_running_mean, get_running_std, PDensity from chem_mcmc import potentials from chem_mcmc import plotting from chem_mcmc import constants from time import time # EXAMPLE 1 do this once temperature = 600 lower = 0. upper = 12. # ~ 80k is converged (10 s) , ~ 800k is very well converged mcmc_steps = 20000 potential = LogGaussian(A=[2., 0.8], mu=[4, 8]) p_group = ParticleGroup.random_square(number=1, lower=lower, upper=upper, dimension=1, kind='r') p_group.attach_external_potential(potential) propagator = Propagator(p_group, termo_properties=['potential_energy', 'trajectory']) propagator.propagate_mcmc_nvt(steps=mcmc_steps, temperature=temperature, max_delta=1.0) propagator.burn_in(100) position = propagator.get_trajectory()[:,0,0] energy = propagator.get_potential_energy() pdensity = PDensity(temperature, potential, lower, upper) plotting.plot_analysis(position, pdensity, lower, upper, potential, bounds_potential = [-5, 40]) runmean_position = get_running_mean(position) runstd_position = get_running_std(position) runmean_energy = get_running_mean(energy) runstd_energy = get_running_std(energy) properties = [get_running_mean(position), get_running_std(position), get_running_mean(energy), get_running_std(energy)] titles = [r'Mean position ($\AA$)', r'Std. position ($\AA$)', r'Mean potential energy (kcal/mol)', r'Std. potential energy (kcal/mol)'] titlex = r'MCMC steps'
def replica_remc(random_square_kwargs, propagate_mcmc_nvt_onep_kwargs, potential, termo_properties, minimization_steps, burn_in_steps, mcmc_steps, replica_idx, connections): p_group = ParticleGroup.random_square(**random_square_kwargs) p_group.attach_pairwise_potential(potential) propagator = Propagator(p_group, termo_properties=termo_properties) propagator.minimize(minimization_steps) iterations = mcmc_steps // propagate_mcmc_nvt_onep_kwargs['steps'] replica_acceptance = 0 for iteration_idx in range(iterations): propagator.propagate_mcmc_nvt_onep(**propagate_mcmc_nvt_onep_kwargs) # Only returns true if the parities are different, for even # iterations this has to be done by odd replicas # for odd iterations this has to be done by even replicas if iteration_idx % 2 != replica_idx % 2: # for even iterations I have to exchange 0-1 2-3 4-5 6-7 so the # processes in charge of the exchange will be 0 2 3 4 6 ... etc # This means I will only do the calculation if the process index is # even, but I first need to receive a value from the processes with # odd process index # odd indices send data to even indices right_energy = propagator.get_potential_energy()[-1] right_temperature = propagate_mcmc_nvt_onep_kwargs['temperature'] connections['left'].send( (right_energy, right_temperature, replica_idx)) # after doing this I try to receive some coordinates, if I receive # something then the exchange was successful! if I receive None then # the exchange was not successful left_particle_group = connections['left'].recv() if left_particle_group is not None: # if the exchange was successful I do this connections['left'].send(propagator.particle_group) propagator.particle_group = left_particle_group propagator.store_termo(temperature=right_temperature) replica_acceptance += 1 else: # if the exchange was unsuccessful I store the same # conformation again and continue propagator.store_termo(temperature=right_temperature) # Only returns true if the parities are equal, for even # iterations this has to be done by even replicas # for odd iterations this has to be done by odd replicas # This is the code that actually manages the exchange calculations if iteration_idx % 2 == replica_idx % 2: # Here I receive data and calculate the mcmc exchange # probability left_energy = propagator.get_potential_energy()[-1] left_temperature = propagate_mcmc_nvt_onep_kwargs['temperature'] left_beta = 1 / (constants.kb * left_temperature) # this blocks until there is something to receive right_energy, right_temperature, right_replica_idx = connections[ 'right'].recv() right_beta = 1 / (constants.kb * right_temperature) mc_factor = np.exp( (left_beta - right_beta) * (left_energy - right_energy)) if propagator.prng.uniform(low=0., high=1.) < mc_factor: # first I send the particle group through the pipe connections['right'].send(propagator.particle_group) # afterwards I receive the particle right particle_group # from the pipe I change the coordinates of all the # particles to be those of the particle group and I store # the termo properties as if the temperature was the # temperature of this specific replica propagator.particle_group = connections['right'].recv() propagator.store_termo(temperature=left_temperature) replica_acceptance += 1 else: # I just send an "exchange rejected signal" and store the # termo properties again connections['right'].send(None) propagator.store_termo(temperature=left_temperature) replica_acceptance_rate = replica_acceptance / iterations