Commit b0ea9c50 authored by Wouter Kessels's avatar Wouter Kessels

Also save initialization

parent 211e54cf
This diff is collapsed.
......@@ -45,20 +45,28 @@ def pipeline(bodies, settings):
# Combine info about the bodies in one object
info = Bodies(bodies)
# First delete old temporary data files
del_temps()
# Save initialization to temp file in temp directory
now = datetime.now().strftime("%y_%m_%d_%H_%M_%S")
temp_folder = "/home/student/project3/temp_" + now + "_data"
os.makedirs(temp_folder)
save_init(bodies, info, settings, temp_folder)
# Do all calculations!
print("Calculating...\n")
bodies = calc(bodies, settings)
print("Calculations completed!\n\n")
# First delete old temporary data files
print("Saving...\n")
del_temps()
# Save output data to temporary file
temp_name = save_results(bodies, info, settings, overwrite=False, name=None)
print("Saving...\n")
temp_name_file, temp_name_folder = save_results(bodies, info, settings, overwrite=False, name=None, folder=temp_folder)
print("Temporary data saved\n\n")
return bodies, info, temp_name
return bodies, info, temp_name_file, temp_name_folder
......@@ -76,22 +84,39 @@ def calc(bodies, settings):
# Fit 3D trajectory to 2D ellipse
# r, center, phi, axes = fit_ellipse(body.trajectory)
# # Plot fit (temporary)
# print('Plot ellipse fit')
# R = np.arange(0,2*np.pi, 0.1)
# a, b = axes
# xx = center[0] + a*np.cos(R)*np.cos(phi) - b*np.sin(R)*np.sin(phi)
# yy = center[1] + a*np.cos(R)*np.sin(phi) + b*np.sin(R)*np.cos(phi)
# plt.plot(body.trajectory[0],body.trajectory[1],'.')
# plt.plot(xx,yy, color = 'red')
# plt.show()
return bodies
def save_results(bodies, info, settings, overwrite, name=None, temp_name=None, description="temporary"):
def save_init(bodies, info, settings, folder):
data = {
'bodies' : bodies,
'info' : info,
'settings' : settings,
'description' : 'initialization'
}
file = folder + "/initialization.pkl"
out_file = open(file, "wb")
pickle.dump(data, out_file)
out_file.close()
return file
def save_progress(positions, velocities, time, bodies, settings):
data = {
'positions' : positions,
'velocities' : velocities,
'time' : time,
'bodies' : bodies,
'settings' : settings
}
def save_results(bodies, info, settings, overwrite, name=None, folder=None, temp_name=None, description="temporary"):
"""Save the acquired data to a file, temporary or lasting.
Parameters
......@@ -116,12 +141,8 @@ def save_results(bodies, info, settings, overwrite, name=None, temp_name=None, d
# TODO: build in check whether name already exists in directory
if not overwrite:
now = datetime.now().strftime("%y_%m_%d_%H_%M_%S")
name_folder = "/home/student/project3/temp_" + now + "_data"
name_file = name_folder + "/temporary.pkl"
name = folder + "/temporary.pkl"
description = 'temporary'
os.makedirs(name_folder)
elif overwrite:
# Check whether new file path is unique and temp_file path is specified
if not os.path.isfile(name):
......@@ -139,14 +160,14 @@ def save_results(bodies, info, settings, overwrite, name=None, temp_name=None, d
'settings': settings
}
out_file = open(name_file, "wb")
out_file = open(name, "wb")
pickle.dump(data, out_file)
out_file.close()
return name_file
return name, folder
def del_temp(folder):
def del_temp(name):
"""Delete specified temporary file.
Parameter
......@@ -154,7 +175,7 @@ def del_temp(folder):
name: str
File path and name of temporary file to delete
"""
shutil.rmtree(folder)
os.remove(name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment