def test_run_niter2(self): modified_fire1 = ModifiedFireCPP(_xrand, _EG()) res1 = modified_fire1.run() modified_fire2 = ModifiedFireCPP(_xrand, _EG()) res2 = modified_fire2.run(res1.nsteps // 2) res2 = modified_fire2.run() self.assert_same(res1, res2)
def test_run_niter3(self): modified_fire1 = ModifiedFireCPP(_xrand, _EG()) res1 = modified_fire1.run(10) modified_fire2 = ModifiedFireCPP(_xrand, _EG()) res2 = modified_fire2.run(5) res2 = modified_fire2.run(5) self.assert_same(res1, res2)
class Config2D(object): def __init__(self, nparticles_x, amplitude): self.LX = nparticles_x self.LY = self.LX self.nparticles_x = nparticles_x self.N = self.nparticles_x ** 2 self.amplitude = amplitude self.x = np.zeros(2 * self.N) for particle in xrange(self.N): pid = 2 * particle self.x[pid] = particle % self.LX self.x[pid + 1] = int(particle / self.LX) self.x_initial = [xi + np.random.uniform(- self.amplitude, self.amplitude) for xi in self.x] self.radius = 0.25 self.sca = 1.1 self.radii = np.ones(self.N) * self.radius self.eps = 1 self.boxvec = np.array([self.LX, self.LY]) self.potential = HS_WCA(self.eps, self.sca, self.radii, ndim=2, boxvec=self.boxvec) self.rcut = 2 * (1 + self.sca) * self.radius self.ncellx_scale = 1 self.potential_cells = HS_WCAPeriodicCellLists(self.eps, self.sca, self.radii, self.boxvec, self.x_initial, self.rcut, ndim = 2, ncellx_scale = self.ncellx_scale) self.tol = 1e-7 self.maxstep = 1 self.nstepsmax = 1e5 def optimize(self, nr_samples = 1): self.optimizer = ModifiedFireCPP(self.x_initial, self.potential, tol = self.tol, maxstep = self.maxstep) self.optimizer_ = LBFGS_CPP(self.x_initial, self.potential) self.optimizer_cells = ModifiedFireCPP(self.x_initial, self.potential_cells, tol = self.tol, maxstep = self.maxstep) self.optimizer_cells_ = LBFGS_CPP(self.x_initial, self.potential_cells) t0 = time.time() self.optimizer.run(self.nstepsmax) t1 = time.time() self.optimizer_cells.run(self.nstepsmax) t2 = time.time() self.optimizer_.run(self.nstepsmax) t3 = time.time() self.optimizer_cells_.run(self.nstepsmax) t4 = time.time() res_x_final = self.optimizer.get_result() res_x_final_cells = self.optimizer_cells.get_result() self.x_final = res_x_final.coords self.x_final_cells = res_x_final_cells.coords print "number of particles:", self.N print "time no cell lists:", t1 - t0, "sec" print "time cell lists:", t2 - t1, "sec" print "ratio:", (t1 - t0) / (t2 - t1) assert(res_x_final.success) assert(res_x_final_cells.success) for (xci, xi) in zip(self.x_final_cells, self.x_final): passed = (np.abs(xci - xi) < 1e-10 * self.N) if (passed is False): print "xci", xci print "xi", xi assert(passed) print "energy no cell lists:", res_x_final.energy print "energy cell lists:", res_x_final_cells.energy self.t_ratio = (t1 - t0) / (t2 - t1) self.t_ratio_lbfgs = (t3 - t2) / (t4 - t3)
def do_check(self, pot, **kwargs): modified_fire = ModifiedFireCPP(np.zeros(4), pot, stepback=True, **kwargs) modified_fire.run() modified_fire.reset(_xrand) res = modified_fire.run() self.assertAlmostEqual(res.energy, _emin, 4) self.assertTrue(res.success) self.assertLess(np.max(np.abs(res.coords - _xmin)), 1e-2) self.assertGreater(res.nfev, 0)
def test_event_raise(self): class EventException(BaseException): pass def myevent(*args, **kwargs): raise EventException with self.assertRaises(EventException): modified_fire = ModifiedFireCPP(_xrand, _EG(), events=[myevent]) modified_fire.run()
def modifiedfire_cpp(coords, pot, **kwargs): if not hasattr(pot, "getEnergyGradient"): # for compatibility with old quenchers. # assume pot is a getEnergyGradient function pot = _getEnergyGradientWrapper(pot) modifiedfire = ModifiedFireCPP(coords, pot, **kwargs) return modifiedfire.run()
def test_raises(self): pot = _lj_cpp._ErrorPotential() with self.assertRaises(RuntimeError): modified_fire = ModifiedFireCPP(_xrand, pot) modified_fire.run()
def test_raises(self): with self.assertRaises(NotImplementedError): modified_fire = ModifiedFireCPP(_xrand, _Raise()) modified_fire.run()
class Test2dMinimization(unittest.TestCase): def setUp(self): np.random.seed(42) self.L_mobile = 4 self.L_total = self.L_mobile + 2 self.nr_particles_mobile = self.L_mobile * self.L_mobile self.nr_particles_total = self.L_total * self.L_total self.nr_particles_frozen = self.nr_particles_total - self.nr_particles_mobile self.box_dimension = 2 self.ndof = self.nr_particles_total * self.box_dimension self.n_frozen_dof = self.nr_particles_frozen * self.box_dimension self.frozen_dof = [] self.frozen_atoms = [] for particle_index in range(self.nr_particles_total): xmean = int(particle_index % self.L_total) ymean = int(particle_index / self.L_total) if ymean == 0 or ymean == self.L_total - 1 or xmean == 0 or xmean == self.L_total - 1: self.frozen_dof.append(particle_index * self.box_dimension) self.frozen_dof.append(particle_index * self.box_dimension + 1) self.frozen_atoms.append(particle_index) self.eps = 1 self.x = np.zeros(self.ndof) for p in range(self.nr_particles_total): xmean = int(p % self.L_total) ymean = int(p / self.L_total) self.x[p * self.box_dimension] = xmean + 0.1 * np.random.rand() self.x[p * self.box_dimension + 1] = ymean + 0.1 * np.random.rand() self.radii = np.asarray([0.3 + 0.01 * np.random.rand() for _ in range(self.nr_particles_total)]) self.sca = 1 self.rcut = 2 * (1 + self.sca) * np.amax(self.radii) self.boxvec = (self.L_total + self.rcut) * np.ones(self.box_dimension) self.pot_cells_N_frozen_N = HS_WCA(eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.box_dimension, boxvec=self.boxvec, use_periodic=True, use_frozen=False, use_cell_lists=False) self.pot_cells_Y_frozen_N = HS_WCA(eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.box_dimension, boxvec=self.boxvec, use_periodic=True, use_frozen=False, use_cell_lists=True, reference_coords=self.x, rcut=self.rcut) self.pot_cells_N_frozen_Y = HS_WCA(eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.box_dimension, boxvec=self.boxvec, use_periodic=True, use_frozen=True, use_cell_lists=False, frozen_atoms=self.frozen_atoms, reference_coords=self.x) self.pot_cells_Y_frozen_Y = HS_WCA(eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.box_dimension, boxvec=self.boxvec, use_periodic=True, use_frozen=True, use_cell_lists=True, reference_coords=self.x, frozen_atoms=self.frozen_atoms, rcut=self.rcut) self.x_red = [] for atom in range(self.nr_particles_total): if atom not in self.frozen_atoms: self.x_red.extend(self.x[atom * self.box_dimension : (atom + 1) * self.box_dimension]) self.opt_NN = ModifiedFireCPP(self.x, self.pot_cells_N_frozen_N) self.opt_YN = ModifiedFireCPP(self.x, self.pot_cells_Y_frozen_N) self.opt_NY = ModifiedFireCPP(self.x_red, self.pot_cells_N_frozen_Y) self.opt_YY = ModifiedFireCPP(self.x_red, self.pot_cells_Y_frozen_Y) def test_energies(self): self.res_e_before_cells_N_frozen_N = self.opt_NN.get_result() self.res_e_before_cells_Y_frozen_N = self.opt_YN.get_result() self.res_e_before_cells_N_frozen_Y = self.opt_NY.get_result() self.res_e_before_cells_Y_frozen_Y = self.opt_YY.get_result() self.assertAlmostEqual(self.res_e_before_cells_N_frozen_N.energy, self.res_e_before_cells_N_frozen_Y.energy, places=8) self.assertAlmostEqual(self.res_e_before_cells_Y_frozen_N.energy, self.res_e_before_cells_N_frozen_N.energy, places=8) self.assertAlmostEqual(self.res_e_before_cells_N_frozen_N.energy, self.res_e_before_cells_Y_frozen_Y.energy, places=8) self.assertAlmostEqual(self.pot_cells_N_frozen_N.getEnergy(self.x), self.pot_cells_Y_frozen_N.getEnergy(self.x), places=8) def test_minimization(self): self.opt_NN.run() self.opt_YN.run() self.opt_NY.run() self.opt_YY.run() self.res_NN = self.opt_NN.get_result() self.res_YN = self.opt_YN.get_result() self.res_NY = self.opt_NY.get_result() self.res_YY = self.opt_YY.get_result() self.assertTrue(self.res_NN.success) self.assertTrue(self.res_YN.success) self.assertTrue(self.res_NY.success) self.assertTrue(self.res_YY.success) self.assertAlmostEqual(self.res_NY.energy, self.res_YY.energy, delta=1e-10) self.assertAlmostEqual(self.pot_cells_N_frozen_N.getEnergy(self.res_NN.coords), self.pot_cells_Y_frozen_N.getEnergy(self.res_YN.coords), delta=1e-10)
class Test2dMinimization(unittest.TestCase): def setUp(self): np.random.seed(42) self.L_mobile = 4 self.L_total = self.L_mobile + 2 self.nr_particles_mobile = self.L_mobile * self.L_mobile self.nr_particles_total = self.L_total * self.L_total self.nr_particles_frozen = self.nr_particles_total - self.nr_particles_mobile self.box_dimension = 2 self.ndof = self.nr_particles_total * self.box_dimension self.n_frozen_dof = self.nr_particles_frozen * self.box_dimension self.frozen_dof = [] self.frozen_atoms = [] for particle_index in xrange(self.nr_particles_total): xmean = int(particle_index % self.L_total) ymean = int(particle_index / self.L_total) if ymean == 0 or ymean == self.L_total - 1 or xmean == 0 or xmean == self.L_total - 1: self.frozen_dof.append(particle_index * self.box_dimension) self.frozen_dof.append(particle_index * self.box_dimension + 1) self.frozen_atoms.append(particle_index) self.eps = 1 self.x = np.zeros(self.ndof) for p in xrange(self.nr_particles_total): xmean = int(p % self.L_total) ymean = int(p / self.L_total) self.x[p * self.box_dimension] = xmean + 0.1 * np.random.rand() self.x[p * self.box_dimension + 1] = ymean + 0.1 * np.random.rand() self.radii = np.asarray([ 0.3 + 0.01 * np.random.rand() for _ in xrange(self.nr_particles_total) ]) self.sca = 1 self.rcut = 2 * (1 + self.sca) * np.amax(self.radii) self.boxvec = (self.L_total + self.rcut) * np.ones(self.box_dimension) self.pot_cells_N_frozen_N = HS_WCA(eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.box_dimension, boxvec=self.boxvec, use_periodic=True, use_frozen=False, use_cell_lists=False) self.pot_cells_Y_frozen_N = HS_WCA(eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.box_dimension, boxvec=self.boxvec, use_periodic=True, use_frozen=False, use_cell_lists=True, reference_coords=self.x, rcut=self.rcut) self.pot_cells_N_frozen_Y = HS_WCA(eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.box_dimension, boxvec=self.boxvec, use_periodic=True, use_frozen=True, use_cell_lists=False, frozen_atoms=self.frozen_atoms, reference_coords=self.x) self.pot_cells_Y_frozen_Y = HS_WCA(eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.box_dimension, boxvec=self.boxvec, use_periodic=True, use_frozen=True, use_cell_lists=True, reference_coords=self.x, frozen_atoms=self.frozen_atoms, rcut=self.rcut) self.x_red = [] for atom in xrange(self.nr_particles_total): if atom not in self.frozen_atoms: self.x_red.extend(self.x[atom * self.box_dimension:(atom + 1) * self.box_dimension]) self.opt_NN = ModifiedFireCPP(self.x, self.pot_cells_N_frozen_N) self.opt_YN = ModifiedFireCPP(self.x, self.pot_cells_Y_frozen_N) self.opt_NY = ModifiedFireCPP(self.x_red, self.pot_cells_N_frozen_Y) self.opt_YY = ModifiedFireCPP(self.x_red, self.pot_cells_Y_frozen_Y) def test_energies(self): self.res_e_before_cells_N_frozen_N = self.opt_NN.get_result() self.res_e_before_cells_Y_frozen_N = self.opt_YN.get_result() self.res_e_before_cells_N_frozen_Y = self.opt_NY.get_result() self.res_e_before_cells_Y_frozen_Y = self.opt_YY.get_result() self.assertAlmostEqual(self.res_e_before_cells_N_frozen_N.energy, self.res_e_before_cells_N_frozen_Y.energy, delta=1e-10) self.assertAlmostEqual(self.res_e_before_cells_Y_frozen_N.energy, self.res_e_before_cells_N_frozen_N.energy, delta=1e-10) self.assertAlmostEqual(self.res_e_before_cells_N_frozen_N.energy, self.res_e_before_cells_Y_frozen_Y.energy, delta=1e-10) self.assertAlmostEqual(self.pot_cells_N_frozen_N.getEnergy(self.x), self.pot_cells_Y_frozen_N.getEnergy(self.x), delta=1e-10) def test_minimization(self): self.opt_NN.run() self.opt_YN.run() self.opt_NY.run() self.opt_YY.run() self.res_NN = self.opt_NN.get_result() self.res_YN = self.opt_YN.get_result() self.res_NY = self.opt_NY.get_result() self.res_YY = self.opt_YY.get_result() self.assertTrue(self.res_NN.success) self.assertTrue(self.res_YN.success) self.assertTrue(self.res_NY.success) self.assertTrue(self.res_YY.success) self.assertAlmostEqual(self.res_NY.energy, self.res_YY.energy, delta=1e-10) self.assertAlmostEqual( self.pot_cells_N_frozen_N.getEnergy(self.res_NN.coords), self.pot_cells_Y_frozen_N.getEnergy(self.res_YN.coords), delta=1e-10)
class Config2D(object): def __init__(self, nparticles_x, amplitude): self.ndim = 2 self.LX = nparticles_x self.LY = self.LX self.nparticles_x = nparticles_x self.N = self.nparticles_x**self.ndim self.dof = self.ndim * self.N self.amplitude = amplitude self.x = np.zeros(self.dof) for particle in xrange(self.N): pid = self.ndim * particle self.x[pid] = particle % self.LX self.x[pid + 1] = int(particle / self.LX) self.x_initial = np.asarray([ xi + np.random.uniform(-self.amplitude, self.amplitude) for xi in self.x ]) self.x_initial = np.reshape(self.x_initial, (self.N, 2)) self.x_initial[:, 0] -= np.mean(self.x_initial[:, 0]) self.x_initial[:, 1] -= np.mean(self.x_initial[:, 1]) self.x_initial = self.x_initial.flatten() #self.radius = 0.3 #self.sca = 1.5 self.radius = 0.25 self.sca = 1.8 self.radii = np.ones(self.N) * self.radius self.eps = 1.0 self.boxvec = np.array([self.LX, self.LY]) self.potential = HS_WCA(use_periodic=use_periodic, eps=self.eps, sca=self.sca, radii=self.radii.copy(), ndim=self.ndim, boxvec=self.boxvec.copy()) self.potential_ = HS_WCA(use_periodic=use_periodic, eps=self.eps, sca=self.sca, radii=self.radii.copy(), ndim=self.ndim, boxvec=self.boxvec.copy()) self.rcut = 2 * (1 + self.sca) * self.radius self.ncellx_scale = 1 self.potential_cells = HS_WCA(use_periodic=use_periodic, use_cell_lists=True, eps=self.eps, sca=self.sca, radii=self.radii.copy(), boxvec=self.boxvec.copy(), rcut=self.rcut, ndim=self.ndim, ncellx_scale=self.ncellx_scale) self.potential_cells_ = HS_WCA(use_periodic=use_periodic, use_cell_lists=True, eps=self.eps, sca=self.sca, radii=self.radii.copy(), boxvec=self.boxvec.copy(), rcut=self.rcut, ndim=self.ndim, ncellx_scale=self.ncellx_scale) self.tol = 1e-7 self.maxstep = np.amax(self.radii) self.nstepsmax = int(1e6) assert (self.boxvec[0] == self.boxvec[1]) print "x_initial energy:", self.potential.getEnergy(self.x_initial) print "x_initial cells energy:", self.potential_cells.getEnergy( self.x_initial) assert (self.potential.getEnergy( self.x_initial) == self.potential_.getEnergy(self.x_initial)) assert (self.potential_cells.getEnergy( self.x_initial) == self.potential_cells_.getEnergy(self.x_initial)) #assert abs(self.potential.getEnergy(self.x_initial) - self.potential_cells.getEnergy(self.x_initial)) < 1e-10 assert np.allclose(self.potential.getEnergy(self.x_initial), self.potential_cells.getEnergy(self.x_initial), rtol=1e-10) print self.boxvec #plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) def optimize(self, nr_samples=1): self.optimizer = ModifiedFireCPP(self.x_initial.copy(), self.potential, dtmax=1, maxstep=self.maxstep, tol=self.tol, nsteps=1e8, verbosity=-1, iprint=-1) self.optimizer_ = LBFGS_CPP(self.x_initial.copy(), self.potential_) self.optimizer_cells = ModifiedFireCPP(self.x_initial.copy(), self.potential_cells, dtmax=1, maxstep=self.maxstep, tol=self.tol, nsteps=1e8, verbosity=-1, iprint=-1) self.optimizer_cells_ = LBFGS_CPP(self.x_initial.copy(), self.potential_cells_) print "initial E, x:", self.potential.getEnergy(self.x_initial.copy()) print "initial E, x_:", self.potential_cells.getEnergy( self.x_initial.copy()) t0 = time.time() print "self.optimizer.run(self.nstepsmax)", self.nstepsmax self.optimizer.run(self.nstepsmax) self.res_x_final = self.optimizer.get_result() t1 = time.time() self.optimizer_cells.run(self.nstepsmax) self.res_x_final_cells = self.optimizer_cells.get_result() t2 = time.time() self.x_final = self.res_x_final.coords self.x_final_cells = self.res_x_final_cells.coords print "fire final E, x:", self.optimizer.get_result().energy print "fire final E, x_cells:", self.optimizer_cells.get_result( ).energy print "fire final E, plain: ", self.potential.getEnergy(self.x_final) print "fire final E, cell: ", self.potential_cells.getEnergy( self.x_final_cells) print "fire number of particles:", self.N print "fire time no cell lists:", t1 - t0, "sec" print "fire time cell lists:", t2 - t1, "sec" print "fire ratio:", (t1 - t0) / (t2 - t1) if not self.res_x_final.success or not self.res_x_final_cells.success: print "-------------" print "res_x_final.rms:", self.res_x_final.rms print "res_x_final.nfev:", self.res_x_final.nfev print "res_x_final_cells.rms:", self.res_x_final_cells.rms print "res_x_final_cells.nfev:", self.res_x_final_cells.nfev print "self.res_x_final.success", self.res_x_final.success print "self.res_x_final_cells.success", self.res_x_final_cells.success print "-------------" plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_cells, self.radii, self.boxvec, sca=self.sca) self.optimizer_.run(self.nstepsmax) self.res_x_final_ = self.optimizer_.get_result() t3 = time.time() self.optimizer_cells_.run(self.nstepsmax) self.res_x_final_cells_ = self.optimizer_cells_.get_result() t4 = time.time() self.x_final_ = self.res_x_final_.coords self.x_final_cells_ = self.res_x_final_cells_.coords print "lbfgs final E, x:", self.optimizer_.get_result().energy print "lbfgs final E, x_cells:", self.optimizer_cells_.get_result( ).energy print "lbfgs final E, plain: ", self.potential_.getEnergy( self.x_final_) print "lbfgs final E, cell: ", self.potential_cells_.getEnergy( self.x_final_cells_) print "lbfgs number of particles:", self.N print "lbfgs time no cell lists:", t3 - t2, "sec" print "lbfgs time cell lists:", t4 - t3, "sec" print "lbfgs ratio:", (t3 - t2) / (t4 - t3) if not self.res_x_final_.success or not self.res_x_final_cells_.success or not self.res_x_final.success or not self.res_x_final_cells.success: print "-------------" print "res_x_final_.rms:", self.res_x_final_.rms print "res_x_final_.nfev:", self.res_x_final_.nfev print "res_x_final_cells_.rms:", self.res_x_final_cells_.rms print "res_x_final_cells_.nfev:", self.res_x_final_cells_.nfev print "self.res_x_final_.success", self.res_x_final.success print "self.res_x_final_cells_.success", self.res_x_final_cells.success print "-------------" plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_cells_, self.radii, self.boxvec, sca=self.sca) assert (self.res_x_final.success) assert (self.res_x_final_cells.success) assert (self.res_x_final_.success) assert (self.res_x_final_cells_.success) for (xci, xi) in zip(self.x_final_cells, self.x_final): passed = (np.abs(xci - xi) < 1e-10) if (passed is False): print "xci", xci print "xi", xi assert (passed) print "energy no cell lists:", self.res_x_final.energy print "energy cell lists:", self.res_x_final_cells.energy self.t_ratio = (t1 - t0) / (t2 - t1) self.t_ratio_lbfgs = (t3 - t2) / (t4 - t3)
class Config2DFrozenBoundary(object): def __init__(self, nparticles_x, amplitude): self.ndim = 2 self.LX = nparticles_x self.LY = self.LX self.nparticles_x = nparticles_x self.N = self.nparticles_x**self.ndim self.amplitude = amplitude self.dof = self.ndim * self.N self.x = np.zeros(self.dof) self.frozen_atoms = [] for particle in xrange(self.N): pid = self.ndim * particle xcoor = particle % self.LX ycoor = int(particle / self.LX) self.x[pid] = xcoor self.x[pid + 1] = ycoor if xcoor == 0 or xcoor == self.LX - 1 or ycoor == 0 or ycoor == self.LY - 1: self.frozen_atoms.append(particle) self.x_initial = copy.copy(self.x) for particle in xrange(self.N): if particle not in self.frozen_atoms: pid = self.ndim * particle self.x_initial[pid] += np.random.uniform( -self.amplitude, self.amplitude) self.x_initial[pid + 1] += np.random.uniform( -self.amplitude, self.amplitude) self.x_initial = np.reshape(self.x_initial, (self.N, 2)) self.x_initial[:, 0] -= np.mean(self.x_initial[:, 0]) self.x_initial[:, 1] -= np.mean(self.x_initial[:, 1]) self.x_initial = self.x_initial.flatten() min_x = np.amin(self.x_initial) if min_x < 0: self.x_initial -= min_x #self.radius = 0.3 #self.sca = 1.5 self.radius = 0.25 self.sca = 1.8 self.radii = np.ones(self.N) * self.radius self.eps = 1.0 max_edge = np.amax([ np.amax(self.x_initial), np.abs(np.amin(self.x_initial)) ]) + 2 * self.amplitude + (1 + self.sca) * self.radius self.boxvec = np.array([max_edge, max_edge]) self.frozen_atoms1 = np.array(self.frozen_atoms) self.frozen_atoms2 = np.array(self.frozen_atoms) print "self.frozen_atoms1", self.frozen_atoms1 self.potential = HS_WCA(use_frozen=True, use_periodic=use_periodic_frozen, reference_coords=self.x_initial, frozen_atoms=self.frozen_atoms1, eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.ndim, boxvec=self.boxvec) self.rcut = 2 * (1 + self.sca) * self.radius self.ncellx_scale = 1.0 self.potential_cells = HS_WCA(use_frozen=True, use_periodic=use_periodic_frozen, use_cell_lists=True, eps=self.eps, sca=self.sca, radii=self.radii, boxvec=self.boxvec, reference_coords=self.x_initial, rcut=self.rcut, ndim=self.ndim, ncellx_scale=self.ncellx_scale, frozen_atoms=self.frozen_atoms2) self.tol = 1e-7 self.maxstep = np.amax(self.radii) self.nstepsmax = int(1e6) assert (self.boxvec[0] == self.boxvec[1]) self.x_initial_red = reduce_coordinates(self.x_initial, self.frozen_atoms, self.ndim) print "x_initial energy:", self.potential.getEnergy(self.x_initial_red) print "x_initial cells energy:", self.potential_cells.getEnergy( self.x_initial_red) #assert abs(self.potential.getEnergy(self.x_initial_red) - self.potential_cells.getEnergy(self.x_initial_red)) < 1e-10 assert np.allclose(self.potential.getEnergy(self.x_initial_red), self.potential_cells.getEnergy(self.x_initial_red), rtol=1e-10) print self.boxvec def optimize(self, nr_samples=1): self.x_initial_red = reduce_coordinates(self.x_initial, self.frozen_atoms, self.ndim) self.optimizer = ModifiedFireCPP(self.x_initial_red.copy(), self.potential, tol=self.tol, maxstep=self.maxstep) self.optimizer_ = LBFGS_CPP(self.x_initial_red.copy(), self.potential) self.optimizer_cells = ModifiedFireCPP(self.x_initial_red.copy(), self.potential_cells, tol=self.tol, maxstep=self.maxstep) self.optimizer_cells_ = LBFGS_CPP(self.x_initial_red.copy(), self.potential_cells) t0 = time.time() print "self.optimizer.run(self.nstepsmax)", self.nstepsmax self.optimizer.run(self.nstepsmax) self.res_x_final = self.optimizer.get_result() t1 = time.time() self.optimizer_cells.run(self.nstepsmax) self.res_x_final_cells = self.optimizer_cells.get_result() t2 = time.time() self.x_final = self.res_x_final.coords self.x_final_cells = self.res_x_final_cells.coords print "fire final E, x:", self.optimizer.get_result().energy print "fire final E, x_cells:", self.optimizer_cells.get_result( ).energy print "fire final E, plain: ", self.potential.getEnergy(self.x_final) print "fire final E, cell: ", self.potential_cells.getEnergy( self.x_final_cells) print "fire number of particles:", self.N print "fire time no cell lists:", t1 - t0, "sec" print "fire time cell lists:", t2 - t1, "sec" print "fire ratio:", (t1 - t0) / (t2 - t1) if not self.res_x_final.success or not self.res_x_final_cells.success: print "-------------" print "res_x_final.rms:", self.res_x_final.rms print "res_x_final.nfev:", self.res_x_final.nfev print "res_x_final_cells.rms:", self.res_x_final_cells.rms print "res_x_final_cells.nfev:", self.res_x_final_cells.nfev print "self.res_x_final.success", self.res_x_final.success print "self.res_x_final_cells.success", self.res_x_final_cells.success print "-------------" plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_cells, self.radii, self.boxvec, sca=self.sca) self.optimizer_.run(self.nstepsmax) self.res_x_final_ = self.optimizer_.get_result() t3 = time.time() self.optimizer_cells_.run(self.nstepsmax) self.res_x_final_cells_ = self.optimizer_cells_.get_result() t4 = time.time() self.x_final_ = self.res_x_final_.coords self.x_final_cells_ = self.res_x_final_cells_.coords print "lbfgs final E, x:", self.optimizer_.get_result().energy print "lbfgs final E, x_cells:", self.optimizer_cells_.get_result( ).energy print "lbfgs final E, plain: ", self.potential.getEnergy(self.x_final_) print "lbfgs final E, cell: ", self.potential_cells.getEnergy( self.x_final_cells_) print "lbfgs number of particles:", self.N print "lbfgs time no cell lists:", t3 - t2, "sec" print "lbfgs time cell lists:", t4 - t3, "sec" print "lbfgs ratio:", (t3 - t2) / (t4 - t3) if not self.res_x_final_.success or not self.res_x_final_cells_.success or not self.res_x_final.success or not self.res_x_final_cells.success: print "-------------" print "res_x_final_.rms:", self.res_x_final_.rms print "res_x_final_.nfev:", self.res_x_final_.nfev print "res_x_final_cells_.rms:", self.res_x_final_cells_.rms print "res_x_final_cells_.nfev:", self.res_x_final_cells_.nfev print "self.res_x_final_.success", self.res_x_final.success print "self.res_x_final_cells_.success", self.res_x_final_cells.success print "-------------" plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_cells_, self.radii, self.boxvec, sca=self.sca) assert (self.res_x_final.success) assert (self.res_x_final_cells.success) assert (self.res_x_final_.success) assert (self.res_x_final_cells_.success) for (xci, xi) in zip(self.x_final_cells, self.x_final): passed = (np.abs(xci - xi) < 1e-10) if (passed is False): print "xci", xci print "xi", xi assert (passed) print "energy no cell lists:", self.res_x_final.energy print "energy cell lists:", self.res_x_final_cells.energy self.t_ratio = (t1 - t0) / (t2 - t1) self.t_ratio_lbfgs = (t3 - t2) / (t4 - t3)
class Config2DFrozenBoundary(object): def __init__(self, nparticles_x, amplitude): self.ndim = 2 self.LX = nparticles_x self.LY = self.LX self.nparticles_x = nparticles_x self.N = self.nparticles_x ** self.ndim self.amplitude = amplitude self.dof = self.ndim * self.N self.x = np.zeros(self.dof) self.frozen_atoms = [] for particle in xrange(self.N): pid = self.ndim * particle xcoor = particle % self.LX ycoor = int(particle / self.LX) self.x[pid] = xcoor self.x[pid + 1] = ycoor if xcoor == 0 or xcoor == self.LX - 1 or ycoor == 0 or ycoor == self.LY - 1: self.frozen_atoms.append(particle) self.x_initial = copy.copy(self.x) for particle in xrange(self.N): if particle not in self.frozen_atoms: pid = self.ndim * particle self.x_initial[pid] += np.random.uniform(- self.amplitude, self.amplitude) self.x_initial[pid + 1] += np.random.uniform(- self.amplitude, self.amplitude) self.x_initial = np.reshape(self.x_initial, (self.N,2)) self.x_initial[:,0] -= np.mean(self.x_initial[:,0]) self.x_initial[:,1] -= np.mean(self.x_initial[:,1]) self.x_initial = self.x_initial.flatten() min_x = np.amin(self.x_initial) if min_x < 0: self.x_initial -= min_x #self.radius = 0.3 #self.sca = 1.5 self.radius = 0.25 self.sca = 1.8 self.radii = np.ones(self.N) * self.radius self.eps = 1.0 max_edge = np.amax([np.amax(self.x_initial), np.abs(np.amin(self.x_initial))]) + 2 * self.amplitude + (1 + self.sca) * self.radius self.boxvec = np.array([max_edge, max_edge]) self.frozen_atoms1 = np.array(self.frozen_atoms) self.frozen_atoms2 = np.array(self.frozen_atoms) print "self.frozen_atoms1", self.frozen_atoms1 self.potential = HS_WCA(use_frozen=True, use_periodic=use_periodic_frozen, reference_coords=self.x_initial, frozen_atoms=self.frozen_atoms1, eps=self.eps, sca=self.sca, radii=self.radii, ndim=self.ndim, boxvec=self.boxvec) self.rcut = 2 * (1 + self.sca) * self.radius self.ncellx_scale = 1.0 self.potential_cells = HS_WCA(use_frozen=True, use_periodic=use_periodic_frozen, use_cell_lists=True, eps=self.eps, sca=self.sca, radii=self.radii, boxvec=self.boxvec, reference_coords=self.x_initial, rcut=self.rcut, ndim=self.ndim, ncellx_scale=self.ncellx_scale, frozen_atoms=self.frozen_atoms2) self.tol = 1e-7 self.maxstep = np.amax(self.radii) self.nstepsmax = int(1e6) assert(self.boxvec[0]==self.boxvec[1]) self.x_initial_red = reduce_coordinates(self.x_initial,self.frozen_atoms,self.ndim) print "x_initial energy:", self.potential.getEnergy(self.x_initial_red) print "x_initial cells energy:", self.potential_cells.getEnergy(self.x_initial_red) #assert abs(self.potential.getEnergy(self.x_initial_red) - self.potential_cells.getEnergy(self.x_initial_red)) < 1e-10 assert np.allclose(self.potential.getEnergy(self.x_initial_red), self.potential_cells.getEnergy(self.x_initial_red), rtol=1e-10) print self.boxvec def optimize(self, nr_samples=1): self.x_initial_red = reduce_coordinates(self.x_initial,self.frozen_atoms,self.ndim) self.optimizer = ModifiedFireCPP(self.x_initial_red.copy(), self.potential, tol = self.tol, maxstep = self.maxstep) self.optimizer_ = LBFGS_CPP(self.x_initial_red.copy(), self.potential) self.optimizer_cells = ModifiedFireCPP(self.x_initial_red.copy(), self.potential_cells, tol = self.tol, maxstep = self.maxstep) self.optimizer_cells_ = LBFGS_CPP(self.x_initial_red.copy(), self.potential_cells) t0 = time.time() print "self.optimizer.run(self.nstepsmax)", self.nstepsmax self.optimizer.run(self.nstepsmax) self.res_x_final = self.optimizer.get_result() t1 = time.time() self.optimizer_cells.run(self.nstepsmax) self.res_x_final_cells = self.optimizer_cells.get_result() t2 = time.time() self.x_final = self.res_x_final.coords self.x_final_cells = self.res_x_final_cells.coords print "fire final E, x:", self.optimizer.get_result().energy print "fire final E, x_cells:", self.optimizer_cells.get_result().energy print "fire final E, plain: ", self.potential.getEnergy(self.x_final) print "fire final E, cell: ", self.potential_cells.getEnergy(self.x_final_cells) print "fire number of particles:", self.N print "fire time no cell lists:", t1 - t0, "sec" print "fire time cell lists:", t2 - t1, "sec" print "fire ratio:", (t1 - t0) / (t2 - t1) if not self.res_x_final.success or not self.res_x_final_cells.success: print "-------------" print "res_x_final.rms:", self.res_x_final.rms print "res_x_final.nfev:", self.res_x_final.nfev print "res_x_final_cells.rms:", self.res_x_final_cells.rms print "res_x_final_cells.nfev:", self.res_x_final_cells.nfev print "self.res_x_final.success", self.res_x_final.success print "self.res_x_final_cells.success", self.res_x_final_cells.success print "-------------" plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_cells, self.radii, self.boxvec, sca=self.sca) self.optimizer_.run(self.nstepsmax) self.res_x_final_ = self.optimizer_.get_result() t3 = time.time() self.optimizer_cells_.run(self.nstepsmax) self.res_x_final_cells_ = self.optimizer_cells_.get_result() t4 = time.time() self.x_final_ = self.res_x_final_.coords self.x_final_cells_ = self.res_x_final_cells_.coords print "lbfgs final E, x:", self.optimizer_.get_result().energy print "lbfgs final E, x_cells:", self.optimizer_cells_.get_result().energy print "lbfgs final E, plain: ", self.potential.getEnergy(self.x_final_) print "lbfgs final E, cell: ", self.potential_cells.getEnergy(self.x_final_cells_) print "lbfgs number of particles:", self.N print "lbfgs time no cell lists:", t3 - t2, "sec" print "lbfgs time cell lists:", t4 - t3, "sec" print "lbfgs ratio:", (t3 - t2) / (t4 - t3) if not self.res_x_final_.success or not self.res_x_final_cells_.success or not self.res_x_final.success or not self.res_x_final_cells.success: print "-------------" print "res_x_final_.rms:", self.res_x_final_.rms print "res_x_final_.nfev:", self.res_x_final_.nfev print "res_x_final_cells_.rms:", self.res_x_final_cells_.rms print "res_x_final_cells_.nfev:", self.res_x_final_cells_.nfev print "self.res_x_final_.success", self.res_x_final.success print "self.res_x_final_cells_.success", self.res_x_final_cells.success print "-------------" plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_cells_, self.radii, self.boxvec, sca=self.sca) assert(self.res_x_final.success) assert(self.res_x_final_cells.success) assert(self.res_x_final_.success) assert(self.res_x_final_cells_.success) for (xci, xi) in zip(self.x_final_cells, self.x_final): passed = (np.abs(xci - xi) < 1e-10) if (passed is False): print "xci", xci print "xi", xi assert(passed) print "energy no cell lists:", self.res_x_final.energy print "energy cell lists:", self.res_x_final_cells.energy self.t_ratio = (t1 - t0) / (t2 - t1) self.t_ratio_lbfgs = (t3 - t2) / (t4 - t3)
def modifiedfire_cpp(coords, pot, **kwargs): modifiedfire = ModifiedFireCPP(coords, pot, **kwargs) return modifiedfire.run()
class Config2D(object): def __init__(self, nparticles_x, amplitude): self.ndim = 2 self.LX = nparticles_x self.LY = self.LX self.nparticles_x = nparticles_x self.N = self.nparticles_x ** self.ndim self.dof = self.ndim * self.N self.amplitude = amplitude self.x = np.zeros(self.dof) for particle in xrange(self.N): pid = self.ndim * particle self.x[pid] = particle % self.LX self.x[pid + 1] = int(particle / self.LX) self.x_initial = np.asarray([xi + np.random.uniform(- self.amplitude, self.amplitude) for xi in self.x]) self.x_initial = np.reshape(self.x_initial, (self.N,2)) self.x_initial[:,0] -= np.mean(self.x_initial[:,0]) self.x_initial[:,1] -= np.mean(self.x_initial[:,1]) self.x_initial = self.x_initial.flatten() #self.radius = 0.3 #self.sca = 1.5 self.radius = 0.25 self.sca = 1.8 self.radii = np.ones(self.N) * self.radius self.eps = 1.0 self.boxvec = np.array([self.LX, self.LY]) self.potential = HS_WCA(use_periodic=use_periodic, eps=self.eps, sca=self.sca, radii=self.radii.copy(), ndim=self.ndim, boxvec=self.boxvec.copy()) self.potential_ = HS_WCA(use_periodic=use_periodic, eps=self.eps, sca=self.sca, radii=self.radii.copy(), ndim=self.ndim, boxvec=self.boxvec.copy()) self.rcut = 2 * (1 + self.sca) * self.radius self.ncellx_scale = 1 self.potential_cells = HS_WCA(use_periodic=use_periodic, use_cell_lists=True, eps=self.eps, sca=self.sca, radii=self.radii.copy(), boxvec=self.boxvec.copy(), rcut=self.rcut, ndim=self.ndim, ncellx_scale=self.ncellx_scale) self.potential_cells_ = HS_WCA(use_periodic=use_periodic, use_cell_lists=True, eps=self.eps, sca=self.sca, radii=self.radii.copy(), boxvec=self.boxvec.copy(), rcut=self.rcut, ndim=self.ndim, ncellx_scale=self.ncellx_scale) self.tol = 1e-7 self.maxstep = np.amax(self.radii) self.nstepsmax = int(1e6) assert(self.boxvec[0]==self.boxvec[1]) print "x_initial energy:", self.potential.getEnergy(self.x_initial) print "x_initial cells energy:", self.potential_cells.getEnergy(self.x_initial) assert(self.potential.getEnergy(self.x_initial) == self.potential_.getEnergy(self.x_initial)) assert(self.potential_cells.getEnergy(self.x_initial) == self.potential_cells_.getEnergy(self.x_initial)) #assert abs(self.potential.getEnergy(self.x_initial) - self.potential_cells.getEnergy(self.x_initial)) < 1e-10 assert np.allclose(self.potential.getEnergy(self.x_initial), self.potential_cells.getEnergy(self.x_initial), rtol=1e-10) print self.boxvec #plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) def optimize(self, nr_samples = 1): self.optimizer = ModifiedFireCPP(self.x_initial.copy(), self.potential, dtmax=1, maxstep=self.maxstep, tol=self.tol, nsteps=1e8, verbosity=-1, iprint=-1) self.optimizer_ = LBFGS_CPP(self.x_initial.copy(), self.potential_) self.optimizer_cells = ModifiedFireCPP(self.x_initial.copy(), self.potential_cells, dtmax=1, maxstep=self.maxstep, tol=self.tol, nsteps=1e8, verbosity=-1, iprint=-1) self.optimizer_cells_ = LBFGS_CPP(self.x_initial.copy(), self.potential_cells_) print "initial E, x:", self.potential.getEnergy(self.x_initial.copy()) print "initial E, x_:", self.potential_cells.getEnergy(self.x_initial.copy()) t0 = time.time() print "self.optimizer.run(self.nstepsmax)", self.nstepsmax self.optimizer.run(self.nstepsmax) self.res_x_final = self.optimizer.get_result() t1 = time.time() self.optimizer_cells.run(self.nstepsmax) self.res_x_final_cells = self.optimizer_cells.get_result() t2 = time.time() self.x_final = self.res_x_final.coords self.x_final_cells = self.res_x_final_cells.coords print "fire final E, x:", self.optimizer.get_result().energy print "fire final E, x_cells:", self.optimizer_cells.get_result().energy print "fire final E, plain: ", self.potential.getEnergy(self.x_final) print "fire final E, cell: ", self.potential_cells.getEnergy(self.x_final_cells) print "fire number of particles:", self.N print "fire time no cell lists:", t1 - t0, "sec" print "fire time cell lists:", t2 - t1, "sec" print "fire ratio:", (t1 - t0) / (t2 - t1) if not self.res_x_final.success or not self.res_x_final_cells.success: print "-------------" print "res_x_final.rms:", self.res_x_final.rms print "res_x_final.nfev:", self.res_x_final.nfev print "res_x_final_cells.rms:", self.res_x_final_cells.rms print "res_x_final_cells.nfev:", self.res_x_final_cells.nfev print "self.res_x_final.success", self.res_x_final.success print "self.res_x_final_cells.success", self.res_x_final_cells.success print "-------------" plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_cells, self.radii, self.boxvec, sca=self.sca) self.optimizer_.run(self.nstepsmax) self.res_x_final_ = self.optimizer_.get_result() t3 = time.time() self.optimizer_cells_.run(self.nstepsmax) self.res_x_final_cells_ = self.optimizer_cells_.get_result() t4 = time.time() self.x_final_ = self.res_x_final_.coords self.x_final_cells_ = self.res_x_final_cells_.coords print "lbfgs final E, x:", self.optimizer_.get_result().energy print "lbfgs final E, x_cells:", self.optimizer_cells_.get_result().energy print "lbfgs final E, plain: ", self.potential_.getEnergy(self.x_final_) print "lbfgs final E, cell: ", self.potential_cells_.getEnergy(self.x_final_cells_) print "lbfgs number of particles:", self.N print "lbfgs time no cell lists:", t3 - t2, "sec" print "lbfgs time cell lists:", t4 - t3, "sec" print "lbfgs ratio:", (t3 - t2) / (t4 - t3) if not self.res_x_final_.success or not self.res_x_final_cells_.success or not self.res_x_final.success or not self.res_x_final_cells.success: print "-------------" print "res_x_final_.rms:", self.res_x_final_.rms print "res_x_final_.nfev:", self.res_x_final_.nfev print "res_x_final_cells_.rms:", self.res_x_final_cells_.rms print "res_x_final_cells_.nfev:", self.res_x_final_cells_.nfev print "self.res_x_final_.success", self.res_x_final.success print "self.res_x_final_cells_.success", self.res_x_final_cells.success print "-------------" plot_disks(self.x_initial, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_, self.radii, self.boxvec, sca=self.sca) plot_disks(self.x_final_cells_, self.radii, self.boxvec, sca=self.sca) assert(self.res_x_final.success) assert(self.res_x_final_cells.success) assert(self.res_x_final_.success) assert(self.res_x_final_cells_.success) for (xci, xi) in zip(self.x_final_cells, self.x_final): passed = (np.abs(xci - xi) < 1e-10) if (passed is False): print "xci", xci print "xi", xi assert(passed) print "energy no cell lists:", self.res_x_final.energy print "energy cell lists:", self.res_x_final_cells.energy self.t_ratio = (t1 - t0) / (t2 - t1) self.t_ratio_lbfgs = (t3 - t2) / (t4 - t3)