I am trying to evaluate a numerical integration for my research project. But I couldn't figure out the error that I faced. When I tried the previous code it worked and the related parts of the codes were same.
I could understand that x
and xAcc
don't have the same dimension, but I thought that I corrected it with xPositions[i, :] = x
line.
import numpy as np
np.seterr(invalid="ignore")
m = 1
x = np.array([1, 5, 9])
y = np.array([16, 20, 24])
def GetLJForce(r, epsilon, sigma):
return 48 * epsilon * np.power(sigma, 12) / np.power(r, 13) - 24 * epsilon * np.power(sigma, 6) / np.power(r, 7)
def GetAcc(xPositions, yPositions):
global xAcc
global yAcc
xAcc = np.zeros((xPositions.size, xPositions.size), dtype=object)
yAcc = np.zeros((xPositions.size, xPositions.size), dtype=object)
for i in range(0, xPositions.shape[0]-1):
for j in range(i+1, xPositions.shape[0]-1):
r_x = xPositions[j] - xPositions[i]
r_y = yPositions[j] - yPositions[i]
rmag = np.sqrt(r_x*r_x + r_y*r_y)
if(rmag[0]==0 or rmag[1]==0 or rmag[2]==0):
rmag += 1
force_scalar = GetLJForce(rmag, 0.84, 2.56)
force_x = force_scalar * r_x / rmag
force_y = force_scalar * r_y / rmag
xAcc[i,j] = force_x / m
xAcc[j,i] = - force_x / m
yAcc[i,j] = force_y / m
yAcc[j,i] = - force_y / m
else:
force_scalar = GetLJForce(rmag, 0.84, 2.56)
force_x = force_scalar * r_x / rmag
force_y = force_scalar * r_y / rmag
xAcc[i,j] = force_x / m
xAcc[j,i] = - force_x / m
yAcc[i,j] = force_y / m
yAcc[j,i] = - force_y / m
return np.sum(xAcc, axis=0), np.sum(yAcc, axis=0)
def UpdatexPos(x, v_x, a_x, dt):
return x + v_x*dt + 0.5*a_x*dt*dt
def UpdateyPos(y, v_y, a_y, dt):
return y + v_y*dt + 0.5*a_y*dt*dt
def UpdatexVel(v_x, a_x, a1_x, dt):
return v_x + 0.5*(a_x + a1_x)*dt
def UpdateyVel(v_y, a_y, a1_y, dt):
return v_y + 0.5*(a_y + a1_y)*dt
def RunMD(dt, number_of_steps, x, y):
xPositions = np.zeros((number_of_steps, 3))
yPositions = np.zeros((number_of_steps, 3))
v_x = 0
v_y = 0
a_x = GetAcc(xPositions, yPositions)[0]
a_y = GetAcc(xPositions, yPositions)[1]
for i in range(number_of_steps):
x = UpdatexPos(x, v_x, a_x, dt)
y = UpdateyPos(y, v_y, a_y, dt)
a1_x = GetAcc(xPositions, yPositions)[0]
a1_y = GetAcc(xPositions, yPositions)[1]
v_x = UpdatexVel(v_x, a_x, a1_x, dt)
v_y = UpdateyVel(v_y, a_y, a1_y, dt)
a_x = np.array(a1_x)
a_y = np.array(a1_y)
xPositions[i, :] = x
yPositions[i, :] = y
return xPositions, yPositions
sim_xpos = RunMD(0.1, 1000, x, y)[0]
sim_ypos = RunMD(0.1, 1000, x, y)[1]
np.savetxt("atomsx1.txt", sim_xpos)
np.savetxt("atomsy1.txt", sim_ypos)
Based on your code I can't understand why do have to declare global xAcc
in this way. You don't seem to use it anywhere outside of the function.
In any case, the line xPositions[i, :] = x
will not help you to change your xAcc
size, because it was declared earlier at xPositions = np.zeros((number_of_steps, 3))
.
I see that xAcc = np.zeros((xPositions.size, xPositions.size), dtype=object)
is declared as a square matrix (3000, 3000), and at this line return np.sum(xAcc, axis=0), np.sum(yAcc, axis=0)
you reduced it at only one axis, so that's why you get xAcc
as a vector of length 3000. Maybe reducing at another axis will help in your case np.sum(xAcc)
, in this way you will get an array of lenth 3.
It would be helpful to see the problem at hand.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.