#!/bin/env python3 import numpy as np import argparse def readPositions(planet_name, directory): """ This function reads a csv file with the state of the planet system and it create a matrix with the positions of a given planet at different times :param planet_name: input the planet name with a NON capital first letter :param directory: input the directory :return: a numpy array of sizes 365 X 3 """ m = 365 n = 3 planet_position = np.zeros((m, n), dtype=np.float64) # we need this if condition because the name of the files in the Trajectories folder is different from the name # of the files that we get out of the code and are stored in the direcotry dumps if directory == "Trajectories/": last = 11 elif directory == "dumps/": last = 10 else: print("wrong directory for reference or solution files") for i in range(0, m): if directory == "Trajectories/": filename = directory + "step-" + '{:04}'.format(i) + ".csv" elif directory == "dumps/": filename = directory + "step-" + '{:05}'.format(i) + ".csv" else: print("wrong directory for reference or solution files") with open(filename, 'r') as f: data = f.readlines() for line in data: words = line.split() if words[last] == planet_name: planet_position[i, 0:n] = np.array(words[0:n], dtype=np.float64) return planet_position def computeError(positions_sol, positions_ref): """ This function takes two matrices with the positions of a given planet at different times and it computes the integral error. :param positions_sol: a numpy array of sizes 365 X 3 that contains the computed coordinates XYZ of the planets at different times :param positions_ref: a numpy array of sizes 365 X 3 that contains the reference coordinates XYZ of the planets at different times :return: type float64 containing the integral error of the simulation """ # check if the number of rows of positions_sol is equal to the one of positions_ref if positions_ref.shape[0] != positions_ref.shape[0]: raise RuntimeError('number of steps of the reference and the computed positions are not matching') error = np.sqrt(np.sum(np.power(np.subtract(positions_sol, positions_ref), 2))) return error if __name__ == "__main__": parser = argparse.ArgumentParser(description='Compute_error') parser.add_argument('planet_name', type=str, help='specify the name of the planet') parser.add_argument('directory_ref', type=str, help='directory with the reference files for all the steps from 1 to 365') parser.add_argument('directory_sol', type=str, help='directory with the solution files for all the steps from 1 to 365') args = parser.parse_args() planet_name = args.planet_name # getting the path directory_ref = args.directory_ref directory_sol = args.directory_sol # getting the reference positions of the planet at different times positions_ref = readPositions(planet_name, directory_ref) # getting the computed positions of the planet at different times positions_sol = readPositions(planet_name, directory_sol) # compute the error between the reference positions and the computed ones error = computeError(positions_sol, positions_ref) print("The integral error between the reference and computed positions of the planet "+planet_name+" is: ") print(error)