Skip to content

Commit

Permalink
rework particle access
Browse files Browse the repository at this point in the history
  • Loading branch information
christophlohrmann committed Dec 7, 2021
1 parent a17c2d6 commit 6fe8480
Show file tree
Hide file tree
Showing 98 changed files with 986 additions and 909 deletions.
8 changes: 4 additions & 4 deletions doc/tutorials/charged_system/charged_system.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
" ROD_CHARGE_DENS, N_rod_beads, ROD_TYPE)\n",
"\n",
"# check that the particle setup was done correctly\n",
"assert abs(sum(system.part[:].q)) < 1e-10\n",
"assert abs(sum(system.part.all().q)) < 1e-10\n",
"assert np.all(system.part.select(type=ROD_TYPE).fix)"
]
},
Expand Down Expand Up @@ -303,15 +303,15 @@
"\n",
" # Initialize integrator to obtain initial forces\n",
" system.integrator.run(0)\n",
" maxforce = np.max(np.linalg.norm(system.part[:].f, axis=1))\n",
" maxforce = np.max(np.linalg.norm(system.part.all().f, axis=1))\n",
" energy = system.analysis.energy()['total']\n",
"\n",
" i = 0\n",
" while i < sd_params['max_steps'] // sd_params['emstep']:\n",
" prev_maxforce = maxforce\n",
" prev_energy = energy\n",
" system.integrator.run(sd_params['emstep'])\n",
" maxforce = np.max(np.linalg.norm(system.part[:].f, axis=1))\n",
" maxforce = np.max(np.linalg.norm(system.part.all().f, axis=1))\n",
" relforce = np.abs((maxforce - prev_maxforce) / prev_maxforce)\n",
" energy = system.analysis.energy()['total']\n",
" relener = np.abs((energy - prev_energy) / prev_energy)\n",
Expand Down Expand Up @@ -944,7 +944,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions doc/tutorials/ferrofluid/ferrofluid_part1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@
" system.integrator.run(100)\n",
"\n",
" # Save current system state as a plot\n",
" x_data, y_data = system.part[:].pos_folded[:, 0], system.part[:].pos_folded[:, 1]\n",
" x_data, y_data = system.part.all().pos_folded[:, 0], system.part.all().pos_folded[:, 1]\n",
" ax.figure.canvas.draw()\n",
" part.set_data(x_data, y_data)\n",
" print(\"progress: {:3.0f}%\".format((i + 1) * 100. / LOOPS), end=\"\\r\")\n",
Expand Down Expand Up @@ -905,7 +905,7 @@
"plt.ylim(0, BOX_SIZE)\n",
"plt.xlabel('x-position', fontsize=20)\n",
"plt.ylabel('y-position', fontsize=20)\n",
"plt.plot(system.part[:].pos_folded[:, 0], system.part[:].pos_folded[:, 1], 'o')\n",
"plt.plot(system.part.all().pos_folded[:, 0], system.part.all().pos_folded[:, 1], 'o')\n",
"plt.show()"
]
},
Expand Down Expand Up @@ -1026,7 +1026,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down
14 changes: 7 additions & 7 deletions doc/tutorials/ferrofluid/ferrofluid_part2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"pos = box_size * np.hstack((np.random.random((N_PART, 2)), np.zeros((N_PART, 1))))\n",
"\n",
"# Add particles\n",
"system.part.add(pos=pos, rotation=N_PART * [(1, 1, 1)], dip=dip, fix=N_PART * [(0, 0, 1)])\n",
"particles = system.part.add(pos=pos, rotation=N_PART * [(True, True, True)], dip=dip, fix=N_PART * [(False, False, True)])\n",
"\n",
"# Remove overlap between particles by means of the steepest descent method\n",
"system.integrator.set_steepest_descent(\n",
Expand Down Expand Up @@ -289,7 +289,7 @@
"plt.ylim(0, box_size)\n",
"plt.xlabel('x-position', fontsize=20)\n",
"plt.ylabel('y-position', fontsize=20)\n",
"plt.plot(system.part[:].pos_folded[:, 0], system.part[:].pos_folded[:, 1], 'o')\n",
"plt.plot(particles.pos_folded[:, 0], particles.pos_folded[:, 1], 'o')\n",
"plt.show()"
]
},
Expand Down Expand Up @@ -351,7 +351,7 @@
" system.integrator.run(50)\n",
"\n",
" # Save current system state as a plot\n",
" xdata, ydata = system.part[:].pos_folded[:, 0], system.part[:].pos_folded[:, 1]\n",
" xdata, ydata = particles.pos_folded[:, 0], particles.pos_folded[:, 1]\n",
" ax.figure.canvas.draw()\n",
" part.set_data(xdata, ydata)\n",
" print(\"progress: {:3.0f}%\".format(i + 1), end=\"\\r\")\n",
Expand Down Expand Up @@ -498,7 +498,7 @@
"outputs": [],
"source": [
"# remove all particles\n",
"system.part[:].remove()\n",
"system.part.clear()\n",
"system.thermostat.turn_off()\n",
"\n",
"# Random dipole moments\n",
Expand All @@ -514,7 +514,7 @@
"pos = box_size * np.hstack((np.random.random((N_PART, 2)), np.zeros((N_PART, 1))))\n",
"\n",
"# Add particles\n",
"system.part.add(pos=pos, rotation=N_PART * [(1, 1, 1)], dip=dip, fix=N_PART * [(0, 0, 1)])\n",
"particles = system.part.add(pos=pos, rotation=N_PART * [(True, True, True)], dip=dip, fix=N_PART * [(False, False, True)])\n",
"\n",
"# Remove overlap between particles by means of the steepest descent method\n",
"system.integrator.set_steepest_descent(f_max=0, gamma=0.1, max_displacement=0.05)\n",
Expand Down Expand Up @@ -570,7 +570,7 @@
"source": [
"```python\n",
"import espressomd.observables\n",
"dipm_tot = espressomd.observables.MagneticDipoleMoment(ids=system.part[:].id)\n",
"dipm_tot = espressomd.observables.MagneticDipoleMoment(ids=particles.id)\n",
"```"
]
},
Expand Down Expand Up @@ -1041,7 +1041,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions doc/tutorials/ferrofluid/ferrofluid_part3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"pos = box_size * np.random.random((N, 3))\n",
"\n",
"# Add particles\n",
"system.part.add(pos=pos, rotation=N * [(1, 1, 1)], dip=dip)\n",
"particles = system.part.add(pos=pos, rotation=N * [(True, True, True)], dip=dip)\n",
"\n",
"# Remove overlap between particles by means of the steepest descent method\n",
"system.integrator.set_steepest_descent(\n",
Expand Down Expand Up @@ -275,7 +275,7 @@
"outputs": [],
"source": [
"import espressomd.observables\n",
"dipm_tot_calc = espressomd.observables.MagneticDipoleMoment(ids=system.part[:].id)"
"dipm_tot_calc = espressomd.observables.MagneticDipoleMoment(ids=particles.id)"
]
},
{
Expand Down Expand Up @@ -670,7 +670,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down
39 changes: 23 additions & 16 deletions doc/tutorials/lennard_jones/lennard_jones.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@
"\n",
"### Placing and accessing particles\n",
"\n",
"Particles in the simulation can be added and accessed via the <tt>part</tt> property of the System class. Individual particles are referred to by an integer id, e.g., <tt>system.part[0]</tt>. If <tt>id</tt> is unspecified, an unused particle id is automatically assigned. It is also possible to use common python iterators and slicing operations to add or access several particles at once.\n",
"Particles in the simulation can be added and accessed via the <tt>part</tt> property of the System class. Individual particles should be referred to by the particle handle returned upon creation. You can also retrieve them by an integer id, e.g. <tt>system.part.by_id(0)</tt>. If <tt>id</tt> is unspecified when creating a particle, an unused particle id is automatically assigned. It is also possible to use common python iterators and slicing operations to add or access several particles at once.\n",
"\n",
"Particles can be grouped into several types, so that, e.g., a binary fluid can be simulated. Particle types are identified by integer ids, which are set via the particles' <tt>type</tt> attribute. If it is not specified, zero is implied."
]
Expand All @@ -325,11 +325,11 @@
"source": [
"<!-- **Exercise:** -->\n",
"\n",
"* Create <tt>N_PART</tt> particles at random positions.\n",
"* Create <tt>N_PART</tt> particles at random positions, store their handles in a variable called ``particles``.\n",
"\n",
" Use [system.part.add()](https://espressomd.github.io/doc/espressomd.html#espressomd.particle_data.ParticleList).\n",
" \n",
" Either write a loop or use an (<tt>N_PART</tt> x 3) array for positions.\n",
" Use an (<tt>N_PART</tt> x 3) array for positions.\n",
" Use <tt>np.random.random()</tt> to generate random numbers."
]
},
Expand All @@ -340,7 +340,7 @@
},
"source": [
"```python\n",
"system.part.add(type=[0] * N_PART, pos=np.random.random((N_PART, 3)) * system.box_l)\n",
"particles = system.part.add(type=[0] * N_PART, pos=np.random.random((N_PART, 3)) * system.box_l)\n",
"```"
]
},
Expand Down Expand Up @@ -375,16 +375,23 @@
"outputs": [],
"source": [
"# Access position of a single particle\n",
"print(\"position of particle with id 0:\", system.part[0].pos)\n",
"print(\"position of particle with id 0:\", system.part.by_id(0).pos)\n",
"\n",
"# Iterate over the first five particles for the purpose of demonstration.\n",
"# For accessing all particles, use a slice: system.part[:]\n",
"for i in range(5):\n",
" print(\"id\", i, \"position:\", system.part[i].pos)\n",
" print(\"id\", i, \"velocity:\", system.part[i].v)\n",
"first_five = system.part.by_ids(range(5))\n",
"for p in first_five:\n",
" print(\"id\", p.id, \"position:\", p.pos)\n",
" print(\"id\", p.id, \"velocity:\", p.v)\n",
"\n",
"# Obtain all particle positions\n",
"cur_pos = system.part[:].pos"
"# Obtain particle positions for the particles created until now\n",
"cur_pos = particles.pos"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also get all particles using ``system.part.all()``, but ``particles`` already contains all particles that are in the simulation so far."
]
},
{
Expand All @@ -400,7 +407,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(system.part[0])"
"print(system.part.by_id(0))"
]
},
{
Expand Down Expand Up @@ -540,12 +547,12 @@
"\n",
"# Initialize integrator to obtain initial forces\n",
"system.integrator.run(0)\n",
"old_force = np.max(np.linalg.norm(system.part[:].f, axis=1))\n",
"old_force = np.max(np.linalg.norm(system.part.all().f, axis=1))\n",
"\n",
"\n",
"while system.time / system.time_step < MAX_STEPS:\n",
" system.integrator.run(EM_STEP)\n",
" force = np.max(np.linalg.norm(system.part[:].f, axis=1))\n",
" force = np.max(np.linalg.norm(system.part.all().f, axis=1))\n",
" rel_force = np.abs((force - old_force) / old_force)\n",
" print(f'rel. force change: {rel_force:.2e}')\n",
" if rel_force < F_TOL:\n",
Expand Down Expand Up @@ -983,7 +990,7 @@
},
"source": [
"```python\n",
"rdf_obs = espressomd.observables.RDF(ids1=system.part[:].id, min_r=R_MIN, max_r=R_MAX, n_r_bins=N_BINS)\n",
"rdf_obs = espressomd.observables.RDF(ids1=system.part.all().id, min_r=R_MIN, max_r=R_MAX, n_r_bins=N_BINS)\n",
"rdf_acc = espressomd.accumulators.MeanVarianceCalculator(obs=rdf_obs, delta_N=steps_per_subsample)\n",
"system.auto_update_accumulators.add(rdf_acc)\n",
"```"
Expand Down Expand Up @@ -1211,7 +1218,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
"for _ in range(100):\n",
" system.integrator.run(50)\n",
" constrain_surface_particles()\n",
" force_max = np.max(np.linalg.norm(system.part[:].f, axis=1))\n",
" force_max = np.max(np.linalg.norm(system.part.all().f, axis=1))\n",
" logging.info(f\"maximal force: {force_max:.1f}\")\n",
" if force_max < 10.:\n",
" break\n",
Expand Down Expand Up @@ -385,7 +385,7 @@
"source": [
"```python\n",
"# Calculate the center of mass position (com) and the moment of inertia (momI) of the colloid\n",
"com = np.average(surface_parts.pos, 0) # system.part[:].pos returns an n-by-3 array\n",
"com = np.average(surface_parts.pos, 0) # surface_parts.pos returns an n-by-3 array\n",
"momI = 0\n",
"for p in surface_parts:\n",
" momI += np.power(np.linalg.norm(com - p.pos), 2)\n",
Expand Down Expand Up @@ -474,7 +474,7 @@
"outputs": [],
"source": [
"# Check charge neutrality\n",
"assert np.abs(np.sum(system.part[:].q)) < 1E-10"
"assert np.abs(np.sum(system.part.all().q)) < 1E-10"
]
},
{
Expand Down Expand Up @@ -620,7 +620,7 @@
"metadata": {},
"outputs": [],
"source": [
"system.part[:].v = (0, 0, 0)"
"system.part.all().v = (0, 0, 0)"
]
},
{
Expand Down
12 changes: 6 additions & 6 deletions samples/chamber_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
new_part.remove()
n -= 1

p_bubbles = np.where(system.part[:].type == bubble_type)[0]
p_bubbles = system.part.select(type=bubble_type)

# TEMP CHANGE PARTICLES
bpos = [np.random.random() * (pore_xl - snake_head_sigma * 4) +
Expand All @@ -291,7 +291,7 @@
max_displacement=0.01)
system.integrator.run(10000)
system.integrator.set_vv()
p_startpos = system.part[:].pos
p_startpos = system.part.all().pos

# THERMOSTAT
system.thermostat.set_langevin(kT=temperature, gamma=gamma, seed=42)
Expand Down Expand Up @@ -344,7 +344,7 @@ def set_particle_force():


def restart():
system.part[:].pos = p_startpos
system.part.all().pos = p_startpos
system.galilei.kill_particle_motion()
system.galilei.kill_particle_forces()

Expand All @@ -358,7 +358,7 @@ def explode():
if not exploding:
exploding = True
expl_time = time.time()
for p in system.part[p_bubbles]:
for p in p_bubbles:
dv = p.pos - p_head.pos
lv = np.linalg.norm(dv)
if lv < expl_range:
Expand Down Expand Up @@ -478,14 +478,14 @@ def T_to_g(temp):
temp_r -= dtemp
if temp_r < 0:
temp_r = 0.0
for p in system.part[p_bubbles]:
for p in p_bubbles:
if p.pos[0] > pore_xr:
p.v = [0, 0, 0]
else:
temp_l -= dtemp
if temp_l < 0:
temp_l = 0.0
for p in system.part[p_bubbles]:
for p in p_bubbles:
if p.pos[0] < pore_xl:
p.v = [0, 0, 0]

Expand Down
2 changes: 1 addition & 1 deletion samples/dancing.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
gravity = espressomd.constraints.Gravity(g=[0, -1, 0])
system.constraints.add(gravity)

obs = espressomd.observables.ParticlePositions(ids=system.part[:].id)
obs = espressomd.observables.ParticlePositions(ids=system.part.all().id)
acc = espressomd.accumulators.TimeSeries(obs=obs, delta_N=1)
system.auto_update_accumulators.add(acc)
acc.update()
Expand Down
11 changes: 6 additions & 5 deletions samples/electrophoresis.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@
q=np.resize((1, -1), N_IONS),
type=np.resize((1, 2), N_IONS))

logging.info(f"particle types: {system.part[:].type}\n")
logging.info(f"total charge: {np.sum(system.part[:].q)}")
all_partcls = system.part.all()
logging.info(f"particle types: {all_partcls.type}\n")
logging.info(f"total charge: {np.sum(all_partcls.q)}")

# warm-up integration
###############################################################
Expand All @@ -138,8 +139,8 @@
# apply external force (external electric field)
#############################################################
n_part = len(system.part)
system.part[:].ext_force = np.dstack(
(system.part[:].q * np.ones(n_part) * E_FIELD, np.zeros(n_part), np.zeros(n_part)))[0]
all_partcls.ext_force = np.dstack(
(all_partcls.q * np.ones(n_part) * E_FIELD, np.zeros(n_part), np.zeros(n_part)))[0]

# equilibration
#############################################################
Expand Down Expand Up @@ -169,7 +170,7 @@
if i % 100 == 0:
logging.info(f"\rsampling: {i:4d}")
system.integrator.run(N_INT_STEPS)
pos[i] = system.part[:N_MONOMERS].pos
pos[i] = system.part.by_ids(range(N_MONOMERS)).pos

logging.info("\nsampling finished!\n")

Expand Down
Loading

0 comments on commit 6fe8480

Please sign in to comment.