def runError(r, v, m, numParticles, n): for k in eps: r, v, m = initial_Conditions(r, v, m, fileName) # initial energy E0 = nrg.energynew(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_uint(numParticles), ctypes.c_double(k)) for i in np.arange(len(timeStep_iter)): # Holds relative error for each time step rel_err_iter = np.zeros(int(M.ceil(numSteps[i]))) r, v, m = initial_Conditions(r, v, m, fileName) #start[i] = timeit.default_timer() for j in np.arange(int(M.ceil(numSteps[i]))): sim.runSim(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_double(timeStep_iter[i]), ctypes.c_uint(numParticles), \ ctypes.c_uint(n), ctypes.c_double(k)) E = nrg.energynew(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_uint(numParticles), ctypes.c_double(k)) rel_err_iter[j] = abs((E0 - E) / E0) #stop[i] = timeit.default_timer() #runTime[i] = stop[i] - start[i] rel_err[i] = max(rel_err_iter) print(k) plt.figure(2) plt.loglog(timeStep_iter, rel_err, label='{}'.format(k)) plt.xlabel('Time Step') plt.ylabel('Relative Error') plt.legend(loc='best') plt.show() return runTime, rel_err
def runError(r, v, m, numParticles, n): # initial energy E0 = nrg.energy(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_uint(numParticles)) for i in np.arange(len(timeStep_iter)): # Holds relative error for each time step rel_err_iter = np.zeros(int(M.ceil(numSteps[i]))) r, v, m = initial_Conditions(r, v, m, fileName) start[i] = timeit.default_timer() for j in np.arange(int(M.ceil(numSteps[i]))): # one full time step for k in np.arange(n): drift.A1(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ ctypes.c_double(timeStep_iter[i]/(n*4.)), ctypes.c_uint(numParticles)) kickA.A2(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_double(timeStep_iter[i]/(n*2.)), ctypes.c_uint(numParticles), \ dirvec.ctypes.data_as(ctypes.POINTER(ctypes.c_double))) drift.A1(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ ctypes.c_double(timeStep_iter[i]/(n*4.)), ctypes.c_uint(numParticles)) # dirvec will now hold the direction vector along particle j to particle i kickB.B(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_double(timeStep_iter[i]), ctypes.c_uint(numParticles), \ dirvec.ctypes.data_as(ctypes.POINTER(ctypes.c_double))) for k in np.arange(n): drift.A1(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ ctypes.c_double(timeStep_iter[i]/(n*4.)), ctypes.c_uint(numParticles)) kickA.A2(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_double(timeStep_iter[i]/(n*2.)), ctypes.c_uint(numParticles), \ dirvec.ctypes.data_as(ctypes.POINTER(ctypes.c_double))) drift.A1(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ ctypes.c_double(timeStep_iter[i]/(n*4.)), ctypes.c_uint(numParticles)) E = nrg.energy(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_uint(numParticles)) rel_err_iter[j] = abs((E0 - E) / E0) stop[i] = timeit.default_timer() runTime[i] = stop[i] - start[i] rel_err[i] = max(rel_err_iter) return runTime, rel_err
r = np.zeros(3 * numParticles) # array to hold positions of particles v = np.zeros(3 * numParticles) # array to hold velocities of particles m = np.zeros(numParticles) # array to hold masses of particles dirvec = np.zeros( 3) # array to find direction vector along particle j to particle i timeStep_iter = np.logspace(-4, -1, 20) # loop over time steps runTime = np.zeros(len(timeStep_iter)) # the total run time rel_err = np.zeros(len(timeStep_iter)) # largest relative error runTimeLF = np.zeros(len(timeStep_iter)) # the total run time for LF rel_errLF = np.zeros(len(timeStep_iter)) # largest relative error for LF eps = 0.001 # softening factor ecc = np.zeros(numParticles) # eccentricity vector status = np.ones(numParticles) # status vector for collisions and ejections rSatellites = np.array([1e5 / R0, 1e5 / R0]) r, v, m = initial_Conditions(r, v, m, fileName) if flag == "-p": r, v, m = initial_Conditions(r, v, m, fileName) runPlot(r, v, m, numSteps, numParticles, dt, n, rSatellites, rH, status, eps) elif flag == "-e": # make error and run time plot runTime, rel_err = runError(r, v, m, numParticles, n) sim = ctypes.CDLL('./runSim.so') sim.runSim(r.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), v.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ m.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), ctypes.c_double(dt), ctypes.c_int(numParticles), \ ctypes.c_int(n), ctypes.c_double(eps), ctypes.c_int(numSteps), ecc.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), \ status.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), rSatellites.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))