Author Topic: constructing meshes for different molecules via for loop  (Read 155 times)

skyus123

  • Newbie
  • *
  • Posts: 3
constructing meshes for different molecules via for loop
« on: February 25, 2019, 11:10:34 PM »
Dear OVITO Community,

I wrote a python script which is able to make meshes for an inputted molecule type. However, when I put this code in a for loop (i want to loop through lots of molecules) it stops working. The change between the two scripts is just a single line so I was a little puzzled as to why adding in the for loop is an issue. I have attached the data file to this post.

Version which works (no for loop)
Code: [Select]
from ovito.io import import_file
from ovito.io import export_file
from ovito.modifiers import ConstructSurfaceModifier
from ovito.modifiers import SelectExpressionModifier

x = 2

node = import_file("dump.mycoords.5000")
modifier1 = SelectExpressionModifier(expression = 'MoleculeIdentifier=={x}'.format(x=x))
node.modifiers.append(modifier1)
mod = ConstructSurfaceModifier(radius = 3.5, smoothing_level = 10, only_selected = True)
node.modifiers.append(mod)
node.compute()
filenameW = "New" + str(x) + ".txt"
export_file(node, filenameW, "txt", multiple_frames = False, columns = ["Frame", "ConstructSurfaceMesh.solid_volume", "ConstructSurfaceMesh.surface_area"])
print("particle %i completed" % (x))

Version which does not work (with for loop)

Code: [Select]
from ovito.io import import_file
from ovito.io import export_file
from ovito.modifiers import ConstructSurfaceModifier
from ovito.modifiers import SelectExpressionModifier

for x in range(1, 107):
node = import_file("dump.mycoords.5000")
modifier1 = SelectExpressionModifier(expression = 'MoleculeIdentifier=={x}'.format(x=x))
node.modifiers.append(modifier1)
mod = ConstructSurfaceModifier(radius = 3.5, smoothing_level = 10, only_selected = True)
node.modifiers.append(mod)
node.compute()
filenameW = "New" + str(x) + ".txt"
export_file(node, filenameW, "txt", multiple_frames = False, columns = ["Frame", "ConstructSurfaceMesh.solid_volume", "ConstructSurfaceMesh.surface_area"])
print("particle %i completed" % (x))

I execute this code in windows 10 with command: ovitos < scriptName.py
I have tried both stable and developmental versions and the issue is the same in both cases.

Sincerely,

Nick

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 595
Re: constructing meshes for different molecules via for loop
« Reply #1 on: February 26, 2019, 02:44:34 PM »
Hi Nick,

You wrote that it "stops working". Could you please elaborate on what happens. Do you get an error message? Do you get wrong or no results?

Just from looking at the scripts I don't see an obvious reason why the second version shouldn't work. However, since all loop iterations operate on the same input data file, I suggest you rearrange the script a bit in order to avoid loading the same input file over and over again. The following version will re-use the existing pipeline, just reconfiguring one of the modifiers during each iteration:

Code: [Select]
node = import_file("dump.mycoords.5000")
modifier1 = SelectExpressionModifier()
node.modifiers.append(modifier1)
mod = ConstructSurfaceModifier(radius = 3.5, smoothing_level = 10, only_selected = True)
node.modifiers.append(mod)
for x in range(1, 107):
modifier1.expression = 'MoleculeIdentifier=={x}'.format(x=x)
filenameW = "New" + str(x) + ".txt"
export_file(node, filenameW, "txt", multiple_frames = False, columns = ["Frame", "ConstructSurfaceMesh.solid_volume", "ConstructSurfaceMesh.surface_area"])
print("particle %i completed" % (x))

-Alex
« Last Edit: February 26, 2019, 02:46:33 PM by Alexander Stukowski »

skyus123

  • Newbie
  • *
  • Posts: 3
Re: constructing meshes for different molecules via for loop
« Reply #2 on: February 26, 2019, 06:32:13 PM »
Hi Alex,

Thank you for the quick reply. To clarify, when I say "stops working" I mean that there is no output produced (file not created).

When I run the script without the for loop I get:

J:\Ovito>ovitos < analysis1.py
This is OVITO's interactive Python interpreter. Use quit() or Ctrl-Z to exit.
>>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> DataCollection()
>>> >>> >>> particle 2 completed
>>>
now exiting InteractiveConsole...

In this case you see the call to the DataCollection function and the output of the print statement I included. A file is also produced in the directory.

The output file is correct, the contents are shown below:

# "Frame" "ConstructSurfaceMesh.solid_volume" "ConstructSurfaceMesh.surface_area"
0 2264.9599072012784 991.6877454209083

When I run the script with the for loop I get:

J:\Ovito>ovitos < analysis.py
This is OVITO's interactive Python interpreter. Use quit() or Ctrl-Z to exit.
>>> >>> >>> >>> >>> >>> >>> >>> ... ... ... ... ... ... ... ... ... ...
now exiting InteractiveConsole...

Here I don't see the call to the DataCollection function. There is no print statement, and no file is produced in the directory.

Sincerely,

Nick

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 595
Re: constructing meshes for different molecules via for loop
« Reply #3 on: February 26, 2019, 06:39:47 PM »
I didn't pay much attention to this detail, but I think you are getting this error due to the way you are running the script from the command line. You do

   ovitos < scriptName.py

but instead you should just do

   ovitos scriptName.py

Piping the script file into the interpreter using the "<" operator seems to work with your first script (a fact that is very surprising to me) but not with the second script containing the loop, probably because the indented loop code block gets not executed correctly.
« Last Edit: February 26, 2019, 06:41:37 PM by Alexander Stukowski »

skyus123

  • Newbie
  • *
  • Posts: 3
Re: constructing meshes for different molecules via for loop
« Reply #4 on: February 26, 2019, 07:29:32 PM »
Hi Alex,

Yes, you are correct, that fixes the problem. Thank you very much for your help!

Sincerely,

Nick