Skip to content

Commit

Permalink
Merge pull request #31 from rinikerlab/build_release_1
Browse files Browse the repository at this point in the history
test Pull request
  • Loading branch information
schroederb authored Aug 27, 2020
2 parents bd31eb2 + c93bb2c commit f618e14
Show file tree
Hide file tree
Showing 14 changed files with 663 additions and 104 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macOS-latest, ubuntu-latest]
python-version: [3.6, 3.7]
os: [macOS-latest, ubuntu-latest, windows-latest]
python-version: [3.6, 3.7, 3.8]

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -67,3 +67,4 @@ jobs:
file: ./coverage.xml
flags: unittests
name: codecov-${{ matrix.os }}-py${{ matrix.python-version }}

7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,10 @@ ENV/
ensembler/potentials/look_up_tables
examples/dev/img
examples/dev/ttt.ipynb


#test results:
ensembler/tests/tmp_test_*

#jupyter_nB
**/.ipynb_checkpoints
2 changes: 1 addition & 1 deletion ensembler/potentials/_basicPotentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def ene(self, positions: Union[Number, Sized]) -> Union[Number, Sized]:
return np.squeeze(self._calculate_energies(*np.hsplit(np.array(positions, ndmin=1), self.constants[self.nDim])))

def force(self, positions: Union[Number, Sized]) -> Union[Number, Sized]:
return np.squeeze(self._calculate_dVdpos(*np.hsplit(np.array(positions, ndmin=1), self.constants[self.nDim])))
return np.squeeze(self._calculate_dVdpos(*np.hsplit(np.array(positions, ndmin=1), self.constants[self.nDim]))).T

# just alternative name
def dvdpos(self, positions: Union[Number, Sized]) -> Union[Number, Sized]:
Expand Down
11 changes: 6 additions & 5 deletions ensembler/potentials/biased_potentials/biasOneD.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import numpy as np
import sympy as sp

from ensembler.potentials.OneD import gaussPotential
from ensembler.potentials.OneD import gaussPotential, harmonicOscillatorPotential
from ensembler.potentials._basicPotentials import _potential1DCls
from ensembler.samplers.stochastic import langevinIntegrator

from ensembler.samplers.stochastic import langevinIntegrator
"""
BIAS BASECLASS
"""
Expand Down Expand Up @@ -109,7 +110,7 @@ class addedPotentials(_potential1DCls):
name: str = "Added Potential Enhanced Sampling System"
position = sp.symbols("r")

def __init__(self, origPotential, addPotential):
def __init__(self, origPotential=harmonicOscillatorPotential(), addPotential=gaussPotential()):
'''
__init__
This is the Constructor of the addedPotential class.
Expand Down Expand Up @@ -152,7 +153,7 @@ class metadynamicsPotential(_potential1DCls):
name: str = "Metadynamics Enhanced Sampling System using grid bias"
position = sp.symbols("r")

def __init__(self, origPotential, amplitude=0.1, sigma=1, n_trigger=100, bias_grid_min=0, bias_grid_max=10,
def __init__(self, origPotential=harmonicOscillatorPotential(), amplitude=0.1, sigma=1, n_trigger=100, bias_grid_min=0, bias_grid_max=10,
numbins=100):

'''
Expand Down Expand Up @@ -307,7 +308,7 @@ def force(self, positions):
-------
'''

current_bin = self._find_nearest(self.bin_centers, positions)
current_bin = np.apply_over_axes(self._find_nearest, a= np.array(positions), axes=0) #self._find_nearest(self.bin_centers, positions)
force= np.squeeze(self._calculate_dVdpos(np.squeeze(positions)) + self.bias_grid_force[current_bin])
return force

Expand Down
45 changes: 18 additions & 27 deletions ensembler/potentials/biased_potentials/biasTwoD.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
import sympy as sp

from ensembler.potentials.TwoD import gaussPotential
from ensembler.potentials.TwoD import gaussPotential, harmonicOscillatorPotential
from ensembler.potentials._basicPotentials import _potential2DCls
from ensembler.util.ensemblerTypes import system

Expand All @@ -26,7 +26,7 @@ class addedPotentials(_potential2DCls):
nDim: int = sp.symbols("nDim")
position: sp.Matrix = sp.Matrix([sp.symbols("r")])

def __init__(self, origPotential, addPotential):
def __init__(self, origPotential=harmonicOscillatorPotential(), addPotential=gaussPotential()):
'''
__init__
This is the Constructor of the addedPotential class.
Expand All @@ -44,10 +44,7 @@ def __init__(self, origPotential, addPotential):

self.constants = {**origPotential.constants, **addPotential.constants}

self.V_orig = origPotential.V + self.addPotential.V

self.V = self.V_orig.subs(self.constants)
self.dVdpos = sp.diff(self.V, self.position)
self.V_functional = origPotential.V_functional + self.addPotential.V_functional

super().__init__()

Expand All @@ -61,7 +58,7 @@ def _initialize_functions(self):
TIME DEPENDENT BIASES
"""


#TODO: I don't think this does, what it should!
class metadynamicsPotential(_potential2DCls):
'''
The metadynamics bias potential adds 2D Gaussian potentials on top of
Expand All @@ -76,7 +73,7 @@ class metadynamicsPotential(_potential2DCls):
position = sp.symbols("r")
system: system # metadyn-coupled to system

def __init__(self, origPotential, amplitude=1., sigma=(1., 1.), n_trigger=100, bias_grid_min=(0, 0),
def __init__(self, origPotential=harmonicOscillatorPotential(), amplitude=1., sigma=(1., 1.), n_trigger=100, bias_grid_min=(0, 0),
bias_grid_max=(10, 10), numbins=(100, 100)):
'''
Expand Down Expand Up @@ -121,10 +118,8 @@ def __init__(self, origPotential, amplitude=1., sigma=(1., 1.), n_trigger=100, b

self.constants = {**origPotential.constants}

self.V_orig = origPotential.V
self.V_orig_part = self.V_orig.subs(self.constants)
self.dVdpos = sp.diff(self.V_orig_part, self.position)
self.V = self.V_orig_part
self.V_functional = origPotential.V
self.V_orig_part = self.V_functional.subs(self.constants)

super().__init__()

Expand All @@ -133,11 +128,11 @@ def _initialize_functions(self):
nDim = self.constants[self.nDim]
self.position = sp.Matrix([sp.symbols("r_" + str(i)) for i in range(nDim)])

"""
'''
BIAS
"""
'''

# Beautiful integration to system as Condition.
#Beautiful integration to system as Condition.
def apply(self):
self.check_for_metastep(self.system._currentPosition)

Expand All @@ -163,14 +158,6 @@ def check_for_metastep(self, curr_position):
'''
if (self.system.step % self.n_trigger == 0):
self._update_potential(curr_position)
"""
TODO: Remove
if self.current_n%self.n_trigger == 0:
self._update_potential(curr_position)
self.current_n += 1
else:
self.current_n += 1
"""

def _update_potential(self, curr_position):
'''
Expand Down Expand Up @@ -213,8 +200,9 @@ def ene(self, positions):
-------
current energy
'''
current_bin_x = self._find_nearest(self.x_centers, positions[0])
current_bin_y = self._find_nearest(self.y_centers, positions[1])
current_bin_x = np.apply_over_axes(self._find_nearest, a= np.array(positions).T[0], axes=0) #self._find_nearest(self.bin_centers, positions)
current_bin_y = np.apply_over_axes(self._find_nearest, a= np.array(positions).T[1], axes=0) #self._find_nearest(self.bin_centers, positions)

# due to transposed position matrix, x and y are changed here
return np.squeeze(
self._calculate_energies(*np.hsplit(positions, self.constants[self.nDim])) + self.bias_grid_energy[
Expand All @@ -233,10 +221,13 @@ def force(self, positions):
current derivative dh/dpos
-------
'''
current_bin_x = np.apply_over_axes(lambda x: self._find_nearest(self.bin_centers_x, x), a= np.array(positions).T[0], axes=0) #self._find_nearest(self.bin_centers, positions)
current_bin_y = np.apply_over_axes(lambda x: self._find_nearest(self.bin_centers_y, x), a= np.array(positions).T[1], axes=0) #self._find_nearest(self.bin_centers, positions)

current_bin_x = self._find_nearest(self.x_centers, positions[0])
current_bin_y = self._find_nearest(self.y_centers, positions[1])
print(current_bin_x)
# due to transposed position matrix, x and y are changed here
print( self.bias_grid_force[:,current_bin_y,
current_bin_x].reshape(2, 1, 1))
return np.squeeze(
self._calculate_dVdpos(*np.hsplit(positions, self.constants[self.nDim])) + self.bias_grid_force[:,
current_bin_y,
Expand Down
6 changes: 3 additions & 3 deletions ensembler/system/basic_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def __init__(self, potential: _potentialCls, sampler: _samplerCls, conditions: I
self.nparticles = 1 # Todo: adapt it to be multiple particles

# Output
self.currentState: data.basicState = data.basicState(np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan)
self.trajectory: pd.DataFrame = pd.DataFrame(columns=list(self.state.__dict__["_fields"]))
self.currentState = self.state(**{key: np.nan for key in self.state.__dict__["_fields"]})
self.trajectory= pd.DataFrame(columns=list(self.state.__dict__["_fields"]))

# tmpvars - private:
self._currentTotE: (Number) = np.nan
Expand Down Expand Up @@ -212,8 +212,8 @@ def _init_position(self, initial_position=None):
self.initial_position = initial_position
else:
raise Exception("did not understand the initial position!")

self._currentPosition = self.initial_position

self.updateCurrentState()
return self.initial_position

Expand Down
26 changes: 3 additions & 23 deletions ensembler/system/eds_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,15 @@ def __init__(self, potential: pot.envelopedPotential = pot.envelopedPotential(
# Declare Attributes
#################################

self.state = data.envelopedPStstate
self._currentEdsS = eds_s
self._currentEdsEoffs = eds_Eoff
self.state = data.envelopedPStstate

##Physical parameters
self.temperature: float = 298.0
self.mass: float = 1 # for one particle systems!!!!
self.nparticles: int = 1 # Todo: adapt it to be multiple particles
super().__init__(potential=potential, sampler=sampler, conditions=conditions, temperature=temperature,
start_position=start_position)

self.nDim: int = -1
self.nStates: int = 1

# Output
self.initial_position: Iterable[float] or float

self.currentState: data.basicState = data.basicState(np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan)
self.trajectory: pd.DataFrame = pd.DataFrame(columns=list(self.state.__dict__["_fields"]))

# tmpvars - private:
self._currentTotE: (Number) = np.nan
self._currentTotPot: (Number) = np.nan
self._currentTotKin: (Number) = np.nan
self._currentPosition: (Number or Iterable[Number]) = np.nan
self._currentVelocities: (Number or Iterable[Number]) = np.nan
self._currentForce: (Number or Iterable[Number]) = np.nan
self._currentTemperature: (Number or Iterable[Number]) = np.nan

super().__init__(potential=potential, sampler=sampler, conditions=conditions, temperature=temperature,
start_position=start_position)
self.set_s(self._currentEdsS)
self.set_Eoff(self._currentEdsEoffs)

Expand Down
2 changes: 2 additions & 0 deletions ensembler/system/perturbed_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class perturbedSystem(system):
def __init__(self, potential: _perturbedPotentialCls, sampler: _integratorCls,
conditions: Iterable[_conditionCls] = [],
temperature: float = 298.0, start_position: (Iterable[Number] or float) = None, lam: float = 0.0):

self._currentLam = lam

super().__init__(potential=potential, sampler=sampler, conditions=conditions, temperature=temperature,
start_position=start_position)
self.set_lam(lam)
Expand Down
2 changes: 1 addition & 1 deletion ensembler/tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ensembler.analysis.freeEnergyCalculation import zwanzigEquation, threeStateZwanzigReweighting, bennetAcceptanceRatio

#tmp_potentials = tempfile.mkdtemp(dir=os.getcwd(), prefix="test_conditions")
#tmp_potentials = tempfile.mkdtemp(dir=os.getcwd(), prefix="tmp_test_conditions")


class test_ZwanzigEquation(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion ensembler/tests/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ensembler.conditions.box_conditions import periodicBoundaryCondition, boxBoundaryCondition
from ensembler.conditions.restrain_conditions import positionRestraintCondition

tmp_potentials = tempfile.mkdtemp(dir=os.getcwd(), prefix="test_conditions")
tmp_potentials = tempfile.mkdtemp(dir=os.getcwd(), prefix="tmp_test_conditions")


class boxBoundaryCondition(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion ensembler/tests/test_integrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
STOCHASTIC INTEGRATORS
"""
tmp_dir = tempfile.mkdtemp(dir=os.getcwd(), prefix="test_integrators")
tmp_dir = tempfile.mkdtemp(dir=os.getcwd(), prefix="tmp_test_integrators")


class standard_IntegratorTests(unittest.TestCase):
Expand Down
Loading

0 comments on commit f618e14

Please sign in to comment.