Blog: Experiment: Conformational analysis of butane: C4H10_rotate-bond.py

File C4H10_rotate-bond.py, 2.7 KB (added by FrederikHeber, 7 years ago)

python script using pyMoleCuilder for rotating butane around its central C-C bond

Line 
1import pyMoleCuilder
2import os, sys, csv
3
4# check arguments
5if len(sys.argv) < 8:
6 sys.stderr.write("Usage: "+sys.argv[0]+" <source file> <steps> <basis> <theory> <server> <port> <workdir> <energyfile> <conformationfile>\n")
7 sys.exit(255)
8
9order=4
10sourcefile=sys.argv[1]
11steps=int(sys.argv[2])
12basis=sys.argv[3]
13theory=sys.argv[4]
14serveraddress=sys.argv[5]
15serverport=sys.argv[6]
16workdir=sys.argv[7]
17energyfile=sys.argv[8]
18conformationfile=""
19if len(sys.argv) >= 9:
20 conformationfile=sys.argv[9]
21
22# ========================== Stored Session BEGIN ==========================
23pyMoleCuilder.MoleculeLoad(sourcefile)
24pyMoleCuilder.ParserSetParserParameters(set_parser_parameters="mpqc", parser_parameters="basis="+basis+";theory="+theory+";do_gradient=no;")
25energies={}
26stepwidth=int(360/steps)
27for step in range(0,360,stepwidth):
28 pyMoleCuilder.wait()
29 # calculate energy
30 pyMoleCuilder.SelectionAllAtoms()
31 pyMoleCuilder.FragmentationFragmentation(fragment_molecule="BondFragment", distance="3", order=str(order), DoSaturate="1", ExcludeHydrogen="1", output_types="", max_meshwidth="0", grid_level="5", inter_order="0", DoCyclesFull="0", parse_state_files="0")
32 pyMoleCuilder.FragmentationFragmentationAutomation(server_address=serveraddress, server_port=serverport, fragment_executable="", max_meshwidth="0", grid_level="5", near_field_cells="3", interpolation_degree="3", DoLongrange="0", DoValenceOnly="0", DoPrintDebug="0", DoSmearElectronicCharges="0", UseImplicitCharges="0")
33 pyMoleCuilder.FragmentationAnalyseFragmentationResults(store_grids="0", fragment_prefix="BondFragment", UseImplicitCharges="0")
34 # extract energy and place in hash map
35 pyMoleCuilder.wait()
36 with open(workdir+"/BondFragment_Energy.dat", 'rb') as csvfile:
37 energyreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
38 for row in energyreader:
39 if (row[0]==str(order)):
40 energies[step]=row[1]
41 break
42 # select bond
43 pyMoleCuilder.SelectionClearAllAtoms()
44 pyMoleCuilder.SelectionAtomById("0")
45 pyMoleCuilder.SelectionAtomById("1")
46 # advance to next time step
47 pyMoleCuilder.WorldStepWorldTime("1")
48 # rotate bond
49 pyMoleCuilder.MoleculeRotateAroundBond(str(stepwidth), "1")
50# output all
51pyMoleCuilder.ParserSetOutputFormats("tremolo")
52pyMoleCuilder.ParserSetTremoloAtomdata(set_tremolo_atomdata="Id type x=3 u=3 F=3 neighbors=4", reset="1")
53if len(conformationfile) != 0:
54 pyMoleCuilder.WorldOutputAs(conformationfile)
55# save energies as csv
56with open(energyfile, 'wb') as csvfile:
57 energywriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
58 energywriter.writerow(["step", "energy"])
59 for key in sorted(energies.keys()):
60 energywriter.writerow([str(key), str(energies[key])])
61# =========================== Stored Session END ===========================
62
63sys.exit(0)