Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ghutchis
Created September 14, 2023 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghutchis/49a3d98179756e4dc0f44b54ac194be6 to your computer and use it in GitHub Desktop.
Save ghutchis/49a3d98179756e4dc0f44b54ac194be6 to your computer and use it in GitHub Desktop.
Avogadro Batch Render Orbitals
#!/usr/bin/python
import json
import socket
import struct
import glob
import tempfile
import os
import time
class Connection:
'''Process a JSON-RPC request'''
def __init__(self, name="avogadro"):
"""
Connect to the local named pipe
:param name: The name of the named pipe.
"""
# create socket
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# connect
self.sock.connect(tempfile.gettempdir() + "/" + name)
def send_json(self, obj):
"""
Send a JSON-RPC request to the named pipe.
:param obj: The JSON-RPC request object.
"""
self.send_message(json.dumps(obj))
def send_message(self, msg):
"""
Send a message to the named pipe
:param msg: The message to send.
"""
size = len(msg)
header = struct.pack(">I", size)
packet = header + msg.encode("ascii")
self.sock.send(packet)
def receive_message(self, size=1024):
"""
Receive a message from the named pipe.
:param size: The maximum size of the message to receive.
"""
packet = self.sock.recv(size)
return packet[4:]
def recv_json(self):
'''Receive a JSON-RPC response'''
msg = self.recv_message()
try:
return json.loads(msg)
except Exception as exception:
print("error: " + str(exception))
return {}
def close(self):
'''Close the socket to the named pipe'''
self.sock.close()
def sendMessage(method, options):
conn = Connection()
conn.send_json({"jsonrpc": "2.0", "id": 0, "method": method, "params": options})
reply = str(conn.receive_message())
conn.close()
return reply
if __name__ == "__main__":
conn = Connection()
# render the orbital
options = {
"orbital": "homo",
"isovalue": 0.003
}
cwd = os.getcwd()
for fileName in glob.glob("*.fchk"):
# open the file
print("Opening " + fileName)
path = cwd + "/" + fileName
basename = path[:-5] # strip off the .fchk
sendMessage("openFile", {"fileName": path})
sendMessage("renderOrbital", options)
# wait 1 minute to compute the surface
# .. adjust as needed
time.sleep(60)
print("Saving HOMO")
sendMessage("saveGraphic", { "fileName": basename + "-homo.png" })
# also render the electron density
sendMessage("renderElectronDensity", {"isovalue": 0.003})
# wait 1 minute to compute the surface
time.sleep(60)
print("Saving Electron Denisty")
sendMessage("saveGraphic", { "fileName": basename + "-edens.png" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment