diff --git a/data/dispersion csv files/Valetudo.csv b/data/dispersion csv files/Valetudo.csv new file mode 100644 index 000000000..ac371d0fb --- /dev/null +++ b/data/dispersion csv files/Valetudo.csv @@ -0,0 +1,31 @@ +attribute_class; parameter_name; mean_value; standard_deviation; +environment; ensembleMember; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];; +motor; impulse; 1415.15; 35.3; +motor; burnOut; 5.274; 1; +motor; nozzleRadius; 0.021642; 0.0005; +motor; throatRadius; 0.008; 0.0005; +motor; grainSeparation; 0.006; 0.001; +motor; grainDensity; 1707; 50; +motor; grainOuterRadius; 0.0214; 0.2; +motor; grainInitialInnerRadius; .0097;0.000375; +motor; grainInitialHeight; 0.12; 0.001 +rocket; rocketMass; 8.257; 0.001; +rocket; inertiaI; 3.675; 0.03675; +rocket; inertiaZ; 0.007; 0.00007; +rocket; radius; 0.04045; 0.001; +rocket; distanceRocketNozzle; -1.024; .001; +rocket; distanceRocketPropellant; -0.51; 0.001; +rocket; powerOffDrag; 0.864857143; 0.03; +rocket; powerOnDrag; 0.864857143; 0.03; +rocket; noseLength; 0.274; 0.001; +rocket; noseDistanceToCM; 1.134; 0.001 +fins; finSpan; 0.077; 0.0005; +fins; finRootChord; 0.058; 0.0005; +fins; finTipChord; 0.018; 0.0005; +fins; finDistanceToCM; -0.906; 0.001; +parachute; CdSDrogue; 0.4537; 0.07; +parachute; lag_rec; 1; 0.5; +parachute; lag_se; 0.73; 0.16; +flight; inclination; 84.7; 1; +flight; heading; 53; 2; +flight; railLength; 5.7; 0.0005; diff --git a/rocketpy/utilities.py b/rocketpy/utilities.py index dc847ea3c..02ee2ca51 100644 --- a/rocketpy/utilities.py +++ b/rocketpy/utilities.py @@ -41,8 +41,6 @@ def compute_CdS_from_drop_test( # TODO: Needs tests - - def calculateEquilibriumAltitude( rocket_mass, CdS, @@ -111,6 +109,7 @@ def check_constant(f, eps): Parameters ---------- f : array, list + _description_ eps : float _description_ @@ -197,3 +196,46 @@ def du(z, u): velocityFunction() return altitudeFunction, velocityFunction, final_sol + + +def create_dispersion_dictionary(filename): + """Creates a dictionary with the rocket data provided by a .csv file. + File should be organized in four columns: attribute_class, parameter_name, + mean_value, standard_deviation. The first row should be the header. + It is advised to use ";" as separator, but "," should work on most of cases. + + Parameters + ---------- + filename : string + String with the path to the .csv file. + + Returns + ------- + dictionary + Dictionary with all rocket data to be used in dispersion analysis. + """ + try: + file = np.genfromtxt( + filename, usecols=(1, 2, 3), skip_header=1, delimiter=";", dtype=str + ) + except: + print( + "Error: The delimiter should be ';'. Using ',' instead, be aware that some resources might not work as expected. Please consider changing the delimiter to ';'." + ) + file = np.genfromtxt( + filename, usecols=(1, 2, 3), skip_header=1, delimiter=",", dtype=str + ) + analysis_parameters = dict() + for row in file: + if row[0] != "": + if row[2] == "": + try: + analysis_parameters[row[0].strip()] = float(row[1]) + except: + analysis_parameters[row[0].strip()] = eval(row[1]) + else: + try: + analysis_parameters[row[0].strip()] = (float(row[1]), float(row[2])) + except: + analysis_parameters[row[0].strip()] = "" + return analysis_parameters diff --git a/tests/fixtures/dispersion/Valetudo_inputs.csv b/tests/fixtures/dispersion/Valetudo_inputs.csv new file mode 100644 index 000000000..ac371d0fb --- /dev/null +++ b/tests/fixtures/dispersion/Valetudo_inputs.csv @@ -0,0 +1,31 @@ +attribute_class; parameter_name; mean_value; standard_deviation; +environment; ensembleMember; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];; +motor; impulse; 1415.15; 35.3; +motor; burnOut; 5.274; 1; +motor; nozzleRadius; 0.021642; 0.0005; +motor; throatRadius; 0.008; 0.0005; +motor; grainSeparation; 0.006; 0.001; +motor; grainDensity; 1707; 50; +motor; grainOuterRadius; 0.0214; 0.2; +motor; grainInitialInnerRadius; .0097;0.000375; +motor; grainInitialHeight; 0.12; 0.001 +rocket; rocketMass; 8.257; 0.001; +rocket; inertiaI; 3.675; 0.03675; +rocket; inertiaZ; 0.007; 0.00007; +rocket; radius; 0.04045; 0.001; +rocket; distanceRocketNozzle; -1.024; .001; +rocket; distanceRocketPropellant; -0.51; 0.001; +rocket; powerOffDrag; 0.864857143; 0.03; +rocket; powerOnDrag; 0.864857143; 0.03; +rocket; noseLength; 0.274; 0.001; +rocket; noseDistanceToCM; 1.134; 0.001 +fins; finSpan; 0.077; 0.0005; +fins; finRootChord; 0.058; 0.0005; +fins; finTipChord; 0.018; 0.0005; +fins; finDistanceToCM; -0.906; 0.001; +parachute; CdSDrogue; 0.4537; 0.07; +parachute; lag_rec; 1; 0.5; +parachute; lag_se; 0.73; 0.16; +flight; inclination; 84.7; 1; +flight; heading; 53; 2; +flight; railLength; 5.7; 0.0005; diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 3ea384c7c..8ca4837b3 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -1,7 +1,42 @@ from rocketpy import utilities +import numpy as np def test_compute_CdS_from_drop_test(): assert ( utilities.compute_CdS_from_drop_test(31.064, 18, 1.0476) == 0.3492311157844522 ) + + +def test_create__dispersion_dictionary(): + """Test if the function returns a dictionary with the correct keys. + It reads the keys from the dictionary generated by the utilities function + and compares them to the expected. + Be careful if you change the "fixtures/dispersion/Valetudo_inputs.csv" file. + """ + + returned_dict = utilities.create_dispersion_dictionary( + "tests/fixtures/dispersion/Valetudo_inputs.csv" + ) + + test_array = np.genfromtxt( + "tests/fixtures/dispersion/Valetudo_inputs.csv", + usecols=(1, 2, 3), + skip_header=1, + delimiter=";", + dtype=str, + ) + test_dict = dict() + for row in test_array: + if row[0] != "": + if row[2] == "": + try: + test_dict[row[0].strip()] = float(row[1]) + except: + test_dict[row[0].strip()] = eval(row[1]) + else: + try: + test_dict[row[0].strip()] = (float(row[1]), float(row[2])) + except: + test_dict[row[0].strip()] = "" + assert returned_dict == test_dict