| 1 | import pyMoleCuilder as mol | 
|---|
| 2 | import sys, os, math | 
|---|
| 3 | import numpy | 
|---|
| 4 |  | 
|---|
| 5 | if len(sys.argv) < 5: | 
|---|
| 6 | print 'Usage: '+sys.argv[0]+' <input> <path> <steps> <no_atoms> <use bondgraph>' | 
|---|
| 7 | sys.exit(1) | 
|---|
| 8 |  | 
|---|
| 9 | steps=int(sys.argv[3]) | 
|---|
| 10 | equilibrium_distance=1.6 | 
|---|
| 11 | no_atoms=int(sys.argv[4]) | 
|---|
| 12 | inputfile=sys.argv[1] | 
|---|
| 13 | forcespath=sys.argv[2] | 
|---|
| 14 | forcesfile="ising.forces" | 
|---|
| 15 | use_bondgraph=sys.argv[5] | 
|---|
| 16 |  | 
|---|
| 17 | # creating input file | 
|---|
| 18 | atomstart=7.6-1.6*math.floor(no_atoms/2) | 
|---|
| 19 | print "Creating "+inputfile | 
|---|
| 20 | with 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 |  | 
|---|
| 34 | print "Parsing from "+inputfile | 
|---|
| 35 | mol.WorldInput(inputfile) | 
|---|
| 36 | mol.SelectionAllAtoms() | 
|---|
| 37 | mol.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 | 
|---|
| 43 | p=[0.] * (no_atoms+1) | 
|---|
| 44 | p[0]=1. | 
|---|
| 45 | p[no_atoms-1]=-2. | 
|---|
| 46 | p[no_atoms]=1. | 
|---|
| 47 | zeros=numpy.roots(p) | 
|---|
| 48 | print("Roots of p "+str(p)+" are "+str(zeros)) | 
|---|
| 49 | damping=numpy.real(zeros[-1]) | 
|---|
| 50 | print "Using damping factor of "+str(damping) | 
|---|
| 51 |  | 
|---|
| 52 | for 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.WorldStepWorldTime("1") | 
|---|
| 106 | mol.MoleculeForceAnnealing(forcespath+'/'+forcesfile, ".1", "%d" % (steps), "%d" % (no_atoms-1), "%lg" % (damping), use_bondgraph) | 
|---|
| 107 | mol.wait() | 
|---|
| 108 |  | 
|---|
| 109 | sys.exit(0) | 
|---|