File: Scientific_28.html

package info (click to toggle)
python-scientific 2.2-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,368 kB
  • ctags: 2,396
  • sloc: python: 6,468; ansic: 3,643; xml: 3,596; makefile: 79; sh: 27
file content (61 lines) | stat: -rw-r--r-- 2,069 bytes parent folder | download | duplicates (3)
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
<a name="Module:Scientific.Physics.Potential"><h1>Module Scientific.Physics.Potential</h1></a>

<p>This module offers two strategies for automagically calculating the
gradients (and optionally force constants) of a potential energy
function (or any other function of vectors, for that matter).  The
more convenient strategy is to create an object of the class
PotentialWithGradients. It takes a regular Python function object
defining the potential energy and is itself a callable object
returning the energy and its gradients with respect to all arguments
that are vectors.</p>

Example:

<pre>
def _harmonic(k,r1,r2):
    dr = r2-r1
    return k*dr*dr
harmonic = PotentialWithGradients(_harmonic)
energy, gradients = harmonic(1., Vector(0,3,1), Vector(1,2,0))
print energy, gradients
</pre>
<p>  prints</p>

<pre>
3.0
[Vector(-2.0,2.0,2.0), Vector(2.0,-2.0,-2.0)]
</pre>
The disadvantage of this procedure is that if one of the arguments is a
vector parameter, rather than a position, an unnecessary gradient will
be calculated. A more flexible method is to insert calls to two
function from this module into the definition of the energy
function. The first, DerivVectors(), is called to indicate which
vectors correspond to gradients, and the second, EnergyGradients(),
extracts energy and gradients from the result of the calculation.
The above example is therefore equivalent to

<pre>
def harmonic(k, r1, r2):
    r1, r2 = DerivVectors(r1, r2)
    dr = r2-r1
    e = k*dr*dr
    return EnergyGradients(e,2)
</pre>
To include the force constant matrix, the above example has to be
modified as follows:

<pre>
def _harmonic(k,r1,r2):
    dr = r2-r1
    return k*dr*dr
harmonic = PotentialWithGradientsAndForceConstants(_harmonic)
energy, gradients, force_constants = harmonic(1.,Vector(0,3,1),Vector(1,2,0))
print energy
print gradients
print force_constants
</pre>
<p>The force constants are returned as a nested list representing a
matrix. This can easily be converted to an array for further
processing if the numerical extensions to Python are available.
</p>