Precautions
Here we discuss some precautions when using mdapy.
[1]:
import mdapy as mp
import polars as pl
mp.init()
[Taichi] version 1.7.1, llvm 15.0.1, commit 0f143b2f, win, python 3.10.14
[Taichi] Starting on arch=x64
[2]:
mp.__version__
[2]:
'0.11.1'
[3]:
system = mp.System('../../../example/solidliquid.data')
[4]:
system
[4]:
Filename: ../../../example/solidliquid.data
Atom Number: 8192
Simulation Box:
[[52.01907282 0. 0. ]
[ 0. 52.01907282 0. ]
[ 0. 0. 52.01907282]
[-0.39698541 -0.39698541 -0.39698541]]
TimeStep: 0
Boundary: [1, 1, 1]
Particle Information:
shape: (8_192, 8)
┌──────┬──────┬───────────┬───────────┬───────────┬───────────┬──────────┬───────────┐
│ id ┆ type ┆ x ┆ y ┆ z ┆ vx ┆ vy ┆ vz │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞══════╪══════╪═══════════╪═══════════╪═══════════╪═══════════╪══════════╪═══════════╡
│ 7913 ┆ 1 ┆ 2.64108 ┆ 3.2152 ┆ 1.74197 ┆ 2.90096 ┆ 1.72875 ┆ -1.77975 │
│ 4098 ┆ 1 ┆ -0.091857 ┆ 0.4658 ┆ 0.936675 ┆ -0.166803 ┆ 0.959803 ┆ 3.70312 │
│ 4414 ┆ 1 ┆ 51.3076 ┆ 2.93742 ┆ 2.12484 ┆ 4.50605 ┆ -1.06924 ┆ -1.41931 │
│ 4 ┆ 1 ┆ 3.57773 ┆ -0.024408 ┆ 2.31072 ┆ -6.04709 ┆ 5.23451 ┆ 9.18538 │
│ 37 ┆ 1 ┆ 45.219 ┆ 19.5654 ┆ -0.241386 ┆ -5.20306 ┆ 3.79694 ┆ -2.15249 │
│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
│ 1936 ┆ 1 ┆ 32.3371 ┆ 29.817 ┆ 25.6971 ┆ 5.55254 ┆ -3.03374 ┆ 1.8186 │
│ 5887 ┆ 1 ┆ 30.7777 ┆ 5.709 ┆ 25.6752 ┆ -3.2635 ┆ -5.90409 ┆ 4.00862 │
│ 3968 ┆ 1 ┆ 22.2868 ┆ 25.2811 ┆ 51.5631 ┆ 6.79441 ┆ -7.16023 ┆ -0.666231 │
│ 5927 ┆ 1 ┆ 30.8122 ┆ 12.6453 ┆ 25.6768 ┆ 1.40187 ┆ 0.723993 ┆ 3.83902 │
│ 1888 ┆ 1 ┆ 13.1155 ┆ 28.3524 ┆ 25.6479 ┆ 5.00834 ┆ -2.91066 ┆ 5.65182 │
└──────┴──────┴───────────┴───────────┴───────────┴───────────┴──────────┴───────────┘
Check the position information
[5]:
system.pos[:5]
[5]:
array([[ 2.64108e+00, 3.21520e+00, 1.74197e+00],
[-9.18574e-02, 4.65800e-01, 9.36675e-01],
[ 5.13076e+01, 2.93742e+00, 2.12484e+00],
[ 3.57773e+00, -2.44078e-02, 2.31072e+00],
[ 4.52190e+01, 1.95654e+01, -2.41386e-01]])
Generally speaking, we do not need to change the position. So the pos information is inmutable for users. If you try to change it, it will raise an error. This is for data safety.
[6]:
system.pos[0, 0] = 1.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[6], line 1
----> 1 system.pos[0, 0] = 1.
ValueError: assignment destination is read-only
If you really need to change ths positions for system object, you can modify the system.data and update it mannually!!! Considering you want let atoms with z < 10 moves right 2 along x, you can do like below:
[7]:
system.update_data(
system.data.
with_columns(
pl.when(pl.col('z')<10).
then(pl.col('x')+2).
otherwise(pl.col('x')).
alias('x')),
update_pos=True)
# The first parameter is a new Dataframe, the second indicates mdapy will update the position information.
[8]:
system.data.head() # x[0] from 2.64108 to 4.64108
[8]:
shape: (5, 8)
| id | type | x | y | z | vx | vy | vz |
|---|---|---|---|---|---|---|---|
| i64 | i64 | f64 | f64 | f64 | f64 | f64 | f64 |
| 7913 | 1 | 4.64108 | 3.2152 | 1.74197 | 2.90096 | 1.72875 | -1.77975 |
| 4098 | 1 | 1.9081426 | 0.4658 | 0.936675 | -0.166803 | 0.959803 | 3.70312 |
| 4414 | 1 | 53.3076 | 2.93742 | 2.12484 | 4.50605 | -1.06924 | -1.41931 |
| 4 | 1 | 5.57773 | -0.024408 | 2.31072 | -6.04709 | 5.23451 | 9.18538 |
| 37 | 1 | 47.219 | 19.5654 | -0.241386 | -5.20306 | 3.79694 | -2.15249 |
[9]:
system.pos[:5] # updated correspondingly.
[9]:
array([[ 4.6410800e+00, 3.2152000e+00, 1.7419700e+00],
[ 1.9081426e+00, 4.6580000e-01, 9.3667500e-01],
[ 5.3307600e+01, 2.9374200e+00, 2.1248400e+00],
[ 5.5777300e+00, -2.4407800e-02, 2.3107200e+00],
[ 4.7219000e+01, 1.9565400e+01, -2.4138600e-01]])
velocity is all the same with position.
[10]:
system.vel[:5]
[10]:
array([[ 2.90096 , 1.72875 , -1.77975 ],
[-0.166803, 0.959803, 3.70312 ],
[ 4.50605 , -1.06924 , -1.41931 ],
[-6.04709 , 5.23451 , 9.18538 ],
[-5.20306 , 3.79694 , -2.15249 ]])
If you try to modify it directly, raise an error.
[11]:
system.vel[0, 0] = 3.5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[11], line 1
----> 1 system.vel[0, 0] = 3.5
ValueError: assignment destination is read-only
You can change the data to modify the velocities.
[12]:
system.data[0, 'vx'] = 3.5
[13]:
system.update_vel() # Update the velocity mannually.
system.data.head()
[14]:
system.data.head() # vx[0] from 2.90096 to 3.5
[14]:
shape: (5, 8)
| id | type | x | y | z | vx | vy | vz |
|---|---|---|---|---|---|---|---|
| i64 | i64 | f64 | f64 | f64 | f64 | f64 | f64 |
| 7913 | 1 | 4.64108 | 3.2152 | 1.74197 | 3.5 | 1.72875 | -1.77975 |
| 4098 | 1 | 1.9081426 | 0.4658 | 0.936675 | -0.166803 | 0.959803 | 3.70312 |
| 4414 | 1 | 53.3076 | 2.93742 | 2.12484 | 4.50605 | -1.06924 | -1.41931 |
| 4 | 1 | 5.57773 | -0.024408 | 2.31072 | -6.04709 | 5.23451 | 9.18538 |
| 37 | 1 | 47.219 | 19.5654 | -0.241386 | -5.20306 | 3.79694 | -2.15249 |
[15]:
system.vel[:5] # updated correspondingly.
[15]:
array([[ 3.5 , 1.72875 , -1.77975 ],
[-0.166803, 0.959803, 3.70312 ],
[ 4.50605 , -1.06924 , -1.41931 ],
[-6.04709 , 5.23451 , 9.18538 ],
[-5.20306 , 3.79694 , -2.15249 ]])