Skip to content

Commit

Permalink
Merge pull request espressomd#6 from jonaslandsgesell/python3fix
Browse files Browse the repository at this point in the history
Python3fix
  • Loading branch information
fweik committed Apr 27, 2018
2 parents 2288a17 + 7742a02 commit f8dd490
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
36 changes: 30 additions & 6 deletions src/python/espressomd/integrate.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,30 @@ cdef class Integrator(object):

cdef str _method
cdef object _steepest_descent_params
cdef object _isotropic_npt_params

def __init__(self):
self._method = "VV"
self._steepest_descent_params = {}
self._isotropic_npt_params = {}

def __getstate__(self):
state = {}
state['_method'] = self._method
state['_steepest_descent_params'] = self._steepest_descent_params
state['_isotropic_npt_params'] = self._isotropic_npt_params
return state

def __setstate__(self, state):
self._method = state['_method']
if self._method == "STEEPEST_DESCENT":
self.set_steepest_descent(state['_steepest_descent_params'])
elif self._method == "NVT":
self.set_nvt()
elif self._method == "NPT":
npt_params = state['_isotropic_npt_params']
self.set_isotropic_npt(npt_params['ext_pressure'], npt_params['piston'], direction=npt_params['direction'], cubic_box=npt_params['cubic_box'])


def run(self, steps=1, recalc_forces=False, reuse_forces=False):
"""
Expand All @@ -50,8 +70,7 @@ cdef class Integrator(object):
Reuse the forces from previous time step.
"""

if self._method == "VV":
if self._method == "VV" or self._method == "NVT" or self._method == "NPT":
check_type_or_throw_except(
steps, 1, int, "Integrate requires a positive integer for the number of steps")
check_type_or_throw_except(
Expand All @@ -67,6 +86,8 @@ cdef class Integrator(object):
steps,
self._steepest_descent_params["max_displacement"])
mpi_minimize_energy()
else:
raise ValueError("No integrator method set!")

def set_steepest_descent(self, *args, **kwargs):
"""
Expand All @@ -76,7 +97,6 @@ cdef class Integrator(object):
:class:`espressomd.minimize_energy.MinimizeEnergy`
"""

req = ["f_max", "gamma", "max_displacement"]
for key in kwargs:
if not key in req:
Expand All @@ -97,7 +117,7 @@ cdef class Integrator(object):
Set the integration method to NVT.
"""

self._method = "NVT"
integrate_set_nvt()

def set_isotropic_npt(self, ext_pressure, piston, direction=[0,0,0], cubic_box=False):
Expand All @@ -116,14 +136,18 @@ cdef class Integrator(object):
If this optional parameter is true, a cubic box is assumed.
"""

self._method = "NPT"
self._isotropic_npt_params['ext_pressure'] = ext_pressure
self._isotropic_npt_params['piston'] = piston
self._isotropic_npt_params['direction'] = direction
self._isotropic_npt_params['cubic_box'] = cubic_box
if "NPT" not in espressomd.code_info.features():
raise Exception("NPT is not compiled in")
check_type_or_throw_except(
ext_pressure, 1, float, "NPT parameter ext_pressure must be a float")
check_type_or_throw_except(
piston, 1, float, "NPT parameter piston must be a float")
check_type_or_throw_except(
direction, 3, int, "NPT parameter direction must be an three ints")
direction, 3, int, "NPT parameter direction must be an array-like of three ints")
if (integrate_set_npt_isotropic(ext_pressure, piston, direction[0], direction[1], direction[2], cubic_box)):
handle_errors("Encoutered errors setting up the NPT integrator")
19 changes: 10 additions & 9 deletions src/python/espressomd/reaction_ensemble.pyx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
include "myconfig.pxi"
from libcpp.vector cimport vector
from libcpp.string cimport string
from libc.string cimport strdup
from utils import to_char_pointer
import numpy as np

class WangLandauHasConverged(Exception):
Expand Down Expand Up @@ -496,7 +494,8 @@ cdef class WangLandauReactionEnsemble(ReactionAlgorithm):
raise ValueError(
"At least the following keys have to be given as keyword arguments: " + self._required_keys_add_collective_variable_degree_of_association().__str__() + " got " + kwargs.__str__())
self._params[k] = kwargs[k]
self.WLRptr.add_new_CV_potential_energy(to_char_pointer(self._params["filename"]), self._params["delta"])
filname_potential_energy_boundaries_file=self._params["filename"].encode("utf-8")
self.WLRptr.add_new_CV_potential_energy(filname_potential_energy_boundaries_file, self._params["delta"])

def _valid_keys_add_collective_variable_potential_energy(self):
return "filename", "delta"
Expand Down Expand Up @@ -540,7 +539,7 @@ cdef class WangLandauReactionEnsemble(ReactionAlgorithm):

self.WLRptr.final_wang_landau_parameter = self._params[
"final_wang_landau_parameter"]
self.WLRptr.output_filename = to_char_pointer(self._params["full_path_to_output_filename"])
self.WLRptr.output_filename = self._params["full_path_to_output_filename"].encode("utf-8")
self.WLRptr.do_not_sample_reaction_partition_function = self._params[
"do_not_sample_reaction_partition_function"]

Expand All @@ -552,7 +551,8 @@ cdef class WangLandauReactionEnsemble(ReactionAlgorithm):
Loads the dumped wang landau potential file.
"""
self.WLRptr.load_wang_landau_checkpoint("checkpoint")
checkpoint_name="checkpoint".encode("utf-8")
self.WLRptr.load_wang_landau_checkpoint(checkpoint_name)

def write_wang_landau_checkpoint(self):
"""
Expand All @@ -561,7 +561,8 @@ cdef class WangLandauReactionEnsemble(ReactionAlgorithm):
number of executed trial moves.
"""
self.WLRptr.write_wang_landau_checkpoint("checkpoint")
checkpoint_name="checkpoint".encode("utf-8")
self.WLRptr.write_wang_landau_checkpoint(checkpoint_name)

def update_maximum_and_minimum_energies_at_current_state(self):
"""
Expand All @@ -584,16 +585,16 @@ cdef class WangLandauReactionEnsemble(ReactionAlgorithm):
:meth:`update_maximum_and_minimum_energies_at_current_state` was used.
"""
self.WLRptr.write_out_preliminary_energy_run_results(
"preliminary_energy_run_results")
filename="preliminary_energy_run_results".encode("utf-8")
self.WLRptr.write_out_preliminary_energy_run_results(filename)

def write_wang_landau_results_to_file(self, filename):
"""
This writes out the wang landau potential as a function of the used
collective variables.
"""
self.WLRptr.write_wang_landau_results_to_file(filename)
self.WLRptr.write_wang_landau_results_to_file(filename.encode("utf-8"))


def displacement_mc_move_for_particles_of_type(self, type_mc, particle_number_to_be_changed=1):
Expand Down

0 comments on commit f8dd490

Please sign in to comment.