Author Topic: Batch processing of xsf files to render isosurfaces  (Read 63 times)

chrisewolf

  • Newbie
  • *
  • Posts: 3
Batch processing of xsf files to render isosurfaces
« on: August 06, 2019, 11:54:07 AM »
Dear all,

I have just started using OVITO today and I am already in love with the render quality - isosurfaces really look magnificent. Since I am working mostly remotely I would like to do some very basic batch processing

read xsf file
set atom radii H 0.3
set atom radii C 0.5
set atom X radius 1.0
set atom H color blue
set atom C color grey
set atom X color red
set bonds with cutoff 2
create isosurface red at -0.001 with 50% transparency
create isosurface blue at 0.001 with 50% transparency
turn off the simulation cell
render each viewport to a png file (FILENAME_X.png, FILENAME_Y.png...)

do this for ~60 files

what is the easiest way of doing batch processing as in the example above? Looking at the script examples I think this should be very easy but I am a bit overwhelmed by the number of classes and functions here.

I know that as absolute skeleton code for reading xsf files and writing png files the following suffices

Code: [Select]
# Import OVITO modules.
from ovito.io import *
from ovito.modifiers import *
import math
from ovito.vis import Viewport

# Import NumPy module.
import numpy

pipeline = import_file("spin_dn_K002_B031orb_dn.xsf")

data = pipeline.compute()
pipeline.add_to_scene()
data.cell.vis.enabled = False
vp = Viewport()
vp.type = Viewport.Type.Perspective
vp.camera_pos = (-100, -150, 150)
vp.camera_dir = (2, 3, -3)
vp.fov = math.radians(60.0)
vp.zoom_all()
vp.render_image(size=(800,600), filename="figure.png", background=(1,1,1), frame=0)

I also saw that I can iterate through atoms in my pipeline by

Code: [Select]
tprop = data.particles['Particle Type']
for i,t in enumerate(tprop):
  print('Atom', i, 'is of type', tprop.type_by_id(t).name)

what is not clear to me is how to apply the modifiers which I would usually do by clicking in this manner to atoms if a specific type..

Sorry, a very basic question, I do know that.

Thanks in advance for any help!

Best,
Chris

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 637
Re: Batch processing of xsf files to render isosurfaces
« Reply #1 on: August 07, 2019, 06:01:43 PM »
Hi Chris,

Please excuse the late response. We have been busy these days with launching our new website.

I have compiled a Python script for you below, which is supposed to demonstrate how the various tasks you listed can be accomplished.
I think most of the steps are self-explanatory. But of course you can ask further questions if you have any.

-Alex

Code: [Select]
# Import OVITO modules.
from ovito.io import *
from ovito.modifiers import *
from ovito.vis import Viewport
import math

# Import NumPy module.
import numpy

pipeline = import_file("CO_homo.xsf")
pipeline.add_to_scene()

# Get access to the loaded data:
source_data = pipeline.source.data

# Hide simulation box:
source_data.cell.vis.enabled = False

# Configure visual representation of oxygen atoms:
type_O = source_data.particles.particle_types.type_by_name('O')
type_O.radius = 0.8
type_O.color = (0.2, 0.2, 1.0)

# Add modifier that creates bonds between atoms.
modifier = CreateBondsModifier(cutoff = 2.0)
pipeline.modifiers.append(modifier)
# Configure the BondsVis element:
modifier.vis.width = 0.3

# Add modifier that creates an isosurface.
modifier = CreateIsosurfaceModifier(isolevel = 0.016, property = 'G98CUBE')
pipeline.modifiers.append(modifier)
# Configure the SurfaceMeshVis element:
modifier.vis.surface_transparency = 0.5

# Render an image:
vp = Viewport()
vp.type = Viewport.Type.Perspective
vp.camera_pos = (-100, -150, 150)
vp.camera_dir = (2, 3, -3)
vp.fov = math.radians(60.0)
vp.zoom_all()
vp.render_image(size=(800,600), filename="figure.png", background=(1,1,1), frame=0)

chrisewolf

  • Newbie
  • *
  • Posts: 3
Re: Batch processing of xsf files to render isosurfaces
« Reply #2 on: August 09, 2019, 12:24:13 PM »
Dear Alex,

there is really no need to apologize for giving me something free half a day late - I really appreciate your effort very much and it works very well.

I found a few points of curiosity and I wonder if this is by design or by "user mistake":

- the isolevel seems different between the GUI input and the script. If I set the same value (+/-0.0001)  I get two fairly different outputs compared to

Code: [Select]
        modifier = CreateIsosurfaceModifier(isolevel = 0.0001,  property = 'UNKNOWN')
        pipeline.modifiers.append(modifier)
        # Configure the SurfaceMeshVis element:
        modifier.vis.surface_transparency = 0.5
        modifier.vis.surface_color = (0.0, 0.0, 1.0)

is there a way to first read the "isolevel histogram" shown on the right in the GUI version and then "set isovalue to 10% o fthe maximum value"?

note that in the GUI "Operate on" is Voxel grid: imported
and Field quantity:  UNKNOWN
(this xsf file was generated by Quantum espresso)


Thanks again for providing this amazing tool for free and your support online!

Best,
Chris


Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 637
Re: Batch processing of xsf files to render isosurfaces
« Reply #3 on: August 09, 2019, 01:46:25 PM »
I don't see a reason why the isosurfaces should come out differently in the GUI and the scripting interface. At last not if you use the exact same isolevel value. Two slightly different values, like +0.0001 and -0.0001, can make a big difference though if the distribution of field values is very narrow. In your case the histogram shows that the distribution is very narrow indeed. The vast majority of values is very close to zero. Thus, the generated isosurface will be very sensitive to small changes in the isolevel parameter.

You can access the field values loaded from the XSF file as follows:

Code: [Select]
source_data.grids['imported']['UNKNOWN'][...]

This expression gives you a Numpy array containing the values of all grid cells. Use it to analyze the value distribution or compute the isolevel dynamically in your script.

chrisewolf

  • Newbie
  • *
  • Posts: 3
Re: Batch processing of xsf files to render isosurfaces
« Reply #4 on: August 09, 2019, 01:53:44 PM »
Sorry, I should have clarified that I render it with plus and minus 0.0001 separately (colored red and blue, respectively);

I will see what I can get using the method you described!

Thanks a lot!
Chris

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 637
Re: Batch processing of xsf files to render isosurfaces
« Reply #5 on: August 09, 2019, 02:03:27 PM »
Ah ok, then this was a misunderstanding.

You've attached two pictures to your previous post. Note that they are identical. I am wondering if your intention was to show the different outcomes from GUI and script?