source: tests/Python/ForceAnnealing/pre/ising_model_chain.py@ 93fd2a6

ForceAnnealing_with_BondGraph_continued
Last change on this file since 93fd2a6 was 93fd2a6, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

tempcommit: Fixes to Python tests due to changed anneal() and modified way of trajectory steps usage.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1import pyMoleCuilder as mol
2import sys, os, math
3import numpy
4
5if len(sys.argv) < 5:
6 print 'Usage: '+sys.argv[0]+' <input> <path> <steps> <no_atoms> <use bondgraph>'
7 sys.exit(1)
8
9steps=int(sys.argv[3])
10equilibrium_distance=1.6
11no_atoms=int(sys.argv[4])
12inputfile=sys.argv[1]
13forcespath=sys.argv[2]
14forcesfile="ising.forces"
15use_bondgraph=sys.argv[5]
16
17# creating input file
18atomstart=7.6-1.6*math.floor(no_atoms/2)
19print "Creating "+inputfile
20with open(inputfile, 'w') as f:
21 f.write("# ATOMDATA\ttype\tId\tx=3\tu=3\tF=3\tneighbors=4\n")
22 f.write("# Box\t20\t0\t0\t0\t20\t0\t0\t0\t20\n")
23 for i in range(1, no_atoms+1):
24 atompos=atomstart+1.6*float(i)
25 if i==math.floor(no_atoms/2+1):
26 atompos=atompos-.5
27 if i==1:
28 f.write("C\t%d\t%lg\t10\t10\t0\t0\t0\t0\t0\t0\t%d\t0\t0\t0\n" % (i, atompos, i+1));
29 elif i==no_atoms:
30 f.write("C\t%d\t%lg\t10\t10\t0\t0\t0\t0\t0\t0\t%d\t0\t0\t0\n" % (i, atompos, i-1));
31 else:
32 f.write("C\t%d\t%lg\t10\t10\t0\t0\t0\t0\t0\t0\t%d\t%d\t0\t0\n" % (i, atompos, i-1, i+1));
33
34print "Parsing from "+inputfile
35mol.WorldInput(inputfile)
36mol.SelectionAllAtoms()
37mol.CommandVerbose("4")
38
39# calculate damping factor from finite geometric series
40# s_n/a = \sum^{n-1}_{k=0} r^k = (1-r^n)/(1-r) -> s_(n+1)/a -1 = \sum^{n}_{k=1} r^k = (1-r^(n+1))/(1-r) - 1
41# \sum^{n}_{k=1} r^k := 1 and 1 = (1-r^(n+1))/(1-r) - 1 -> 2*(1-r) = 1 - r^(n+1) -> 1 - 2*r + r^(n+1) = 0
42# find root: p[0] is coefficient of monomial with highest power
43p=[0.] * (no_atoms+1)
44p[0]=1.
45p[no_atoms-1]=-2.
46p[no_atoms]=1.
47zeros=numpy.roots(p)
48print("Roots of p "+str(p)+" are "+str(zeros))
49damping=numpy.real(zeros[-1])
50print "Using damping factor of "+str(damping)
51
52for i in range(0, steps):
53 # TODO: Python interface should have something to iterate over selected atoms
54 # and molecules and get information on their internal status
55
56 # read current atomic positions
57 outputfile=forcespath+'/'+forcesfile+'.xyz'
58 try:
59 os.remove(outputfile)
60 except: OSError
61 #
62 mol.WorldOutputAs(outputfile)
63 mol.wait()
64 distances=[]
65 coords=[0.,0.,0.]
66 try:
67 skiplines=2+i*(1+1+no_atoms+1) # no_atoms, comment, no_atoms atoms, empty line
68 with open(outputfile) as f:
69 for line in f:
70 if skiplines != 0:
71 skiplines=skiplines-1
72 continue
73 line=line.replace('\t',' ')
74 print "LINE: "+line
75 [elementtype, X, Y, Z] = line.split(' ', 4)
76 if coords!=[0.,0.,0.]:
77 distances.append(math.sqrt((coords[0]-float(X))**2+(coords[1]-float(Y))**2+(coords[2]-float(Z))**2))
78 coords=[float(X),float(Y),float(Z)]
79 except IOError:
80 print 'Warning: '+outputfile+' not readable.'
81 sys.exit(1)
82
83 assert(len(distances)==no_atoms-1)
84
85 #
86 # generate Ising model forces and store in file
87 #
88 # i.e. we have spring forces between neighboring atoms depending on their distance
89 forces=[]
90 for d in distances:
91 forces.append( d - equilibrium_distance );
92
93 # generate new forces file
94
95 with open(forcespath+'/'+forcesfile, 'w') as f:
96 f.write('# atom\tf_x\tf_y\tf_z\n')
97 for i in range(len(distances)+1):
98 force=0
99 if i!=0:
100 force=force-forces[i-1]
101 if (i != len(distances)):
102 force=force+forces[i]
103 f.write("%d\t%f\t0.\t0.\n" % (i+1, force))
104
105 mol.MoleculeForceAnnealing(forcespath+'/'+forcesfile, ".1", "%d" % (steps), "%d" % (no_atoms-1), "%lg" % (damping), use_bondgraph)
106 mol.wait()
107
108sys.exit(0)
Note: See TracBrowser for help on using the repository browser.