File: Environment.py

package info (click to toggle)
mmtk 2.7.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,788 kB
  • ctags: 6,600
  • sloc: python: 18,050; ansic: 12,400; makefile: 129; csh: 3
file content (106 lines) | stat: -rw-r--r-- 2,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# This module defines environment objects for universes.
#
# Written by Konrad Hinsen
#

"""
Environment objects

Environment objects are objects that define a simulation system without
being composed of atoms. Examples are thermostats, barostats, external
fields, etc.
"""

__docformat__ = 'restructuredtext'

from Scientific import N

#
# The environment object base class
#
class EnvironmentObject(object):

    is_environment_object = 1

    def checkCompatibilityWith(self, other):
        pass

    def description(self):
        return "o('Environment." + self.__class__.__name__ + \
               `tuple(self.parameters)` + "')"

# Type check

def isEnvironmentObject(object):
    return hasattr(object, 'is_environment_object')

#
# Nose thermostat class
#
class NoseThermostat(EnvironmentObject):

    """
    Nose thermostat for Molecular Dynamics

    A thermostat object can be added to a universe and will then
    modify the integration algorithm to a simulation of an NVT
    ensemble.
    """

    def __init__(self, temperature, relaxation_time = 0.2):
        """
        :param temperature: the temperature set by the thermostat
        :type temperature: float
        :param relaxation_time: the relaxation time of the
                                thermostat coordinate
        :type relaxation_time: float
        """
        self.arguments = (temperature, relaxation_time)
        self.parameters = N.array([temperature, relaxation_time])
        self.coordinates = N.array([0., 0.])

    def setTemperature(self, temperature):
        self.parameters[0] = temperature

    def setRelaxationTime(self, t):
        self.parameters[1] = t

    def checkCompatibilityWith(self, other):
        if other.__class__ is NoseThermostat:
            raise ValueError("the universe already has a thermostat")

#
# Andersen barostat class
#
class AndersenBarostat(EnvironmentObject):

    """
    Andersen barostat for Molecular Dynamics

    A barostat object can be added to a universe and will then
    together with a thermostat object modify the integration algorithm
    to a simulation of an NPT ensemble.
    """

    def __init__(self, pressure, relaxation_time = 1.5):
        """
        :param pressure: the pressure set by the barostat
        :type pressure: float
        :param relaxation_time: the relaxation time of the
                                barostat coordinate
        :type relaxation_time: float
        """
        self.arguments = (pressure, relaxation_time)
        self.parameters = N.array([pressure, relaxation_time])
        self.coordinates = N.array([0.])

    def setPressure(self, pressure):
        self.parameters[0] = pressure

    def setRelaxationTime(self, t):
        self.parameters[1] = t

    def checkCompatibilityWith(self, other):
        if other.__class__ is AndersenBarostat:
            raise ValueError("the universe already has a barostat")