Forum Navigation
You need to log in to create posts and topics.

Changing atom types

Hi,

I am wondering how can I simply change an atom type after loading a file in python ovito? I am able to change the type id by

data.particles_.particle_types_[...][i] = 3

However, I am not sure how to change the label (for example to H instead of default Type3). Thanks

Hi,

each type has a unique id, a human-readable name and other attributes like color and radius which you can control. This is explained here:

https://www.ovito.org/docs/current/python/modules/ovito_data.php#ovito.data.ParticleType

and in the section "Typed properties" of this manual entry:

https://www.ovito.org/docs/current/python/modules/ovito_data.php#ovito.data.Particles

If you want to add a type name to particle type 3 you can do the following:

types = pipeline.source.data.particles.particle_types
types.type_by_id(3).name = "H"

-Constanze

You may also have a look at the documentation of the FileSource class. It contains another example of how to set the particle type names after importing a simulation file.

Thanks for your response. Actually, I have attempted to do this already but the problem is that after loading a data object and modifying one of the types through let's say the following command, "particle_types" object is not updated. In other words, if we have one type of atom initially and try to modify one of the species, "data.particles_.particle_types_.types" still will show one type atom instead of two. So, I wouldn't be able to assign name or id to the newly specified particle.

data.particles_["Particle Type"][...][i]= 2

Thanks

I see, so after assigning the new particle type property to the particle, you should also add a new ParticleType() object to the types list of the Particle Type property.

from ovito.data import *

def modify(frame, data):
   ptypes =  data.particles_.particle_types_
   #Give particle 100 a new particle type 3
   ptypes[99] = 3  
   #Now add the new type id and name to the types list of the "Particle Type" property
   ptypes.types.append(ParticleType( id = 3, name = "H"))
   
   for type in ptypes.types:
       print (type.id, type.name)

-Constanze

Hi,

I have a follow up question. Could one change the atom types within the gui Ovito? I tried using Python script, however, changing "Particle Type" is not allowed.

Thanks,

Hi,

please make sure you don't forget to use the underscore notation, to make the property array modifiable, see also
https://www.ovito.org/docs/current/python/introduction/data_manipulation.php?#modifying-properties.

#Option 1
data.particles_.particle_types_
#Option 2
data.particles_.["Particle Type_"]

Thanks very much, Constanze. I actually used above commands within python interface before, however, I am trying to find a way to do this in gui Ovito. I assume using Python script modifier, one should be able to do atom type modification but I believe this modification should be operative before passing the ".compute()" command (in other words, directly applied to the pipeline rather than "data" object). Otherwise I'll get the following error:

numpy.asanyarray(self)[idx] = value # Note to users: Write access to a property requires a 'with' Python statement.
ValueError: assignment destination is read-only

Below is the body of my Python script "modifier" within gui Ovito:

from ovito.data import *

def modify(frame, data):
    
    if data.particles != None:
        pt = data.particles_.particle_types_
        pt.types.append(ParticleType(id=2, name='Cu', color=(0.0, 1.0, 0.0)))
        data.particles_["Particle Type_"][1]=2
        for type in pt.types:
            print(type.id)

Thanks,

-Shahriar

The error message suggests that you are using an older version of OVITO, where you needed a "with" python statement to be able to modify a particle property. Note that calling compute() is not necessary when using a python script modifier in the GUI, since the pipeline is automatically evaluated for you when you press the execute button.

Both scripts posted in this thread can be used in the GUI. Updating your OVITO version to the latest version of OVITO Pro will fix the error message.

Do you only want to change the particle type of the second particle (particle index 1), like you did in the script you posted here? If that's the case than you could use a Compute Property Modifier in the GUI with the following settings:

Output property: Particle Type
Expression: ParticleIndex == 1 ? 2 : ParticleType

This will overwrite the particle type property of the second particle in the list (i.e. with index 1) to type 2 and leave it unchanged for all other particles.