Ben
Ben

Reputation: 342

For loop to loop through files

I have the code below and it works like I want it to. The only thing I want to change to it is to add a for loop. Pay attention to this line please:

 node = import_file("../Angle1/1eV/cascade.dump",multiple_frames = True)

Notice the 1eV in there. I want to loop from 1 to 10eV. How can I insert a for loop to go from 1 to 100 eV like this so that I don't have to manually right it out which will take a while?

# Import OVITO modules.
from ovito.io import *
from ovito.modifiers import *

# Import NumPy module.
import numpy
import sys



node = import_file("../Angle1/1eV/cascade.dump",multiple_frames = True)
ovito.dataset.anim.current_frame = 1

mod = WignerSeitzAnalysisModifier(per_type_occupancies = True)
mod.reference.load("position_perfect_300.dump")
node.modifiers.append(mod)
node.compute()
node.modifiers.append(SelectExpressionModifier(expression = 'ParticleType==1 && Occupancy.1==0 && Occupancy.2==0'))
node.compute()

f=open("1.out",'w')
sys.stdout = f


print("Number of O vacant sites: %i" % numpy.count_nonzero(node.output.particle_properties['Selection']))

f.close()

Upvotes: 0

Views: 66

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

Simple: just make your loop start like this, then keep the same indentation incuding your final print statement (that I changed so it doesn't override sys.stdout, and I open the file before the loop so messages don't get overwritten):

with open("1.out",'w') as f:

    for i in range(1,11):
        node = import_file("../Angle1/{}eV/cascade.dump".format(i),multiple_frames = True)
        ovito.dataset.anim.current_frame = 1

        mod = WignerSeitzAnalysisModifier(per_type_occupancies = True)
        mod.reference.load("position_perfect_300.dump")
        node.modifiers.append(mod)
        node.compute()
        node.modifiers.append(SelectExpressionModifier(expression = 'ParticleType==1 && Occupancy.1==0 && Occupancy.2==0'))
        node.compute()

        f.write("Number of O vacant sites: %i\n" % numpy.count_nonzero(node.output.particle_properties['Selection']))

Upvotes: 2

Related Questions