def generate_state(index): coords = index * np.ones((N_ATOMS, 3)) vels = index * np.ones((N_ATOMS, 3)) alpha = float(index) / 10. energy = float(index) return SystemState(coords, vels, alpha, energy)
def generate_state(index): coords = index * np.ones((N_ATOMS, 3)) vels = index * np.ones((N_ATOMS, 3)) alpha = float(index) / 10. energy = float(index) box_vectors = np.zeros(3) return SystemState(coords, vels, alpha, energy, box_vectors)
def _run_param_mc(self, state): if not self._parameter_manager.has_parameters(): return state if self._options.param_mcmc_steps is None: raise RuntimeError( "There are sampled parameters, but param_mcmc_steps is not set." ) energy = self.get_energy(state) for _ in range(self._options.param_mcmc_steps): trial_params = self._parameter_manager.sample(state.parameters) if not self._parameter_manager.is_valid(trial_params): accept = False else: trial_state = SystemState( state.positions, state.velocities, state.alpha, state.energy, state.box_vector, trial_params, state.mappings, ) trial_energy = self.get_energy(trial_state) delta = trial_energy - energy if delta < 0: accept = True else: if random.random() < math.exp(-delta): accept = True else: accept = False if accept: state = trial_state energy = trial_energy # Update transfomers in case we rejected the # last MCMC move if not accept: self._transformers_update(state) return state
def test_should_reject_impossible_move(self): # setup start = np.array([ [0.0, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, -2.0], [1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0], ]) end = np.array([ [0.0, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, -2.0], [1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0], ]) state = SystemState(start, np.zeros_like(start), 0.0, 0.0, np.zeros(3)) mock_runner = mock.Mock() mock_runner.get_energy.return_value = 1000.0 mover = mc.RandomTorsionMover( AtomIndex(0), AtomIndex(1), [AtomIndex(4), AtomIndex(5)]) # exercise with mock.patch("meld.system.montecarlo._generate_uniform_angle" ) as mock_gen_angle: mock_gen_angle.return_value = 90.0 new_state, accepted = mover.trial(state, mock_runner) # assert self.assertEqual(new_state.energy, 0.0) self.assertEqual(accepted, False) np.testing.assert_array_almost_equal(new_state.positions, end)