Skip to content

Commit

Permalink
Patch 0.14.0.0 (#25)
Browse files Browse the repository at this point in the history
* Adding Blender rendering

* Polishing class and example script

* Polishing example
  • Loading branch information
ifilot authored Nov 14, 2023
1 parent dc975e8 commit 47a996f
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 269 deletions.
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.npy
*.jpg
*.ply
*.png
*.txt
133 changes: 133 additions & 0 deletions examples/blender_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from pyqint import Molecule, PyQInt, FosterBoys, GeometryOptimization, HF
from pyqint import MoleculeBuilder, BlenderRender
import pyqint
import numpy as np
import matplotlib.pyplot as plt
import os
import subprocess

#
# Plot the isosurfaces for a number of molecules, prior and after
# orbital localization. Note that this script has been designed to be executed
# in a Linux Ubuntu 22.04 LTS (WSL) container.
#

outpath = os.path.dirname(__file__)

def main():
build_orbitals_co()
build_orbitals_ch4()
build_orbitals_ethylene()

def build_orbitals_co():
"""
Build a montage image of the canonical and localized molecular orbitals
of CO
"""
molname = 'CO'
mol = Molecule('CO')
mol.add_atom('C', 0, 0, -1.08232106)
mol.add_atom('O', 0, 0, 1.08232106)
res = HF().rhf(mol, 'sto3g')
resfb = FosterBoys(res).run()

build(molname, res, resfb, nrows=2)

def build_orbitals_ch4():
"""
Build a montage image of the canonical and localized molecular orbitals
of CH4
"""
molname = 'CH4'
mol = MoleculeBuilder().from_name('ch4')
res = GeometryOptimization().run(mol, 'sto3g')['data']
resfb = FosterBoys(res).run()

build(molname, res, resfb, nrows=3)

def build_orbitals_ethylene():
"""
Build a montage image of the canonical and localized molecular orbitals
of CH4
"""
molname = 'ethylene'
mol = MoleculeBuilder().from_name('ethylene')
res = GeometryOptimization().run(mol, 'sto3g')['data']
resfb = FosterBoys(res).run()

build(molname, res, resfb, nrows=2)

def build(molname, res, resfb, nrows=2):
"""
Build isosurfaces, montage and print energies to a file
:param molname: Name of the molecule
:type molname: string
:param res: Results of a Hartree-Fock calculation
:type res: dictionary
:param resfb: Results of a Foster-Boys localization
:type resfb: dictionary
:param nrows: Number of rows in the montage
:type nrows: int
"""
build_isosurfaces(molname, res, resfb)
montage(molname, nrows)
store_energies(os.path.join(os.path.dirname(__file__), 'MO_%s_energies.txt' % molname), res['orbe'], resfb['orbe'])

def build_isosurfaces(molname, res, resfb):
"""
Builds isosurfaces.
:param molname: Name of the molecule
:type molname: string
:param res: Result of a Hartree-Fock calculation
:type res: dictionary
:param resfb: Result of a Foster-Boys localization
:type resfb: dictionary
"""
br = BlenderRender()
br.render_molecular_orbitals(res['mol'], res['cgfs'], res['orbc'], outpath,
prefix='MO_CAN_%s' % molname)

br.render_molecular_orbitals(resfb['mol'], res['cgfs'], resfb['orbc'], outpath,
prefix='MO_FB_%s' % molname)

def montage(molname, nrows=2):
"""
Produce a montage of the images
:param molname: Name of the molecule
:type molname: string
:param nrows: Number of rows in the montage
:type nrows: int
"""
out = subprocess.check_output(
['montage', 'MO_CAN_%s_????.png' % molname, '-tile', 'x%i' % nrows, '-geometry', '128x128+2+2', 'MO_%s_CAN.png' % molname],
cwd=os.path.dirname(__file__)
)

out = subprocess.check_output(
['montage', 'MO_FB_%s_????.png' % molname, '-tile', 'x%i' % nrows, '-geometry', '128x128+2+2', 'MO_%s_FB.png' % molname],
cwd=os.path.dirname(__file__)
)

def store_energies(filename, orbe, orbe_fb):
"""
Stores energies.
:param filename: Name of the molecule
:type filename: string
:param orbe: Array of the canonical molecular orbital energies
:type orbe: list or numpy array
:param orbe_fb: Array of the localized molecular orbital energies
:type orbe_fb: list or numpy array
"""
f = open(filename, 'w')
f.write('id localized canonical\n')
f.write('----------------------------\n')
for i in range(len(orbe)):
f.write("%02i %12.4f %12.4f\n" % (i+1, orbe[i], orbe_fb[i]))
f.close()

if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions examples/foster_boys.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def optimize_co():

res = GeometryOptimization().run(mol, 'sto3g')

print(res['data']['nuclei'])

return res['data']

def build_contourplot(cgfs, coeff, sz=2, npts=50, plane='xy'):
Expand Down
2 changes: 1 addition & 1 deletion meta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package:
name: "pyqint"
version: "0.13.1.0"
version: "0.14.0.0"

source:
path: .
Expand Down
2 changes: 1 addition & 1 deletion pyqint/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.13.1.0"
__version__ = "0.14.0.0"

3 changes: 3 additions & 0 deletions pyqint/blender/blender_render_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ def set_environment(settings):
Specify canvas size, remove default objects, reset positions of
camera and light, define film and set background
"""
print('Set render engine to: CYCLES')
bpy.context.scene.render.engine = 'CYCLES'
print('Set rendering device to GPU')
bpy.context.scene.cycles.device = 'GPU'
bpy.context.scene.render.resolution_x = settings['resolution']
bpy.context.scene.render.resolution_y = settings['resolution']
print('Setting resolution to: ', settings['resolution'])
bpy.context.scene.cycles.samples = 1024
bpy.context.scene.cycles.tile_size = 2048

Expand Down
Loading

0 comments on commit 47a996f

Please sign in to comment.