Error while initializing particle types with unequal number

25 views
Skip to first unread message

Ramalingam Kailasham

unread,
Nov 20, 2023, 10:27:40 AM11/20/23
to hoomd-users
Dear Team

I am using the following script to initialize a system with 300 particles in a 2D box, and to assign particle types (A,B,C) to the individual particles. When I set each particle type to have an equal number of constituents (100 each), the code works fine. However, when I set an unequal number (say "A" has 100 particles, "B" has 90 and "C" has 110), the code throws up the following error: 

Traceback (most recent call last):
  File "/home/rkailash/learning_hoomd/box2d_create_diff_types.py", line 33, in <module>
    f.append(snapshot)
  File "/home/rkailash/anaconda3/envs/myenv/lib/python3.11/site-packages/gsd/hoomd.py", line 749, in append
    frame.validate()
  File "/home/rkailash/anaconda3/envs/myenv/lib/python3.11/site-packages/gsd/hoomd.py", line 460, in validate
    self.particles.validate()
  File "/home/rkailash/anaconda3/envs/myenv/lib/python3.11/site-packages/gsd/hoomd.py", line 221, in validate
    self.typeid = numpy.ascontiguousarray(self.typeid,
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.


Here is the code snippet: 

#### Code taken from tutorial : https://hoomd-blue.readthedocs.io/en/v3.11.0/tutorial/00-Introducing-HOOMD-blue/03-Initializing-the-System-State.html#

import itertools
import math
import hoomd
import numpy
import gsd.hoomd


N_particles = 300

spacing = 1.1
K = math.ceil(N_particles**(1 / 2))
L = 50

x = numpy.linspace(-L/2., L/2., K, endpoint=False)
position = list(itertools.product(x, repeat=2))

for i in range(len(position)):
    position[i]=(position[i][0],position[i][1],0.)

snapshot = gsd.hoomd.Snapshot()
snapshot.configuration.box = [L, L, 0, 0, 0, 0]
snapshot.particles.N = N_particles
snapshot.particles.position = position[0:N_particles]
#snapshot.particles.typeid = [[0] * 100, [1] * 100, [2] * 100] #this line works
snapshot.particles.typeid = [[0] * 100, [1] * 90, [2] * 110] # this line does not

snapshot.particles.types = ['A','B','C']

op_file_name='N%s_L%s_trial.gsd' %(N_particles,L)

with gsd.hoomd.open(name=op_file_name, mode='xb') as f:
    f.append(snapshot)

Can you please help?

Thank You

Avishek Kumar

unread,
Nov 20, 2023, 11:02:10 AM11/20/23
to hoomd-users
Hi,

You need to assign the typeid in a 1D array. Currently, you are assigning it to a 2D array, which is causing an error. I have revised your code in HOOMD v4.1.0, and it is functioning properly now.

import itertools
import math
import hoomd
import numpy
import gsd.hoomd

N_particles = 300

spacing = 1.1
K = math.ceil(N_particles**(1 / 2))
L = 50
x = numpy.linspace(-L/2., L/2., K, endpoint=False)
position = list(itertools.product(x, repeat=2))
for i in range(len(position)):
    position[i]=(position[i][0],position[i][1],0.)
snapshot = gsd.hoomd.Frame()

snapshot.configuration.box = [L, L, 0, 0, 0, 0]
snapshot.particles.N = N_particles
snapshot.particles.position = position[0:N_particles]
#snapshot.particles.typeid = [[0] * 100, [1] * 100, [2] * 100] #this line works
type_s = [0] * 100 + [1] * 90 + [2] * 110
print("types",type_s)
snapshot.particles.typeid = type_s # this line does not

snapshot.particles.types = ['A','B','C']

with gsd.hoomd.open(name='trial.gsd', mode='x') as f:
    f.append(snapshot)

Regards,
Avishek

Ramalingam Kailasham

unread,
Nov 20, 2023, 11:13:50 AM11/20/23
to hoomd...@googlegroups.com
Hi Avishek

Thank you very much for the updated code and providing an explanation for what was wrong with the original snippet. I highly appreciate it.

Warm Regards

Kailash

--
You received this message because you are subscribed to a topic in the Google Groups "hoomd-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hoomd-users/KTH402KBv90/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hoomd-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hoomd-users/23897b72-4c3e-4d30-b57c-52dab53a526an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages