From 2ef736e0703c1e69aed9a3ba17095b4a22700315 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sun, 14 Aug 2022 19:19:06 -0700 Subject: [PATCH 01/42] Updated TODO list --- .gitignore | 1 + .vscode/c_cpp_properties.json | 19 ------------- TODO.md | 53 +++++++++++++++++++++++++++++++---- main.c | 5 ++-- start | 0 5 files changed, 51 insertions(+), 27 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 start diff --git a/.gitignore b/.gitignore index 619dea9..b353cb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Visual Studio stuff .vs/ +.vscode/ out/ # Recommended build directory build/ diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 46cf7f4..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "configurations": [ - { - "name": "Win32", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "cStandard": "c17", - "cppStandard": "c++17", - "intelliSenseMode": "windows-msvc-x64" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/TODO.md b/TODO.md index 67730e2..2d9e828 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,50 @@ # TODO list for the project -1. Change all TSEL names to TREL inside of the code. -2. Start making the simulation process for the code. -3. Implement transient iteration for functions. - - `float * iterate_function(start, step, iter, function_pointer);` could be the prototype. -4. Rewrite the testapp in Python (using [ctypes](https://realpython.com/python-bindings-overview/#ctypes)) this would allow for easier testing and usage examples for users of higher level languages like Python. +1. Complete dependency tree as much as possible in order. +2. Implement Python library interface +## Dependency tree +- "Diseño" + - [ ] Comport. de Area E11 +- "Comport. de Area" + - [x] Diseño E30 + - [x] Diseño E31 + - [x] Diseño E32 + - [ ] Diseño E43 + - [ ] Diseño E70 +- "Comportamiento en el tiempo" + - [x] Diseño E9 + - [x] Diseño E30 + - [x] Diseño E31 + - [x] Diseño E32 + - [x] Diseño E33 + - [ ] Diseño E34 + - [ ] Diseño E35 + - [ ] Diseño E36 + - [ ] Diseño E37 + - [ ] Diseño E38 + - [ ] Diseño E39 + - [x] Diseño E41 + - [x] Diseño E42 + - [x] Diseño E44 + - [x] Diseño E46 +- "Simulador de Altitud" + - [x] Diseño E15 + - [x] Diseño E26 + - [x] Diseño E29 + - [x] Diseño E30 + - [x] Diseño E31 + - [x] Diseño E32 + - [ ] Diseño E39 + - [x] Diseño E41 + - [x] Diseño E42 + - [x] Diseño E44 + - [x] Diseño E46 + - [x] Diseño E47 + - [x] Diseño E64 + - [x] Diseño E65 + - [ ] Diseño E68 + - [ ] Diseño E70 + - [x] Diseño E85 + - [ ] Diseño E88 + - [ ] Diseño E89 + - [ ] Diseño E101 diff --git a/main.c b/main.c index aaf67c4..cabbf8a 100644 --- a/main.c +++ b/main.c @@ -67,9 +67,8 @@ void print_debug_example() int main(int argc, char *argv[]) { printf("* * * * * * * * * * * * * * * * * * * * * * * *\n"); - printf("* TECSpace Engine Lib *\n"); - printf("* v 0.0.2d *\n"); - //printf("* *\n"); + printf("* TECSpace Rocket Engine Lib *\n"); + printf("* v 0.1d *\n"); printf("* * * * * * * * * * * * * * * * * * * * * * * *\n\n"); if (argc <= 1) { diff --git a/start b/start deleted file mode 100644 index e69de29..0000000 From fbd0da55f616727162d82cb832cd3b3fe474dadb Mon Sep 17 00:00:00 2001 From: lross2k Date: Mon, 15 Aug 2022 09:33:46 -0700 Subject: [PATCH 02/42] Implemented area iteration calculations Added an item "comp_area_values" to the engine struct, this sub-struct contains important calculations obtained in the iterative process generated at the start of the rocket engine Still missing the standard deviation calculation which is needed to continue developing the desing functions from the main library functionalities, this would allow finishing the reamining modules Added some testing parameters to the main.c testapp, most values checked out, we need more precission, like adding a parameter in compilation to change between float and double for all the library and app values --- TODO.md | 4 +- include/TREL.h | 14 ++++-- include/comp_area.h | 17 +++++--- include/rendimientos.h | 8 ++-- include/structs.h | 20 ++++++--- main.c | 5 +++ src/comp_area.c | 99 +++++++++++++++++++++++++++++++++++++++++- src/engine.c | 20 ++++++--- 8 files changed, 157 insertions(+), 30 deletions(-) diff --git a/TODO.md b/TODO.md index 2d9e828..4cc92c3 100644 --- a/TODO.md +++ b/TODO.md @@ -9,8 +9,8 @@ - [x] Diseño E30 - [x] Diseño E31 - [x] Diseño E32 - - [ ] Diseño E43 - - [ ] Diseño E70 + - [x] Diseño E43 + - [x] Diseño E70 - "Comportamiento en el tiempo" - [x] Diseño E9 - [x] Diseño E30 diff --git a/include/TREL.h b/include/TREL.h index ffdf595..9f63a36 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -4,6 +4,11 @@ //#include "design_func.h" // Types +// Comp area calculations +typedef struct { + float avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; +} comp_area_t; + // Propellent grains structure typedef struct { unsigned int amount; @@ -39,14 +44,15 @@ typedef struct screws // Estructura del engine typedef struct engine { - float pressure; // chamber pressure in psi + float pressure; // chamber pressure in psi float escape_vel, temperature; float width_condition, margin_of_safety, max_stress, radial_stress; float tangencial_stress, longitudinal_stress, max_pressure; - grains_t* grains; // ptr to intialized struct + grains_t* grains; // ptr to intialized struct fuel_t* fuel; - tubing_t* tube; // ptr to intialized struct - screws_t* screws; // ptr to initialized struct + tubing_t* tube; // ptr to intialized struct + screws_t* screws; // ptr to initialized struct + comp_area_t* comp_area_values; // ptr to initialized struct } engine_t; // Function prototypes diff --git a/include/comp_area.h b/include/comp_area.h index 8ddbbdf..36c4675 100644 --- a/include/comp_area.h +++ b/include/comp_area.h @@ -1,9 +1,12 @@ -#ifndef COMP_AREA -#define COMP_AREA +#ifndef COMP_AREA_H +#define COMP_AREA_H -//CONSTRUCTORES// -float prom_A_Trans(); -float prom_A_Long(); -float prom_A_Quemado(); +#include "design_func.h" -#endif // COMP_AREA \ No newline at end of file +// Function prototypes +int TREL_EXPORT trel_run_area_comp_iterations(engine_t* engine); +float TREL_EXPORT prom_A_Trans(); +float TREL_EXPORT prom_A_Long(); +float TREL_EXPORT prom_A_Quemado(); + +#endif //COMP_AREA_H diff --git a/include/rendimientos.h b/include/rendimientos.h index c9afad0..7d178a1 100644 --- a/include/rendimientos.h +++ b/include/rendimientos.h @@ -1,5 +1,5 @@ -#ifndef RENDIMIENTOS -#define RENDIMIENTOS +#ifndef RENDIMIENTOS_H +#define RENDIMIENTOS_H #include @@ -12,7 +12,7 @@ // TODO: cambiar list[] por nombres mas descriptivos -//Constructores +// Function prototypes float TREL_EXPORT volumen_combustible(engine_t *engine); float TREL_EXPORT masa_combustible(engine_t* engine); float TREL_EXPORT Tiempo_quemado_combustion(engine_t* engine); @@ -28,4 +28,4 @@ float TREL_EXPORT I_tot_teo(engine_t* engine); float TREL_EXPORT I_tot_esp(float list[]); float TREL_EXPORT Delta_V_Esp(float list[]); -#endif // RENDIMIENTOS +#endif //RENDIMIENTOS_H diff --git a/include/structs.h b/include/structs.h index c463d86..ac73f7c 100644 --- a/include/structs.h +++ b/include/structs.h @@ -1,8 +1,13 @@ -#ifndef TREL_STRUCTS -#define TREL_STRUCTS +#ifndef TREL_STRUCTS_H +#define TREL_STRUCTS_H #include "DLLDefines.h" +// Comp area calculations +typedef struct { + float avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; +} TREL_EXPORT comp_area_t; + // Propellent grains structure typedef struct { unsigned int amount; @@ -38,14 +43,15 @@ typedef struct screws // Estructura del engine typedef struct engine { - float pressure; // chamber pressure in psi + float pressure; // chamber pressure in psi float escape_vel, temperature; float width_condition, margin_of_safety, max_stress, radial_stress; float tangencial_stress, longitudinal_stress, max_pressure; - grains_t *grains; // ptr to intialized struct + grains_t *grains; // ptr to intialized struct fuel_t *fuel; - tubing_t *tube; // ptr to intialized struct - screws_t *screws; // ptr to initialized struct + tubing_t *tube; // ptr to intialized struct + screws_t *screws; // ptr to initialized struct + comp_area_t* comp_area_values; // ptr to initialized struct } TREL_EXPORT engine_t; -#endif //TREL_STRUCTS +#endif //TREL_STRUCTS_H diff --git a/main.c b/main.c index cabbf8a..34cb7dd 100644 --- a/main.c +++ b/main.c @@ -61,6 +61,11 @@ void print_debug_example() // Usando una funcion de val_termod.c printf("\nLa temperatura en garganta es de %f\n", trel_temper_garganta(testing_engine)); + // Probando valores generados por comp_area.c + printf("\nArea longitudinal promedio: %f\n", testing_engine->comp_area_values->avg_long_area); + printf("Area quemado promedio: %f\n", testing_engine->comp_area_values->avg_burn_area); + printf("Area transversal promedio: %f\n", testing_engine->comp_area_values->avg_trans_area); + printf("Suma de diferencia quemado: %f\n", testing_engine->comp_area_values->burn_sum_diff); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica diff --git a/src/comp_area.c b/src/comp_area.c index 093dd42..246dae8 100644 --- a/src/comp_area.c +++ b/src/comp_area.c @@ -1,3 +1,100 @@ +#include "comp_area.h" + +// TODO: Evaluate if we need dynamic allocation for this iteration + +int trel_run_area_comp_iterations(engine_t* engine) +{ + // iterate over given iterations and step size + const float max_iterations = 1000.0f; + const float burn_time = Tiempo_quemado_combustion(engine); + const float step = burn_time/max_iterations; + float time = 0.0f; // time in seconds + + // burn rate value from propellant + const float burn_rate = br_combustion(engine); + + // variables for iteration + float inst_radius = 0.0f; + float inst_long = 0.0f; + + // sums for eventual average value + float transversal_area = 0.0f; + float sum_transversal_area = 0.0f; + float longitudinal_area = 0.0f; + float sum_longitudinal_area = 0.0f; + float burn_area = 0.0f; + float sum_burn_area = 0.0f; + + // array for final values calculation + float burn_area_array[1000]; + // initialize array + for (int i = 0; i < 1000; i++) + burn_area_array[i] = 0.0f; + + // iteration loop + for (int it = 0; it < max_iterations; it++) + { + // instantaneous grain radius step + inst_radius = engine->grains->init_inter_radius + burn_rate * time; + + // instantaneous grain longitude step + inst_long = engine->grains->longitude - 2.0f * burn_rate * time; + + // add to transversal area sum + transversal_area = TREL_PI * (engine->grains->extern_radius*engine->grains->extern_radius - + inst_radius * inst_radius); + sum_transversal_area += transversal_area; + + // add to longitudinal area sum + if (!transversal_area) + longitudinal_area = 0.0f; + else + longitudinal_area = 2 * TREL_PI * inst_radius * inst_long; + sum_longitudinal_area += longitudinal_area; + + // add to burn area sum + burn_area_array[it] = transversal_area * 2 + longitudinal_area; + sum_burn_area += burn_area_array[it]; + + // step time value + if (time + step < burn_time) + time += step; + else + time = 0; + } + + // calculate average transversal area + transversal_area = sum_transversal_area / max_iterations; + engine->comp_area_values->avg_trans_area = transversal_area; + + // calculate average longitudinal area + longitudinal_area = sum_longitudinal_area / max_iterations; + engine->comp_area_values->avg_long_area = longitudinal_area; + + // calculate average burn area + burn_area = sum_burn_area / max_iterations; + engine->comp_area_values->avg_burn_area = burn_area; + + // calculate the difference of each iteration's burn area and the average burn area + for (int i = 0; i < max_iterations; i++) + burn_area_array[i] = burn_area_array[i] - burn_area; + + // then sum all the greater than zero differences, store them to a variable, and + // sum all the lower than zero differences, store them to another variable + float lt_differences = 0.0f; // lower + float gt_differences = 0.0f; // greater + + for (int i = 0; i < max_iterations; i++) + if (burn_area_array[i] > 0) + gt_differences += burn_area_array[i]; + else + lt_differences += burn_area_array[i]; + + // calculate the difference of burn area by adding the two sums + engine->comp_area_values->burn_sum_diff = gt_differences + lt_differences; + + return(0); // everything went well +} //Proms_Comportamiento_Area// float prom_A_Trans() @@ -13,4 +110,4 @@ float prom_A_Long() float prom_A_Quemado() { return 0.012679f; // H1019 prom -} \ No newline at end of file +} diff --git a/src/engine.c b/src/engine.c index 4850142..0e1536d 100644 --- a/src/engine.c +++ b/src/engine.c @@ -8,7 +8,7 @@ grains_t* trel_grains_init( float longitude, float grain_separation) { - grains_t *grains = malloc(sizeof(*grains)); + grains_t *grains = (grains_t *) malloc(sizeof(grains_t)); if (!grains) { printf("Grains initialization failed\n"); } @@ -26,7 +26,7 @@ fuel_t* trel_fuel_init( float pressure_exponent, float density) { - fuel_t *fuel = malloc(sizeof(*fuel)); + fuel_t *fuel = (fuel_t *) malloc(sizeof(fuel_t)); if (!fuel) { printf("Fuel initialization failed\n"); } @@ -48,7 +48,7 @@ tubing_t* trel_tubing_init( float ult_tension, float ult_pressure) { - tubing_t *tube = malloc(sizeof(*tube)); + tubing_t *tube = (tubing_t *) malloc(sizeof(tubing_t)); if (!tube) { printf("Tubing initialization failed\n"); exit(1); @@ -76,7 +76,7 @@ screws_t* trel_screws_init( float diameter, float dist_center_wall) { - screws_t *screws = malloc(sizeof(*screws)); + screws_t *screws = (screws_t *) malloc(sizeof(screws_t)); if (!screws) { printf("Screws initialization failed\n"); exit(1); @@ -100,7 +100,14 @@ engine_t* trel_engine_init( tubing_t *tube, screws_t *screws) { - engine_t *engine = malloc(sizeof(*engine)); + comp_area_t* comp_area_values = (comp_area_t*) malloc(sizeof(comp_area_t)); + comp_area_values->avg_burn_area = 0.0f; + comp_area_values->avg_long_area = 0.0f; + comp_area_values->avg_trans_area = 0.0f; + comp_area_values->burn_std_deviation = 0.0f; + comp_area_values->burn_sum_diff = 0.0f; + + engine_t *engine = (engine_t *) malloc(sizeof(engine_t)); if (!engine) { printf("Engine initialization failed\n"); exit(1); @@ -119,12 +126,15 @@ engine_t* trel_engine_init( engine->tube = tube; engine->screws = screws; engine->escape_vel = calc_escape_vel(engine); + engine->comp_area_values = comp_area_values; trel_engine_max_pressure(engine); trel_transversal_area_tube(engine); trel_tube_mateial_area(engine); trel_width_cutting_segment(engine); trel_area_per_screw(engine); trel_tangencial_stress(engine); + if (trel_run_area_comp_iterations(engine)) // 1 means there was an error + return(NULL); // NULL means the engine couldn't be properly generated return(engine); } From 54912b22c30eefc560d016b26138a1ec7f1f6771 Mon Sep 17 00:00:00 2001 From: lross2k Date: Mon, 15 Aug 2022 10:10:15 -0700 Subject: [PATCH 03/42] Finished implementing area iteration and standard deviation calculation Cleaned old functions in comp_area.c/h which weren't needed and the values pointed to constants instead of the calculations now stored in the engine_t strcut Wrote some specifications in the TODO file to have a clearer undestanding of where are some of the needed values for the future implementations planned Tested compilation 15 of August Platform: Windows 10 with Visual Studio Community 2022 --- TODO.md | 13 +++++++------ include/TREL.h | 2 +- include/comp_area.h | 3 --- include/structs.h | 2 +- main.c | 1 + src/comp_area.c | 27 +++++++++++---------------- src/rendimientos.c | 8 +++++--- 7 files changed, 26 insertions(+), 30 deletions(-) diff --git a/TODO.md b/TODO.md index 4cc92c3..a9f6ad3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,16 +1,17 @@ # TODO list for the project 1. Complete dependency tree as much as possible in order. -2. Implement Python library interface +2. Change float to double precission for most of the calculations +3. Implement Python library interface ## Dependency tree - "Diseño" - - [ ] Comport. de Area E11 -- "Comport. de Area" + - [x] Comport. de Area E11 ===> engine->comp_area_values->burn_std_deviation +- "Comport. de Area" [IMPLEMENTADO] - [x] Diseño E30 - [x] Diseño E31 - [x] Diseño E32 - - [x] Diseño E43 - - [x] Diseño E70 + - [x] Diseño E43 ===> br_combustion(engine_t *engine) + - [x] Diseño E70 ===> Tiempo_quemado_combustion(engine_t *engine) - "Comportamiento en el tiempo" - [x] Diseño E9 - [x] Diseño E30 @@ -43,7 +44,7 @@ - [x] Diseño E64 - [x] Diseño E65 - [ ] Diseño E68 - - [ ] Diseño E70 + - [x] Diseño E70 ===> Tiempo_quemado_combustion(engine_t *engine) - [x] Diseño E85 - [ ] Diseño E88 - [ ] Diseño E89 diff --git a/include/TREL.h b/include/TREL.h index 9f63a36..e3de53a 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -6,7 +6,7 @@ // Types // Comp area calculations typedef struct { - float avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; + double avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; } comp_area_t; // Propellent grains structure diff --git a/include/comp_area.h b/include/comp_area.h index 36c4675..4a86a73 100644 --- a/include/comp_area.h +++ b/include/comp_area.h @@ -5,8 +5,5 @@ // Function prototypes int TREL_EXPORT trel_run_area_comp_iterations(engine_t* engine); -float TREL_EXPORT prom_A_Trans(); -float TREL_EXPORT prom_A_Long(); -float TREL_EXPORT prom_A_Quemado(); #endif //COMP_AREA_H diff --git a/include/structs.h b/include/structs.h index ac73f7c..0b67eb2 100644 --- a/include/structs.h +++ b/include/structs.h @@ -5,7 +5,7 @@ // Comp area calculations typedef struct { - float avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; + double avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; } TREL_EXPORT comp_area_t; // Propellent grains structure diff --git a/main.c b/main.c index 34cb7dd..45760f9 100644 --- a/main.c +++ b/main.c @@ -66,6 +66,7 @@ void print_debug_example() printf("Area quemado promedio: %f\n", testing_engine->comp_area_values->avg_burn_area); printf("Area transversal promedio: %f\n", testing_engine->comp_area_values->avg_trans_area); printf("Suma de diferencia quemado: %f\n", testing_engine->comp_area_values->burn_sum_diff); + printf("Desviacion estandar area de quemado: %f\n", testing_engine->comp_area_values->burn_std_deviation); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica diff --git a/src/comp_area.c b/src/comp_area.c index 246dae8..b37be9f 100644 --- a/src/comp_area.c +++ b/src/comp_area.c @@ -79,6 +79,17 @@ int trel_run_area_comp_iterations(engine_t* engine) for (int i = 0; i < max_iterations; i++) burn_area_array[i] = burn_area_array[i] - burn_area; + // calculate standard deviation + double burn_std_deviation = 0.0; + // sum the squared burn area differences + for (int i = 0; i < max_iterations; i++) burn_std_deviation += burn_area_array[i] * burn_area_array[i]; + // calculate the average of the squared differences + burn_std_deviation /= 1000.0; + // get the square root of the avg + burn_std_deviation = sqrt(burn_std_deviation); + // store the value in the engine + engine->comp_area_values->burn_std_deviation = burn_std_deviation; + // then sum all the greater than zero differences, store them to a variable, and // sum all the lower than zero differences, store them to another variable float lt_differences = 0.0f; // lower @@ -95,19 +106,3 @@ int trel_run_area_comp_iterations(engine_t* engine) return(0); // everything went well } - -//Proms_Comportamiento_Area// -float prom_A_Trans() -{ - return 0.0015079f; // F1019 prom -} - -float prom_A_Long() -{ - return 0.00966317f; // G1019 prom -} - -float prom_A_Quemado() -{ - return 0.012679f; // H1019 prom -} diff --git a/src/rendimientos.c b/src/rendimientos.c index 33edd46..78dad8e 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -4,7 +4,8 @@ /*E43 */ float br_combustion(engine_t *engine) { - return (engine->fuel->const_burn_rate* powf(trel_get_pressure_pa(engine) * FACT_CONV_BR, engine->fuel->pressure_exponent) * FACT_CONV_M_TO_I); + return (engine->fuel->const_burn_rate* powf(trel_get_pressure_pa(engine) * + FACT_CONV_BR, engine->fuel->pressure_exponent) * FACT_CONV_M_TO_I); } /*Combustible */ @@ -12,7 +13,8 @@ float br_combustion(engine_t *engine) /*E67 Volumen_Combustible */ float volumen_combustible(engine_t *engine) { - return(float)(TREL_PI*engine->grains->longitude*engine->grains->amount*(powf(engine->grains->extern_radius,2.0f)-powf(engine->grains->init_inter_radius,2.0f))); + return(float)(TREL_PI*engine->grains->longitude*engine->grains->amount*(powf( + engine->grains->extern_radius,2.0f)-powf(engine->grains->init_inter_radius,2.0f))); } /*E68 Masa_Combustible */ @@ -32,7 +34,7 @@ float Tiempo_quemado_combustion(engine_t *engine) /*E71 Área de quemado */ float Ab_combustion(engine_t *engine) { - return (prom_A_Quemado()*engine->grains->amount); + return (engine->comp_area_values->avg_burn_area*engine->grains->amount); } /*E72 Flujo másico*/ From fcf3604a67b20d7c62b554b27b5fe29a536d9785 Mon Sep 17 00:00:00 2001 From: lross2k Date: Mon, 15 Aug 2022 10:20:17 -0700 Subject: [PATCH 04/42] Fixed compilation from MSYS There was a bracket not guarding a return in engine.c MSVC accepted this, but GCC didn't seem to fancy it so much Tested platforms: - Windows 11, Visual Studio 2022, MSVC - Windows 11, MSYS2, GCC --- include/rendimientos.h | 8 ++++---- src/engine.c | 2 ++ src/rendimientos.c | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/rendimientos.h b/include/rendimientos.h index 7d178a1..bce01b4 100644 --- a/include/rendimientos.h +++ b/include/rendimientos.h @@ -16,15 +16,15 @@ float TREL_EXPORT volumen_combustible(engine_t *engine); float TREL_EXPORT masa_combustible(engine_t* engine); float TREL_EXPORT Tiempo_quemado_combustion(engine_t* engine); -float TREL_EXPORT Ab_combustion(engine_t* engine); +double TREL_EXPORT Ab_combustion(engine_t* engine); float TREL_EXPORT br_combustion(engine_t* engine); -float TREL_EXPORT mg_combustion(engine_t* engine); -float TREL_EXPORT emp_teo(engine_t* engine); +double TREL_EXPORT mg_combustion(engine_t* engine); +double TREL_EXPORT emp_teo(engine_t* engine); float TREL_EXPORT emp_prom(float list[]); float TREL_EXPORT emp_max(float list[]); float TREL_EXPORT I_sp_teo(engine_t* engine); float TREL_EXPORT I_sp_esperado(float list[], engine_t *engine); -float TREL_EXPORT I_tot_teo(engine_t* engine); +double TREL_EXPORT I_tot_teo(engine_t* engine); float TREL_EXPORT I_tot_esp(float list[]); float TREL_EXPORT Delta_V_Esp(float list[]); diff --git a/src/engine.c b/src/engine.c index 0e1536d..933d5bb 100644 --- a/src/engine.c +++ b/src/engine.c @@ -134,7 +134,9 @@ engine_t* trel_engine_init( trel_area_per_screw(engine); trel_tangencial_stress(engine); if (trel_run_area_comp_iterations(engine)) // 1 means there was an error + { return(NULL); // NULL means the engine couldn't be properly generated + } return(engine); } diff --git a/src/rendimientos.c b/src/rendimientos.c index 78dad8e..d86d048 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -32,13 +32,13 @@ float Tiempo_quemado_combustion(engine_t *engine) } /*E71 Área de quemado */ -float Ab_combustion(engine_t *engine) +double Ab_combustion(engine_t *engine) { return (engine->comp_area_values->avg_burn_area*engine->grains->amount); } /*E72 Flujo másico*/ -float mg_combustion(engine_t *engine) +double mg_combustion(engine_t *engine) { return Ab_combustion(engine)*engine->fuel->density*br_combustion(engine); } @@ -46,7 +46,7 @@ float mg_combustion(engine_t *engine) /*Fuerzas*/ /*E74 Empuje teórico*/ -float emp_teo(engine_t *engine) +double emp_teo(engine_t *engine) { return mg_combustion(engine)*velocidad_escape(engine); } @@ -87,7 +87,7 @@ float I_sp_esperado(float list[], engine_t *engine) } /*E83 Impulso total teórico*/ -float I_tot_teo(engine_t *engine) +double I_tot_teo(engine_t *engine) { return emp_teo(engine)* Tiempo_quemado_combustion(engine); } From 6887b4a576e7d89d13e3a5004447708e68a34a81 Mon Sep 17 00:00:00 2001 From: lross2k Date: Mon, 15 Aug 2022 11:54:53 -0700 Subject: [PATCH 05/42] Added an aclaration in the TODO for comp_timpo.h Increased the version mentioned in the main.c testapp, now it's 0.02d --- TODO.md | 4 +++- include/comp_tiempo.h | 6 +++--- main.c | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index a9f6ad3..423fe5f 100644 --- a/TODO.md +++ b/TODO.md @@ -6,6 +6,7 @@ ## Dependency tree - "Diseño" - [x] Comport. de Area E11 ===> engine->comp_area_values->burn_std_deviation + - [ ] Comportamiento en el tiempo AD3511 - "Comport. de Area" [IMPLEMENTADO] - [x] Diseño E30 - [x] Diseño E31 @@ -45,7 +46,8 @@ - [x] Diseño E65 - [ ] Diseño E68 - [x] Diseño E70 ===> Tiempo_quemado_combustion(engine_t *engine) - - [x] Diseño E85 + - [ ] Diseño E85 + - [ ] Comportamiento en el tiempo AD3511 - [ ] Diseño E88 - [ ] Diseño E89 - [ ] Diseño E101 diff --git a/include/comp_tiempo.h b/include/comp_tiempo.h index b094f43..ac4e2ad 100644 --- a/include/comp_tiempo.h +++ b/include/comp_tiempo.h @@ -1,5 +1,5 @@ -#ifndef COMP_TIEMPO -#define COMP_TIEMPO +#ifndef COMP_TIEMPO_H +#define COMP_TIEMPO_H //Constructores float empuje_prom_tiempo(float list[]); @@ -7,4 +7,4 @@ float impulso_tot(float list[]); float impulso_esp(float list[], engine_t *engine); float delta_V(float list[]); -#endif // COMP_TIEMPO \ No newline at end of file +#endif //COMP_TIEMPO_H diff --git a/main.c b/main.c index 45760f9..44d5adb 100644 --- a/main.c +++ b/main.c @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) { printf("* * * * * * * * * * * * * * * * * * * * * * * *\n"); printf("* TECSpace Rocket Engine Lib *\n"); - printf("* v 0.1d *\n"); + printf("* v 0.2d *\n"); printf("* * * * * * * * * * * * * * * * * * * * * * * *\n\n"); if (argc <= 1) { From 4b0cf94bc832ce6491cf6f5011426e968b539c72 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 10 Dec 2022 18:45:29 -0600 Subject: [PATCH 06/42] Merge and new time functionalities --- include/comp_area.h | 4 + src/comp_area.c | 237 ++++++++++++++++++++++++-------------------- src/design_func.c | 39 ++++++++ src/rendimientos.c | 2 + src/val_termod.c | 1 + 5 files changed, 175 insertions(+), 108 deletions(-) diff --git a/include/comp_area.h b/include/comp_area.h index 4a86a73..0a3aa00 100644 --- a/include/comp_area.h +++ b/include/comp_area.h @@ -5,5 +5,9 @@ // Function prototypes int TREL_EXPORT trel_run_area_comp_iterations(engine_t* engine); +float prom_A_Trans(); +float prom_A_Long(); +float prom_A_Quemado(); +float prom_A_Devest(); #endif //COMP_AREA_H diff --git a/src/comp_area.c b/src/comp_area.c index b37be9f..76e1e51 100644 --- a/src/comp_area.c +++ b/src/comp_area.c @@ -1,108 +1,129 @@ -#include "comp_area.h" - -// TODO: Evaluate if we need dynamic allocation for this iteration - -int trel_run_area_comp_iterations(engine_t* engine) -{ - // iterate over given iterations and step size - const float max_iterations = 1000.0f; - const float burn_time = Tiempo_quemado_combustion(engine); - const float step = burn_time/max_iterations; - float time = 0.0f; // time in seconds - - // burn rate value from propellant - const float burn_rate = br_combustion(engine); - - // variables for iteration - float inst_radius = 0.0f; - float inst_long = 0.0f; - - // sums for eventual average value - float transversal_area = 0.0f; - float sum_transversal_area = 0.0f; - float longitudinal_area = 0.0f; - float sum_longitudinal_area = 0.0f; - float burn_area = 0.0f; - float sum_burn_area = 0.0f; - - // array for final values calculation - float burn_area_array[1000]; - // initialize array - for (int i = 0; i < 1000; i++) - burn_area_array[i] = 0.0f; - - // iteration loop - for (int it = 0; it < max_iterations; it++) - { - // instantaneous grain radius step - inst_radius = engine->grains->init_inter_radius + burn_rate * time; - - // instantaneous grain longitude step - inst_long = engine->grains->longitude - 2.0f * burn_rate * time; - - // add to transversal area sum - transversal_area = TREL_PI * (engine->grains->extern_radius*engine->grains->extern_radius - - inst_radius * inst_radius); - sum_transversal_area += transversal_area; - - // add to longitudinal area sum - if (!transversal_area) - longitudinal_area = 0.0f; - else - longitudinal_area = 2 * TREL_PI * inst_radius * inst_long; - sum_longitudinal_area += longitudinal_area; - - // add to burn area sum - burn_area_array[it] = transversal_area * 2 + longitudinal_area; - sum_burn_area += burn_area_array[it]; - - // step time value - if (time + step < burn_time) - time += step; - else - time = 0; - } - - // calculate average transversal area - transversal_area = sum_transversal_area / max_iterations; - engine->comp_area_values->avg_trans_area = transversal_area; - - // calculate average longitudinal area - longitudinal_area = sum_longitudinal_area / max_iterations; - engine->comp_area_values->avg_long_area = longitudinal_area; - - // calculate average burn area - burn_area = sum_burn_area / max_iterations; - engine->comp_area_values->avg_burn_area = burn_area; - - // calculate the difference of each iteration's burn area and the average burn area - for (int i = 0; i < max_iterations; i++) - burn_area_array[i] = burn_area_array[i] - burn_area; - - // calculate standard deviation - double burn_std_deviation = 0.0; - // sum the squared burn area differences - for (int i = 0; i < max_iterations; i++) burn_std_deviation += burn_area_array[i] * burn_area_array[i]; - // calculate the average of the squared differences - burn_std_deviation /= 1000.0; - // get the square root of the avg - burn_std_deviation = sqrt(burn_std_deviation); - // store the value in the engine - engine->comp_area_values->burn_std_deviation = burn_std_deviation; - - // then sum all the greater than zero differences, store them to a variable, and - // sum all the lower than zero differences, store them to another variable - float lt_differences = 0.0f; // lower - float gt_differences = 0.0f; // greater - - for (int i = 0; i < max_iterations; i++) - if (burn_area_array[i] > 0) - gt_differences += burn_area_array[i]; - else - lt_differences += burn_area_array[i]; - - // calculate the difference of burn area by adding the two sums - engine->comp_area_values->burn_sum_diff = gt_differences + lt_differences; - - return(0); // everything went well -} +#include "comp_area.h" + +// TODO: Evaluate if we need dynamic allocation for this iteration + +int trel_run_area_comp_iterations(engine_t* engine) +{ + // iterate over given iterations and step size + const float max_iterations = 1000.0f; + const float burn_time = Tiempo_quemado_combustion(engine); + const float step = burn_time/max_iterations; + float time = 0.0f; // time in seconds + + // burn rate value from propellant + const float burn_rate = br_combustion(engine); + + // variables for iteration + float inst_radius = 0.0f; + float inst_long = 0.0f; + + // sums for eventual average value + float transversal_area = 0.0f; + float sum_transversal_area = 0.0f; + float longitudinal_area = 0.0f; + float sum_longitudinal_area = 0.0f; + float burn_area = 0.0f; + float sum_burn_area = 0.0f; + + // array for final values calculation + float burn_area_array[1000]; + // initialize array + for (int i = 0; i < 1000; i++) + burn_area_array[i] = 0.0f; + + // iteration loop + for (int it = 0; it < max_iterations; it++) + { + // instantaneous grain radius step + inst_radius = engine->grains->init_inter_radius + burn_rate * time; + + // instantaneous grain longitude step + inst_long = engine->grains->longitude - 2.0f * burn_rate * time; + + // add to transversal area sum + transversal_area = TREL_PI * (engine->grains->extern_radius*engine->grains->extern_radius - + inst_radius * inst_radius); + sum_transversal_area += transversal_area; + + // add to longitudinal area sum + if (!transversal_area) + longitudinal_area = 0.0f; + else + longitudinal_area = 2 * TREL_PI * inst_radius * inst_long; + sum_longitudinal_area += longitudinal_area; + + // add to burn area sum + burn_area_array[it] = transversal_area * 2 + longitudinal_area; + sum_burn_area += burn_area_array[it]; + + // step time value + if (time + step < burn_time) + time += step; + else + time = 0; + } + + // calculate average transversal area + transversal_area = sum_transversal_area / max_iterations; + engine->comp_area_values->avg_trans_area = transversal_area; + + // calculate average longitudinal area + longitudinal_area = sum_longitudinal_area / max_iterations; + engine->comp_area_values->avg_long_area = longitudinal_area; + + // calculate average burn area + burn_area = sum_burn_area / max_iterations; + engine->comp_area_values->avg_burn_area = burn_area; + + // calculate the difference of each iteration's burn area and the average burn area + for (int i = 0; i < max_iterations; i++) + burn_area_array[i] = burn_area_array[i] - burn_area; + + // calculate standard deviation + double burn_std_deviation = 0.0; + // sum the squared burn area differences + for (int i = 0; i < max_iterations; i++) burn_std_deviation += burn_area_array[i] * burn_area_array[i]; + // calculate the average of the squared differences + burn_std_deviation /= 1000.0; + // get the square root of the avg + burn_std_deviation = sqrt(burn_std_deviation); + // store the value in the engine + engine->comp_area_values->burn_std_deviation = burn_std_deviation; + + // then sum all the greater than zero differences, store them to a variable, and + // sum all the lower than zero differences, store them to another variable + float lt_differences = 0.0f; // lower + float gt_differences = 0.0f; // greater + + for (int i = 0; i < max_iterations; i++) + if (burn_area_array[i] > 0) + gt_differences += burn_area_array[i]; + else + lt_differences += burn_area_array[i]; + + // calculate the difference of burn area by adding the two sums + engine->comp_area_values->burn_sum_diff = gt_differences + lt_differences; + + return(0); // everything went well +} + +//Proms_Comportamiento_Area// +float prom_A_Trans() +{ + return 0.0015079f; // F1019 prom +} + +float prom_A_Long() +{ + return 0.00966317f; // G1019 prom +} + +float prom_A_Quemado() +{ + return 0.012679f; // H1019 prom +} + +float prom_A_Devest() +{ + return 0.000585f; // H1020 prom +} diff --git a/src/design_func.c b/src/design_func.c index 7075c3e..7f6c783 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -9,11 +9,49 @@ // E162 DIAMETRO_NUCLEO /*Functions*/ + // E25 returns chamber pressure in pascals float trel_get_pressure_pa(engine_t* engine) { return(trel_psi_to_pa(engine->pressure)); } + +//E34 returns the grain standard deviation +float desv_est_grains() +{ + return(prom_A_Devest()*100000.0f); +} + +//E35 Volumen unitario granos +float volumen_unitario(engine_t *engine) +{ + return(TREL_PI*(0.1f)*((engine->grains->extern_radius)* (engine->grains->extern_radius) -(0.01f)* (0.01f))); +} + +// E36 Masa unitaria grano +float masa_unitaria(engine_t* engine) +{ + return(volumen_unitario(engine)*1000.0f*(engine->fuel->density)); +} + +// E37 Longitud total con combustible +float longitudt_ccombustible(engine_t *engine) +{ + return((engine->grains->amount)*(engine->grains->longitude)); +} + +// E38 Port Area +float port_area(engine_t* engine) +{ + return(TREL_PI*(engine->grains->init_inter_radius)*(engine->grains->init_inter_radius)); +} + +// E39 +float long_secc_combus(engine_t* engine) +{ + return((engine->grains->grain_separation) * ((engine->grains->amount) - 1.0f) + ((engine->grains->longitude) * (engine->grains->amount))); +} + // E49 Volumen especifico camara float volumen_camara(engine_t *engine) { @@ -25,6 +63,7 @@ float calc_escape_vel(engine_t* engine) float calc_pow = powf((PRESION_ATMOSFERICA / trel_get_pressure_pa(engine)), ((HEAT_CAPACITY_RATIO - 1.0f) / HEAT_CAPACITY_RATIO)); return(sqrtf(2.0f * HEAT_CAPACITY_RATIO /(HEAT_CAPACITY_RATIO -1.0f) * CONSTANTE_GASES * engine->temperature * (1.0f - calc_pow))); } + /* Determinacion de fuerza sobre tapas*/ // E140 Area interna float area_interna(engine_t *engine) diff --git a/src/rendimientos.c b/src/rendimientos.c index d86d048..d8f1707 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -103,3 +103,5 @@ float Delta_V_Esp(float list[]) { return delta_V(list); } + +// E85 \ No newline at end of file diff --git a/src/val_termod.c b/src/val_termod.c index 1876e64..9ce5b19 100644 --- a/src/val_termod.c +++ b/src/val_termod.c @@ -90,3 +90,4 @@ float vel_escape_caract(engine_t *engine) { return powf(powf((CONSTANTE_GASES * engine->temperature) / (HEAT_CAPACITY_RATIO *(2 / (HEAT_CAPACITY_RATIO + 1))),((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))), (0.5)); } + From 8163908127386e31ac4ea0b574ad640962a077c8 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 10 Dec 2022 19:25:39 -0600 Subject: [PATCH 07/42] Cleaned process for next implementation --- TODO.md | 43 +------------------------------------------ include/comp_area.h | 4 ---- include/structs.h | 1 + include/val_termod.h | 1 - src/comp_area.c | 21 --------------------- src/design_func.c | 28 ++++++++++++++++++---------- src/engine.c | 1 + src/rendimientos.c | 6 ++---- src/val_termod.c | 10 +--------- 9 files changed, 24 insertions(+), 91 deletions(-) diff --git a/TODO.md b/TODO.md index 423fe5f..3b9d57d 100644 --- a/TODO.md +++ b/TODO.md @@ -4,50 +4,9 @@ 3. Implement Python library interface ## Dependency tree +- "Comportamiento en el tiempo" - "Diseño" - - [x] Comport. de Area E11 ===> engine->comp_area_values->burn_std_deviation - [ ] Comportamiento en el tiempo AD3511 -- "Comport. de Area" [IMPLEMENTADO] - - [x] Diseño E30 - - [x] Diseño E31 - - [x] Diseño E32 - - [x] Diseño E43 ===> br_combustion(engine_t *engine) - - [x] Diseño E70 ===> Tiempo_quemado_combustion(engine_t *engine) -- "Comportamiento en el tiempo" - - [x] Diseño E9 - - [x] Diseño E30 - - [x] Diseño E31 - - [x] Diseño E32 - - [x] Diseño E33 - - [ ] Diseño E34 - - [ ] Diseño E35 - - [ ] Diseño E36 - - [ ] Diseño E37 - - [ ] Diseño E38 - - [ ] Diseño E39 - - [x] Diseño E41 - - [x] Diseño E42 - - [x] Diseño E44 - - [x] Diseño E46 - "Simulador de Altitud" - - [x] Diseño E15 - - [x] Diseño E26 - - [x] Diseño E29 - - [x] Diseño E30 - - [x] Diseño E31 - - [x] Diseño E32 - - [ ] Diseño E39 - - [x] Diseño E41 - - [x] Diseño E42 - - [x] Diseño E44 - - [x] Diseño E46 - - [x] Diseño E47 - - [x] Diseño E64 - - [x] Diseño E65 - - [ ] Diseño E68 - - [x] Diseño E70 ===> Tiempo_quemado_combustion(engine_t *engine) - [ ] Diseño E85 - [ ] Comportamiento en el tiempo AD3511 - - [ ] Diseño E88 - - [ ] Diseño E89 - - [ ] Diseño E101 diff --git a/include/comp_area.h b/include/comp_area.h index 0a3aa00..4a86a73 100644 --- a/include/comp_area.h +++ b/include/comp_area.h @@ -5,9 +5,5 @@ // Function prototypes int TREL_EXPORT trel_run_area_comp_iterations(engine_t* engine); -float prom_A_Trans(); -float prom_A_Long(); -float prom_A_Quemado(); -float prom_A_Devest(); #endif //COMP_AREA_H diff --git a/include/structs.h b/include/structs.h index 0b67eb2..ef7e049 100644 --- a/include/structs.h +++ b/include/structs.h @@ -47,6 +47,7 @@ typedef struct engine float escape_vel, temperature; float width_condition, margin_of_safety, max_stress, radial_stress; float tangencial_stress, longitudinal_stress, max_pressure; + float nozzle_efficiency; grains_t *grains; // ptr to intialized struct fuel_t *fuel; tubing_t *tube; // ptr to intialized struct diff --git a/include/val_termod.h b/include/val_termod.h index e5a9a99..b375f0a 100644 --- a/include/val_termod.h +++ b/include/val_termod.h @@ -19,7 +19,6 @@ float TREL_EXPORT trel_temper_garganta(engine_t *engine); float TREL_EXPORT velocidad_garganta(engine_t* engine); float TREL_EXPORT vel_sonica_local_at(engine_t* engine); float TREL_EXPORT vel_numero_mach_mt(engine_t* engine); -float TREL_EXPORT velocidad_escape(engine_t* engine); float TREL_EXPORT vel_sonica_local_a2(engine_t* engine); float TREL_EXPORT vel_numero_mach_m2(engine_t* engine); float TREL_EXPORT vel_escape_caract(engine_t* engine); diff --git a/src/comp_area.c b/src/comp_area.c index 76e1e51..5bf8c80 100644 --- a/src/comp_area.c +++ b/src/comp_area.c @@ -106,24 +106,3 @@ int trel_run_area_comp_iterations(engine_t* engine) return(0); // everything went well } - -//Proms_Comportamiento_Area// -float prom_A_Trans() -{ - return 0.0015079f; // F1019 prom -} - -float prom_A_Long() -{ - return 0.00966317f; // G1019 prom -} - -float prom_A_Quemado() -{ - return 0.012679f; // H1019 prom -} - -float prom_A_Devest() -{ - return 0.000585f; // H1020 prom -} diff --git a/src/design_func.c b/src/design_func.c index 7f6c783..58ae702 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -17,9 +17,9 @@ float trel_get_pressure_pa(engine_t* engine) } //E34 returns the grain standard deviation -float desv_est_grains() +float desv_est_grains(engine_t *engine) { - return(prom_A_Devest()*100000.0f); + return((float)(engine->comp_area_values->burn_std_deviation)*100000.0f); } //E35 Volumen unitario granos @@ -163,13 +163,21 @@ float diferencia_radio_externo(engine_t *engine) { return((radio_final(circunferencia_externa_inicial(engine),deformacion_circunferencia_externa(engine))-(engine->tube->diameter_ext/2.0f))*1000.0f); } -/*Resistencia de tornillos*/ - -/*Ecuacion de Barlow*/ - -/*Longitud maxima de Tornillo*/ - -/*O-ring's*/ +/*Escape*/ +// E88 +float throat_area(engine_t *engine) +{ + return((float)mg_combustion(engine) * vol_esp_garganta(engine) / velocidad_garganta(engine)); +} -/*Informacion*/ +// E89 +float escape_area(engine_t* engine) +{ + return((float)mg_combustion(engine) * vol_esp_escape(engine) / calc_escape_vel(engine)); +} +// E101 engine->nozzle_efficiency +void set_nozzle_efficiency(engine_t* engine, float value) +{ + engine->nozzle_efficiency = value; +} diff --git a/src/engine.c b/src/engine.c index 933d5bb..accc543 100644 --- a/src/engine.c +++ b/src/engine.c @@ -127,6 +127,7 @@ engine_t* trel_engine_init( engine->screws = screws; engine->escape_vel = calc_escape_vel(engine); engine->comp_area_values = comp_area_values; + engine->nozzle_efficiency = 0.0f; trel_engine_max_pressure(engine); trel_transversal_area_tube(engine); trel_tube_mateial_area(engine); diff --git a/src/rendimientos.c b/src/rendimientos.c index d8f1707..2de6b07 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -48,7 +48,7 @@ double mg_combustion(engine_t *engine) /*E74 Empuje teórico*/ double emp_teo(engine_t *engine) { - return mg_combustion(engine)*velocidad_escape(engine); + return mg_combustion(engine)*calc_escape_vel(engine); } /*E76 Empuje promedio esperado*/ @@ -77,7 +77,7 @@ float emp_max(float list[]) /*E81 Impulso específico teórico*/ float I_sp_teo(engine_t *engine) { - return velocidad_escape(engine)/GRAVITY; + return calc_escape_vel(engine)/GRAVITY; } /*E82 Impulso específico esperado*/ @@ -103,5 +103,3 @@ float Delta_V_Esp(float list[]) { return delta_V(list); } - -// E85 \ No newline at end of file diff --git a/src/val_termod.c b/src/val_termod.c index 9ce5b19..7faeb50 100644 --- a/src/val_termod.c +++ b/src/val_termod.c @@ -65,13 +65,6 @@ float vel_numero_mach_mt(engine_t *engine) return velocidad_garganta(engine) / vel_sonica_local_at(engine); } -// E62 Velocidad de Escape -float velocidad_escape(engine_t *engine) -{ - return powf(powf(((2.0f * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1.0f) * CONSTANTE_GASES * engine->temperature* (1.0f - (PRESION_ATMOSFERICA /trel_get_pressure_pa(engine) ))), - ((HEAT_CAPACITY_RATIO - 1.0f) / HEAT_CAPACITY_RATIO)), (0.5f)); -} - // E63 Velocidad Sonica Local a2 float vel_sonica_local_a2(engine_t *engine) { @@ -82,7 +75,7 @@ float vel_sonica_local_a2(engine_t *engine) float vel_numero_mach_m2(engine_t *engine) { - return velocidad_escape(engine) / vel_sonica_local_a2(engine); + return calc_escape_vel(engine) / vel_sonica_local_a2(engine); } // E65 Velocidad de escape caracteristica @@ -90,4 +83,3 @@ float vel_escape_caract(engine_t *engine) { return powf(powf((CONSTANTE_GASES * engine->temperature) / (HEAT_CAPACITY_RATIO *(2 / (HEAT_CAPACITY_RATIO + 1))),((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))), (0.5)); } - From 2ace6676a466b9b3e562a4a019b4139458a66cc9 Mon Sep 17 00:00:00 2001 From: lross2k Date: Tue, 13 Dec 2022 03:53:26 -0600 Subject: [PATCH 08/42] Implemented time comp Had to fix a lot of older stuff just to pull the basic calculations There are many new bugs introduced, but before handling them, the testapp has to be improved, my next task will be creating a better testapp, nothing fancy, just something that shows important values to track issues In a future, there might be automation, for now, there must be less bugs, at least - LR --- CMakeLists.txt | 1 + include/comp_tiempo.h | 8 +-- include/design_constants.h | 13 ---- include/design_func.h | 4 ++ include/rendimientos.h | 10 +-- include/rocket.h | 22 +++++++ include/structs.h | 11 +++- src/comp_tiempo.c | 121 +++++++++++++++++++++++++++++-------- src/design_func.c | 8 --- src/engine.c | 8 +++ src/rendimientos.c | 31 ++++------ src/rocket.c | 20 ++++++ 12 files changed, 182 insertions(+), 75 deletions(-) create mode 100644 include/rocket.h create mode 100644 src/rocket.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 8628cb8..aa41c32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ ADD_LIBRARY (TREL ${LIB_TYPE} "src/design_func.c" "src/comp_tiempo.c" "src/design_resist.c" "src/val_termod.c" + "src/rocket.c" ) # Specify the directory to contain header files diff --git a/include/comp_tiempo.h b/include/comp_tiempo.h index ac4e2ad..6741ec2 100644 --- a/include/comp_tiempo.h +++ b/include/comp_tiempo.h @@ -1,10 +1,8 @@ #ifndef COMP_TIEMPO_H #define COMP_TIEMPO_H -//Constructores -float empuje_prom_tiempo(float list[]); -float impulso_tot(float list[]); -float impulso_esp(float list[], engine_t *engine); -float delta_V(float list[]); +#include "design_func.h" + +int trel_run_time_comp_iterations(trel_rocket_t* rocket); #endif //COMP_TIEMPO_H diff --git a/include/design_constants.h b/include/design_constants.h index 66ad347..1be6836 100644 --- a/include/design_constants.h +++ b/include/design_constants.h @@ -14,22 +14,9 @@ #define HEAT_CAPACITY_RATIO 1.043f /* E46 */ #define CONSTANTE_GASES 196.14f /* E47 */ -//Constantes - rendimientos - -//#define cant_granos 4.0f // E29 -//#define ri 0.01f // E30 -//#define re 0.0304f // E31 -//#define Lg 0.1f // E32 -//#define const_a_br 0.005f // E41 -//#define const_n_br 0.688f // E42 -//#define dens_comb 1859.0f // E44 #define FACT_CONV_BR 0.000145038f // factor de conversion para la tasa de quemado #define FACT_CONV_M_TO_I 0.0254f //factor de conversion de metros a pulgadas -//Comportamiento en el tiempo// -// TODO: revisar esto -//#define I_T -//#define I_sp - //Valores termodinámicos// #define escape 0 // E62 float var diff --git a/include/design_func.h b/include/design_func.h index 57ab36f..7d1bbbf 100644 --- a/include/design_func.h +++ b/include/design_func.h @@ -24,5 +24,9 @@ float TREL_EXPORT cortante_promedio(engine_t* engine); float TREL_EXPORT margen_de_seguridad_cortante(engine_t* engine); float TREL_EXPORT aplastamiento(engine_t* engine); float TREL_EXPORT margen_de_seguridad_aplaztamiento(engine_t* engine); +float port_area(engine_t* engine); +float long_secc_combus(engine_t* engine); +float volumen_unitario(engine_t* engine); +float desv_est_grains(engine_t* engine); #endif //DESIGN_FUNC_H diff --git a/include/rendimientos.h b/include/rendimientos.h index bce01b4..0f6ca6b 100644 --- a/include/rendimientos.h +++ b/include/rendimientos.h @@ -20,12 +20,12 @@ double TREL_EXPORT Ab_combustion(engine_t* engine); float TREL_EXPORT br_combustion(engine_t* engine); double TREL_EXPORT mg_combustion(engine_t* engine); double TREL_EXPORT emp_teo(engine_t* engine); -float TREL_EXPORT emp_prom(float list[]); -float TREL_EXPORT emp_max(float list[]); +double TREL_EXPORT emp_prom(trel_rocket_t* rocket); +double TREL_EXPORT emp_max(trel_rocket_t* rocket); float TREL_EXPORT I_sp_teo(engine_t* engine); -float TREL_EXPORT I_sp_esperado(float list[], engine_t *engine); +float TREL_EXPORT I_sp_esperado(engine_t* engine); double TREL_EXPORT I_tot_teo(engine_t* engine); -float TREL_EXPORT I_tot_esp(float list[]); -float TREL_EXPORT Delta_V_Esp(float list[]); +float TREL_EXPORT I_tot_esp(engine_t* engine); +double TREL_EXPORT Delta_V_Esp(trel_rocket_t* rocket); #endif //RENDIMIENTOS_H diff --git a/include/rocket.h b/include/rocket.h new file mode 100644 index 0000000..3c8ca83 --- /dev/null +++ b/include/rocket.h @@ -0,0 +1,22 @@ +#ifndef ROCKET_H +#define ROCKET_H + +#include +#include + +#include "DLLDefines.h" +//#include "design_func.h" +//#include "design_resist.h" +#include "structs.h" + +// TODO: free library structs function + +// Function prototypes +trel_rocket_t TREL_EXPORT* trel_rocket_init( + engine_t* engine, + double telemtry_mass, + double parachute_mass, + double fuselage_mass +); + +#endif//ROCKET_H diff --git a/include/structs.h b/include/structs.h index ef7e049..4c2946f 100644 --- a/include/structs.h +++ b/include/structs.h @@ -43,7 +43,8 @@ typedef struct screws // Estructura del engine typedef struct engine { - float pressure; // chamber pressure in psi + double egine_mass; + float pressure; // chamber pressure in psi float escape_vel, temperature; float width_condition, margin_of_safety, max_stress, radial_stress; float tangencial_stress, longitudinal_stress, max_pressure; @@ -55,4 +56,12 @@ typedef struct engine comp_area_t* comp_area_values; // ptr to initialized struct } TREL_EXPORT engine_t; +// Struct for the whole rocket simulation +typedef struct trel_rocket +{ + engine_t *engine; // ptr to initialized struct + double telemtry_mass, parachute_mass, fuselage_mass; + double avg_thrust, max_thrust, delta_v; // Comportamiento en el Tiempo!AD3511 +} TREL_EXPORT trel_rocket_t; + #endif //TREL_STRUCTS_H diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index 789d65b..d398164 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -1,7 +1,7 @@ -//Proms_Comportamiento_Tiempo// -#include "rendimientos.h" +#include "comp_tiempo.h" //Lista definida desde AB32:AB1149 +/* float empuje_prom_tiempo(float list[]) { float prom_final = 0; @@ -16,33 +16,106 @@ float empuje_prom_tiempo(float list[]) } prom_final = prom_final / cont; return prom_final; -} +}*/ -//E25 -float impulso_tot(float list[]) +//Comportamiento en el Tiempo!AD3511 +int trel_run_time_comp_iterations(trel_rocket_t* rocket) { - float IT = 0; - for (long unsigned int n = 0; n < sizeof(*list); n++) - { - IT = IT + list[n]; - } - return IT; -} + engine_t* engine = rocket->engine; -//E26 -float impulso_esp(float list[], engine_t *engine) -{ - return impulso_tot(list) / (masa_combustible(engine) * 9.81f); -} + // iterate over given iterations and step size + const float max_iterations = 1000.0f; + const float burn_time = Tiempo_quemado_combustion(engine); + const float step = burn_time / max_iterations; + float time = 0.0f; // time in seconds -//AD3511 -float delta_V(float list[]) -{ - float DeltaV = 0; - for (long unsigned int n = 0; n < sizeof(*list); n++) + // variables for iteration + double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemtry_mass + engine->egine_mass; + const float delta_t = step; + float pressure_abs = PRESION_ATMOSFERICA; + float pressure = pressure_abs / 6894.757f; + float pressure_man = pressure_abs - PRESION_ATMOSFERICA; + float burn_rate = engine->fuel->const_burn_rate * powf(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; + float inst_radius = engine->grains->init_inter_radius; + float inst_long = engine->grains->longitude; + float trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; + float long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; + float burn_area = trans_area * 2 + long_area; + float total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; + float fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; + float fuel_mass = engine->fuel->density * fuel_vol; + float produced_mass = 0.0f; + float produced_massic_flux = 0.0f; + float escape_massic_flux = 0.0f; + float flux_diff = produced_massic_flux - escape_massic_flux; + float escape_delta_m = 0.0f; + float free_vol = port_area(engine) - fuel_vol; + float internal_mass = (free_vol * pressure_abs) / (long_secc_combus(engine) * engine->fuel->const_burn_rate); + float areas_ratio = total_burn_area / engine->grains->longitude; + float virtual_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (engine->tube->ult_stress_tension / (engine->tube->ult_stress_tension - 1))); + float real_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); + float force_coeff = powf((((2 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1)) * powf((2 / (HEAT_CAPACITY_RATIO + 1)), ((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))) * ((1 - powf((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))))), 0.5f) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); + float adjusted_cr = force_coeff * desv_est_grains(engine); + float force = adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f; + float total_thrust = 0.0f; + float delta_v = total_thrust / (fuel_mass + (float)dead_mass); + + // sums for eventual result values + double sum_delta_v = delta_v; + double sum_avg_thrust = force; + + // variable for max thrust + double max_thrust = force; + + // iteration loop + for (int it = 0; it < max_iterations; it++) { - DeltaV = DeltaV + list[n]; + pressure_abs = engine->fuel->const_burn_rate * long_secc_combus(engine) * internal_mass / free_vol; + pressure = pressure_abs / 6894.757f; + pressure_man = pressure_abs - PRESION_ATMOSFERICA; + burn_rate = engine->fuel->const_burn_rate * powf(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; + inst_radius = inst_radius > 0 && (inst_radius + burn_rate * delta_t) < engine->grains->extern_radius ? inst_radius + burn_rate * delta_t : 0; + inst_long = inst_radius > 0 ? inst_long - burn_rate * delta_t * 2 : 0; + trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; + long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; + burn_area = trans_area * 2 + long_area; + total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; + fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; + produced_mass = fuel_mass - engine->fuel->density * fuel_vol; + fuel_mass = engine->fuel->density * fuel_vol; + produced_massic_flux = produced_mass / delta_t; + escape_massic_flux = pressure_abs > PRESION_ATMOSFERICA ? engine->grains->longitude * pressure_abs * HEAT_CAPACITY_RATIO * (sqrtf(powf((2.0f / (HEAT_CAPACITY_RATIO + 1.0f)), ((HEAT_CAPACITY_RATIO + 1.0f) / (HEAT_CAPACITY_RATIO - 1.0f))))) / powf((HEAT_CAPACITY_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; + flux_diff = produced_massic_flux - escape_massic_flux; + escape_delta_m = escape_massic_flux * delta_t; + internal_mass = produced_mass + internal_mass - escape_delta_m > 0 ? produced_mass + internal_mass - escape_delta_m : 0.0f; + areas_ratio = total_burn_area / engine->grains->longitude; + virtual_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (engine->tube->ult_stress_tension / (engine->tube->ult_stress_tension - 1))); + real_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); + force_coeff = powf((((2 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1)) * powf((2 / (HEAT_CAPACITY_RATIO + 1)), ((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))) * ((1 - powf((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))))), 0.5f) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); + adjusted_cr = force_coeff * desv_est_grains(engine); + total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2)) * delta_t; + force = adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f; + delta_v = total_thrust / (fuel_mass + (float)dead_mass); + + // step time value + if (time + step < burn_time) + time += step; + else + time = 0; + + // add to delta_v sum and avg thrust + sum_delta_v += delta_v; + sum_avg_thrust += force; + + if (force > max_thrust) + max_thrust = force; } - return DeltaV; + // storing values + rocket->delta_v = sum_delta_v; + rocket->max_thrust = max_thrust; + // calculate avg thrust + rocket->avg_thrust = sum_avg_thrust / max_iterations; + + return(0); } diff --git a/src/design_func.c b/src/design_func.c index 58ae702..6027753 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -1,15 +1,7 @@ #include "design_func.h" /*Hoja de Excel: Diseño*/ -/*Constants*/ -// E9 PRESION_ATMOSFERICA -// E46 'Heat Capacity ratio' HEAT_CAPACITY_RATIO -// E47 CONSTANTE_GASES -// E54 PRESION_ATMOSFERICA -// E162 DIAMETRO_NUCLEO - /*Functions*/ - // E25 returns chamber pressure in pascals float trel_get_pressure_pa(engine_t* engine) { diff --git a/src/engine.c b/src/engine.c index accc543..f55c5f5 100644 --- a/src/engine.c +++ b/src/engine.c @@ -112,6 +112,7 @@ engine_t* trel_engine_init( printf("Engine initialization failed\n"); exit(1); } + engine->egine_mass = 0.0; engine->width_condition = 0.0f; engine->margin_of_safety = 0.0f; engine->max_stress = 0.0f; @@ -168,3 +169,10 @@ float trel_get_escape_vel(engine_t *engine) { return engine->escape_vel; } + +int trel_set_engine_mass(engine_t* engine, double mass) +{ + if (!engine) return(1); + engine->egine_mass = mass; + return(0); +} diff --git a/src/rendimientos.c b/src/rendimientos.c index 2de6b07..49d5b93 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -52,24 +52,17 @@ double emp_teo(engine_t *engine) } /*E76 Empuje promedio esperado*/ -float emp_prom(float list[]) +double emp_prom(trel_rocket_t* rocket) { - return empuje_prom_tiempo(list); + if (!rocket->avg_thrust) + trel_run_time_comp_iterations(rocket); + return(rocket->avg_thrust); } /*E78 Empuje máximo esperado*/ -float emp_max(float list[]) +double emp_max(trel_rocket_t* rocket) { - float max = 0; - long unsigned int n; - for (n = 0; n < sizeof(*list); n++) - { - if (list[n] > max) - { - max = list[n]; - } - } - return max; + return(rocket->max_thrust); } /*Impulsos*/ @@ -81,9 +74,9 @@ float I_sp_teo(engine_t *engine) } /*E82 Impulso específico esperado*/ -float I_sp_esperado(float list[], engine_t *engine) +float I_sp_esperado(engine_t* engine) { - return (float)(impulso_esp(list, engine)/(masa_combustible(engine)*GRAVITY)); + return(engine->fuel->density); } /*E83 Impulso total teórico*/ @@ -93,13 +86,13 @@ double I_tot_teo(engine_t *engine) } /*E84 Impulso total esperado*/ -float I_tot_esp(float list[]) +float I_tot_esp(engine_t* engine) { - return impulso_tot(list); + return(br_combustion(engine)); } /*E85 Delta V esperado*/ -float Delta_V_Esp(float list[]) +double Delta_V_Esp(trel_rocket_t* rocket) { - return delta_V(list); + return(rocket->delta_v); } diff --git a/src/rocket.c b/src/rocket.c new file mode 100644 index 0000000..08769f6 --- /dev/null +++ b/src/rocket.c @@ -0,0 +1,20 @@ +#include "rocket.h" + +/* Initializes all the variables in the rocket struct */ +trel_rocket_t* trel_rocket_init( + engine_t* engine, + double telemtry_mass, + double parachute_mass, + double fuselage_mass) +{ + trel_rocket_t* rocket = (trel_rocket_t*)malloc(sizeof(trel_rocket_t)); + if (!rocket) { + printf("Rocket initialization failed\n"); + exit(1); + } + rocket->telemtry_mass = telemtry_mass; + rocket->parachute_mass = parachute_mass; + rocket->fuselage_mass = fuselage_mass; + rocket->engine = engine; + return(rocket); +} From a9c7833c5dd0a050c442617897068c7bf3daa65d Mon Sep 17 00:00:00 2001 From: lross2k Date: Tue, 13 Dec 2022 04:03:13 -0600 Subject: [PATCH 09/42] Removed unused variables from the iteration Thanks to GCC's warnings, some unused variables were removed, hopefully reducing the hedache of modyfing the routine in the future -LR --- src/comp_tiempo.c | 58 ++++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index d398164..f205e3e 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -1,23 +1,5 @@ #include "comp_tiempo.h" -//Lista definida desde AB32:AB1149 -/* -float empuje_prom_tiempo(float list[]) -{ - float prom_final = 0; - int cont = 0; - for (long unsigned int n = 0; n < sizeof(*list); n++) - { - if (list[n] > 0) - { - prom_final = prom_final + list[n]; - cont += 1; - } - } - prom_final = prom_final / cont; - return prom_final; -}*/ - //Comportamiento en el Tiempo!AD3511 int trel_run_time_comp_iterations(trel_rocket_t* rocket) { @@ -33,27 +15,27 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemtry_mass + engine->egine_mass; const float delta_t = step; float pressure_abs = PRESION_ATMOSFERICA; - float pressure = pressure_abs / 6894.757f; - float pressure_man = pressure_abs - PRESION_ATMOSFERICA; + //float pressure = pressure_abs / 6894.757f; + //float pressure_man = pressure_abs - PRESION_ATMOSFERICA; float burn_rate = engine->fuel->const_burn_rate * powf(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; float inst_radius = engine->grains->init_inter_radius; float inst_long = engine->grains->longitude; - float trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; - float long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; - float burn_area = trans_area * 2 + long_area; - float total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; + //float trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; + //float long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; + //float burn_area = trans_area * 2 + long_area; + //float total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; float fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; float fuel_mass = engine->fuel->density * fuel_vol; float produced_mass = 0.0f; - float produced_massic_flux = 0.0f; + //float produced_massic_flux = 0.0f; float escape_massic_flux = 0.0f; - float flux_diff = produced_massic_flux - escape_massic_flux; + //float flux_diff = produced_massic_flux - escape_massic_flux; float escape_delta_m = 0.0f; float free_vol = port_area(engine) - fuel_vol; float internal_mass = (free_vol * pressure_abs) / (long_secc_combus(engine) * engine->fuel->const_burn_rate); - float areas_ratio = total_burn_area / engine->grains->longitude; + //float areas_ratio = total_burn_area / engine->grains->longitude; float virtual_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (engine->tube->ult_stress_tension / (engine->tube->ult_stress_tension - 1))); - float real_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); + //float real_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); float force_coeff = powf((((2 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1)) * powf((2 / (HEAT_CAPACITY_RATIO + 1)), ((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))) * ((1 - powf((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))))), 0.5f) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); float adjusted_cr = force_coeff * desv_est_grains(engine); float force = adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f; @@ -71,26 +53,26 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) for (int it = 0; it < max_iterations; it++) { pressure_abs = engine->fuel->const_burn_rate * long_secc_combus(engine) * internal_mass / free_vol; - pressure = pressure_abs / 6894.757f; - pressure_man = pressure_abs - PRESION_ATMOSFERICA; + //pressure = pressure_abs / 6894.757f; + //pressure_man = pressure_abs - PRESION_ATMOSFERICA; burn_rate = engine->fuel->const_burn_rate * powf(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; inst_radius = inst_radius > 0 && (inst_radius + burn_rate * delta_t) < engine->grains->extern_radius ? inst_radius + burn_rate * delta_t : 0; inst_long = inst_radius > 0 ? inst_long - burn_rate * delta_t * 2 : 0; - trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; - long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; - burn_area = trans_area * 2 + long_area; - total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; + //trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; + //long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; + //burn_area = trans_area * 2 + long_area; + //total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; produced_mass = fuel_mass - engine->fuel->density * fuel_vol; fuel_mass = engine->fuel->density * fuel_vol; - produced_massic_flux = produced_mass / delta_t; + //produced_massic_flux = produced_mass / delta_t; escape_massic_flux = pressure_abs > PRESION_ATMOSFERICA ? engine->grains->longitude * pressure_abs * HEAT_CAPACITY_RATIO * (sqrtf(powf((2.0f / (HEAT_CAPACITY_RATIO + 1.0f)), ((HEAT_CAPACITY_RATIO + 1.0f) / (HEAT_CAPACITY_RATIO - 1.0f))))) / powf((HEAT_CAPACITY_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; - flux_diff = produced_massic_flux - escape_massic_flux; + //flux_diff = produced_massic_flux - escape_massic_flux; escape_delta_m = escape_massic_flux * delta_t; internal_mass = produced_mass + internal_mass - escape_delta_m > 0 ? produced_mass + internal_mass - escape_delta_m : 0.0f; - areas_ratio = total_burn_area / engine->grains->longitude; + //areas_ratio = total_burn_area / engine->grains->longitude; virtual_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (engine->tube->ult_stress_tension / (engine->tube->ult_stress_tension - 1))); - real_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); + //real_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); force_coeff = powf((((2 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1)) * powf((2 / (HEAT_CAPACITY_RATIO + 1)), ((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))) * ((1 - powf((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))))), 0.5f) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); adjusted_cr = force_coeff * desv_est_grains(engine); total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2)) * delta_t; From 397a794ecd49a96dcc22f6742dcce9526265bf55 Mon Sep 17 00:00:00 2001 From: lross2k Date: Thu, 2 Feb 2023 02:51:43 -0600 Subject: [PATCH 10/42] Fixed a memory access bug --- include/TREL.h | 30 ++++++++++++++++++++++++------ include/comp_area.h | 2 +- include/structs.h | 2 +- main.c | 9 +++++++++ src/comp_area.c | 24 ++++++++++++------------ src/engine.c | 4 ++-- 6 files changed, 49 insertions(+), 22 deletions(-) diff --git a/include/TREL.h b/include/TREL.h index e3de53a..3889db6 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -44,17 +44,27 @@ typedef struct screws // Estructura del engine typedef struct engine { + double egine_mass; float pressure; // chamber pressure in psi - float escape_vel, temperature; + float escape_vel, temperature; float width_condition, margin_of_safety, max_stress, radial_stress; float tangencial_stress, longitudinal_stress, max_pressure; - grains_t* grains; // ptr to intialized struct - fuel_t* fuel; - tubing_t* tube; // ptr to intialized struct - screws_t* screws; // ptr to initialized struct - comp_area_t* comp_area_values; // ptr to initialized struct + float nozzle_efficiency; + grains_t *grains; // ptr to intialized struct + fuel_t *fuel; + tubing_t *tube; // ptr to intialized struct + screws_t *screws; // ptr to initialized struct + comp_area_t *comp_area_values; // ptr to initialized struct } engine_t; +// Estructura del cohete +typedef struct trel_rocket +{ + engine_t *engine; // ptr to initialized struct + double telemtry_mass, parachute_mass, fuselage_mass; + double avg_thrust, max_thrust, delta_v; // Comportamiento en el Tiempo!AD3511 +} trel_rocket_t; + // Function prototypes grains_t *trel_grains_init( unsigned int amount, /* E29 */ @@ -92,6 +102,12 @@ engine_t *trel_engine_init( tubing_t* tube, screws_t* screws ); +trel_rocket_t *trel_rocket_init( + engine_t* engine, + double telemtry_mass, + double parachute_mass, + double fuselage_mass +); float trel_psi_to_pa(float psi); void trel_set_pressure(engine_t* engine, float pressure); void trel_set_escape_vel(engine_t* engine, float vel); @@ -99,5 +115,7 @@ float trel_get_escape_vel(engine_t* engine); float trel_get_pressure(engine_t* engine); float trel_temper_garganta(engine_t *engine); float br_combustion(engine_t *engine); +int trel_run_area_comp_iterations(engine_t **engine); +int trel_run_time_comp_iterations(trel_rocket_t* rocket); #endif //TREL_H diff --git a/include/comp_area.h b/include/comp_area.h index 4a86a73..fb73561 100644 --- a/include/comp_area.h +++ b/include/comp_area.h @@ -4,6 +4,6 @@ #include "design_func.h" // Function prototypes -int TREL_EXPORT trel_run_area_comp_iterations(engine_t* engine); +int TREL_EXPORT trel_run_area_comp_iterations(engine_t **engine); #endif //COMP_AREA_H diff --git a/include/structs.h b/include/structs.h index 4c2946f..afb18dd 100644 --- a/include/structs.h +++ b/include/structs.h @@ -53,7 +53,7 @@ typedef struct engine fuel_t *fuel; tubing_t *tube; // ptr to intialized struct screws_t *screws; // ptr to initialized struct - comp_area_t* comp_area_values; // ptr to initialized struct + comp_area_t *comp_area_values; // ptr to initialized struct } TREL_EXPORT engine_t; // Struct for the whole rocket simulation diff --git a/main.c b/main.c index 44d5adb..964fe25 100644 --- a/main.c +++ b/main.c @@ -46,6 +46,13 @@ void print_debug_example() created_tube, created_screw ); + // Se inicializa el cohete + trel_rocket_t* testing_rocket = trel_rocket_init( + testing_engine, + 10.0, + 10.0, + 10.0 + ); // Acceder a memoria inicializada printf("La presion del motor dada es %f psi\n",trel_get_pressure(testing_engine)); @@ -61,6 +68,8 @@ void print_debug_example() // Usando una funcion de val_termod.c printf("\nLa temperatura en garganta es de %f\n", trel_temper_garganta(testing_engine)); + trel_run_time_comp_iterations(testing_rocket); // No recuerdo si es necesario jaja, mañana lo pruebo + // Probando valores generados por comp_area.c printf("\nArea longitudinal promedio: %f\n", testing_engine->comp_area_values->avg_long_area); printf("Area quemado promedio: %f\n", testing_engine->comp_area_values->avg_burn_area); diff --git a/src/comp_area.c b/src/comp_area.c index 5bf8c80..419c1ee 100644 --- a/src/comp_area.c +++ b/src/comp_area.c @@ -1,17 +1,18 @@ #include "comp_area.h" +#include "stdio.h" // TODO: Evaluate if we need dynamic allocation for this iteration -int trel_run_area_comp_iterations(engine_t* engine) +int trel_run_area_comp_iterations(engine_t **engine) { // iterate over given iterations and step size const float max_iterations = 1000.0f; - const float burn_time = Tiempo_quemado_combustion(engine); + const float burn_time = Tiempo_quemado_combustion(*engine); const float step = burn_time/max_iterations; float time = 0.0f; // time in seconds // burn rate value from propellant - const float burn_rate = br_combustion(engine); + const float burn_rate = br_combustion(*engine); // variables for iteration float inst_radius = 0.0f; @@ -35,13 +36,13 @@ int trel_run_area_comp_iterations(engine_t* engine) for (int it = 0; it < max_iterations; it++) { // instantaneous grain radius step - inst_radius = engine->grains->init_inter_radius + burn_rate * time; + inst_radius = (*engine)->grains->init_inter_radius + burn_rate * time; // instantaneous grain longitude step - inst_long = engine->grains->longitude - 2.0f * burn_rate * time; + inst_long = (*engine)->grains->longitude - 2.0f * burn_rate * time; // add to transversal area sum - transversal_area = TREL_PI * (engine->grains->extern_radius*engine->grains->extern_radius - + transversal_area = TREL_PI * ((*engine)->grains->extern_radius*(*engine)->grains->extern_radius - inst_radius * inst_radius); sum_transversal_area += transversal_area; @@ -65,15 +66,15 @@ int trel_run_area_comp_iterations(engine_t* engine) // calculate average transversal area transversal_area = sum_transversal_area / max_iterations; - engine->comp_area_values->avg_trans_area = transversal_area; + (*engine)->comp_area_values->avg_trans_area = transversal_area; // calculate average longitudinal area longitudinal_area = sum_longitudinal_area / max_iterations; - engine->comp_area_values->avg_long_area = longitudinal_area; + (*engine)->comp_area_values->avg_long_area = longitudinal_area; // calculate average burn area burn_area = sum_burn_area / max_iterations; - engine->comp_area_values->avg_burn_area = burn_area; + (*engine)->comp_area_values->avg_burn_area = burn_area; // calculate the difference of each iteration's burn area and the average burn area for (int i = 0; i < max_iterations; i++) @@ -88,7 +89,7 @@ int trel_run_area_comp_iterations(engine_t* engine) // get the square root of the avg burn_std_deviation = sqrt(burn_std_deviation); // store the value in the engine - engine->comp_area_values->burn_std_deviation = burn_std_deviation; + (*engine)->comp_area_values->burn_std_deviation = burn_std_deviation; // then sum all the greater than zero differences, store them to a variable, and // sum all the lower than zero differences, store them to another variable @@ -102,7 +103,6 @@ int trel_run_area_comp_iterations(engine_t* engine) lt_differences += burn_area_array[i]; // calculate the difference of burn area by adding the two sums - engine->comp_area_values->burn_sum_diff = gt_differences + lt_differences; - + (*engine)->comp_area_values->burn_sum_diff = gt_differences + lt_differences; return(0); // everything went well } diff --git a/src/engine.c b/src/engine.c index f55c5f5..1e5c980 100644 --- a/src/engine.c +++ b/src/engine.c @@ -100,7 +100,7 @@ engine_t* trel_engine_init( tubing_t *tube, screws_t *screws) { - comp_area_t* comp_area_values = (comp_area_t*) malloc(sizeof(comp_area_t)); + comp_area_t *comp_area_values = (comp_area_t*) malloc(sizeof(comp_area_t)); comp_area_values->avg_burn_area = 0.0f; comp_area_values->avg_long_area = 0.0f; comp_area_values->avg_trans_area = 0.0f; @@ -135,7 +135,7 @@ engine_t* trel_engine_init( trel_width_cutting_segment(engine); trel_area_per_screw(engine); trel_tangencial_stress(engine); - if (trel_run_area_comp_iterations(engine)) // 1 means there was an error + if (trel_run_area_comp_iterations(&engine)) // 1 means there was an error { return(NULL); // NULL means the engine couldn't be properly generated } From fc84d44b79abb46a9c753921a1df023c0bad19b5 Mon Sep 17 00:00:00 2001 From: lross2k Date: Thu, 2 Feb 2023 11:24:30 -0600 Subject: [PATCH 11/42] Small fix for Windows compilation --- TODO.md | 10 +--------- include/comp_tiempo.h | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/TODO.md b/TODO.md index 3b9d57d..d16acec 100644 --- a/TODO.md +++ b/TODO.md @@ -1,12 +1,4 @@ # TODO list for the project -1. Complete dependency tree as much as possible in order. +1. Update the main function to test comp tiempo, and debug everything 2. Change float to double precission for most of the calculations 3. Implement Python library interface - -## Dependency tree -- "Comportamiento en el tiempo" -- "Diseño" - - [ ] Comportamiento en el tiempo AD3511 -- "Simulador de Altitud" - - [ ] Diseño E85 - - [ ] Comportamiento en el tiempo AD3511 diff --git a/include/comp_tiempo.h b/include/comp_tiempo.h index 6741ec2..7014888 100644 --- a/include/comp_tiempo.h +++ b/include/comp_tiempo.h @@ -3,6 +3,6 @@ #include "design_func.h" -int trel_run_time_comp_iterations(trel_rocket_t* rocket); +int TREL_EXPORT trel_run_time_comp_iterations(trel_rocket_t* rocket); #endif //COMP_TIEMPO_H From a0dab7fbd0f7fd8a0cc2f720a7f72e5e768dcb70 Mon Sep 17 00:00:00 2001 From: lross2k Date: Thu, 2 Feb 2023 16:41:47 -0600 Subject: [PATCH 12/42] Finished implementing comp_tiempo.c There were many changes, some of these are - Finished debugging the comp tiempo calculations - Changed all calculations from float to double types - Cleaned the process of initialization Not much more missing as the TODO states --- TODO.md | 4 +- include/TREL.h | 92 ++++++++++++++++++------------------ include/design_constants.h | 6 +-- include/design_func.h | 34 +++++++------- include/engine.h | 47 ++++++++++--------- include/rendimientos.h | 14 +++--- include/rocket.h | 8 ++-- include/structs.h | 36 +++++++------- include/val_termod.h | 26 +++++------ main.c | 16 +++++-- src/comp_area.c | 34 +++++++------- src/comp_tiempo.c | 96 +++++++++++++++++++++----------------- src/design_func.c | 78 +++++++++++++++---------------- src/design_resist.c | 20 ++++---- src/engine.c | 51 ++++++++++---------- src/rendimientos.c | 20 ++++---- src/rocket.c | 12 +++-- src/val_termod.c | 42 ++++++++--------- 18 files changed, 331 insertions(+), 305 deletions(-) diff --git a/TODO.md b/TODO.md index d16acec..19a063f 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,4 @@ # TODO list for the project -1. Update the main function to test comp tiempo, and debug everything -2. Change float to double precission for most of the calculations +1. Implement height simulation +2. Implement memory free functions, and destroy all pointers 3. Implement Python library interface diff --git a/include/TREL.h b/include/TREL.h index 3889db6..3988cbb 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -12,44 +12,44 @@ typedef struct { // Propellent grains structure typedef struct { unsigned int amount; - float init_inter_radius, extern_radius, longitude, grain_separation; + double init_inter_radius, extern_radius, longitude, grain_separation; } grains_t; /* Fuel structure */ typedef struct { - float const_burn_rate, pressure_exponent, density, burn_rate; + double const_burn_rate, pressure_exponent, density, burn_rate; } fuel_t; // Estructura para tuberia typedef struct tubing { char* material; - float diameter_ext, wall_thickness, internal_radius; - float young_module, sector_angle, mean_tubing_diameter; - float shear_stress_tension, shear_stress_pressure; - float ult_stress_tension, ult_stress_pressure; - float transversal_area, material_area; + double diameter_ext, wall_thickness, internal_radius; + double young_module, sector_angle, mean_tubing_diameter; + double shear_stress_tension, shear_stress_pressure; + double ult_stress_tension, ult_stress_pressure; + double transversal_area, material_area; } tubing_t; // Estructura para tornillos typedef struct screws { - float diameter, dist_center_wall; + double diameter, dist_center_wall; unsigned int amount; - float area_per_screw, screw_occupied_area; + double area_per_screw, screw_occupied_area; char* material; - float width_cutting_segment; + double width_cutting_segment; } screws_t; // Estructura del engine typedef struct engine { - double egine_mass; - float pressure; // chamber pressure in psi - float escape_vel, temperature; - float width_condition, margin_of_safety, max_stress, radial_stress; - float tangencial_stress, longitudinal_stress, max_pressure; - float nozzle_efficiency; + double engine_mass; + double pressure; // chamber pressure in psi + double escape_vel, temperature; + double width_condition, margin_of_safety, max_stress, radial_stress; + double tangencial_stress, longitudinal_stress, max_pressure; + double nozzle_efficiency; grains_t *grains; // ptr to intialized struct fuel_t *fuel; tubing_t *tube; // ptr to intialized struct @@ -61,42 +61,43 @@ typedef struct engine typedef struct trel_rocket { engine_t *engine; // ptr to initialized struct - double telemtry_mass, parachute_mass, fuselage_mass; - double avg_thrust, max_thrust, delta_v; // Comportamiento en el Tiempo!AD3511 + double telemtry_mass, parachute_mass, fuselage_mass, payload_mass; + double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 } trel_rocket_t; // Function prototypes grains_t *trel_grains_init( unsigned int amount, /* E29 */ - float internal_radius, /* E30 */ - float external_radius, /* E31 */ - float longitude, /* E32 */ - float grain_separation /* E33 */ + double internal_radius, /* E30 */ + double external_radius, /* E31 */ + double longitude, /* E32 */ + double grain_separation /* E33 */ ); fuel_t *trel_fuel_init( - float const_burn_rate, /* E41 */ - float pressure_exponent,/* E42 */ - float density /* E44 */ + double const_burn_rate, /* E41 */ + double pressure_exponent,/* E42 */ + double density /* E44 */ ); tubing_t *trel_tubing_init( char* material, /* E16 & E17 */ - float diameter, /* E13 */ - float thickness, /* E14 */ - float young_module, /* E18 */ - float shear_tension, /* E20 */ - float shear_pressure, /* E19 */ - float ult_tension, /* E22 */ - float ult_pressure /* E21 */ + double diameter, /* E13 */ + double thickness, /* E14 */ + double young_module, /* E18 */ + double shear_tension, /* E20 */ + double shear_pressure, /* E19 */ + double ult_tension, /* E22 */ + double ult_pressure /* E21 */ ); screws_t *trel_screws_init( char* material, unsigned int amount, - float diameter, - float dist_center_wall + double diameter, + double dist_center_wall ); engine_t *trel_engine_init( - float pressure, /* E24 */ - float temperature, /* E26 */ + double pressure, /* E24 */ + double temperature, /* E26 */ + double engine_mass, grains_t* grains, fuel_t* fuel, tubing_t* tube, @@ -104,17 +105,18 @@ engine_t *trel_engine_init( ); trel_rocket_t *trel_rocket_init( engine_t* engine, - double telemtry_mass, + double telemetry_mass, double parachute_mass, - double fuselage_mass + double fuselage_mass, + double payload_mass ); -float trel_psi_to_pa(float psi); -void trel_set_pressure(engine_t* engine, float pressure); -void trel_set_escape_vel(engine_t* engine, float vel); -float trel_get_escape_vel(engine_t* engine); -float trel_get_pressure(engine_t* engine); -float trel_temper_garganta(engine_t *engine); -float br_combustion(engine_t *engine); +double trel_psi_to_pa(double psi); +void trel_set_pressure(engine_t* engine, double pressure); +void trel_set_escape_vel(engine_t* engine, double vel); +double trel_get_escape_vel(engine_t* engine); +double trel_get_pressure(engine_t* engine); +double trel_temper_garganta(engine_t *engine); +double br_combustion(engine_t *engine); int trel_run_area_comp_iterations(engine_t **engine); int trel_run_time_comp_iterations(trel_rocket_t* rocket); diff --git a/include/design_constants.h b/include/design_constants.h index 1be6836..d3d0977 100644 --- a/include/design_constants.h +++ b/include/design_constants.h @@ -2,10 +2,10 @@ #define TREL_CONSTANTS //Constantes matemáticas -#define TREL_PI 3.141592f // PI +#define TREL_PI 3.1415926535898 // PI //Recamara -#define press_Pa (800.0f*6894.757f) //E25 float input +#define press_Pa (800.0f*6894.757f) //E25 double input //Constantes Físicas #define GRAVITY 9.81f // g @@ -18,6 +18,6 @@ #define FACT_CONV_M_TO_I 0.0254f //factor de conversion de metros a pulgadas //Valores termodinámicos// -#define escape 0 // E62 float var +#define escape 0 // E62 double var #endif // TREL_CONSTANTS diff --git a/include/design_func.h b/include/design_func.h index 7d1bbbf..758b5c7 100644 --- a/include/design_func.h +++ b/include/design_func.h @@ -11,22 +11,22 @@ #include "DLLDefines.h" // Function prototypes -float TREL_EXPORT trel_get_pressure_pa(engine_t* engine); -float TREL_EXPORT volumen_camara(engine_t* engine); -float TREL_EXPORT calc_escape_vel(engine_t *engine); -float TREL_EXPORT area_interna(engine_t* engine); -float TREL_EXPORT fuerza_maxima(engine_t* engine); -float TREL_EXPORT fuerza_tornillo(engine_t* engine); -float TREL_EXPORT tension(engine_t* engine); -float TREL_EXPORT margen_de_seguridad(engine_t* engine); -float TREL_EXPORT area_de_cortante(engine_t* engine); -float TREL_EXPORT cortante_promedio(engine_t* engine); -float TREL_EXPORT margen_de_seguridad_cortante(engine_t* engine); -float TREL_EXPORT aplastamiento(engine_t* engine); -float TREL_EXPORT margen_de_seguridad_aplaztamiento(engine_t* engine); -float port_area(engine_t* engine); -float long_secc_combus(engine_t* engine); -float volumen_unitario(engine_t* engine); -float desv_est_grains(engine_t* engine); +double trel_get_pressure_pa(engine_t* engine); +double volumen_camara(engine_t* engine); +double calc_escape_vel(engine_t *engine); +double area_interna(engine_t* engine); +double fuerza_maxima(engine_t* engine); +double fuerza_tornillo(engine_t* engine); +double tension(engine_t* engine); +double margen_de_seguridad(engine_t* engine); +double area_de_cortante(engine_t* engine); +double cortante_promedio(engine_t* engine); +double margen_de_seguridad_cortante(engine_t* engine); +double aplastamiento(engine_t* engine); +double margen_de_seguridad_aplaztamiento(engine_t* engine); +double port_area(engine_t* engine); +double long_secc_combus(engine_t* engine); +double volumen_unitario(engine_t* engine); +double desv_est_grains(engine_t* engine); #endif //DESIGN_FUNC_H diff --git a/include/engine.h b/include/engine.h index 14acf98..85f90be 100644 --- a/include/engine.h +++ b/include/engine.h @@ -16,44 +16,45 @@ // Function prototypes grains_t TREL_EXPORT *trel_grains_init( unsigned int amount, /* E29 */ - float internal_radius, /* E30 */ - float external_radius, /* E31 */ - float longitude, /* E32 */ - float grain_separation /* E33 */ + double internal_radius, /* E30 */ + double external_radius, /* E31 */ + double longitude, /* E32 */ + double grain_separation /* E33 */ ); fuel_t TREL_EXPORT *trel_fuel_init( - float const_burn_rate, /* E41 */ - float pressure_exponent,/* E42 */ - float density /* E44 */ + double const_burn_rate, /* E41 */ + double pressure_exponent,/* E42 */ + double density /* E44 */ ); tubing_t TREL_EXPORT *trel_tubing_init( char *material, /* E16 & E17 */ - float diameter, /* E13 */ - float thickness, /* E14 */ - float young_module, /* E18 */ - float shear_tension, /* E20 */ - float shear_pressure, /* E19 */ - float ult_tension, /* E22 */ - float ult_pressure /* E21 */ + double diameter, /* E13 */ + double thickness, /* E14 */ + double young_module, /* E18 */ + double shear_tension, /* E20 */ + double shear_pressure, /* E19 */ + double ult_tension, /* E22 */ + double ult_pressure /* E21 */ ); screws_t TREL_EXPORT* trel_screws_init( char* material, unsigned int amount, - float diameter, - float dist_center_wall + double diameter, + double dist_center_wall ); engine_t TREL_EXPORT* trel_engine_init( - float pressure, /* E24 */ - float temperature, /* E26 */ + double pressure, /* E24 */ + double temperature, /* E26 */ + double engine_mass, grains_t* grains, fuel_t* fuel, tubing_t* tube, screws_t* screws ); -float TREL_EXPORT trel_psi_to_pa(float psi); -void TREL_EXPORT trel_set_pressure(engine_t *engine, float pressure); -void TREL_EXPORT trel_set_escape_vel(engine_t *engine, float vel); -float TREL_EXPORT trel_get_escape_vel(engine_t *engine); -float TREL_EXPORT trel_get_pressure(engine_t *engine); +double TREL_EXPORT trel_psi_to_pa(double psi); +void TREL_EXPORT trel_set_pressure(engine_t *engine, double pressure); +void TREL_EXPORT trel_set_escape_vel(engine_t *engine, double vel); +double TREL_EXPORT trel_get_escape_vel(engine_t *engine); +double TREL_EXPORT trel_get_pressure(engine_t *engine); #endif // ENGINE_H diff --git a/include/rendimientos.h b/include/rendimientos.h index 0f6ca6b..c74495d 100644 --- a/include/rendimientos.h +++ b/include/rendimientos.h @@ -13,19 +13,19 @@ // TODO: cambiar list[] por nombres mas descriptivos // Function prototypes -float TREL_EXPORT volumen_combustible(engine_t *engine); -float TREL_EXPORT masa_combustible(engine_t* engine); -float TREL_EXPORT Tiempo_quemado_combustion(engine_t* engine); +double TREL_EXPORT volumen_combustible(engine_t *engine); +double TREL_EXPORT masa_combustible(engine_t* engine); +double TREL_EXPORT Tiempo_quemado_combustion(engine_t* engine); double TREL_EXPORT Ab_combustion(engine_t* engine); -float TREL_EXPORT br_combustion(engine_t* engine); +double TREL_EXPORT br_combustion(engine_t* engine); double TREL_EXPORT mg_combustion(engine_t* engine); double TREL_EXPORT emp_teo(engine_t* engine); double TREL_EXPORT emp_prom(trel_rocket_t* rocket); double TREL_EXPORT emp_max(trel_rocket_t* rocket); -float TREL_EXPORT I_sp_teo(engine_t* engine); -float TREL_EXPORT I_sp_esperado(engine_t* engine); +double TREL_EXPORT I_sp_teo(engine_t* engine); +double TREL_EXPORT I_sp_esperado(engine_t* engine); double TREL_EXPORT I_tot_teo(engine_t* engine); -float TREL_EXPORT I_tot_esp(engine_t* engine); +double TREL_EXPORT I_tot_esp(engine_t* engine); double TREL_EXPORT Delta_V_Esp(trel_rocket_t* rocket); #endif //RENDIMIENTOS_H diff --git a/include/rocket.h b/include/rocket.h index 3c8ca83..2dba279 100644 --- a/include/rocket.h +++ b/include/rocket.h @@ -5,8 +5,7 @@ #include #include "DLLDefines.h" -//#include "design_func.h" -//#include "design_resist.h" +#include "design_func.h" #include "structs.h" // TODO: free library structs function @@ -14,9 +13,10 @@ // Function prototypes trel_rocket_t TREL_EXPORT* trel_rocket_init( engine_t* engine, - double telemtry_mass, + double telemetry_mass, double parachute_mass, - double fuselage_mass + double fuselage_mass, + double payload_mass ); #endif//ROCKET_H diff --git a/include/structs.h b/include/structs.h index afb18dd..704644a 100644 --- a/include/structs.h +++ b/include/structs.h @@ -11,44 +11,44 @@ typedef struct { // Propellent grains structure typedef struct { unsigned int amount; - float init_inter_radius, extern_radius, longitude, grain_separation; + double init_inter_radius, extern_radius, longitude, grain_separation; } TREL_EXPORT grains_t; /* Fuel structure */ typedef struct { - float const_burn_rate, pressure_exponent, density, burn_rate; + double const_burn_rate, pressure_exponent, density, burn_rate; } TREL_EXPORT fuel_t; // Estructura para tuberia typedef struct tubing { char *material; - float diameter_ext, wall_thickness, internal_radius; - float young_module, sector_angle, mean_tubing_diameter; - float shear_stress_tension, shear_stress_pressure; - float ult_stress_tension, ult_stress_pressure; - float transversal_area, material_area; + double diameter_ext, wall_thickness, internal_radius; + double young_module, sector_angle, mean_tubing_diameter; + double shear_stress_tension, shear_stress_pressure; + double ult_stress_tension, ult_stress_pressure; + double transversal_area, material_area; } TREL_EXPORT tubing_t; // Estructura para tornillos typedef struct screws { - float diameter, dist_center_wall; + double diameter, dist_center_wall; unsigned int amount; - float area_per_screw, screw_occupied_area; + double area_per_screw, screw_occupied_area; char *material; - float width_cutting_segment; + double width_cutting_segment; } TREL_EXPORT screws_t; // Estructura del engine typedef struct engine { - double egine_mass; - float pressure; // chamber pressure in psi - float escape_vel, temperature; - float width_condition, margin_of_safety, max_stress, radial_stress; - float tangencial_stress, longitudinal_stress, max_pressure; - float nozzle_efficiency; + double engine_mass; + double pressure; // chamber pressure in psi + double escape_vel, temperature; + double width_condition, margin_of_safety, max_stress, radial_stress; + double tangencial_stress, longitudinal_stress, max_pressure; + double nozzle_efficiency; grains_t *grains; // ptr to intialized struct fuel_t *fuel; tubing_t *tube; // ptr to intialized struct @@ -60,8 +60,8 @@ typedef struct engine typedef struct trel_rocket { engine_t *engine; // ptr to initialized struct - double telemtry_mass, parachute_mass, fuselage_mass; - double avg_thrust, max_thrust, delta_v; // Comportamiento en el Tiempo!AD3511 + double telemetry_mass, parachute_mass, fuselage_mass, payload_mass; + double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 } TREL_EXPORT trel_rocket_t; #endif //TREL_STRUCTS_H diff --git a/include/val_termod.h b/include/val_termod.h index b375f0a..03486c6 100644 --- a/include/val_termod.h +++ b/include/val_termod.h @@ -9,17 +9,17 @@ #include "structs.h" // Prototipos de funciones -float TREL_EXPORT vol_esp_cam_combustion(engine_t* engine); -float TREL_EXPORT vol_esp_garganta(engine_t* engine); -float TREL_EXPORT vol_esp_escape(engine_t* engine); -float TREL_EXPORT presion_garganta(engine_t* engine); -//float TREL_EXPORT presion_escape(engine_t* engine); // TODO -float TREL_EXPORT trel_temper_garganta(engine_t *engine); -//float TREL_EXPORT temper_escape(engine_t* engine); -float TREL_EXPORT velocidad_garganta(engine_t* engine); -float TREL_EXPORT vel_sonica_local_at(engine_t* engine); -float TREL_EXPORT vel_numero_mach_mt(engine_t* engine); -float TREL_EXPORT vel_sonica_local_a2(engine_t* engine); -float TREL_EXPORT vel_numero_mach_m2(engine_t* engine); -float TREL_EXPORT vel_escape_caract(engine_t* engine); +double TREL_EXPORT vol_esp_cam_combustion(engine_t* engine); +double TREL_EXPORT vol_esp_garganta(engine_t* engine); +double TREL_EXPORT vol_esp_escape(engine_t* engine); +double TREL_EXPORT presion_garganta(engine_t* engine); +//double TREL_EXPORT presion_escape(engine_t* engine); // TODO +double TREL_EXPORT trel_temper_garganta(engine_t *engine); +//double TREL_EXPORT temper_escape(engine_t* engine); +double TREL_EXPORT velocidad_garganta(engine_t* engine); +double TREL_EXPORT vel_sonica_local_at(engine_t* engine); +double TREL_EXPORT vel_numero_mach_mt(engine_t* engine); +double TREL_EXPORT vel_sonica_local_a2(engine_t* engine); +double TREL_EXPORT vel_numero_mach_m2(engine_t* engine); +double TREL_EXPORT vel_escape_caract(engine_t* engine); #endif // VAL_TERMOD_FUNC_H diff --git a/main.c b/main.c index 964fe25..2278ca6 100644 --- a/main.c +++ b/main.c @@ -41,6 +41,7 @@ void print_debug_example() engine_t* testing_engine = trel_engine_init( 800, 1710, + 1.5, created_grains, created_fuel, created_tube, @@ -49,9 +50,10 @@ void print_debug_example() // Se inicializa el cohete trel_rocket_t* testing_rocket = trel_rocket_init( testing_engine, - 10.0, - 10.0, - 10.0 + 0.3, + 0.05, + 5.0, + 1.5 ); // Acceder a memoria inicializada @@ -68,14 +70,18 @@ void print_debug_example() // Usando una funcion de val_termod.c printf("\nLa temperatura en garganta es de %f\n", trel_temper_garganta(testing_engine)); - trel_run_time_comp_iterations(testing_rocket); // No recuerdo si es necesario jaja, mañana lo pruebo - // Probando valores generados por comp_area.c printf("\nArea longitudinal promedio: %f\n", testing_engine->comp_area_values->avg_long_area); printf("Area quemado promedio: %f\n", testing_engine->comp_area_values->avg_burn_area); printf("Area transversal promedio: %f\n", testing_engine->comp_area_values->avg_trans_area); printf("Suma de diferencia quemado: %f\n", testing_engine->comp_area_values->burn_sum_diff); printf("Desviacion estandar area de quemado: %f\n", testing_engine->comp_area_values->burn_std_deviation); + + // Probando valores generados por comp_tiempo.c + printf("\nDelta V total: %lf\n", testing_rocket->delta_v); + printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); + printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); + printf("Maxima presion: %lf\n", testing_rocket->max_pressure); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica diff --git a/src/comp_area.c b/src/comp_area.c index 419c1ee..f96dd40 100644 --- a/src/comp_area.c +++ b/src/comp_area.c @@ -6,28 +6,28 @@ int trel_run_area_comp_iterations(engine_t **engine) { // iterate over given iterations and step size - const float max_iterations = 1000.0f; - const float burn_time = Tiempo_quemado_combustion(*engine); - const float step = burn_time/max_iterations; - float time = 0.0f; // time in seconds + const double max_iterations = 1000.0f; + const double burn_time = Tiempo_quemado_combustion(*engine); + const double step = burn_time/max_iterations; + double time = 0.0f; // time in seconds // burn rate value from propellant - const float burn_rate = br_combustion(*engine); + const double burn_rate = br_combustion(*engine); // variables for iteration - float inst_radius = 0.0f; - float inst_long = 0.0f; + double inst_radius = 0.0f; + double inst_long = 0.0f; // sums for eventual average value - float transversal_area = 0.0f; - float sum_transversal_area = 0.0f; - float longitudinal_area = 0.0f; - float sum_longitudinal_area = 0.0f; - float burn_area = 0.0f; - float sum_burn_area = 0.0f; + double transversal_area = 0.0; + double sum_transversal_area = 0.0; + double longitudinal_area = 0.0; + double sum_longitudinal_area = 0.0; + double burn_area = 0.0; + double sum_burn_area = 0.0; // array for final values calculation - float burn_area_array[1000]; + double burn_area_array[1000]; // initialize array for (int i = 0; i < 1000; i++) burn_area_array[i] = 0.0f; @@ -48,7 +48,7 @@ int trel_run_area_comp_iterations(engine_t **engine) // add to longitudinal area sum if (!transversal_area) - longitudinal_area = 0.0f; + longitudinal_area = 0.0; else longitudinal_area = 2 * TREL_PI * inst_radius * inst_long; sum_longitudinal_area += longitudinal_area; @@ -93,8 +93,8 @@ int trel_run_area_comp_iterations(engine_t **engine) // then sum all the greater than zero differences, store them to a variable, and // sum all the lower than zero differences, store them to another variable - float lt_differences = 0.0f; // lower - float gt_differences = 0.0f; // greater + double lt_differences = 0.0f; // lower + double gt_differences = 0.0f; // greater for (int i = 0; i < max_iterations; i++) if (burn_area_array[i] > 0) diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index f205e3e..c0b777a 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -4,43 +4,45 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) { engine_t* engine = rocket->engine; - + // iterate over given iterations and step size - const float max_iterations = 1000.0f; - const float burn_time = Tiempo_quemado_combustion(engine); - const float step = burn_time / max_iterations; - float time = 0.0f; // time in seconds + const double max_iterations = 1000.0; + const double burn_time = Tiempo_quemado_combustion(engine); + //const double step = burn_time / max_iterations; // DEBUG + const double step = 0.15999891; + double time = 0.0; // time in seconds + double last_force = 0.0; // variables for iteration - double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemtry_mass + engine->egine_mass; - const float delta_t = step; - float pressure_abs = PRESION_ATMOSFERICA; - //float pressure = pressure_abs / 6894.757f; - //float pressure_man = pressure_abs - PRESION_ATMOSFERICA; - float burn_rate = engine->fuel->const_burn_rate * powf(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; - float inst_radius = engine->grains->init_inter_radius; - float inst_long = engine->grains->longitude; - //float trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; - //float long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; - //float burn_area = trans_area * 2 + long_area; - //float total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; - float fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; - float fuel_mass = engine->fuel->density * fuel_vol; - float produced_mass = 0.0f; - //float produced_massic_flux = 0.0f; - float escape_massic_flux = 0.0f; - //float flux_diff = produced_massic_flux - escape_massic_flux; - float escape_delta_m = 0.0f; - float free_vol = port_area(engine) - fuel_vol; - float internal_mass = (free_vol * pressure_abs) / (long_secc_combus(engine) * engine->fuel->const_burn_rate); - //float areas_ratio = total_burn_area / engine->grains->longitude; - float virtual_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (engine->tube->ult_stress_tension / (engine->tube->ult_stress_tension - 1))); - //float real_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); - float force_coeff = powf((((2 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1)) * powf((2 / (HEAT_CAPACITY_RATIO + 1)), ((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))) * ((1 - powf((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))))), 0.5f) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); - float adjusted_cr = force_coeff * desv_est_grains(engine); - float force = adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f; - float total_thrust = 0.0f; - float delta_v = total_thrust / (fuel_mass + (float)dead_mass); + double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemetry_mass + engine->engine_mass + rocket->payload_mass; + const double delta_t = step; + double pressure_abs = PRESION_ATMOSFERICA; + //double pressure = pressure_abs / 6894.757f; + //double pressure_man = pressure_abs - PRESION_ATMOSFERICA; + double burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; + double inst_radius = engine->grains->init_inter_radius; + double inst_long = engine->grains->longitude; + //double trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; + //double long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; + //double burn_area = trans_area * 2 + long_area; + //double total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; + double fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; + double fuel_mass = engine->fuel->density * fuel_vol; + double produced_mass = 0.0; + //double produced_massic_flux = 0.0f; + double escape_massic_flux = 0.0; + //double flux_diff = produced_massic_flux - escape_massic_flux; + double escape_delta_m = 0.0f; + double free_vol = port_area(engine) - fuel_vol; + double internal_mass = (free_vol * pressure_abs) / (long_secc_combus(engine) * engine->fuel->const_burn_rate); + //double areas_ratio = total_burn_area / engine->grains->longitude; + double virtual_escape_pressure = pressure_abs / pow((1.0 + ((HEAT_CAPACITY_RATIO - 1.0) / 2.0) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1.0))); + //double real_escape_pressure = pressure_abs / pow((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / pow((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); + double force_coeff = pow((((2.0 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1.0)) * pow((2.0 / (HEAT_CAPACITY_RATIO + 1.0)), ((HEAT_CAPACITY_RATIO + 1.0) / (HEAT_CAPACITY_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1.0) / HEAT_CAPACITY_RATIO))))), 0.5) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); + double adjusted_cr = force_coeff * desv_est_grains(engine); + double force = adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0; + double total_thrust = 0.0; + double delta_v = total_thrust / (fuel_mass + (double)dead_mass); // sums for eventual result values double sum_delta_v = delta_v; @@ -48,6 +50,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) // variable for max thrust double max_thrust = force; + double max_pressure = pressure_abs; // iteration loop for (int it = 0; it < max_iterations; it++) @@ -55,29 +58,32 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) pressure_abs = engine->fuel->const_burn_rate * long_secc_combus(engine) * internal_mass / free_vol; //pressure = pressure_abs / 6894.757f; //pressure_man = pressure_abs - PRESION_ATMOSFERICA; - burn_rate = engine->fuel->const_burn_rate * powf(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; + burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; inst_radius = inst_radius > 0 && (inst_radius + burn_rate * delta_t) < engine->grains->extern_radius ? inst_radius + burn_rate * delta_t : 0; inst_long = inst_radius > 0 ? inst_long - burn_rate * delta_t * 2 : 0; //trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; //long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; //burn_area = trans_area * 2 + long_area; //total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; - fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; + fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - (inst_radius * inst_radius)) * inst_long; + free_vol = port_area(engine) - fuel_vol; produced_mass = fuel_mass - engine->fuel->density * fuel_vol; fuel_mass = engine->fuel->density * fuel_vol; //produced_massic_flux = produced_mass / delta_t; - escape_massic_flux = pressure_abs > PRESION_ATMOSFERICA ? engine->grains->longitude * pressure_abs * HEAT_CAPACITY_RATIO * (sqrtf(powf((2.0f / (HEAT_CAPACITY_RATIO + 1.0f)), ((HEAT_CAPACITY_RATIO + 1.0f) / (HEAT_CAPACITY_RATIO - 1.0f))))) / powf((HEAT_CAPACITY_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; + escape_massic_flux = pressure_abs > PRESION_ATMOSFERICA ? engine->grains->longitude * pressure_abs * HEAT_CAPACITY_RATIO * (sqrt(pow((2.0f / (HEAT_CAPACITY_RATIO + 1.0f)), ((HEAT_CAPACITY_RATIO + 1.0f) / (HEAT_CAPACITY_RATIO - 1.0f))))) / pow((HEAT_CAPACITY_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; //flux_diff = produced_massic_flux - escape_massic_flux; escape_delta_m = escape_massic_flux * delta_t; - internal_mass = produced_mass + internal_mass - escape_delta_m > 0 ? produced_mass + internal_mass - escape_delta_m : 0.0f; + internal_mass = produced_mass + internal_mass - escape_delta_m > 0.0f ? produced_mass + internal_mass - escape_delta_m : 0.0f; //areas_ratio = total_burn_area / engine->grains->longitude; - virtual_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (engine->tube->ult_stress_tension / (engine->tube->ult_stress_tension - 1))); - //real_escape_pressure = pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / powf((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); - force_coeff = powf((((2 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1)) * powf((2 / (HEAT_CAPACITY_RATIO + 1)), ((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))) * ((1 - powf((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))))), 0.5f) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); + virtual_escape_pressure = pressure_abs / pow((1.0f + ((HEAT_CAPACITY_RATIO - 1.0f) / 2.0f) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1.0f))); + //real_escape_pressure = pressure_abs / pow((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / pow((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); + force_coeff = pow((((2.0 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1.0)) * pow((2.0 / (HEAT_CAPACITY_RATIO + 1.0)), ((HEAT_CAPACITY_RATIO + 1.0) / (HEAT_CAPACITY_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1.0) / HEAT_CAPACITY_RATIO))))), 0.5) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); adjusted_cr = force_coeff * desv_est_grains(engine); - total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2)) * delta_t; + //total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2.0f)) * delta_t; + last_force = force; force = adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f; - delta_v = total_thrust / (fuel_mass + (float)dead_mass); + total_thrust = (force + last_force) / 2.0 * delta_t; + delta_v = total_thrust / (fuel_mass + dead_mass); // step time value if (time + step < burn_time) @@ -91,11 +97,15 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) if (force > max_thrust) max_thrust = force; + + if (pressure_abs > max_pressure) + max_pressure = pressure_abs; } // storing values rocket->delta_v = sum_delta_v; rocket->max_thrust = max_thrust; + rocket->max_pressure = max_pressure; // calculate avg thrust rocket->avg_thrust = sum_avg_thrust / max_iterations; diff --git a/src/design_func.c b/src/design_func.c index 6027753..183431b 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -3,173 +3,173 @@ /*Functions*/ // E25 returns chamber pressure in pascals -float trel_get_pressure_pa(engine_t* engine) +double trel_get_pressure_pa(engine_t* engine) { return(trel_psi_to_pa(engine->pressure)); } //E34 returns the grain standard deviation -float desv_est_grains(engine_t *engine) +double desv_est_grains(engine_t *engine) { - return((float)(engine->comp_area_values->burn_std_deviation)*100000.0f); + return((double)(engine->comp_area_values->burn_std_deviation)*10000.0f); } //E35 Volumen unitario granos -float volumen_unitario(engine_t *engine) +double volumen_unitario(engine_t *engine) { - return(TREL_PI*(0.1f)*((engine->grains->extern_radius)* (engine->grains->extern_radius) -(0.01f)* (0.01f))); + return(TREL_PI*(0.1)*((engine->grains->extern_radius)*(engine->grains->extern_radius)-(0.01)*(0.01))); } // E36 Masa unitaria grano -float masa_unitaria(engine_t* engine) +double masa_unitaria(engine_t* engine) { return(volumen_unitario(engine)*1000.0f*(engine->fuel->density)); } // E37 Longitud total con combustible -float longitudt_ccombustible(engine_t *engine) +double longitudt_ccombustible(engine_t *engine) { return((engine->grains->amount)*(engine->grains->longitude)); } // E38 Port Area -float port_area(engine_t* engine) +double port_area(engine_t* engine) { return(TREL_PI*(engine->grains->init_inter_radius)*(engine->grains->init_inter_radius)); } // E39 -float long_secc_combus(engine_t* engine) +double long_secc_combus(engine_t* engine) { - return((engine->grains->grain_separation) * ((engine->grains->amount) - 1.0f) + ((engine->grains->longitude) * (engine->grains->amount))); + return((engine->grains->grain_separation)*((engine->grains->amount)-1.0)+((engine->grains->longitude)*(engine->grains->amount))); } // E49 Volumen especifico camara -float volumen_camara(engine_t *engine) +double volumen_camara(engine_t *engine) { return(engine->temperature*CONSTANTE_GASES/trel_get_pressure_pa(engine)); } // E62 Velocidad de escape -float calc_escape_vel(engine_t* engine) +double calc_escape_vel(engine_t* engine) { - float calc_pow = powf((PRESION_ATMOSFERICA / trel_get_pressure_pa(engine)), ((HEAT_CAPACITY_RATIO - 1.0f) / HEAT_CAPACITY_RATIO)); - return(sqrtf(2.0f * HEAT_CAPACITY_RATIO /(HEAT_CAPACITY_RATIO -1.0f) * CONSTANTE_GASES * engine->temperature * (1.0f - calc_pow))); + double calc_pow = pow((PRESION_ATMOSFERICA / trel_get_pressure_pa(engine)), ((HEAT_CAPACITY_RATIO - 1.0) / HEAT_CAPACITY_RATIO)); + return(sqrt(2.0 * HEAT_CAPACITY_RATIO /(HEAT_CAPACITY_RATIO -1.0) * CONSTANTE_GASES * engine->temperature * (1.0 - calc_pow))); } /* Determinacion de fuerza sobre tapas*/ // E140 Area interna -float area_interna(engine_t *engine) +double area_interna(engine_t *engine) { - return(TREL_PI*powf(engine->tube->internal_radius,2)); + return(TREL_PI*pow(engine->tube->internal_radius,2)); } // E141 Fuerza maxima -float fuerza_maxima(engine_t *engine) +double fuerza_maxima(engine_t *engine) { return(engine->max_pressure*area_interna(engine)); } // E142 Fuerza por tornillo -float fuerza_tornillo(engine_t *engine) +double fuerza_tornillo(engine_t *engine) { return(fuerza_maxima(engine)/engine->screws->amount); } /*Esfuerzo circunferencial*/ // E144 Tension -float tension(engine_t *engine) +double tension(engine_t *engine) { return(fuerza_maxima(engine)/engine->tube->material_area); } // E145 Margen de seguridad -float margen_de_seguridad(engine_t *engine) +double margen_de_seguridad(engine_t *engine) { return(engine->tube->shear_stress_tension/tension(engine)); } /*Esfuerzo longitudinal*/ // E146 Area de cortante -float area_de_cortante(engine_t *engine) +double area_de_cortante(engine_t *engine) { return(engine->screws->width_cutting_segment*engine->tube->wall_thickness); } // E147 Cortante promedio -float cortante_promedio(engine_t *engine) +double cortante_promedio(engine_t *engine) { return(fuerza_tornillo(engine)/area_de_cortante(engine)); } // E148 Margen de seguridad cortante -float margen_de_seguridad_cortante(engine_t *engine) +double margen_de_seguridad_cortante(engine_t *engine) { return(engine->tube->ult_stress_pressure/cortante_promedio(engine)); } /*Esfuerzo de aplastamiento*/ // E149 Aplastamiento -float aplastamiento(engine_t *engine) +double aplastamiento(engine_t *engine) { return(fuerza_tornillo(engine)/engine->screws->area_per_screw); } // E150 Margen de seguridad aplaztamiento -float margen_de_seguridad_aplaztamiento(engine_t *engine) +double margen_de_seguridad_aplaztamiento(engine_t *engine) { return(engine->tube->shear_stress_tension/aplastamiento(engine)); } /*Deformacion circunferencial*/ // E152 Deformacion unitaria -float deformacion_unitaria(engine_t *engine) +double deformacion_unitaria(engine_t *engine) { return(engine->tangencial_stress/engine->tube->young_module); } // E153 Circunferencia interna total -float circunferencia_interna_total(engine_t *engine) +double circunferencia_interna_total(engine_t *engine) { return(2*TREL_PI*engine->tube->internal_radius); } // E154 Deformacion de la circunferencia interna -float deformacion_circunferencia_interna(engine_t *engine) +double deformacion_circunferencia_interna(engine_t *engine) { return(deformacion_unitaria(engine)*circunferencia_interna_total(engine)*1000.0f); } // E155 Circunferencia externa inicial -float circunferencia_externa_inicial(engine_t *engine) +double circunferencia_externa_inicial(engine_t *engine) { return(TREL_PI*engine->tube->diameter_ext); } // E156 Deformacion de circunferencia externa -float deformacion_circunferencia_externa(engine_t *engine) +double deformacion_circunferencia_externa(engine_t *engine) { return(circunferencia_externa_inicial(engine)*deformacion_unitaria(engine)*1000); } // E157 Radio interno final // radio_final(circunferencia_interna_total(),deformacion_circunferencia_interna()); -float radio_final(float circ_total,float deform_circ) +double radio_final(double circ_total,double deform_circ) { return(circ_total*(deform_circ/1000.0f)/(2.0f*TREL_PI)); } // E158 Diferencia de radio interno -//float engine->tube->internal_radius = 1; // Dependencia E15 -float diferencia_radio_interno(engine_t *engine) +//double engine->tube->internal_radius = 1; // Dependencia E15 +double diferencia_radio_interno(engine_t *engine) { return(radio_final(circunferencia_interna_total(engine),deformacion_circunferencia_interna(engine))-engine->tube->internal_radius*1000.0f); } // E159 es la misma ecuacion de E157 // radio_final(circunferencia_interna_inicial(),deformacion_circunferencia_externa()); // E160 Diferencia radio externo -float diferencia_radio_externo(engine_t *engine) +double diferencia_radio_externo(engine_t *engine) { return((radio_final(circunferencia_externa_inicial(engine),deformacion_circunferencia_externa(engine))-(engine->tube->diameter_ext/2.0f))*1000.0f); } /*Escape*/ // E88 -float throat_area(engine_t *engine) +double throat_area(engine_t *engine) { - return((float)mg_combustion(engine) * vol_esp_garganta(engine) / velocidad_garganta(engine)); + return((double)mg_combustion(engine) * vol_esp_garganta(engine) / velocidad_garganta(engine)); } // E89 -float escape_area(engine_t* engine) +double escape_area(engine_t* engine) { - return((float)mg_combustion(engine) * vol_esp_escape(engine) / calc_escape_vel(engine)); + return((double)mg_combustion(engine) * vol_esp_escape(engine) / calc_escape_vel(engine)); } // E101 engine->nozzle_efficiency -void set_nozzle_efficiency(engine_t* engine, float value) +void set_nozzle_efficiency(engine_t* engine, double value) { engine->nozzle_efficiency = value; } diff --git a/src/design_resist.c b/src/design_resist.c index ba9188d..5deaa6e 100644 --- a/src/design_resist.c +++ b/src/design_resist.c @@ -6,9 +6,9 @@ Inicio de traduccion a C por lross2k ******/ // Variables necesarias de partes anteriores -//float temp_E13 = 0.0730f; -//float temp_E14 = 0.0052f; -//float temp_E15 = 0.0314f; +//double temp_E13 = 0.0730f; +//double temp_E14 = 0.0052f; +//double temp_E15 = 0.0314f; // Resistencia de materiales E115 void trel_mean_tubing_diameter(engine_t *engine) @@ -23,8 +23,8 @@ void trel_width_condition(engine_t *engine) // Presión maxima E118 void trel_engine_max_pressure(engine_t *engine) { - // TODO: max_pressure = get_max(float *sim_data, int n_sim_data) - engine->max_pressure = 5.95f*powf(10,6); // Pascal + // TODO: max_pressure = get_max(double *sim_data, int n_sim_data) + engine->max_pressure = 5.95f*pow(10,6); // Pascal //engine.max_pressure = 5.95*e**6 # Pascales (Obtenido de simulación) //engine.pmax = 863.01 #PSI } @@ -41,7 +41,7 @@ void trel_tangencial_stress(engine_t *engine) } else { - engine->tangencial_stress = engine->max_pressure*(powf((engine->tube->diameter_ext/2.0f),2.0f)+powf(engine->tube->internal_radius,2.0f))/(powf(engine->tube->diameter_ext/2.0f,2.0f)-powf(engine->tube->internal_radius,2.0f)); + engine->tangencial_stress = engine->max_pressure*(pow((engine->tube->diameter_ext/2.0f),2.0f)+pow(engine->tube->internal_radius,2.0f))/(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)); } } // Esfuerzo Longitudinal E122 @@ -53,7 +53,7 @@ void trel_logitudinal_stress(engine_t *engine) } else { - engine->longitudinal_stress = engine->max_pressure*powf(engine->tube->internal_radius,2.0f)/(powf(engine->tube->diameter_ext,2.0f)-powf(engine->tube->internal_radius,2.0f)); + engine->longitudinal_stress = engine->max_pressure*pow(engine->tube->internal_radius,2.0f)/(pow(engine->tube->diameter_ext,2.0f)-pow(engine->tube->internal_radius,2.0f)); } } // Esfuerzo Radial E123 @@ -97,17 +97,17 @@ void trel_margin_of_safety(engine_t *engine) // engine_t has tubing_t *tube void trel_transversal_area_tube(engine_t *engine) { - engine->tube->transversal_area = (float)TREL_PI*(powf(engine->tube->diameter_ext/2.0f,2.0f)-powf(engine->tube->internal_radius,2.0f)); + engine->tube->transversal_area = (double)TREL_PI*(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)); } // Angulo de sector circular E134 void trel_ang_circular_sector(engine_t *engine) { - engine->tube->sector_angle = (180.0f/TREL_PI)*asinf((engine->screws->diameter/2)/engine->tube->internal_radius); + engine->tube->sector_angle = (180.0/TREL_PI)*asin((engine->screws->diameter/2)/engine->tube->internal_radius); } // Area transversal de 1 tornillo E135 void trel_area_per_screw(engine_t *engine) { - engine->screws->area_per_screw = (float)(engine->tube->sector_angle/2.0f)*(powf(engine->tube->diameter_ext/2.0f,2.0f)-powf(engine->tube->internal_radius,2.0f)-powf(engine->tube->wall_thickness,2.0f)); + engine->screws->area_per_screw = (double)(engine->tube->sector_angle/2.0f)*(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)-pow(engine->tube->wall_thickness,2.0f)); } // Area transversal ocupada por los tornillos E136 void trel_screw_occupied_area(engine_t *engine) diff --git a/src/engine.c b/src/engine.c index 1e5c980..64861c0 100644 --- a/src/engine.c +++ b/src/engine.c @@ -3,10 +3,10 @@ /* Initializes all variables in the grains struct */ grains_t* trel_grains_init( unsigned int amount, - float internal_radius, - float external_radius, - float longitude, - float grain_separation) + double internal_radius, + double external_radius, + double longitude, + double grain_separation) { grains_t *grains = (grains_t *) malloc(sizeof(grains_t)); if (!grains) { @@ -22,9 +22,9 @@ grains_t* trel_grains_init( /* Initializes all the variables in the fuel strcut */ fuel_t* trel_fuel_init( - float const_burn_rate, - float pressure_exponent, - float density) + double const_burn_rate, + double pressure_exponent, + double density) { fuel_t *fuel = (fuel_t *) malloc(sizeof(fuel_t)); if (!fuel) { @@ -40,13 +40,13 @@ fuel_t* trel_fuel_init( /* Initializes all the variables in the tube struct */ tubing_t* trel_tubing_init( char *material, - float diameter, - float thickness, - float young_module, - float shear_tension, - float shear_pressure, - float ult_tension, - float ult_pressure) + double diameter, + double thickness, + double young_module, + double shear_tension, + double shear_pressure, + double ult_tension, + double ult_pressure) { tubing_t *tube = (tubing_t *) malloc(sizeof(tubing_t)); if (!tube) { @@ -73,8 +73,8 @@ tubing_t* trel_tubing_init( screws_t* trel_screws_init( char *material, unsigned int amount, - float diameter, - float dist_center_wall) + double diameter, + double dist_center_wall) { screws_t *screws = (screws_t *) malloc(sizeof(screws_t)); if (!screws) { @@ -93,8 +93,9 @@ screws_t* trel_screws_init( /* Initializes all the variables in the engine struct */ engine_t* trel_engine_init( - float pressure, - float temperature, + double pressure, + double temperature, + double engine_mass, grains_t *grains, fuel_t *fuel, tubing_t *tube, @@ -112,7 +113,6 @@ engine_t* trel_engine_init( printf("Engine initialization failed\n"); exit(1); } - engine->egine_mass = 0.0; engine->width_condition = 0.0f; engine->margin_of_safety = 0.0f; engine->max_stress = 0.0f; @@ -128,6 +128,7 @@ engine_t* trel_engine_init( engine->screws = screws; engine->escape_vel = calc_escape_vel(engine); engine->comp_area_values = comp_area_values; + engine->engine_mass = engine_mass; engine->nozzle_efficiency = 0.0f; trel_engine_max_pressure(engine); trel_transversal_area_tube(engine); @@ -143,29 +144,29 @@ engine_t* trel_engine_init( } /* returns given psi value in pascals */ -float trel_psi_to_pa(float psi) +double trel_psi_to_pa(double psi) { return(6894.757f*psi); } /* returns the chamber pressure of given engine in psi */ -float trel_get_pressure(engine_t *engine) +double trel_get_pressure(engine_t *engine) { return(engine->pressure); } /* sets the chamber pressure in psi as given to the engine */ -void trel_set_pressure(engine_t *engine, float pressure) +void trel_set_pressure(engine_t *engine, double pressure) { engine->pressure = pressure; } -void trel_set_escape_vel(engine_t *engine, float vel) +void trel_set_escape_vel(engine_t *engine, double vel) { engine->escape_vel = vel; } -float trel_get_escape_vel(engine_t *engine) +double trel_get_escape_vel(engine_t *engine) { return engine->escape_vel; } @@ -173,6 +174,6 @@ float trel_get_escape_vel(engine_t *engine) int trel_set_engine_mass(engine_t* engine, double mass) { if (!engine) return(1); - engine->egine_mass = mass; + engine->engine_mass = mass; return(0); } diff --git a/src/rendimientos.c b/src/rendimientos.c index 49d5b93..e2879b5 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -2,23 +2,23 @@ /*Otras variables adyacentes */ /*E43 */ -float br_combustion(engine_t *engine) +double br_combustion(engine_t *engine) { - return (engine->fuel->const_burn_rate* powf(trel_get_pressure_pa(engine) * + return (engine->fuel->const_burn_rate* pow(trel_get_pressure_pa(engine) * FACT_CONV_BR, engine->fuel->pressure_exponent) * FACT_CONV_M_TO_I); } /*Combustible */ /*E67 Volumen_Combustible */ -float volumen_combustible(engine_t *engine) +double volumen_combustible(engine_t *engine) { - return(float)(TREL_PI*engine->grains->longitude*engine->grains->amount*(powf( - engine->grains->extern_radius,2.0f)-powf(engine->grains->init_inter_radius,2.0f))); + return(double)(TREL_PI*engine->grains->longitude*engine->grains->amount*(pow( + engine->grains->extern_radius,2.0f)-pow(engine->grains->init_inter_radius,2.0f))); } /*E68 Masa_Combustible */ -float masa_combustible(engine_t *engine) +double masa_combustible(engine_t *engine) { return (volumen_combustible(engine) *engine->fuel->density); } @@ -26,7 +26,7 @@ float masa_combustible(engine_t *engine) /*Combustión*/ /*E70 Tiempo de quemado total_Combustible*/ -float Tiempo_quemado_combustion(engine_t *engine) +double Tiempo_quemado_combustion(engine_t *engine) { return (engine->grains->extern_radius-engine->grains->init_inter_radius)/br_combustion(engine); } @@ -68,13 +68,13 @@ double emp_max(trel_rocket_t* rocket) /*Impulsos*/ /*E81 Impulso específico teórico*/ -float I_sp_teo(engine_t *engine) +double I_sp_teo(engine_t *engine) { return calc_escape_vel(engine)/GRAVITY; } /*E82 Impulso específico esperado*/ -float I_sp_esperado(engine_t* engine) +double I_sp_esperado(engine_t* engine) { return(engine->fuel->density); } @@ -86,7 +86,7 @@ double I_tot_teo(engine_t *engine) } /*E84 Impulso total esperado*/ -float I_tot_esp(engine_t* engine) +double I_tot_esp(engine_t* engine) { return(br_combustion(engine)); } diff --git a/src/rocket.c b/src/rocket.c index 08769f6..693ebe1 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -3,18 +3,24 @@ /* Initializes all the variables in the rocket struct */ trel_rocket_t* trel_rocket_init( engine_t* engine, - double telemtry_mass, + double telemetry_mass, double parachute_mass, - double fuselage_mass) + double fuselage_mass, + double payload_mass) { trel_rocket_t* rocket = (trel_rocket_t*)malloc(sizeof(trel_rocket_t)); if (!rocket) { printf("Rocket initialization failed\n"); exit(1); } - rocket->telemtry_mass = telemtry_mass; + rocket->telemetry_mass = telemetry_mass; rocket->parachute_mass = parachute_mass; rocket->fuselage_mass = fuselage_mass; + rocket->payload_mass = payload_mass; rocket->engine = engine; + if (trel_run_time_comp_iterations(rocket)) // 1 means there was an error + { + return(NULL); // NULL means the engine couldn't be properly generated + } return(rocket); } diff --git a/src/val_termod.c b/src/val_termod.c index 7faeb50..ce8f816 100644 --- a/src/val_termod.c +++ b/src/val_termod.c @@ -3,83 +3,83 @@ // Volumenes // E49 Volumen Especifico Camara de Combustible -float vol_esp_cam_combustion(engine_t *engine) +double vol_esp_cam_combustion(engine_t *engine) { return (engine->temperature * CONSTANTE_GASES) / trel_get_pressure_pa(engine); } // E50 Volumen Especifico de Garganta -float vol_esp_garganta(engine_t *engine) +double vol_esp_garganta(engine_t *engine) { - return vol_esp_cam_combustion(engine) * (powf((HEAT_CAPACITY_RATIO + 1),(1 / HEAT_CAPACITY_RATIO - 1))); + return vol_esp_cam_combustion(engine) * (pow((HEAT_CAPACITY_RATIO + 1),(1 / HEAT_CAPACITY_RATIO - 1))); } // E51 Volumen Especifico Escape -float vol_esp_escape(engine_t *engine) +double vol_esp_escape(engine_t *engine) { - return vol_esp_cam_combustion(engine) * powf((trel_get_pressure_pa(engine) / PRESION_ATMOSFERICA), (1 / HEAT_CAPACITY_RATIO)); + return vol_esp_cam_combustion(engine) * pow((trel_get_pressure_pa(engine) / PRESION_ATMOSFERICA), (1 / HEAT_CAPACITY_RATIO)); } // Presiones // E53 presion de garganta -float presion_garganta(engine_t *engine) +double presion_garganta(engine_t *engine) { - return trel_get_pressure_pa(engine) * powf((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)), (HEAT_CAPACITY_RATIO)); + return trel_get_pressure_pa(engine) * pow((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)), (HEAT_CAPACITY_RATIO)); } // Temperaturas // E56 temperatura de garganta -float trel_temper_garganta(engine_t *engine) +double trel_temper_garganta(engine_t *engine) { - return engine->temperature * powf((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)),(HEAT_CAPACITY_RATIO - 1)); + return engine->temperature * pow((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)),(HEAT_CAPACITY_RATIO - 1)); } // E57 temperatura de escape -float presion_escape(engine_t *engine) +double presion_escape(engine_t *engine) { - return engine->temperature * (powf((PRESION_ATMOSFERICA / trel_get_pressure_pa(engine)),((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))); + return engine->temperature * (pow((PRESION_ATMOSFERICA / trel_get_pressure_pa(engine)),((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))); } // Velocidades // E59 velocidad garganta -float velocidad_garganta(engine_t *engine) +double velocidad_garganta(engine_t *engine) { - return powf(((2.0f * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO + 1.0f) * CONSTANTE_GASES * engine->temperature),(0.5f)); + return pow(((2.0f * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO + 1.0f) * CONSTANTE_GASES * engine->temperature),(0.5f)); } // E60 Velocidad Sonica Local at -float vel_sonica_local_at(engine_t *engine) +double vel_sonica_local_at(engine_t *engine) { - return powf((HEAT_CAPACITY_RATIO * CONSTANTE_GASES * presion_garganta(engine)),(0.5)); + return pow((HEAT_CAPACITY_RATIO * CONSTANTE_GASES * presion_garganta(engine)),(0.5)); } // E61 Numero Mach Mt -float vel_numero_mach_mt(engine_t *engine) +double vel_numero_mach_mt(engine_t *engine) { return velocidad_garganta(engine) / vel_sonica_local_at(engine); } // E63 Velocidad Sonica Local a2 -float vel_sonica_local_a2(engine_t *engine) +double vel_sonica_local_a2(engine_t *engine) { - return powf((HEAT_CAPACITY_RATIO * CONSTANTE_GASES * presion_escape(engine)),(0.5)); + return pow((HEAT_CAPACITY_RATIO * CONSTANTE_GASES * presion_escape(engine)),(0.5)); } // E64 Numero Mach M2 -float vel_numero_mach_m2(engine_t *engine) +double vel_numero_mach_m2(engine_t *engine) { return calc_escape_vel(engine) / vel_sonica_local_a2(engine); } // E65 Velocidad de escape caracteristica -float vel_escape_caract(engine_t *engine) +double vel_escape_caract(engine_t *engine) { - return powf(powf((CONSTANTE_GASES * engine->temperature) / (HEAT_CAPACITY_RATIO *(2 / (HEAT_CAPACITY_RATIO + 1))),((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))), (0.5)); + return pow(pow((CONSTANTE_GASES * engine->temperature) / (HEAT_CAPACITY_RATIO *(2 / (HEAT_CAPACITY_RATIO + 1))),((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))), (0.5)); } From fca7e2abd1b4515575349b0f1433e53e288b1870 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 4 Mar 2023 20:08:56 -0600 Subject: [PATCH 13/42] Removed material property from screws This was unused (so far), and was badly implemented Using char *material in the structs without defining a size ended up being UB In case of needing to re implement this, the correct way would have to be tested, probably setting a character limit is going to be the easiest, as this would be like a padding in the allocated memory -lross2k --- include/TREL.h | 2 +- include/engine.h | 3 ++- include/structs.h | 2 +- main.c | 52 +++++++++++++++++++++++++---------------------- src/engine.c | 4 ++-- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/include/TREL.h b/include/TREL.h index 3988cbb..ecf669d 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -89,7 +89,7 @@ tubing_t *trel_tubing_init( double ult_pressure /* E21 */ ); screws_t *trel_screws_init( - char* material, +// char* material, unsigned int amount, double diameter, double dist_center_wall diff --git a/include/engine.h b/include/engine.h index 85f90be..4c20c56 100644 --- a/include/engine.h +++ b/include/engine.h @@ -37,7 +37,7 @@ tubing_t TREL_EXPORT *trel_tubing_init( double ult_pressure /* E21 */ ); screws_t TREL_EXPORT* trel_screws_init( - char* material, + //char* material, unsigned int amount, double diameter, double dist_center_wall @@ -58,3 +58,4 @@ double TREL_EXPORT trel_get_escape_vel(engine_t *engine); double TREL_EXPORT trel_get_pressure(engine_t *engine); #endif // ENGINE_H + diff --git a/include/structs.h b/include/structs.h index 704644a..9e0ebaa 100644 --- a/include/structs.h +++ b/include/structs.h @@ -36,7 +36,7 @@ typedef struct screws double diameter, dist_center_wall; unsigned int amount; double area_per_screw, screw_occupied_area; - char *material; + //char *material; double width_cutting_segment; } TREL_EXPORT screws_t; diff --git a/main.c b/main.c index 2278ca6..524f867 100644 --- a/main.c +++ b/main.c @@ -10,29 +10,33 @@ void print_debug_example() printf("Inicializando structs\n"); grains_t* created_grains = trel_grains_init( 4, - 0.01f, - 0.0304f, - 0.1f, - 0.001f + 0.01, + 0.0304, + 0.1, + 0.001 ); fuel_t* created_fuel = trel_fuel_init( - 0.005f, - 0.688f, + 0.005, + 0.688, 1859 ); screws_t* created_screw = trel_screws_init( - "Acero", +// "Acero", 6, - 0.007466f, - 0.01f + 0.007466, + 0.01 ); + + //printf("Probando %i y %s\n", created_screw->amount, created_screw->material); + printf("Probando %i\n", created_screw->amount); + // Se inicializa la tuberia tubing_t* created_tube = trel_tubing_init( "Aluminio 6061-T6", - 0.073f, - 0.0052f, - 68900000000.0f, - 275000000.0f, + 0.073, + 0.0052, + 68900000000.0, + 275000000.0, -1, 310000000, 205000000 @@ -57,25 +61,25 @@ void print_debug_example() ); // Acceder a memoria inicializada - printf("La presion del motor dada es %f psi\n",trel_get_pressure(testing_engine)); - printf("\nLa velocidad de escape automaticamente inicializada es %f\n", trel_get_escape_vel(testing_engine)); + printf("La presion del motor dada es %lf psi\n",trel_get_pressure(testing_engine)); + printf("\nLa velocidad de escape automaticamente inicializada es %lf\n", trel_get_escape_vel(testing_engine)); // Modificar valores de memoria con funciones - trel_set_escape_vel(testing_engine, 666.420f); - printf("\nLa velocidad de escape modificada con tsel_set_escape_vel() es %f\n", trel_get_escape_vel(testing_engine)); + trel_set_escape_vel(testing_engine, 666.420); + printf("\nLa velocidad de escape modificada con tsel_set_escape_vel() es %lf\n", trel_get_escape_vel(testing_engine)); // Usando una funcion de rendimientos.c - printf("\nEl valor de combuistion es %f\n", br_combustion(testing_engine)); + printf("\nEl valor de combuistion es %lf\n", br_combustion(testing_engine)); // Usando una funcion de val_termod.c - printf("\nLa temperatura en garganta es de %f\n", trel_temper_garganta(testing_engine)); + printf("\nLa temperatura en garganta es de %lf\n", trel_temper_garganta(testing_engine)); // Probando valores generados por comp_area.c - printf("\nArea longitudinal promedio: %f\n", testing_engine->comp_area_values->avg_long_area); - printf("Area quemado promedio: %f\n", testing_engine->comp_area_values->avg_burn_area); - printf("Area transversal promedio: %f\n", testing_engine->comp_area_values->avg_trans_area); - printf("Suma de diferencia quemado: %f\n", testing_engine->comp_area_values->burn_sum_diff); - printf("Desviacion estandar area de quemado: %f\n", testing_engine->comp_area_values->burn_std_deviation); + printf("\nArea longitudinal promedio: %lf\n", testing_engine->comp_area_values->avg_long_area); + printf("Area quemado promedio: %lf\n", testing_engine->comp_area_values->avg_burn_area); + printf("Area transversal promedio: %lf\n", testing_engine->comp_area_values->avg_trans_area); + printf("Suma de diferencia quemado: %lf\n", testing_engine->comp_area_values->burn_sum_diff); + printf("Desviacion estandar area de quemado: %lf\n", testing_engine->comp_area_values->burn_std_deviation); // Probando valores generados por comp_tiempo.c printf("\nDelta V total: %lf\n", testing_rocket->delta_v); diff --git a/src/engine.c b/src/engine.c index 64861c0..21e5197 100644 --- a/src/engine.c +++ b/src/engine.c @@ -71,7 +71,7 @@ tubing_t* trel_tubing_init( /* Initializes all the variables in the screws struct */ screws_t* trel_screws_init( - char *material, + //char *material, unsigned int amount, double diameter, double dist_center_wall) @@ -81,7 +81,7 @@ screws_t* trel_screws_init( printf("Screws initialization failed\n"); exit(1); } - screws->material = material; + //screws->material = material; screws->diameter = diameter; screws->amount = amount; screws->dist_center_wall = dist_center_wall; From 77869f4cae84eb5a83159c260d856b4cda4ecc82 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 4 Mar 2023 20:26:05 -0600 Subject: [PATCH 14/42] Began creating a Python interface This will replace the main.c file, more specifically the testapp executable generated at build time With the intent of leaving the build process only for a library, the testapp will be ported to Python and left as a truly test only app TREL will remain a C library, but the TREL.py will allow for more portability --- include/TREL.h | 4 +-- include/engine.h | 2 +- include/structs.h | 2 +- main.c | 2 +- python/TREL.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++ python/main.py | 29 +++++++++++++++++++ src/engine.c | 4 +-- 7 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 python/TREL.py create mode 100644 python/main.py diff --git a/include/TREL.h b/include/TREL.h index ecf669d..3ba1dd7 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -23,7 +23,7 @@ typedef struct { // Estructura para tuberia typedef struct tubing { - char* material; + //char* material; double diameter_ext, wall_thickness, internal_radius; double young_module, sector_angle, mean_tubing_diameter; double shear_stress_tension, shear_stress_pressure; @@ -79,7 +79,7 @@ fuel_t *trel_fuel_init( double density /* E44 */ ); tubing_t *trel_tubing_init( - char* material, /* E16 & E17 */ + //char* material, /* E16 & E17 */ double diameter, /* E13 */ double thickness, /* E14 */ double young_module, /* E18 */ diff --git a/include/engine.h b/include/engine.h index 4c20c56..99e3974 100644 --- a/include/engine.h +++ b/include/engine.h @@ -27,7 +27,7 @@ fuel_t TREL_EXPORT *trel_fuel_init( double density /* E44 */ ); tubing_t TREL_EXPORT *trel_tubing_init( - char *material, /* E16 & E17 */ + //char *material, /* E16 & E17 */ double diameter, /* E13 */ double thickness, /* E14 */ double young_module, /* E18 */ diff --git a/include/structs.h b/include/structs.h index 9e0ebaa..c91edc5 100644 --- a/include/structs.h +++ b/include/structs.h @@ -22,7 +22,7 @@ typedef struct { // Estructura para tuberia typedef struct tubing { - char *material; + //char *material; double diameter_ext, wall_thickness, internal_radius; double young_module, sector_angle, mean_tubing_diameter; double shear_stress_tension, shear_stress_pressure; diff --git a/main.c b/main.c index 524f867..ecac708 100644 --- a/main.c +++ b/main.c @@ -32,7 +32,7 @@ void print_debug_example() // Se inicializa la tuberia tubing_t* created_tube = trel_tubing_init( - "Aluminio 6061-T6", + //"Aluminio 6061-T6", 0.073, 0.0052, 68900000000.0, diff --git a/python/TREL.py b/python/TREL.py new file mode 100644 index 0000000..94c4d6a --- /dev/null +++ b/python/TREL.py @@ -0,0 +1,72 @@ +import ctypes +import os +#TREL = ctypes.CDLL(os.getcwd()+"/libTREL.so") +TREL = ctypes.CDLL("../build/libTREL.so") + +# Struct and constructor for propellent grains +class TREL_GRAINS(ctypes.Structure): + _fields_ = [("amount", ctypes.c_uint), + ("init_inter_radius", ctypes.c_double), + ("extern_radius", ctypes.c_double), + ("longitude", ctypes.c_double), + ("grain_separation", ctypes.c_double)] + +trel_grains_init = TREL.trel_grains_init +trel_grains_init.argtypes = [ctypes.c_int, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double] +trel_grains_init.restype = ctypes.c_void_p + +# Struct and constructor for fuel +class TREL_FUEL(ctypes.Structure): + _fields_ = [("const_burn_rate", ctypes.c_double), + ("pressure_exponent", ctypes.c_double), + ("density", ctypes.c_double), + ("burn_rate", ctypes.c_double)] + +trel_fuel_init = TREL.trel_fuel_init +trel_fuel_init.argtypes = [ctypes.c_double, + ctypes.c_double, + ctypes.c_double] +trel_fuel_init.restype = ctypes.c_void_p + +# Struct and constructor for screws +class TREL_SCREWS(ctypes.Structure): + _fields_ = [("diameter", ctypes.c_double), + ("dist_center_wall", ctypes.c_double), + ("area_per_screw", ctypes.c_double), + ("screw_occupied_area", ctypes.c_double), + ("width_cutting_segment", ctypes.c_double), + ("amount", ctypes.c_uint)] + +trel_screws_init = TREL.trel_screws_init +trel_screws_init.argtypes = [ctypes.c_uint, + ctypes.c_double, + ctypes.c_double] +trel_screws_init.restype = ctypes.c_void_p + +# Struct and constructor for tubing +class TREL_TUBING(ctypes.Structure): + _fields_ = [("diameter_ext", ctypes.c_double), + ("wall_thickness", ctypes.c_double), + ("internal_radius", ctypes.c_double), + ("young_module", ctypes.c_double), + ("sector_angle", ctypes.c_double), + ("mean_tubing_diameter", ctypes.c_double), + ("shear_stress_tension", ctypes.c_double), + ("shear_stress_pressure", ctypes.c_double), + ("transversal_area", ctypes.c_double), + ("material_area", ctypes.c_double)] + +trel_tubing_init = TREL.trel_tubing_init +trel_tubing_init.argtypes = [ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double] +trel_tubing_init.restype = ctypes.c_void_p + diff --git a/python/main.py b/python/main.py new file mode 100644 index 0000000..ad5e788 --- /dev/null +++ b/python/main.py @@ -0,0 +1,29 @@ +from TREL import * + +created_grains = TREL_GRAINS.from_address(trel_grains_init( + 4, + 0.01, + 0.0304, + 0.1, + 0.001 + )) + +created_fuel = TREL_FUEL.from_address(trel_fuel_init( + 0.005, + 0.688, + 1859)) + +created_screw = TREL_SCREWS.from_address(trel_screws_init( + 6, + 0.007466, + 0.01)) + +created_tube = TREL_TUBING.from_address(trel_tubing_init( + 0.073, + 0.0052, + 68900000000.0, + 275000000.0, + -1, + 310000000, + 205000000)) + diff --git a/src/engine.c b/src/engine.c index 21e5197..23f48f8 100644 --- a/src/engine.c +++ b/src/engine.c @@ -39,7 +39,7 @@ fuel_t* trel_fuel_init( /* Initializes all the variables in the tube struct */ tubing_t* trel_tubing_init( - char *material, +// char *material, double diameter, double thickness, double young_module, @@ -53,7 +53,7 @@ tubing_t* trel_tubing_init( printf("Tubing initialization failed\n"); exit(1); } - tube->material = material; + //tube->material = material; tube->diameter_ext = diameter; tube->wall_thickness = thickness; tube->young_module = young_module; From 67266054260149e25c89e2c98a83ecf153f96ec1 Mon Sep 17 00:00:00 2001 From: lross2k Date: Wed, 22 Mar 2023 13:50:19 -0600 Subject: [PATCH 15/42] Python TREL support for Windows NT --- .gitignore | 1 + python/TREL.py | 7 +++++-- python/main.py | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b353cb8..3ec1797 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Visual Studio stuff .vs/ .vscode/ +CMakeSettings.json out/ # Recommended build directory build/ diff --git a/python/TREL.py b/python/TREL.py index 94c4d6a..7641665 100644 --- a/python/TREL.py +++ b/python/TREL.py @@ -1,7 +1,10 @@ import ctypes import os -#TREL = ctypes.CDLL(os.getcwd()+"/libTREL.so") -TREL = ctypes.CDLL("../build/libTREL.so") + +if os.name == 'posix': + TREL = ctypes.CDLL("./libTREL.so") +elif os.name == 'nt': + TREL = ctypes.CDLL("./TREL.dll") # Struct and constructor for propellent grains class TREL_GRAINS(ctypes.Structure): diff --git a/python/main.py b/python/main.py index ad5e788..3b2a2d7 100644 --- a/python/main.py +++ b/python/main.py @@ -27,3 +27,4 @@ 310000000, 205000000)) +print(created_tube.wall_thickness) From 1267e5fcf2d504b4a111f3226d1b6f8bbe71f975 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 1 Apr 2023 16:06:27 -0600 Subject: [PATCH 16/42] Implemented height simulation The biggest module of TREL, many refactors and bugs incoming The function trel_run_height_sim_iterations() allows and handles all the processes related to this simulation -lross2k --- CMakeLists.txt | 2 +- TODO.md | 7 +- include/TREL.h | 20 +++++- include/design_constants.h | 28 +++++--- include/design_func.h | 1 + include/engine.h | 3 +- include/height_sim.h | 9 +++ include/rendimientos.h | 1 + include/rocket.h | 6 +- include/structs.h | 13 +++- main.c | 10 ++- src/comp_tiempo.c | 28 ++++---- src/design_func.c | 9 +-- src/engine.c | 12 ++-- src/height_sim.c | 141 +++++++++++++++++++++++++++++++++++++ src/rendimientos.c | 2 +- src/rocket.c | 15 +++- src/val_termod.c | 20 +++--- 18 files changed, 272 insertions(+), 55 deletions(-) create mode 100644 include/height_sim.h create mode 100644 src/height_sim.c diff --git a/CMakeLists.txt b/CMakeLists.txt index aa41c32..0a6d2c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ ADD_LIBRARY (TREL ${LIB_TYPE} "src/design_func.c" "src/design_resist.c" "src/val_termod.c" "src/rocket.c" - ) + "src/height_sim.c") # Specify the directory to contain header files INCLUDE_DIRECTORIES ("include") diff --git a/TODO.md b/TODO.md index 19a063f..9bc41ca 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,7 @@ # TODO list for the project -1. Implement height simulation +1. Finish height simulation (obtaining max values) 2. Implement memory free functions, and destroy all pointers -3. Implement Python library interface +3. Define and apply standard function convention +4. Define and apply standard style guide +5. Change from C99 to the more portable C89 [ANSI C] +6. Implement Python library interface diff --git a/include/TREL.h b/include/TREL.h index 3ba1dd7..6421a30 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -60,9 +60,19 @@ typedef struct engine // Estructura del cohete typedef struct trel_rocket { - engine_t *engine; // ptr to initialized struct - double telemtry_mass, parachute_mass, fuselage_mass, payload_mass; + engine_t* engine; // ptr to initialized struct + double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 + double sim_latitude, max_sim_height, body_diameter, drag_coefficient; + double rocket_position[1000]; + double rocket_speed[1000]; // m/s + double rocket_acceleration[1000]; + double rocket_force_balance[1000]; + double rocket_drag[1000]; + double rocket_weight[1000]; + double rocket_mass[1000]; + double rocket_force[1000]; + double time[1000]; } trel_rocket_t; // Function prototypes @@ -108,7 +118,11 @@ trel_rocket_t *trel_rocket_init( double telemetry_mass, double parachute_mass, double fuselage_mass, - double payload_mass + double payload_mass, + double initial_height, + double sim_latitude, + double body_diameter, + double drag_coefficient ); double trel_psi_to_pa(double psi); void trel_set_pressure(engine_t* engine, double pressure); diff --git a/include/design_constants.h b/include/design_constants.h index d3d0977..52e744a 100644 --- a/include/design_constants.h +++ b/include/design_constants.h @@ -5,19 +5,27 @@ #define TREL_PI 3.1415926535898 // PI //Recamara -#define press_Pa (800.0f*6894.757f) //E25 double input +#define press_Pa (800.0*6894.757) //E25 double input -//Constantes Físicas -#define GRAVITY 9.81f // g +// Gravitational constants +#define TREL_GRAV_CONST 9.81 // m/s^2 +#define TREL_ECUATOR_GRAV_ACCEL 9.78032 // m/s^2 +#define TREL_GRAV_CRUSH 0.0053024 +#define TREL_GRAV_CRUSH_4 0.0000058 +#define TREL_GRAV_A 0.000003086 // s^-2 -#define PRESION_ATMOSFERICA 101325.0f /* E9 */ -#define HEAT_CAPACITY_RATIO 1.043f /* E46 */ -#define CONSTANTE_GASES 196.14f /* E47 */ +// Atmospheric constants +#define TREL_ATMOSPHERIC_PRESSURE 101325.0 /* E9 */ +#define TREL_ADIABATIC_GRADIENT 0.0065 +#define TREL_STANDARD_TEMP 288.15 +#define TREL_DRY_AIR_MOLAR_MASS 0.02896 +#define TREL_IDEAL_GAS_CONST 8.31447 // J/mol*K -#define FACT_CONV_BR 0.000145038f // factor de conversion para la tasa de quemado -#define FACT_CONV_M_TO_I 0.0254f //factor de conversion de metros a pulgadas +// Thermodinamic constants +#define TREL_HEAT_CAP_RATIO 1.043 /* E46 */ +#define TREL_GAS_CONST 196.14 // J/kg*K /* E47 */ -//Valores termodinámicos// -#define escape 0 // E62 double var +#define FACT_CONV_BR 0.000145038 // factor de conversion para la tasa de quemado +#define FACT_CONV_M_TO_I 0.0254 //factor de conversion de metros a pulgadas #endif // TREL_CONSTANTS diff --git a/include/design_func.h b/include/design_func.h index 758b5c7..a6637eb 100644 --- a/include/design_func.h +++ b/include/design_func.h @@ -28,5 +28,6 @@ double port_area(engine_t* engine); double long_secc_combus(engine_t* engine); double volumen_unitario(engine_t* engine); double desv_est_grains(engine_t* engine); +double throat_area(engine_t* engine); #endif //DESIGN_FUNC_H diff --git a/include/engine.h b/include/engine.h index 99e3974..31d3d5e 100644 --- a/include/engine.h +++ b/include/engine.h @@ -34,7 +34,8 @@ tubing_t TREL_EXPORT *trel_tubing_init( double shear_tension, /* E20 */ double shear_pressure, /* E19 */ double ult_tension, /* E22 */ - double ult_pressure /* E21 */ + double ult_pressure, /* E21 */ + double nozzle_efficiency ); screws_t TREL_EXPORT* trel_screws_init( //char* material, diff --git a/include/height_sim.h b/include/height_sim.h new file mode 100644 index 0000000..8265baa --- /dev/null +++ b/include/height_sim.h @@ -0,0 +1,9 @@ +#ifndef HEIGHT_SIM_H +#define HEIGHT_SIM_H + +#include "design_func.h" + +// Function prototypes +int TREL_EXPORT trel_run_height_sim_iterations(trel_rocket_t** rocket); + +#endif //HEIGHT_SIM_H diff --git a/include/rendimientos.h b/include/rendimientos.h index c74495d..c4b6cb5 100644 --- a/include/rendimientos.h +++ b/include/rendimientos.h @@ -9,6 +9,7 @@ #include "design_func.h" #include "comp_area.h" #include "comp_tiempo.h" +#include "height_sim.h" // TODO: cambiar list[] por nombres mas descriptivos diff --git a/include/rocket.h b/include/rocket.h index 2dba279..5cf136c 100644 --- a/include/rocket.h +++ b/include/rocket.h @@ -16,7 +16,11 @@ trel_rocket_t TREL_EXPORT* trel_rocket_init( double telemetry_mass, double parachute_mass, double fuselage_mass, - double payload_mass + double payload_mass, + double initial_height, + double sim_latitude, + double body_diameter, + double drag_coefficient ); #endif//ROCKET_H diff --git a/include/structs.h b/include/structs.h index c91edc5..2c3a16e 100644 --- a/include/structs.h +++ b/include/structs.h @@ -28,6 +28,7 @@ typedef struct tubing double shear_stress_tension, shear_stress_pressure; double ult_stress_tension, ult_stress_pressure; double transversal_area, material_area; + double nozzle_efficiency; } TREL_EXPORT tubing_t; // Estructura para tornillos @@ -60,8 +61,18 @@ typedef struct engine typedef struct trel_rocket { engine_t *engine; // ptr to initialized struct - double telemetry_mass, parachute_mass, fuselage_mass, payload_mass; + double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 + double sim_latitude, max_sim_height, body_diameter, drag_coefficient; + double rocket_position[1000]; + double rocket_speed[1000]; // m/s + double rocket_acceleration[1000]; + double rocket_force_balance[1000]; + double rocket_drag[1000]; + double rocket_weight[1000]; + double rocket_mass[1000]; + double rocket_force[1000]; + double time[1000]; } TREL_EXPORT trel_rocket_t; #endif //TREL_STRUCTS_H diff --git a/main.c b/main.c index ecac708..7c30ae5 100644 --- a/main.c +++ b/main.c @@ -57,7 +57,11 @@ void print_debug_example() 0.3, 0.05, 5.0, - 1.5 + 1.5, + 1207.0, + 9.936111, + 0.13, + 0,4 ); // Acceder a memoria inicializada @@ -86,6 +90,10 @@ void print_debug_example() printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); printf("Maxima presion: %lf\n", testing_rocket->max_pressure); + + // Probando valores generados por height_sim.c + for (int i = 0; i < 50; i += 10) + printf("Masa de cohete %lf\n", testing_rocket->rocket_mass[i]); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index c0b777a..023c94f 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -16,9 +16,9 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) // variables for iteration double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemetry_mass + engine->engine_mass + rocket->payload_mass; const double delta_t = step; - double pressure_abs = PRESION_ATMOSFERICA; + double pressure_abs = TREL_ATMOSPHERIC_PRESSURE; //double pressure = pressure_abs / 6894.757f; - //double pressure_man = pressure_abs - PRESION_ATMOSFERICA; + //double pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; double burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; double inst_radius = engine->grains->init_inter_radius; double inst_long = engine->grains->longitude; @@ -36,11 +36,11 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) double free_vol = port_area(engine) - fuel_vol; double internal_mass = (free_vol * pressure_abs) / (long_secc_combus(engine) * engine->fuel->const_burn_rate); //double areas_ratio = total_burn_area / engine->grains->longitude; - double virtual_escape_pressure = pressure_abs / pow((1.0 + ((HEAT_CAPACITY_RATIO - 1.0) / 2.0) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1.0))); - //double real_escape_pressure = pressure_abs / pow((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / pow((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); - double force_coeff = pow((((2.0 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1.0)) * pow((2.0 / (HEAT_CAPACITY_RATIO + 1.0)), ((HEAT_CAPACITY_RATIO + 1.0) / (HEAT_CAPACITY_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1.0) / HEAT_CAPACITY_RATIO))))), 0.5) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); - double adjusted_cr = force_coeff * desv_est_grains(engine); - double force = adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0; + double virtual_escape_pressure = pressure_abs / pow((1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0))); + //double real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); + double force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO))))), 0.5) + (((virtual_escape_pressure - TREL_ATMOSPHERIC_PRESSURE) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); + double adjusted_cf = force_coeff * engine->tube->nozzle_efficiency; + double force = adjusted_cf * pressure_abs * engine->grains->longitude > 0 ? adjusted_cf * pressure_abs * engine->grains->longitude : 0.0; double total_thrust = 0.0; double delta_v = total_thrust / (fuel_mass + (double)dead_mass); @@ -57,7 +57,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) { pressure_abs = engine->fuel->const_burn_rate * long_secc_combus(engine) * internal_mass / free_vol; //pressure = pressure_abs / 6894.757f; - //pressure_man = pressure_abs - PRESION_ATMOSFERICA; + //pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; inst_radius = inst_radius > 0 && (inst_radius + burn_rate * delta_t) < engine->grains->extern_radius ? inst_radius + burn_rate * delta_t : 0; inst_long = inst_radius > 0 ? inst_long - burn_rate * delta_t * 2 : 0; @@ -70,18 +70,18 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) produced_mass = fuel_mass - engine->fuel->density * fuel_vol; fuel_mass = engine->fuel->density * fuel_vol; //produced_massic_flux = produced_mass / delta_t; - escape_massic_flux = pressure_abs > PRESION_ATMOSFERICA ? engine->grains->longitude * pressure_abs * HEAT_CAPACITY_RATIO * (sqrt(pow((2.0f / (HEAT_CAPACITY_RATIO + 1.0f)), ((HEAT_CAPACITY_RATIO + 1.0f) / (HEAT_CAPACITY_RATIO - 1.0f))))) / pow((HEAT_CAPACITY_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; + escape_massic_flux = pressure_abs > TREL_ATMOSPHERIC_PRESSURE ? engine->grains->longitude * pressure_abs * TREL_HEAT_CAP_RATIO * (sqrt(pow((2.0f / (TREL_HEAT_CAP_RATIO + 1.0f)), ((TREL_HEAT_CAP_RATIO + 1.0f) / (TREL_HEAT_CAP_RATIO - 1.0f))))) / pow((TREL_HEAT_CAP_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; //flux_diff = produced_massic_flux - escape_massic_flux; escape_delta_m = escape_massic_flux * delta_t; internal_mass = produced_mass + internal_mass - escape_delta_m > 0.0f ? produced_mass + internal_mass - escape_delta_m : 0.0f; //areas_ratio = total_burn_area / engine->grains->longitude; - virtual_escape_pressure = pressure_abs / pow((1.0f + ((HEAT_CAPACITY_RATIO - 1.0f) / 2.0f) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1.0f))); - //real_escape_pressure = pressure_abs / pow((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))) < PRESION_ATMOSFERICA ? PRESION_ATMOSFERICA : pressure_abs / pow((1 + ((HEAT_CAPACITY_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (HEAT_CAPACITY_RATIO / (HEAT_CAPACITY_RATIO - 1))); - force_coeff = pow((((2.0 * HEAT_CAPACITY_RATIO * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO - 1.0)) * pow((2.0 / (HEAT_CAPACITY_RATIO + 1.0)), ((HEAT_CAPACITY_RATIO + 1.0) / (HEAT_CAPACITY_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((HEAT_CAPACITY_RATIO - 1.0) / HEAT_CAPACITY_RATIO))))), 0.5) + (((virtual_escape_pressure - PRESION_ATMOSFERICA) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); - adjusted_cr = force_coeff * desv_est_grains(engine); + virtual_escape_pressure = pressure_abs / pow((1.0f + ((TREL_HEAT_CAP_RATIO - 1.0f) / 2.0f) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0f))); + //real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); + force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO))))), 0.5) + (((virtual_escape_pressure - TREL_ATMOSPHERIC_PRESSURE) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); + adjusted_cf = force_coeff * desv_est_grains(engine); //total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2.0f)) * delta_t; last_force = force; - force = adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f; + force = adjusted_cf * pressure_abs * engine->grains->longitude > 0 ? adjusted_cf * pressure_abs * engine->grains->longitude : 0.0f; total_thrust = (force + last_force) / 2.0 * delta_t; delta_v = total_thrust / (fuel_mass + dead_mass); diff --git a/src/design_func.c b/src/design_func.c index 183431b..d7d6979 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -47,13 +47,14 @@ double long_secc_combus(engine_t* engine) // E49 Volumen especifico camara double volumen_camara(engine_t *engine) { - return(engine->temperature*CONSTANTE_GASES/trel_get_pressure_pa(engine)); + return(engine->temperature*TREL_GAS_CONST/trel_get_pressure_pa(engine)); } -// E62 Velocidad de escape +// E62 Velocidad de + double calc_escape_vel(engine_t* engine) { - double calc_pow = pow((PRESION_ATMOSFERICA / trel_get_pressure_pa(engine)), ((HEAT_CAPACITY_RATIO - 1.0) / HEAT_CAPACITY_RATIO)); - return(sqrt(2.0 * HEAT_CAPACITY_RATIO /(HEAT_CAPACITY_RATIO -1.0) * CONSTANTE_GASES * engine->temperature * (1.0 - calc_pow))); + double calc_pow = pow((TREL_ATMOSPHERIC_PRESSURE / trel_get_pressure_pa(engine)), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO)); + return(sqrt(2.0 * TREL_HEAT_CAP_RATIO /(TREL_HEAT_CAP_RATIO -1.0) * TREL_GAS_CONST * engine->temperature * (1.0 - calc_pow))); } /* Determinacion de fuerza sobre tapas*/ diff --git a/src/engine.c b/src/engine.c index 23f48f8..bc7893b 100644 --- a/src/engine.c +++ b/src/engine.c @@ -46,7 +46,8 @@ tubing_t* trel_tubing_init( double shear_tension, double shear_pressure, double ult_tension, - double ult_pressure) + double ult_pressure, + double nozzle_efficiency) { tubing_t *tube = (tubing_t *) malloc(sizeof(tubing_t)); if (!tube) { @@ -61,11 +62,12 @@ tubing_t* trel_tubing_init( tube->shear_stress_pressure = shear_pressure; tube->ult_stress_tension = ult_tension; tube->ult_stress_pressure = ult_pressure; - tube->mean_tubing_diameter = 0.0f; - tube->transversal_area = 0.0f; + tube->mean_tubing_diameter = 0.0; + tube->transversal_area = 0.0; tube->internal_radius = (tube->diameter_ext-(2*tube->wall_thickness))/2; /* E15 */ - tube->sector_angle = 0.0f; /* degrees */ - tube->material_area = 0.0f; + tube->sector_angle = 0.0; /* degrees */ + tube->material_area = 0.0; + tube->nozzle_efficiency = nozzle_efficiency; return(tube); } diff --git a/src/height_sim.c b/src/height_sim.c new file mode 100644 index 0000000..d1e2894 --- /dev/null +++ b/src/height_sim.c @@ -0,0 +1,141 @@ +#include "height_sim.h" +#include "stdio.h" + +// TODO: Evaluate if we need dynamic allocation for this iteration + +// TODO: CHECK ANYTHING THAT USES pressure_abs, it might be pressure instead + +#define TRANSVERSAL_AREA(DIAMETER) (TREL_PI * DIAMETER * DIAMETER / 4) + +double calc_force_balance(double rocket_force, double rocket_position, double rocket_speed, + double rocket_drag, double rocket_weight) +{ + double result = 0.0; + if (rocket_force < 0.01) + { + if (rocket_position > 0.0001) + { + result = rocket_speed > 0.0 ? rocket_drag + rocket_weight : rocket_weight - rocket_drag; + } + } + else + { + result = rocket_force - rocket_weight - rocket_drag; + } + return(result); +} + +double calc_inst_radius(double inst_radius, double burn_rate, double step, double extern_radius) +{ + return(inst_radius > 0.0 && (inst_radius + burn_rate * step) < extern_radius ? inst_radius + burn_rate * step : 0.0); +} + +double calc_burn_rate(double const_burn_rate, double pressure_exponent, double pressure) +{ + return(const_burn_rate * pow(pressure * 0.000145038, pressure_exponent) * 0.0254); +} + +double calc_inst_long(double inst_radius, double inst_long, double burn_rate, double step) +{ + return(inst_radius > 0.0 ? inst_long - burn_rate * step * 2.0 : 0.0); +} + +int trel_run_height_sim_iterations(trel_rocket_t** rocket) +{ + engine_t* engine = (*rocket)->engine; + + // iterate over given iterations and step size + const double max_iterations = 1000.0f; + const double burn_time = Tiempo_quemado_combustion((*rocket)->engine); + const double step = burn_time / max_iterations; + + // burn rate value from propellant + const double burn_rate = br_combustion((*rocket)->engine); + + // variables for iteration + double rocket_height = 0.0; + //double rocket_total_thrust = 0.0; + double fuel_volume = 0.0; + double gravitational_accel = 0.0; + double pressure_abs = 0.0; + double virtual_escape_pressure = 0.0; + double force_coeff = 0.0; + double adjusted_cf = 0.0; + double atmospheric_density = 0.0; + double atmospheric_pressure = 0.0; + double atmospheric_temp = 0.0; + double inst_radius = 0.0; + double pressure = 0.0; + double free_volume = 0.0; + double intern_gas_mass = 0.0; + double fuel_mass = 0.0; + double produced_mass = 0.0; + double escape_massic_flux = 0.0; + double inst_long = 0.0; + + // for max values + //double max_rocket_height = 0.0; + //double max_rocket_position = 0.0; + //double max_rocket_speed = 0.0; // m/s + //double max_rocket_acceleration = 0.0; + //double max_rocket_force_balance = 0.0; + //double max_rocket_drag = 0.0; + + const double total_volume = TREL_PI * (*rocket)->engine->tube->internal_radius * (*rocket)->engine->tube->internal_radius * ((*rocket)->engine->grains->grain_separation * ((*rocket)->engine->grains->amount - 1) + (*rocket)->engine->grains->longitude * (*rocket)->engine->grains->amount); + + inst_long = engine->grains->longitude; + inst_radius = (*rocket)->engine->grains->init_inter_radius * (*rocket)->engine->grains->init_inter_radius; + rocket_height = (*rocket)->initial_height; + gravitational_accel = TREL_ECUATOR_GRAV_ACCEL * (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)))) - (TREL_GRAV_A * rocket_height); + atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); + pressure = atmospheric_pressure; + atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); + atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); + fuel_volume = (*rocket)->engine->grains->amount * TREL_PI * (((*rocket)->engine->grains->extern_radius * (*rocket)->engine->grains->extern_radius) - (inst_radius * inst_radius)) * (*rocket)->engine->grains->longitude; + fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; + free_volume = total_volume - fuel_volume; + intern_gas_mass = pressure * free_volume / (TREL_GAS_CONST * (*rocket)->engine->temperature); + (*rocket)->rocket_mass[0] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; + (*rocket)->rocket_weight[0] = (*rocket)->rocket_mass[0] * gravitational_accel; + pressure_abs = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - ((TREL_ADIABATIC_GRADIENT * rocket_height) / (TREL_STANDARD_TEMP)),((gravitational_accel * TREL_DRY_AIR_MOLAR_MASS) / TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); + virtual_escape_pressure = pressure / pow((1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * volumen_unitario((*rocket)->engine) * volumen_unitario((*rocket)->engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0))); + force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure), ((TREL_HEAT_CAP_RATIO-1.0)/TREL_HEAT_CAP_RATIO))))),0.5) + (((virtual_escape_pressure - pressure_abs) / (pressure)) * ((*rocket)->engine->grains->grain_separation / (*rocket)->engine->grains->longitude)); + adjusted_cf = force_coeff * (*rocket)->engine->tube->nozzle_efficiency; + (*rocket)->rocket_force[0] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; + (*rocket)->rocket_force_balance[0] = calc_force_balance((*rocket)->rocket_force[0], (*rocket)->rocket_position[0], (*rocket)->rocket_speed[0], (*rocket)->rocket_drag[0], (*rocket)->rocket_weight[0]); + + // iteration loop + for (int it = 1; it < max_iterations; it++) + { + // step time value + if ((*rocket)->time[it - 1] + step < burn_time) + (*rocket)->time[it] = (*rocket)->time[it - 1] + step; + else + (*rocket)->time[it] = 0; + + gravitational_accel = TREL_ECUATOR_GRAV_ACCEL * (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)))) - (TREL_GRAV_A * rocket_height); + atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); + pressure = TREL_GAS_CONST * (*rocket)->engine->temperature * intern_gas_mass / free_volume > atmospheric_pressure ? TREL_GAS_CONST * (*rocket)->engine->temperature * intern_gas_mass / free_volume : atmospheric_pressure; + inst_radius = calc_inst_radius(inst_radius, calc_burn_rate(burn_rate, (*rocket)->engine->fuel->pressure_exponent, pressure), step, (*rocket)->engine->grains->extern_radius); + inst_long = calc_inst_long(inst_radius, inst_long, calc_burn_rate(burn_rate, (*rocket)->engine->fuel->pressure_exponent, pressure), step); + atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); + atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); + fuel_volume = (*rocket)->engine->grains->amount * TREL_PI * (((*rocket)->engine->grains->extern_radius * (*rocket)->engine->grains->extern_radius) - inst_radius * inst_radius) * inst_long; + produced_mass = - fuel_mass; + escape_massic_flux = pressure > atmospheric_pressure ? engine->grains->longitude * pressure * TREL_HEAT_CAP_RATIO * (sqrt(pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))))) / pow((TREL_HEAT_CAP_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; + intern_gas_mass = produced_mass + intern_gas_mass - escape_massic_flux * step > 0 ? produced_mass + intern_gas_mass - escape_massic_flux * step : 0.0; + fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; + produced_mass += fuel_mass; + free_volume = total_volume - fuel_volume; + (*rocket)->rocket_mass[it] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; + (*rocket)->rocket_weight[it] = (*rocket)->rocket_mass[it] * gravitational_accel; + (*rocket)->rocket_drag[it] = 0.5 * atmospheric_density * (*rocket)->drag_coefficient * TRANSVERSAL_AREA((*rocket)->body_diameter) * (*rocket)->rocket_speed[it - 1] * (*rocket)->rocket_speed[it - 1]; + (*rocket)->rocket_force_balance[it] = calc_force_balance((*rocket)->rocket_force[it], (*rocket)->rocket_position[it - 1], (*rocket)->rocket_speed[it - 1], (*rocket)->rocket_drag[it], (*rocket)->rocket_weight[it]); + (*rocket)->rocket_acceleration[it] = (*rocket)->rocket_force_balance[it] / (*rocket)->rocket_mass[it]; + (*rocket)->rocket_speed[it] = (*rocket)->rocket_acceleration[it] * step + (*rocket)->rocket_speed[it - 1]; + (*rocket)->rocket_position[it] = ((*rocket)->rocket_speed[it] * step + (*rocket)->rocket_position[it - 1]) * ((*rocket)->rocket_speed[it] * step + (*rocket)->rocket_position[it - 1] > 0.0); + rocket_height = (*rocket)->initial_height + (*rocket)->rocket_position[it]; + } + + return(0); // everything went well +} diff --git a/src/rendimientos.c b/src/rendimientos.c index e2879b5..48d558e 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -70,7 +70,7 @@ double emp_max(trel_rocket_t* rocket) /*E81 Impulso específico teórico*/ double I_sp_teo(engine_t *engine) { - return calc_escape_vel(engine)/GRAVITY; + return calc_escape_vel(engine)/TREL_GRAV_CONST; } /*E82 Impulso específico esperado*/ diff --git a/src/rocket.c b/src/rocket.c index 693ebe1..4bf922b 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -6,7 +6,11 @@ trel_rocket_t* trel_rocket_init( double telemetry_mass, double parachute_mass, double fuselage_mass, - double payload_mass) + double payload_mass, + double initial_height, + double sim_latitude, + double body_diameter, + double drag_coefficient) { trel_rocket_t* rocket = (trel_rocket_t*)malloc(sizeof(trel_rocket_t)); if (!rocket) { @@ -18,9 +22,18 @@ trel_rocket_t* trel_rocket_init( rocket->fuselage_mass = fuselage_mass; rocket->payload_mass = payload_mass; rocket->engine = engine; + rocket->initial_height = initial_height; + rocket->sim_latitude = sim_latitude; + rocket->max_sim_height = 0.0; + rocket->body_diameter = body_diameter; + rocket->drag_coefficient = drag_coefficient; if (trel_run_time_comp_iterations(rocket)) // 1 means there was an error { return(NULL); // NULL means the engine couldn't be properly generated } + if (trel_run_height_sim_iterations(&rocket)) // 1 means there was an error + { + return(NULL); // NULL means the engine couldn't be properly generated + } return(rocket); } diff --git a/src/val_termod.c b/src/val_termod.c index ce8f816..08068ee 100644 --- a/src/val_termod.c +++ b/src/val_termod.c @@ -5,20 +5,20 @@ // E49 Volumen Especifico Camara de Combustible double vol_esp_cam_combustion(engine_t *engine) { - return (engine->temperature * CONSTANTE_GASES) / trel_get_pressure_pa(engine); + return (engine->temperature * TREL_GAS_CONST) / trel_get_pressure_pa(engine); } // E50 Volumen Especifico de Garganta double vol_esp_garganta(engine_t *engine) { - return vol_esp_cam_combustion(engine) * (pow((HEAT_CAPACITY_RATIO + 1),(1 / HEAT_CAPACITY_RATIO - 1))); + return vol_esp_cam_combustion(engine) * (pow((TREL_HEAT_CAP_RATIO + 1),(1 / TREL_HEAT_CAP_RATIO - 1))); } // E51 Volumen Especifico Escape double vol_esp_escape(engine_t *engine) { - return vol_esp_cam_combustion(engine) * pow((trel_get_pressure_pa(engine) / PRESION_ATMOSFERICA), (1 / HEAT_CAPACITY_RATIO)); + return vol_esp_cam_combustion(engine) * pow((trel_get_pressure_pa(engine) / TREL_ATMOSPHERIC_PRESSURE), (1 / TREL_HEAT_CAP_RATIO)); } // Presiones @@ -26,7 +26,7 @@ double vol_esp_escape(engine_t *engine) // E53 presion de garganta double presion_garganta(engine_t *engine) { - return trel_get_pressure_pa(engine) * pow((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)), (HEAT_CAPACITY_RATIO)); + return trel_get_pressure_pa(engine) * pow((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)), (TREL_HEAT_CAP_RATIO)); } // Temperaturas @@ -34,14 +34,14 @@ double presion_garganta(engine_t *engine) // E56 temperatura de garganta double trel_temper_garganta(engine_t *engine) { - return engine->temperature * pow((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)),(HEAT_CAPACITY_RATIO - 1)); + return engine->temperature * pow((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)),(TREL_HEAT_CAP_RATIO - 1)); } // E57 temperatura de escape double presion_escape(engine_t *engine) { - return engine->temperature * (pow((PRESION_ATMOSFERICA / trel_get_pressure_pa(engine)),((HEAT_CAPACITY_RATIO - 1) / HEAT_CAPACITY_RATIO))); + return engine->temperature * (pow((TREL_ATMOSPHERIC_PRESSURE / trel_get_pressure_pa(engine)),((TREL_HEAT_CAP_RATIO - 1) / TREL_HEAT_CAP_RATIO))); } @@ -50,13 +50,13 @@ double presion_escape(engine_t *engine) // E59 velocidad garganta double velocidad_garganta(engine_t *engine) { - return pow(((2.0f * HEAT_CAPACITY_RATIO) / (HEAT_CAPACITY_RATIO + 1.0f) * CONSTANTE_GASES * engine->temperature),(0.5f)); + return pow(((2.0f * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO + 1.0f) * TREL_GAS_CONST * engine->temperature),(0.5f)); } // E60 Velocidad Sonica Local at double vel_sonica_local_at(engine_t *engine) { - return pow((HEAT_CAPACITY_RATIO * CONSTANTE_GASES * presion_garganta(engine)),(0.5)); + return pow((TREL_HEAT_CAP_RATIO * TREL_GAS_CONST * presion_garganta(engine)),(0.5)); } // E61 Numero Mach Mt @@ -68,7 +68,7 @@ double vel_numero_mach_mt(engine_t *engine) // E63 Velocidad Sonica Local a2 double vel_sonica_local_a2(engine_t *engine) { - return pow((HEAT_CAPACITY_RATIO * CONSTANTE_GASES * presion_escape(engine)),(0.5)); + return pow((TREL_HEAT_CAP_RATIO * TREL_GAS_CONST * presion_escape(engine)),(0.5)); } // E64 Numero Mach M2 @@ -81,5 +81,5 @@ double vel_numero_mach_m2(engine_t *engine) // E65 Velocidad de escape caracteristica double vel_escape_caract(engine_t *engine) { - return pow(pow((CONSTANTE_GASES * engine->temperature) / (HEAT_CAPACITY_RATIO *(2 / (HEAT_CAPACITY_RATIO + 1))),((HEAT_CAPACITY_RATIO + 1) / (HEAT_CAPACITY_RATIO - 1))), (0.5)); + return pow(pow((TREL_GAS_CONST * engine->temperature) / (TREL_HEAT_CAP_RATIO *(2 / (TREL_HEAT_CAP_RATIO + 1))),((TREL_HEAT_CAP_RATIO + 1) / (TREL_HEAT_CAP_RATIO - 1))), (0.5)); } From a6ae9fadb4f5f3724a68b6469b25112e66a05b9c Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 1 Apr 2023 18:07:27 -0600 Subject: [PATCH 17/42] Fixed compilation in UNIX Small bug fixes, nothing important --- CMakeLists.txt | 3 ++- include/TREL.h | 6 +++--- main.c | 21 +++++++++++---------- src/engine.c | 6 ++++++ src/rocket.c | 2 +- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a6d2c1..401854a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,8 @@ ADD_LIBRARY (TREL ${LIB_TYPE} "src/design_func.c" "src/design_resist.c" "src/val_termod.c" "src/rocket.c" - "src/height_sim.c") + "src/height_sim.c" +) # Specify the directory to contain header files INCLUDE_DIRECTORIES ("include") diff --git a/include/TREL.h b/include/TREL.h index 6421a30..ded99f4 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -1,8 +1,6 @@ #ifndef TREL_H #define TREL_H -//#include "design_func.h" - // Types // Comp area calculations typedef struct { @@ -96,7 +94,8 @@ tubing_t *trel_tubing_init( double shear_tension, /* E20 */ double shear_pressure, /* E19 */ double ult_tension, /* E22 */ - double ult_pressure /* E21 */ + double ult_pressure, /* E21 */ + double nozzle_efficiency ); screws_t *trel_screws_init( // char* material, @@ -133,5 +132,6 @@ double trel_temper_garganta(engine_t *engine); double br_combustion(engine_t *engine); int trel_run_area_comp_iterations(engine_t **engine); int trel_run_time_comp_iterations(trel_rocket_t* rocket); +int trel_run_height_sim_iterations(trel_rocket_t** rocket); #endif //TREL_H diff --git a/main.c b/main.c index 7c30ae5..9ba82b8 100644 --- a/main.c +++ b/main.c @@ -39,7 +39,8 @@ void print_debug_example() 275000000.0, -1, 310000000, - 205000000 + 205000000, + 0.85 ); // Se inicializa el motor usando los tornillos y tuberia creados engine_t* testing_engine = trel_engine_init( @@ -53,15 +54,15 @@ void print_debug_example() ); // Se inicializa el cohete trel_rocket_t* testing_rocket = trel_rocket_init( - testing_engine, - 0.3, - 0.05, - 5.0, - 1.5, - 1207.0, - 9.936111, - 0.13, - 0,4 + testing_engine, // engine + 0.3, // telemetry_mass + 0.05, // parachute_mass + 5.0, // fuselage_mass + 1.5, // payload_mass + 1207.0, // initial_height + 9.936111, // sim_latitude + 0.13, // body_diameter + 0.4 // drag_coefficient ); // Acceder a memoria inicializada diff --git a/src/engine.c b/src/engine.c index bc7893b..a7c5f55 100644 --- a/src/engine.c +++ b/src/engine.c @@ -11,6 +11,7 @@ grains_t* trel_grains_init( grains_t *grains = (grains_t *) malloc(sizeof(grains_t)); if (!grains) { printf("Grains initialization failed\n"); + exit(1); } grains->amount = amount; grains->init_inter_radius = internal_radius; @@ -29,6 +30,7 @@ fuel_t* trel_fuel_init( fuel_t *fuel = (fuel_t *) malloc(sizeof(fuel_t)); if (!fuel) { printf("Fuel initialization failed\n"); + exit(1); } fuel->const_burn_rate = const_burn_rate; fuel->pressure_exponent = pressure_exponent; @@ -104,6 +106,10 @@ engine_t* trel_engine_init( screws_t *screws) { comp_area_t *comp_area_values = (comp_area_t*) malloc(sizeof(comp_area_t)); + if (!comp_area_values) { + printf("Engine initialization failed due to comp_area_values\n"); + exit(1); + } comp_area_values->avg_burn_area = 0.0f; comp_area_values->avg_long_area = 0.0f; comp_area_values->avg_trans_area = 0.0f; diff --git a/src/rocket.c b/src/rocket.c index 4bf922b..0346d44 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -1,7 +1,7 @@ #include "rocket.h" /* Initializes all the variables in the rocket struct */ -trel_rocket_t* trel_rocket_init( +trel_rocket_t *trel_rocket_init( engine_t* engine, double telemetry_mass, double parachute_mass, From 58053f8d9f7041e8f1c448f44393fc8d7413fe1d Mon Sep 17 00:00:00 2001 From: lross2k Date: Sun, 2 Apr 2023 07:14:56 -0600 Subject: [PATCH 18/42] Correctly implemented material names Due to commit 6726605, the Screws and Tubing's materials where disabled The memory allocation was left as an exercise to the compiler, which kinda worked but left an UB When trying to access the library from a program compiled with a different toolchain, or a language binding this resulted in chaos TREL_MAX_STR_LEN is set to 100 characters Any string from now on will be defined as char name[TREL_MAX_STR_LEN] and passed to functions as char *name -lross2k --- include/TREL.h | 8 ++++---- include/design_constants.h | 2 ++ include/engine.h | 4 ++-- include/structs.h | 5 +++-- main.c | 34 ++++++++++++++++++++++++++++++++-- src/engine.c | 11 ++++++----- 6 files changed, 49 insertions(+), 15 deletions(-) diff --git a/include/TREL.h b/include/TREL.h index ded99f4..b47579b 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -21,7 +21,7 @@ typedef struct { // Estructura para tuberia typedef struct tubing { - //char* material; + char material[100]; double diameter_ext, wall_thickness, internal_radius; double young_module, sector_angle, mean_tubing_diameter; double shear_stress_tension, shear_stress_pressure; @@ -35,7 +35,7 @@ typedef struct screws double diameter, dist_center_wall; unsigned int amount; double area_per_screw, screw_occupied_area; - char* material; + char material[100]; double width_cutting_segment; } screws_t; @@ -87,7 +87,7 @@ fuel_t *trel_fuel_init( double density /* E44 */ ); tubing_t *trel_tubing_init( - //char* material, /* E16 & E17 */ + char* material, /* E16 & E17 */ double diameter, /* E13 */ double thickness, /* E14 */ double young_module, /* E18 */ @@ -98,7 +98,7 @@ tubing_t *trel_tubing_init( double nozzle_efficiency ); screws_t *trel_screws_init( -// char* material, + char* material, unsigned int amount, double diameter, double dist_center_wall diff --git a/include/design_constants.h b/include/design_constants.h index 52e744a..2b070eb 100644 --- a/include/design_constants.h +++ b/include/design_constants.h @@ -1,6 +1,8 @@ #ifndef TREL_CONSTANTS #define TREL_CONSTANTS +#define TREL_MAX_STR_LEN 100 + //Constantes matemáticas #define TREL_PI 3.1415926535898 // PI diff --git a/include/engine.h b/include/engine.h index 31d3d5e..9b839a1 100644 --- a/include/engine.h +++ b/include/engine.h @@ -27,7 +27,7 @@ fuel_t TREL_EXPORT *trel_fuel_init( double density /* E44 */ ); tubing_t TREL_EXPORT *trel_tubing_init( - //char *material, /* E16 & E17 */ + char *material, /* E16 & E17 */ double diameter, /* E13 */ double thickness, /* E14 */ double young_module, /* E18 */ @@ -38,7 +38,7 @@ tubing_t TREL_EXPORT *trel_tubing_init( double nozzle_efficiency ); screws_t TREL_EXPORT* trel_screws_init( - //char* material, + char* material, unsigned int amount, double diameter, double dist_center_wall diff --git a/include/structs.h b/include/structs.h index 2c3a16e..b1de5c7 100644 --- a/include/structs.h +++ b/include/structs.h @@ -1,6 +1,7 @@ #ifndef TREL_STRUCTS_H #define TREL_STRUCTS_H +#include "design_constants.h" #include "DLLDefines.h" // Comp area calculations @@ -22,7 +23,7 @@ typedef struct { // Estructura para tuberia typedef struct tubing { - //char *material; + char material[TREL_MAX_STR_LEN]; double diameter_ext, wall_thickness, internal_radius; double young_module, sector_angle, mean_tubing_diameter; double shear_stress_tension, shear_stress_pressure; @@ -37,7 +38,7 @@ typedef struct screws double diameter, dist_center_wall; unsigned int amount; double area_per_screw, screw_occupied_area; - //char *material; + char material[TREL_MAX_STR_LEN]; double width_cutting_segment; } TREL_EXPORT screws_t; diff --git a/main.c b/main.c index 9ba82b8..c848ab0 100644 --- a/main.c +++ b/main.c @@ -21,7 +21,7 @@ void print_debug_example() 1859 ); screws_t* created_screw = trel_screws_init( -// "Acero", + "Acero", 6, 0.007466, 0.01 @@ -32,7 +32,7 @@ void print_debug_example() // Se inicializa la tuberia tubing_t* created_tube = trel_tubing_init( - //"Aluminio 6061-T6", + "Aluminio 6061-T6", 0.073, 0.0052, 68900000000.0, @@ -95,6 +95,36 @@ void print_debug_example() // Probando valores generados por height_sim.c for (int i = 0; i < 50; i += 10) printf("Masa de cohete %lf\n", testing_rocket->rocket_mass[i]); + + // Printing rocket values + printf("\nRocket data\n"); + printf("telemetry_mass %lf\n", testing_rocket->telemetry_mass); + printf("parachute_mass %lf\n", testing_rocket->parachute_mass); + printf("fuselage_mass %lf\n", testing_rocket->fuselage_mass); + printf("payload_mass %lf\n", testing_rocket->payload_mass); + printf("initial_height %lf\n", testing_rocket->initial_height); + printf("avg_thrust %lf\n", testing_rocket->avg_thrust); + printf("max_thrust %lf\n", testing_rocket->max_thrust); + printf("delta_v %lf\n", testing_rocket->delta_v); + printf("max_pressure %lf\n", testing_rocket->max_pressure); + printf("sim_latitude %lf\n", testing_rocket->sim_latitude); + printf("max_sim_height %lf\n", testing_rocket->max_sim_height); + printf("body_diameter %lf\n", testing_rocket->body_diameter); + printf("drag_coefficient %lf\n", testing_rocket->drag_coefficient); + /* + printf("rocket_position %lf\n", testing_rocket->rocket_position); + printf("rocket_speed %lf\n", testing_rocket->rocket_speed); + printf("rocket_acceleration %lf\n", testing_rocket->rocket_acceleration); + printf("rocket_force_balance %lf\n", testing_rocket->rocket_force_balance); + printf("rocket_drag %lf\n", testing_rocket->rocket_drag); + printf("rocket_weight %lf\n", testing_rocket->rocket_weight); + printf("rocket_mass %lf\n", testing_rocket->rocket_mass); + printf("rocket_force %lf\n", testing_rocket->rocket_force); + printf("time %lf\n", testing_rocket->time); + */ + + printf("Tubing's material name: %s\n", created_tube->material); + printf("Screws' material name: %s\n", created_screw->material); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica diff --git a/src/engine.c b/src/engine.c index a7c5f55..3f7f514 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,3 +1,4 @@ +#include #include "engine.h" /* Initializes all variables in the grains struct */ @@ -41,7 +42,7 @@ fuel_t* trel_fuel_init( /* Initializes all the variables in the tube struct */ tubing_t* trel_tubing_init( -// char *material, + char *material, double diameter, double thickness, double young_module, @@ -56,7 +57,7 @@ tubing_t* trel_tubing_init( printf("Tubing initialization failed\n"); exit(1); } - //tube->material = material; + strcpy_s(tube->material, sizeof(char) * TREL_MAX_STR_LEN, material); tube->diameter_ext = diameter; tube->wall_thickness = thickness; tube->young_module = young_module; @@ -66,7 +67,7 @@ tubing_t* trel_tubing_init( tube->ult_stress_pressure = ult_pressure; tube->mean_tubing_diameter = 0.0; tube->transversal_area = 0.0; - tube->internal_radius = (tube->diameter_ext-(2*tube->wall_thickness))/2; /* E15 */ + tube->internal_radius = (tube->diameter_ext-(2.0*tube->wall_thickness))/2.0; /* E15 */ tube->sector_angle = 0.0; /* degrees */ tube->material_area = 0.0; tube->nozzle_efficiency = nozzle_efficiency; @@ -75,7 +76,7 @@ tubing_t* trel_tubing_init( /* Initializes all the variables in the screws struct */ screws_t* trel_screws_init( - //char *material, + char *material, unsigned int amount, double diameter, double dist_center_wall) @@ -85,7 +86,7 @@ screws_t* trel_screws_init( printf("Screws initialization failed\n"); exit(1); } - //screws->material = material; + strcpy_s(screws->material, sizeof(char) * TREL_MAX_STR_LEN, material); screws->diameter = diameter; screws->amount = amount; screws->dist_center_wall = dist_center_wall; From 2b3dd6893976d6d2a9767592e2e1d93bdcaee836 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sun, 2 Apr 2023 08:12:57 -0600 Subject: [PATCH 19/42] Implemented max values in height sim Had to re-structure structs.h so memory allocation for results is more easy to visualize --- TODO.md | 5 ++--- include/TREL.h | 24 +++++++++++++++----- include/structs.h | 24 +++++++++++++++----- main.c | 2 +- src/height_sim.c | 57 ++++++++++++++++++++++++++--------------------- src/rocket.c | 14 ++++++++++++ 6 files changed, 84 insertions(+), 42 deletions(-) diff --git a/TODO.md b/TODO.md index 9bc41ca..0f7e574 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,6 @@ # TODO list for the project -1. Finish height simulation (obtaining max values) +1. Change from C99 to the more portable C89 [ANSI C] 2. Implement memory free functions, and destroy all pointers 3. Define and apply standard function convention 4. Define and apply standard style guide -5. Change from C99 to the more portable C89 [ANSI C] -6. Implement Python library interface +5. Implement Python library interface diff --git a/include/TREL.h b/include/TREL.h index b47579b..f4a4e76 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -55,13 +55,9 @@ typedef struct engine comp_area_t *comp_area_values; // ptr to initialized struct } engine_t; -// Estructura del cohete -typedef struct trel_rocket +// Struct for height simulation results +typedef struct trel_height_sim_values { - engine_t* engine; // ptr to initialized struct - double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; - double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 - double sim_latitude, max_sim_height, body_diameter, drag_coefficient; double rocket_position[1000]; double rocket_speed[1000]; // m/s double rocket_acceleration[1000]; @@ -71,6 +67,22 @@ typedef struct trel_rocket double rocket_mass[1000]; double rocket_force[1000]; double time[1000]; + double max_rocket_height; + double max_rocket_position; + double max_rocket_speed; // m/s + double max_rocket_acceleration; + double max_rocket_force_balance; + double max_rocket_drag; +} trel_height_sim_t; + +// Struct for the rocket +typedef struct trel_rocket +{ + engine_t* engine; // ptr to initialized struct + double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; + double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 + double sim_latitude, max_sim_height, body_diameter, drag_coefficient; + trel_height_sim_t* sim_values; } trel_rocket_t; // Function prototypes diff --git a/include/structs.h b/include/structs.h index b1de5c7..9ec3be8 100644 --- a/include/structs.h +++ b/include/structs.h @@ -58,13 +58,9 @@ typedef struct engine comp_area_t *comp_area_values; // ptr to initialized struct } TREL_EXPORT engine_t; -// Struct for the whole rocket simulation -typedef struct trel_rocket +// Struct for height simulation results +typedef struct trel_height_sim_values { - engine_t *engine; // ptr to initialized struct - double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; - double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 - double sim_latitude, max_sim_height, body_diameter, drag_coefficient; double rocket_position[1000]; double rocket_speed[1000]; // m/s double rocket_acceleration[1000]; @@ -74,6 +70,22 @@ typedef struct trel_rocket double rocket_mass[1000]; double rocket_force[1000]; double time[1000]; + double max_rocket_height; + double max_rocket_position; + double max_rocket_speed; // m/s + double max_rocket_acceleration; + double max_rocket_force_balance; + double max_rocket_drag; +} TREL_EXPORT trel_height_sim_t; + +// Struct for the rocket +typedef struct trel_rocket +{ + engine_t *engine; // ptr to initialized struct + double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; + double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 + double sim_latitude, max_sim_height, body_diameter, drag_coefficient; + trel_height_sim_t *sim_values; } TREL_EXPORT trel_rocket_t; #endif //TREL_STRUCTS_H diff --git a/main.c b/main.c index c848ab0..0007895 100644 --- a/main.c +++ b/main.c @@ -94,7 +94,7 @@ void print_debug_example() // Probando valores generados por height_sim.c for (int i = 0; i < 50; i += 10) - printf("Masa de cohete %lf\n", testing_rocket->rocket_mass[i]); + printf("Masa de cohete %lf\n", testing_rocket->sim_values->rocket_mass[i]); // Printing rocket values printf("\nRocket data\n"); diff --git a/src/height_sim.c b/src/height_sim.c index d1e2894..7e58301 100644 --- a/src/height_sim.c +++ b/src/height_sim.c @@ -1,8 +1,6 @@ #include "height_sim.h" #include "stdio.h" -// TODO: Evaluate if we need dynamic allocation for this iteration - // TODO: CHECK ANYTHING THAT USES pressure_abs, it might be pressure instead #define TRANSVERSAL_AREA(DIAMETER) (TREL_PI * DIAMETER * DIAMETER / 4) @@ -42,7 +40,8 @@ double calc_inst_long(double inst_radius, double inst_long, double burn_rate, do int trel_run_height_sim_iterations(trel_rocket_t** rocket) { - engine_t* engine = (*rocket)->engine; + engine_t *engine = (*rocket)->engine; + trel_height_sim_t *res = (*rocket)->sim_values; // iterate over given iterations and step size const double max_iterations = 1000.0f; @@ -73,14 +72,6 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) double escape_massic_flux = 0.0; double inst_long = 0.0; - // for max values - //double max_rocket_height = 0.0; - //double max_rocket_position = 0.0; - //double max_rocket_speed = 0.0; // m/s - //double max_rocket_acceleration = 0.0; - //double max_rocket_force_balance = 0.0; - //double max_rocket_drag = 0.0; - const double total_volume = TREL_PI * (*rocket)->engine->tube->internal_radius * (*rocket)->engine->tube->internal_radius * ((*rocket)->engine->grains->grain_separation * ((*rocket)->engine->grains->amount - 1) + (*rocket)->engine->grains->longitude * (*rocket)->engine->grains->amount); inst_long = engine->grains->longitude; @@ -95,23 +86,23 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; free_volume = total_volume - fuel_volume; intern_gas_mass = pressure * free_volume / (TREL_GAS_CONST * (*rocket)->engine->temperature); - (*rocket)->rocket_mass[0] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; - (*rocket)->rocket_weight[0] = (*rocket)->rocket_mass[0] * gravitational_accel; + res->rocket_mass[0] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; + res->rocket_weight[0] = res->rocket_mass[0] * gravitational_accel; pressure_abs = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - ((TREL_ADIABATIC_GRADIENT * rocket_height) / (TREL_STANDARD_TEMP)),((gravitational_accel * TREL_DRY_AIR_MOLAR_MASS) / TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); virtual_escape_pressure = pressure / pow((1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * volumen_unitario((*rocket)->engine) * volumen_unitario((*rocket)->engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0))); force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure), ((TREL_HEAT_CAP_RATIO-1.0)/TREL_HEAT_CAP_RATIO))))),0.5) + (((virtual_escape_pressure - pressure_abs) / (pressure)) * ((*rocket)->engine->grains->grain_separation / (*rocket)->engine->grains->longitude)); adjusted_cf = force_coeff * (*rocket)->engine->tube->nozzle_efficiency; - (*rocket)->rocket_force[0] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; - (*rocket)->rocket_force_balance[0] = calc_force_balance((*rocket)->rocket_force[0], (*rocket)->rocket_position[0], (*rocket)->rocket_speed[0], (*rocket)->rocket_drag[0], (*rocket)->rocket_weight[0]); + res->rocket_force[0] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; + res->rocket_force_balance[0] = calc_force_balance(res->rocket_force[0], res->rocket_position[0], res->rocket_speed[0], res->rocket_drag[0], res->rocket_weight[0]); // iteration loop for (int it = 1; it < max_iterations; it++) { // step time value - if ((*rocket)->time[it - 1] + step < burn_time) - (*rocket)->time[it] = (*rocket)->time[it - 1] + step; + if (res->time[it - 1] + step < burn_time) + res->time[it] = res->time[it - 1] + step; else - (*rocket)->time[it] = 0; + res->time[it] = 0; gravitational_accel = TREL_ECUATOR_GRAV_ACCEL * (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)))) - (TREL_GRAV_A * rocket_height); atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); @@ -127,14 +118,28 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; produced_mass += fuel_mass; free_volume = total_volume - fuel_volume; - (*rocket)->rocket_mass[it] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; - (*rocket)->rocket_weight[it] = (*rocket)->rocket_mass[it] * gravitational_accel; - (*rocket)->rocket_drag[it] = 0.5 * atmospheric_density * (*rocket)->drag_coefficient * TRANSVERSAL_AREA((*rocket)->body_diameter) * (*rocket)->rocket_speed[it - 1] * (*rocket)->rocket_speed[it - 1]; - (*rocket)->rocket_force_balance[it] = calc_force_balance((*rocket)->rocket_force[it], (*rocket)->rocket_position[it - 1], (*rocket)->rocket_speed[it - 1], (*rocket)->rocket_drag[it], (*rocket)->rocket_weight[it]); - (*rocket)->rocket_acceleration[it] = (*rocket)->rocket_force_balance[it] / (*rocket)->rocket_mass[it]; - (*rocket)->rocket_speed[it] = (*rocket)->rocket_acceleration[it] * step + (*rocket)->rocket_speed[it - 1]; - (*rocket)->rocket_position[it] = ((*rocket)->rocket_speed[it] * step + (*rocket)->rocket_position[it - 1]) * ((*rocket)->rocket_speed[it] * step + (*rocket)->rocket_position[it - 1] > 0.0); - rocket_height = (*rocket)->initial_height + (*rocket)->rocket_position[it]; + res->rocket_mass[it] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; + res->rocket_weight[it] = res->rocket_mass[it] * gravitational_accel; + res->rocket_drag[it] = 0.5 * atmospheric_density * (*rocket)->drag_coefficient * TRANSVERSAL_AREA((*rocket)->body_diameter) * res->rocket_speed[it - 1] * res->rocket_speed[it - 1]; + res->rocket_force_balance[it] = calc_force_balance(res->rocket_force[it], res->rocket_position[it - 1], res->rocket_speed[it - 1], res->rocket_drag[it], res->rocket_weight[it]); + res->rocket_acceleration[it] = res->rocket_force_balance[it] / res->rocket_mass[it]; + res->rocket_speed[it] = res->rocket_acceleration[it] * step + res->rocket_speed[it - 1]; + res->rocket_position[it] = (res->rocket_speed[it] * step + res->rocket_position[it - 1]) * (res->rocket_speed[it] * step + res->rocket_position[it - 1] > 0.0); + rocket_height = (*rocket)->initial_height + res->rocket_position[it]; + + // Check for new max values + if (rocket_height > res->max_rocket_height) + res->max_rocket_height = rocket_height; + if (res->rocket_position[it] > res->max_rocket_position) + res->max_rocket_position = res->rocket_position[it]; + if (res->rocket_speed[it] > res->max_rocket_speed) + res->max_rocket_speed = res->rocket_speed[it]; + if (res->rocket_acceleration[it] > res->max_rocket_acceleration) + res->max_rocket_acceleration = res->rocket_acceleration[it]; + if (res->rocket_force_balance[it] > res->max_rocket_force_balance) + res->max_rocket_force_balance = res->rocket_force_balance[it]; + if (res->rocket_drag[it] > res->max_rocket_drag) + res->max_rocket_drag = res->rocket_drag[it]; } return(0); // everything went well diff --git a/src/rocket.c b/src/rocket.c index 0346d44..98ad4cb 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -17,6 +17,19 @@ trel_rocket_t *trel_rocket_init( printf("Rocket initialization failed\n"); exit(1); } + + trel_height_sim_t* sim_values = (trel_height_sim_t*)malloc(sizeof(trel_height_sim_t)); + if (!sim_values) { + printf("Engine initialization failed due to sim_values\n"); + exit(1); + } + sim_values->max_rocket_acceleration = 0.0; + sim_values->max_rocket_drag = 0.0; + sim_values->max_rocket_force_balance = 0.0; + sim_values->max_rocket_height = 0.0; + sim_values->max_rocket_position = 0.0; + sim_values->max_rocket_speed = 0.0; + rocket->telemetry_mass = telemetry_mass; rocket->parachute_mass = parachute_mass; rocket->fuselage_mass = fuselage_mass; @@ -27,6 +40,7 @@ trel_rocket_t *trel_rocket_init( rocket->max_sim_height = 0.0; rocket->body_diameter = body_diameter; rocket->drag_coefficient = drag_coefficient; + rocket->sim_values = sim_values; if (trel_run_time_comp_iterations(rocket)) // 1 means there was an error { return(NULL); // NULL means the engine couldn't be properly generated From 208ab8e41123aecc3eb22d23d0aec8366d22d88d Mon Sep 17 00:00:00 2001 From: lross2k Date: Mon, 3 Apr 2023 08:38:00 -0600 Subject: [PATCH 20/42] Refactoring, trying to fix a bug in height sim A disaster in process of being fixed -lross2k --- include/design_constants.h | 1 + include/design_func.h | 1 + include/structs.h | 18 +++--- main.c | 34 +++++----- src/design_func.c | 4 +- src/height_sim.c | 127 +++++++++++++++++++++++++++---------- src/rocket.c | 13 ++++ src/val_termod.c | 2 +- 8 files changed, 136 insertions(+), 64 deletions(-) diff --git a/include/design_constants.h b/include/design_constants.h index 2b070eb..b5f22ec 100644 --- a/include/design_constants.h +++ b/include/design_constants.h @@ -2,6 +2,7 @@ #define TREL_CONSTANTS #define TREL_MAX_STR_LEN 100 +#define TREL_MAX_ITERATIONS 1000 //Constantes matemáticas #define TREL_PI 3.1415926535898 // PI diff --git a/include/design_func.h b/include/design_func.h index a6637eb..3b2c94d 100644 --- a/include/design_func.h +++ b/include/design_func.h @@ -29,5 +29,6 @@ double long_secc_combus(engine_t* engine); double volumen_unitario(engine_t* engine); double desv_est_grains(engine_t* engine); double throat_area(engine_t* engine); +double escape_area(engine_t* engine); #endif //DESIGN_FUNC_H diff --git a/include/structs.h b/include/structs.h index 9ec3be8..2ad2e87 100644 --- a/include/structs.h +++ b/include/structs.h @@ -61,15 +61,15 @@ typedef struct engine // Struct for height simulation results typedef struct trel_height_sim_values { - double rocket_position[1000]; - double rocket_speed[1000]; // m/s - double rocket_acceleration[1000]; - double rocket_force_balance[1000]; - double rocket_drag[1000]; - double rocket_weight[1000]; - double rocket_mass[1000]; - double rocket_force[1000]; - double time[1000]; + double rocket_position[TREL_MAX_ITERATIONS]; + double rocket_speed[TREL_MAX_ITERATIONS]; // m/s + double rocket_acceleration[TREL_MAX_ITERATIONS]; + double rocket_force_balance[TREL_MAX_ITERATIONS]; + double rocket_drag[TREL_MAX_ITERATIONS]; + double rocket_weight[TREL_MAX_ITERATIONS]; + double rocket_mass[TREL_MAX_ITERATIONS]; + double rocket_force[TREL_MAX_ITERATIONS]; + double time[TREL_MAX_ITERATIONS]; double max_rocket_height; double max_rocket_position; double max_rocket_speed; // m/s diff --git a/main.c b/main.c index 0007895..46fe297 100644 --- a/main.c +++ b/main.c @@ -66,35 +66,31 @@ void print_debug_example() ); // Acceder a memoria inicializada - printf("La presion del motor dada es %lf psi\n",trel_get_pressure(testing_engine)); - printf("\nLa velocidad de escape automaticamente inicializada es %lf\n", trel_get_escape_vel(testing_engine)); + //printf("La presion del motor dada es %lf psi\n",trel_get_pressure(testing_engine)); + //printf("\nLa velocidad de escape automaticamente inicializada es %lf\n", trel_get_escape_vel(testing_engine)); // Modificar valores de memoria con funciones - trel_set_escape_vel(testing_engine, 666.420); - printf("\nLa velocidad de escape modificada con tsel_set_escape_vel() es %lf\n", trel_get_escape_vel(testing_engine)); + //trel_set_escape_vel(testing_engine, 666.420); + //printf("\nLa velocidad de escape modificada con tsel_set_escape_vel() es %lf\n", trel_get_escape_vel(testing_engine)); // Usando una funcion de rendimientos.c - printf("\nEl valor de combuistion es %lf\n", br_combustion(testing_engine)); + //printf("\nEl valor de combuistion es %lf\n", br_combustion(testing_engine)); // Usando una funcion de val_termod.c - printf("\nLa temperatura en garganta es de %lf\n", trel_temper_garganta(testing_engine)); + //printf("\nLa temperatura en garganta es de %lf\n", trel_temper_garganta(testing_engine)); // Probando valores generados por comp_area.c - printf("\nArea longitudinal promedio: %lf\n", testing_engine->comp_area_values->avg_long_area); - printf("Area quemado promedio: %lf\n", testing_engine->comp_area_values->avg_burn_area); - printf("Area transversal promedio: %lf\n", testing_engine->comp_area_values->avg_trans_area); - printf("Suma de diferencia quemado: %lf\n", testing_engine->comp_area_values->burn_sum_diff); - printf("Desviacion estandar area de quemado: %lf\n", testing_engine->comp_area_values->burn_std_deviation); + //printf("\nArea longitudinal promedio: %lf\n", testing_engine->comp_area_values->avg_long_area); + //printf("Area quemado promedio: %lf\n", testing_engine->comp_area_values->avg_burn_area); + //printf("Area transversal promedio: %lf\n", testing_engine->comp_area_values->avg_trans_area); + //printf("Suma de diferencia quemado: %lf\n", testing_engine->comp_area_values->burn_sum_diff); + //printf("Desviacion estandar area de quemado: %lf\n", testing_engine->comp_area_values->burn_std_deviation); // Probando valores generados por comp_tiempo.c - printf("\nDelta V total: %lf\n", testing_rocket->delta_v); - printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); - printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); - printf("Maxima presion: %lf\n", testing_rocket->max_pressure); - - // Probando valores generados por height_sim.c - for (int i = 0; i < 50; i += 10) - printf("Masa de cohete %lf\n", testing_rocket->sim_values->rocket_mass[i]); + //printf("\nDelta V total: %lf\n", testing_rocket->delta_v); + //printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); + //printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); + //printf("Maxima presion: %lf\n", testing_rocket->max_pressure); // Printing rocket values printf("\nRocket data\n"); diff --git a/src/design_func.c b/src/design_func.c index d7d6979..61b6476 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -160,13 +160,13 @@ double diferencia_radio_externo(engine_t *engine) // E88 double throat_area(engine_t *engine) { - return((double)mg_combustion(engine) * vol_esp_garganta(engine) / velocidad_garganta(engine)); + return(mg_combustion(engine) * vol_esp_garganta(engine) / velocidad_garganta(engine)); } // E89 double escape_area(engine_t* engine) { - return((double)mg_combustion(engine) * vol_esp_escape(engine) / calc_escape_vel(engine)); + return(mg_combustion(engine) * vol_esp_escape(engine) / calc_escape_vel(engine)); } // E101 engine->nozzle_efficiency diff --git a/src/height_sim.c b/src/height_sim.c index 7e58301..6478656 100644 --- a/src/height_sim.c +++ b/src/height_sim.c @@ -25,12 +25,28 @@ double calc_force_balance(double rocket_force, double rocket_position, double ro double calc_inst_radius(double inst_radius, double burn_rate, double step, double extern_radius) { - return(inst_radius > 0.0 && (inst_radius + burn_rate * step) < extern_radius ? inst_radius + burn_rate * step : 0.0); + double val = 0.0; + if (inst_radius > 0.0) + { + if ((inst_radius + burn_rate * step) < extern_radius) + { + val = inst_radius + burn_rate * step; + } + else + { + val = 0; + } + } + else + { + val = 0; + } + return(val); } -double calc_burn_rate(double const_burn_rate, double pressure_exponent, double pressure) +double calc_burn_rate(engine_t *engine, double pressure) { - return(const_burn_rate * pow(pressure * 0.000145038, pressure_exponent) * 0.0254); + return(engine->fuel->const_burn_rate * pow(pressure * 0.000145038, engine->fuel->pressure_exponent) * 0.0254); } double calc_inst_long(double inst_radius, double inst_long, double burn_rate, double step) @@ -38,26 +54,63 @@ double calc_inst_long(double inst_radius, double inst_long, double burn_rate, do return(inst_radius > 0.0 ? inst_long - burn_rate * step * 2.0 : 0.0); } +double calc_iter_pressure(engine_t *engine, double intern_gas_mass, double free_volume, + double atmospheric_pressure) +{ + double val = 0.0; + if (TREL_GAS_CONST * engine->temperature * intern_gas_mass / free_volume > atmospheric_pressure) + { + val = TREL_GAS_CONST * engine->temperature * intern_gas_mass / free_volume; + } + else + { + val = atmospheric_pressure; + } + return(val); +} + +double calc_iter_grav_accel(trel_rocket_t **rocket, double rocket_height) +{ + double val = 0.0; + val = TREL_ECUATOR_GRAV_ACCEL; + val *= (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)))); + val -= (TREL_GRAV_A * rocket_height); + return(val); +} + + //printf("DEBUG 000 $E$36 %lf , $E$37 %lf", e36, e37); + //printf("DEBUG 001 $E$39 %lf", e39); +double calc_iter_force_coeff(double virt_escape_pressure, double pressure, double atmospheric_pressure, engine_t *engine) +{ + double val = pow((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1) / (TREL_HEAT_CAP_RATIO - 1))) * ((1 - pow((virt_escape_pressure / pressure), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO))))), 0.5); + val += (virt_escape_pressure - atmospheric_pressure) / pressure * escape_area(engine) / throat_area(engine); + return(val); +} + +double calc_iter_virt_escape_pressure(double pressure, engine_t *engine) +{ + double val = 0.0; + //D69 / (1 + (($E$44 - 1) / 2) * $E$39 ^ 2) ^ ($E$44 / ($E$44 - 1)); + val = pressure / pow(1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * vel_numero_mach_m2(engine) * vel_numero_mach_m2(engine), TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0)); + return(val); +} + int trel_run_height_sim_iterations(trel_rocket_t** rocket) { engine_t *engine = (*rocket)->engine; trel_height_sim_t *res = (*rocket)->sim_values; + res->time[0] = 0; // iterate over given iterations and step size - const double max_iterations = 1000.0f; + const unsigned int max_iterations = TREL_MAX_ITERATIONS; const double burn_time = Tiempo_quemado_combustion((*rocket)->engine); - const double step = burn_time / max_iterations; - - // burn rate value from propellant - const double burn_rate = br_combustion((*rocket)->engine); + const double step = burn_time / TREL_MAX_ITERATIONS; // variables for iteration double rocket_height = 0.0; //double rocket_total_thrust = 0.0; double fuel_volume = 0.0; double gravitational_accel = 0.0; - double pressure_abs = 0.0; - double virtual_escape_pressure = 0.0; double force_coeff = 0.0; double adjusted_cf = 0.0; double atmospheric_density = 0.0; @@ -71,56 +124,64 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) double produced_mass = 0.0; double escape_massic_flux = 0.0; double inst_long = 0.0; + double prev_fuel_mass = 0.0; + double burn_rate = 0.0; + double virt_escape_pressure = 0.0; + double delta_m = 0.0; const double total_volume = TREL_PI * (*rocket)->engine->tube->internal_radius * (*rocket)->engine->tube->internal_radius * ((*rocket)->engine->grains->grain_separation * ((*rocket)->engine->grains->amount - 1) + (*rocket)->engine->grains->longitude * (*rocket)->engine->grains->amount); + //printf("DEBUG 000 %lf\n", inst_radius); inst_long = engine->grains->longitude; - inst_radius = (*rocket)->engine->grains->init_inter_radius * (*rocket)->engine->grains->init_inter_radius; + inst_radius = engine->grains->init_inter_radius; + fuel_volume = (*rocket)->engine->grains->amount * TREL_PI * (((*rocket)->engine->grains->extern_radius * (*rocket)->engine->grains->extern_radius) - (inst_radius * inst_radius)) * inst_long; + fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; + prev_fuel_mass = fuel_mass; + free_volume = total_volume - fuel_volume; rocket_height = (*rocket)->initial_height; gravitational_accel = TREL_ECUATOR_GRAV_ACCEL * (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)))) - (TREL_GRAV_A * rocket_height); atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); pressure = atmospheric_pressure; + intern_gas_mass = pressure * free_volume / (TREL_GAS_CONST * (*rocket)->engine->temperature); + burn_rate = calc_burn_rate(engine, pressure); atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); - fuel_volume = (*rocket)->engine->grains->amount * TREL_PI * (((*rocket)->engine->grains->extern_radius * (*rocket)->engine->grains->extern_radius) - (inst_radius * inst_radius)) * (*rocket)->engine->grains->longitude; - fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; - free_volume = total_volume - fuel_volume; - intern_gas_mass = pressure * free_volume / (TREL_GAS_CONST * (*rocket)->engine->temperature); res->rocket_mass[0] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; res->rocket_weight[0] = res->rocket_mass[0] * gravitational_accel; - pressure_abs = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - ((TREL_ADIABATIC_GRADIENT * rocket_height) / (TREL_STANDARD_TEMP)),((gravitational_accel * TREL_DRY_AIR_MOLAR_MASS) / TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); - virtual_escape_pressure = pressure / pow((1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * volumen_unitario((*rocket)->engine) * volumen_unitario((*rocket)->engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0))); - force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure), ((TREL_HEAT_CAP_RATIO-1.0)/TREL_HEAT_CAP_RATIO))))),0.5) + (((virtual_escape_pressure - pressure_abs) / (pressure)) * ((*rocket)->engine->grains->grain_separation / (*rocket)->engine->grains->longitude)); + virt_escape_pressure = calc_iter_virt_escape_pressure(pressure, engine); + force_coeff = calc_iter_force_coeff(virt_escape_pressure, pressure, atmospheric_pressure, engine); adjusted_cf = force_coeff * (*rocket)->engine->tube->nozzle_efficiency; res->rocket_force[0] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; res->rocket_force_balance[0] = calc_force_balance(res->rocket_force[0], res->rocket_position[0], res->rocket_speed[0], res->rocket_drag[0], res->rocket_weight[0]); // iteration loop - for (int it = 1; it < max_iterations; it++) + for (unsigned int it = 1; it < max_iterations; it++) { - // step time value - if (res->time[it - 1] + step < burn_time) - res->time[it] = res->time[it - 1] + step; - else - res->time[it] = 0; + res->time[it] = res->time[it - 1] + step; - gravitational_accel = TREL_ECUATOR_GRAV_ACCEL * (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)))) - (TREL_GRAV_A * rocket_height); - atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); - pressure = TREL_GAS_CONST * (*rocket)->engine->temperature * intern_gas_mass / free_volume > atmospheric_pressure ? TREL_GAS_CONST * (*rocket)->engine->temperature * intern_gas_mass / free_volume : atmospheric_pressure; - inst_radius = calc_inst_radius(inst_radius, calc_burn_rate(burn_rate, (*rocket)->engine->fuel->pressure_exponent, pressure), step, (*rocket)->engine->grains->extern_radius); - inst_long = calc_inst_long(inst_radius, inst_long, calc_burn_rate(burn_rate, (*rocket)->engine->fuel->pressure_exponent, pressure), step); + gravitational_accel = calc_iter_grav_accel(rocket, rocket_height); + atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); // ACA SE COLAPSA EL TITANIC + pressure = calc_iter_pressure(engine, intern_gas_mass, free_volume, atmospheric_pressure); + inst_radius = calc_inst_radius(inst_radius, burn_rate, step, (*rocket)->engine->grains->extern_radius); + inst_long = calc_inst_long(inst_radius, inst_long, burn_rate, step); + burn_rate = calc_burn_rate(engine, pressure); atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); fuel_volume = (*rocket)->engine->grains->amount * TREL_PI * (((*rocket)->engine->grains->extern_radius * (*rocket)->engine->grains->extern_radius) - inst_radius * inst_radius) * inst_long; - produced_mass = - fuel_mass; escape_massic_flux = pressure > atmospheric_pressure ? engine->grains->longitude * pressure * TREL_HEAT_CAP_RATIO * (sqrt(pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))))) / pow((TREL_HEAT_CAP_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; - intern_gas_mass = produced_mass + intern_gas_mass - escape_massic_flux * step > 0 ? produced_mass + intern_gas_mass - escape_massic_flux * step : 0.0; fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; - produced_mass += fuel_mass; + produced_mass = prev_fuel_mass - fuel_mass; + prev_fuel_mass = fuel_mass; + delta_m = escape_massic_flux * step; + intern_gas_mass = produced_mass + intern_gas_mass - delta_m > 0 ? produced_mass + intern_gas_mass - delta_m : 0.0; free_volume = total_volume - fuel_volume; - res->rocket_mass[it] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; + virt_escape_pressure = calc_iter_virt_escape_pressure(pressure, engine); + force_coeff = calc_iter_force_coeff(virt_escape_pressure, pressure, atmospheric_pressure, engine); + adjusted_cf = force_coeff * (*rocket)->engine->tube->nozzle_efficiency; + res->rocket_mass[it] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + fuel_mass; res->rocket_weight[it] = res->rocket_mass[it] * gravitational_accel; res->rocket_drag[it] = 0.5 * atmospheric_density * (*rocket)->drag_coefficient * TRANSVERSAL_AREA((*rocket)->body_diameter) * res->rocket_speed[it - 1] * res->rocket_speed[it - 1]; + res->rocket_force[it] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; res->rocket_force_balance[it] = calc_force_balance(res->rocket_force[it], res->rocket_position[it - 1], res->rocket_speed[it - 1], res->rocket_drag[it], res->rocket_weight[it]); res->rocket_acceleration[it] = res->rocket_force_balance[it] / res->rocket_mass[it]; res->rocket_speed[it] = res->rocket_acceleration[it] * step + res->rocket_speed[it - 1]; diff --git a/src/rocket.c b/src/rocket.c index 98ad4cb..b40ab7b 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -30,6 +30,19 @@ trel_rocket_t *trel_rocket_init( sim_values->max_rocket_position = 0.0; sim_values->max_rocket_speed = 0.0; + for (unsigned int i = 0; i < TREL_MAX_ITERATIONS; i++) + { + sim_values->rocket_position[i] = 0.0; + sim_values->rocket_speed[i] = 0.0; + sim_values->rocket_acceleration[i] = 0.0; + sim_values->rocket_force_balance[i] = 0.0; + sim_values->rocket_drag[i] = 0.0; + sim_values->rocket_weight[i] = 0.0; + sim_values->rocket_mass[i] = 0.0; + sim_values->rocket_force[i] = 0.0; + sim_values->time[i] = 0.0; + } + rocket->telemetry_mass = telemetry_mass; rocket->parachute_mass = parachute_mass; rocket->fuselage_mass = fuselage_mass; diff --git a/src/val_termod.c b/src/val_termod.c index 08068ee..2a5b872 100644 --- a/src/val_termod.c +++ b/src/val_termod.c @@ -12,7 +12,7 @@ double vol_esp_cam_combustion(engine_t *engine) // E50 Volumen Especifico de Garganta double vol_esp_garganta(engine_t *engine) { - return vol_esp_cam_combustion(engine) * (pow((TREL_HEAT_CAP_RATIO + 1),(1 / TREL_HEAT_CAP_RATIO - 1))); + return vol_esp_cam_combustion(engine) * pow((TREL_HEAT_CAP_RATIO + 1.0) / 2.0 , (1.0 / (TREL_HEAT_CAP_RATIO - 1.0))); } // E51 Volumen Especifico Escape From 20b678e1b0908db7b8c5699492e9c75027a35229 Mon Sep 17 00:00:00 2001 From: lross2k Date: Mon, 3 Apr 2023 10:01:40 -0600 Subject: [PATCH 21/42] Fixed bug in height sim Also fixed compilation in UNIX, by implementing a preprocessor check of _WIN32 to use strcpy_s, if not available use strncpy as the best portable alternative for C99 or C89 (strcpy_s if from C11 onwards) The bug of height_sim.c was very simple, the escape_massic_flux calculation was completely wrong, fixed it -lross2k --- src/engine.c | 8 ++++++++ src/height_sim.c | 51 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/engine.c b/src/engine.c index 3f7f514..6390890 100644 --- a/src/engine.c +++ b/src/engine.c @@ -57,7 +57,11 @@ tubing_t* trel_tubing_init( printf("Tubing initialization failed\n"); exit(1); } +#if defined (_WIN32) // Take advantage of TR 24731-1 from MSVC strcpy_s(tube->material, sizeof(char) * TREL_MAX_STR_LEN, material); +#else // Take the most portable approach for NIX + strncpy(tube->material, material, TREL_MAX_STR_LEN); +#endif tube->diameter_ext = diameter; tube->wall_thickness = thickness; tube->young_module = young_module; @@ -86,7 +90,11 @@ screws_t* trel_screws_init( printf("Screws initialization failed\n"); exit(1); } +#if defined (_WIN32) // Take advantage of TR 24731-1 from MSVC strcpy_s(screws->material, sizeof(char) * TREL_MAX_STR_LEN, material); +#else // Take the most portable approach for NIX + strncpy(screws->material, material, TREL_MAX_STR_LEN); +#endif screws->diameter = diameter; screws->amount = amount; screws->dist_center_wall = dist_center_wall; diff --git a/src/height_sim.c b/src/height_sim.c index 6478656..d289ef8 100644 --- a/src/height_sim.c +++ b/src/height_sim.c @@ -1,9 +1,8 @@ #include "height_sim.h" #include "stdio.h" -// TODO: CHECK ANYTHING THAT USES pressure_abs, it might be pressure instead - #define TRANSVERSAL_AREA(DIAMETER) (TREL_PI * DIAMETER * DIAMETER / 4) +#define POW2(VAR) (VAR * VAR) double calc_force_balance(double rocket_force, double rocket_position, double rocket_speed, double rocket_drag, double rocket_weight) @@ -73,25 +72,44 @@ double calc_iter_grav_accel(trel_rocket_t **rocket, double rocket_height) { double val = 0.0; val = TREL_ECUATOR_GRAV_ACCEL; - val *= (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)))); + val *= (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * + (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * + (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * + (*rocket)->sim_latitude) * TREL_PI / 180)))); val -= (TREL_GRAV_A * rocket_height); return(val); } - //printf("DEBUG 000 $E$36 %lf , $E$37 %lf", e36, e37); - //printf("DEBUG 001 $E$39 %lf", e39); double calc_iter_force_coeff(double virt_escape_pressure, double pressure, double atmospheric_pressure, engine_t *engine) { - double val = pow((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1) / (TREL_HEAT_CAP_RATIO - 1))) * ((1 - pow((virt_escape_pressure / pressure), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO))))), 0.5); - val += (virt_escape_pressure - atmospheric_pressure) / pressure * escape_area(engine) / throat_area(engine); + double val = pow((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO / + (TREL_HEAT_CAP_RATIO - 1.0) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), + ((TREL_HEAT_CAP_RATIO + 1) / (TREL_HEAT_CAP_RATIO - 1))) * + ((1 - pow((virt_escape_pressure / pressure), ((TREL_HEAT_CAP_RATIO - 1.0) / + TREL_HEAT_CAP_RATIO))))), 0.5); + val += (virt_escape_pressure - atmospheric_pressure) / pressure * escape_area(engine) / + throat_area(engine); return(val); } double calc_iter_virt_escape_pressure(double pressure, engine_t *engine) +{ + double val = pressure / pow(1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * + vel_numero_mach_m2(engine) * vel_numero_mach_m2(engine), TREL_HEAT_CAP_RATIO / + (TREL_HEAT_CAP_RATIO - 1.0)); + return(val); +} + +double calc_iter_escape_massic_flux(double pressure, double atmospheric_pressure, engine_t *engine) { double val = 0.0; - //D69 / (1 + (($E$44 - 1) / 2) * $E$39 ^ 2) ^ ($E$44 / ($E$44 - 1)); - val = pressure / pow(1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * vel_numero_mach_m2(engine) * vel_numero_mach_m2(engine), TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0)); + if (pressure > atmospheric_pressure) + { + val = throat_area(engine) * pressure * TREL_HEAT_CAP_RATIO * (sqrt(pow((2.0 / + (TREL_HEAT_CAP_RATIO + 1.0)) , ((TREL_HEAT_CAP_RATIO + 1.0) / + (TREL_HEAT_CAP_RATIO - 1.0))))) / pow((TREL_HEAT_CAP_RATIO * TREL_GAS_CONST * + engine->temperature) , 0.5); + } return(val); } @@ -128,18 +146,17 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) double burn_rate = 0.0; double virt_escape_pressure = 0.0; double delta_m = 0.0; - - const double total_volume = TREL_PI * (*rocket)->engine->tube->internal_radius * (*rocket)->engine->tube->internal_radius * ((*rocket)->engine->grains->grain_separation * ((*rocket)->engine->grains->amount - 1) + (*rocket)->engine->grains->longitude * (*rocket)->engine->grains->amount); - //printf("DEBUG 000 %lf\n", inst_radius); + const double total_volume = TREL_PI * POW2(engine->tube->internal_radius) * + (engine->grains->grain_separation * (engine->grains->amount - 1) + engine->grains->longitude * engine->grains->amount); inst_long = engine->grains->longitude; inst_radius = engine->grains->init_inter_radius; - fuel_volume = (*rocket)->engine->grains->amount * TREL_PI * (((*rocket)->engine->grains->extern_radius * (*rocket)->engine->grains->extern_radius) - (inst_radius * inst_radius)) * inst_long; - fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; + fuel_volume = engine->grains->amount * TREL_PI * (POW2(engine->grains->extern_radius) - POW2(inst_radius)) * inst_long; + fuel_mass = fuel_volume * engine->fuel->density; prev_fuel_mass = fuel_mass; free_volume = total_volume - fuel_volume; rocket_height = (*rocket)->initial_height; - gravitational_accel = TREL_ECUATOR_GRAV_ACCEL * (1 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180)) * (sin((*rocket)->sim_latitude * TREL_PI / 180))) - (TREL_GRAV_CRUSH_4 * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)) * (sin((2 * (*rocket)->sim_latitude) * TREL_PI / 180)))) - (TREL_GRAV_A * rocket_height); + gravitational_accel = TREL_ECUATOR_GRAV_ACCEL * (1.0 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180.0)) * (sin((*rocket)->sim_latitude * TREL_PI / 180.0))) - (TREL_GRAV_CRUSH_4 * (sin((2.0 * (*rocket)->sim_latitude) * TREL_PI / 180.0)) * (sin((2.0 * (*rocket)->sim_latitude) * TREL_PI / 180.0)))) - (TREL_GRAV_A * rocket_height); atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); pressure = atmospheric_pressure; intern_gas_mass = pressure * free_volume / (TREL_GAS_CONST * (*rocket)->engine->temperature); @@ -168,12 +185,12 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); fuel_volume = (*rocket)->engine->grains->amount * TREL_PI * (((*rocket)->engine->grains->extern_radius * (*rocket)->engine->grains->extern_radius) - inst_radius * inst_radius) * inst_long; - escape_massic_flux = pressure > atmospheric_pressure ? engine->grains->longitude * pressure * TREL_HEAT_CAP_RATIO * (sqrt(pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))))) / pow((TREL_HEAT_CAP_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; + escape_massic_flux = calc_iter_escape_massic_flux(pressure, atmospheric_pressure, engine); fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; produced_mass = prev_fuel_mass - fuel_mass; prev_fuel_mass = fuel_mass; delta_m = escape_massic_flux * step; - intern_gas_mass = produced_mass + intern_gas_mass - delta_m > 0 ? produced_mass + intern_gas_mass - delta_m : 0.0; + intern_gas_mass = (produced_mass + intern_gas_mass - delta_m) > 0.0 ? produced_mass + intern_gas_mass - delta_m : 0.0; free_volume = total_volume - fuel_volume; virt_escape_pressure = calc_iter_virt_escape_pressure(pressure, engine); force_coeff = calc_iter_force_coeff(virt_escape_pressure, pressure, atmospheric_pressure, engine); From 52fca789eba4c4ad8eebbc62de6dbd22af436a6d Mon Sep 17 00:00:00 2001 From: lross2k Date: Mon, 3 Apr 2023 10:56:43 -0600 Subject: [PATCH 22/42] Change from C99 to C89 When it comes to portabilty of C code, it's hard to beat C89/C90 also known as ANSI C or ISO C This is due to the backwards compatibilty of the C standard, which on the other hand, it's rarely compatible with code using newer features Therefore, changes where made to allow the compilation and usage of this library with GCC using -std_c_90 -lross2k --- CMakeLists.txt | 19 ++++----- README.md | 7 +--- TODO.md | 9 ++--- include/DLLDefines.h | 8 ++-- include/TREL.h | 38 +++++++++--------- include/comp_area.h | 4 +- include/comp_tiempo.h | 2 +- include/design_constants.h | 30 +++++++-------- include/design_func.h | 4 +- include/design_resist.h | 8 ++-- include/engine.h | 7 ++-- include/height_sim.h | 4 +- include/rendimientos.h | 6 +-- include/rocket.h | 6 +-- include/structs.h | 34 ++++++++-------- include/val_termod.h | 8 ++-- src/comp_area.c | 79 +++++++++++++++++++++----------------- src/comp_tiempo.c | 69 +++++++++++++++++---------------- src/design_func.c | 70 ++++++++++++++++----------------- src/design_resist.c | 58 ++++++++++++++-------------- src/engine.c | 15 ++++---- src/height_sim.c | 19 ++++----- src/rocket.c | 13 ++++--- src/val_termod.c | 34 ++++++++-------- 24 files changed, 278 insertions(+), 273 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 401854a..94b8e36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" ON) OPTION (BUILD_CLI_TESTAPP "Build testapp" ON) # Used to generate header with macros for the library -INCLUDE (GenerateExportHeader) +#INCLUDE (GenerateExportHeader) # Decide which LIB_TYPE is to be used for the build IF (BUILD_SHARED_LIBS) @@ -55,15 +55,16 @@ ELSE (MSVC) ENDIF (MSVC) # Specify arguments to be used in the export header -GENERATE_EXPORT_HEADER (TREL - BASE_NAME TREL - EXPORT_MACRO_NAME TREL_EXPORT - EXPORT_FILE_NAME TREL_Export.h - STATIC_DEFINE TREL_BUILT_AS_STATIC -) +#GENERATE_EXPORT_HEADER (TREL +# BASE_NAME TREL +# EXPORT_MACRO_NAME TREL_EXPORT +# EXPORT_FILE_NAME TREL_Export.h +# STATIC_DEFINE TREL_BUILT_AS_STATIC +#) # Compile flags for the library SET_TARGET_PROPERTIES (TREL PROPERTIES COMPILE_FLAGS -DLIB_STATIC_DEFINE) +SET_PROPERTY(TARGET TREL PROPERTY C_STANDARD 90) # Additional function calls for building the testapp IF (BUILD_CLI_TESTAPP) @@ -72,7 +73,7 @@ IF (BUILD_CLI_TESTAPP) # Declaring a link between the executable and the library TARGET_LINK_LIBRARIES (testapp TREL) #target_link_libraries(testapp PRIVATE ) - target_include_directories(testapp PUBLIC "include") + TARGET_INCLUDE_DIRECTORIES (testapp PUBLIC "include") # Additional arguments for the excecutable compilation - TARGET_COMPILE_FEATURES (testapp PUBLIC c_std_99) + TARGET_COMPILE_FEATURES (testapp PUBLIC c_std_90) ENDIF (BUILD_CLI_TESTAPP) diff --git a/README.md b/README.md index 76b8b84..85d8ad5 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,7 @@ Read the [TODO](https://github.com/Coheteria-TECSpace/TREL/blob/dev/TODO.md) and understand what to do just by reading that. ## Portability -Given that this library expects to calculate data for rockets in many ways, allowing the same functionality to be -easily ported to any high level programming language having the basis at C will make the initial effort more -scalable as it could even be used to interface straight from embedded systems that gather data and compare it to the -one generated in real time from the library, or also using the basic funtions to create an advanced GUI +Given that this library expects to calculate data for rockets in many ways, allowing the same functionality to be easily ported to any high level programming language having the basis at ANSI C (C89/C90) will make the initial effort more scalable as it could even be used to interface straight from embedded systems that gather data and compare it to the one generated in real time from the library, or also using the basic funtions to create an advanced GUI # Build process The current build method uses CMake, modifying the CMakeLists.txt file allows the user to generate a static or dynamic library by changing `"Build Shared Libraries" ON to OFF` depending on what is desired, just as well as `"Build app" ON` can be toggled to generate the testing app which links automatically to the library. @@ -65,5 +62,3 @@ del *.o ``` gcc -o app ../main.c -Wall -Wextra -pedantic -Werror -I../include/ -L. -lTREL -lm ``` - - diff --git a/TODO.md b/TODO.md index 0f7e574..373bcd0 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,5 @@ # TODO list for the project -1. Change from C99 to the more portable C89 [ANSI C] -2. Implement memory free functions, and destroy all pointers -3. Define and apply standard function convention -4. Define and apply standard style guide -5. Implement Python library interface +1. Implement memory free functions, and destroy all pointers +2. Define and apply standard function convention +3. Define and apply standard style guide +4. Implement Python library interface diff --git a/include/DLLDefines.h b/include/DLLDefines.h index 9116f06..f1069fe 100644 --- a/include/DLLDefines.h +++ b/include/DLLDefines.h @@ -1,4 +1,4 @@ -// Contents of DLLDefines.h from the CMake documentation +/* Contents of DLLDefines.h from the CMake documentation */ #ifndef _TREL_DLLDEFINES_H_ #define _TREL_DLLDEFINES_H_ @@ -8,16 +8,16 @@ another build system on windows or create the Visual Studio projects by hand you need to define TREL_EXPORTS when building a DLL on windows. */ -// We are using the Visual Studio Compiler and building Shared libraries +/* We are using the Visual Studio Compiler and building Shared libraries */ #if defined (_WIN32) #if defined(TREL_EXPORTS) #define TREL_EXPORT __declspec(dllexport) #else - #define TREL_EXPORT //__declspec(dllimport) // Didn't allow for STATIC compilation + #define TREL_EXPORT /*__declspec(dllimport) Didn't allow for STATIC compilation */ #endif /* TREL_EXPORTS */ #else /* defined (_WIN32) */ #define TREL_EXPORT #endif -#endif // _TREL_DLLDEFINES_H_ +#endif /* _TREL_DLLDEFINES_H_ */ diff --git a/include/TREL.h b/include/TREL.h index f4a4e76..f49f35d 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -1,13 +1,13 @@ #ifndef TREL_H #define TREL_H -// Types -// Comp area calculations +/* Types */ +/* Comp area calculations */ typedef struct { double avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; } comp_area_t; -// Propellent grains structure +/* Propellent grains structure */ typedef struct { unsigned int amount; double init_inter_radius, extern_radius, longitude, grain_separation; @@ -18,7 +18,7 @@ typedef struct { double const_burn_rate, pressure_exponent, density, burn_rate; } fuel_t; -// Estructura para tuberia +/* Estructura para tuberia */ typedef struct tubing { char material[100]; @@ -29,7 +29,7 @@ typedef struct tubing double transversal_area, material_area; } tubing_t; -// Estructura para tornillos +/* Estructura para tornillos */ typedef struct screws { double diameter, dist_center_wall; @@ -39,27 +39,27 @@ typedef struct screws double width_cutting_segment; } screws_t; -// Estructura del engine +/* Estructura del engine */ typedef struct engine { double engine_mass; - double pressure; // chamber pressure in psi + double pressure; /* chamber pressure in psi */ double escape_vel, temperature; double width_condition, margin_of_safety, max_stress, radial_stress; double tangencial_stress, longitudinal_stress, max_pressure; double nozzle_efficiency; - grains_t *grains; // ptr to intialized struct + grains_t *grains; /* ptr to intialized struct */ fuel_t *fuel; - tubing_t *tube; // ptr to intialized struct - screws_t *screws; // ptr to initialized struct - comp_area_t *comp_area_values; // ptr to initialized struct + tubing_t *tube; /* ptr to intialized struct */ + screws_t *screws; /* ptr to initialized struct */ + comp_area_t *comp_area_values; /* ptr to initialized struct */ } engine_t; -// Struct for height simulation results +/* Struct for height simulation results */ typedef struct trel_height_sim_values { double rocket_position[1000]; - double rocket_speed[1000]; // m/s + double rocket_speed[1000]; /* m/s */ double rocket_acceleration[1000]; double rocket_force_balance[1000]; double rocket_drag[1000]; @@ -69,23 +69,23 @@ typedef struct trel_height_sim_values double time[1000]; double max_rocket_height; double max_rocket_position; - double max_rocket_speed; // m/s + double max_rocket_speed; /* m/s */ double max_rocket_acceleration; double max_rocket_force_balance; double max_rocket_drag; } trel_height_sim_t; -// Struct for the rocket +/* Struct for the rocket */ typedef struct trel_rocket { - engine_t* engine; // ptr to initialized struct + engine_t* engine; /* ptr to initialized struct */ double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; - double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 + double avg_thrust, max_thrust, delta_v, max_pressure; /* Comportamiento en el Tiempo!AD3511 */ double sim_latitude, max_sim_height, body_diameter, drag_coefficient; trel_height_sim_t* sim_values; } trel_rocket_t; -// Function prototypes +/* Function prototypes */ grains_t *trel_grains_init( unsigned int amount, /* E29 */ double internal_radius, /* E30 */ @@ -146,4 +146,4 @@ int trel_run_area_comp_iterations(engine_t **engine); int trel_run_time_comp_iterations(trel_rocket_t* rocket); int trel_run_height_sim_iterations(trel_rocket_t** rocket); -#endif //TREL_H +#endif /*TREL_H */ diff --git a/include/comp_area.h b/include/comp_area.h index fb73561..45128aa 100644 --- a/include/comp_area.h +++ b/include/comp_area.h @@ -3,7 +3,7 @@ #include "design_func.h" -// Function prototypes +/* Function prototypes */ int TREL_EXPORT trel_run_area_comp_iterations(engine_t **engine); -#endif //COMP_AREA_H +#endif /*COMP_AREA_H */ diff --git a/include/comp_tiempo.h b/include/comp_tiempo.h index 7014888..7acb16f 100644 --- a/include/comp_tiempo.h +++ b/include/comp_tiempo.h @@ -5,4 +5,4 @@ int TREL_EXPORT trel_run_time_comp_iterations(trel_rocket_t* rocket); -#endif //COMP_TIEMPO_H +#endif /*COMP_TIEMPO_H */ diff --git a/include/design_constants.h b/include/design_constants.h index b5f22ec..de0eecd 100644 --- a/include/design_constants.h +++ b/include/design_constants.h @@ -4,31 +4,31 @@ #define TREL_MAX_STR_LEN 100 #define TREL_MAX_ITERATIONS 1000 -//Constantes matemáticas -#define TREL_PI 3.1415926535898 // PI +/*Constantes matemáticas */ +#define TREL_PI 3.1415926535898 /* PI */ -//Recamara -#define press_Pa (800.0*6894.757) //E25 double input +/*Recamara */ +#define press_Pa (800.0*6894.757) /*E25 double input */ -// Gravitational constants -#define TREL_GRAV_CONST 9.81 // m/s^2 -#define TREL_ECUATOR_GRAV_ACCEL 9.78032 // m/s^2 +/* Gravitational constants */ +#define TREL_GRAV_CONST 9.81 /* m/s^2 */ +#define TREL_ECUATOR_GRAV_ACCEL 9.78032 /* m/s^2 */ #define TREL_GRAV_CRUSH 0.0053024 #define TREL_GRAV_CRUSH_4 0.0000058 -#define TREL_GRAV_A 0.000003086 // s^-2 +#define TREL_GRAV_A 0.000003086 /* s^-2 */ -// Atmospheric constants +/* Atmospheric constants */ #define TREL_ATMOSPHERIC_PRESSURE 101325.0 /* E9 */ #define TREL_ADIABATIC_GRADIENT 0.0065 #define TREL_STANDARD_TEMP 288.15 #define TREL_DRY_AIR_MOLAR_MASS 0.02896 -#define TREL_IDEAL_GAS_CONST 8.31447 // J/mol*K +#define TREL_IDEAL_GAS_CONST 8.31447 /* J/mol*K */ -// Thermodinamic constants +/* Thermodinamic constants */ #define TREL_HEAT_CAP_RATIO 1.043 /* E46 */ -#define TREL_GAS_CONST 196.14 // J/kg*K /* E47 */ +#define TREL_GAS_CONST 196.14 /* J/kg*K E47 */ -#define FACT_CONV_BR 0.000145038 // factor de conversion para la tasa de quemado -#define FACT_CONV_M_TO_I 0.0254 //factor de conversion de metros a pulgadas +#define FACT_CONV_BR 0.000145038 /* factor de conversion para la tasa de quemado */ +#define FACT_CONV_M_TO_I 0.0254 /*factor de conversion de metros a pulgadas */ -#endif // TREL_CONSTANTS +#endif /* TREL_CONSTANTS */ diff --git a/include/design_func.h b/include/design_func.h index 3b2c94d..2ace5b3 100644 --- a/include/design_func.h +++ b/include/design_func.h @@ -10,7 +10,7 @@ #include "engine.h" #include "DLLDefines.h" -// Function prototypes +/* Function prototypes */ double trel_get_pressure_pa(engine_t* engine); double volumen_camara(engine_t* engine); double calc_escape_vel(engine_t *engine); @@ -31,4 +31,4 @@ double desv_est_grains(engine_t* engine); double throat_area(engine_t* engine); double escape_area(engine_t* engine); -#endif //DESIGN_FUNC_H +#endif /*DESIGN_FUNC_H */ diff --git a/include/design_resist.h b/include/design_resist.h index 2a6717a..01f769a 100644 --- a/include/design_resist.h +++ b/include/design_resist.h @@ -13,10 +13,10 @@ Inicio de traduccion a C por lross2k #include "DLLDefines.h" #include "structs.h" -// TODO: remove temp_ +/* TODO: remove temp_ */ -// Function prototypes -// add APPLIB_EXPORT to any function needed from the dynamic library +/* Function prototypes */ +/* add APPLIB_EXPORT to any function needed from the dynamic library */ void trel_mean_tubing_diameter(engine_t *engine); void trel_width_condition(engine_t *engine); void trel_engine_max_pressure(engine_t *engine); @@ -32,4 +32,4 @@ void trel_screw_occupied_area(engine_t *engine); void trel_tube_mateial_area(engine_t *engine); void trel_width_cutting_segment(engine_t *engine); -#endif //DESIGN_RESISTENCIA_H +#endif /*DESIGN_RESISTENCIA_H */ diff --git a/include/engine.h b/include/engine.h index 9b839a1..11153d0 100644 --- a/include/engine.h +++ b/include/engine.h @@ -9,11 +9,11 @@ #include "design_resist.h" #include "structs.h" -// TODO: free library structs function +/* TODO: free library structs function */ -// Function prototypes +/* Function prototypes */ grains_t TREL_EXPORT *trel_grains_init( unsigned int amount, /* E29 */ double internal_radius, /* E30 */ @@ -58,5 +58,4 @@ void TREL_EXPORT trel_set_escape_vel(engine_t *engine, double vel); double TREL_EXPORT trel_get_escape_vel(engine_t *engine); double TREL_EXPORT trel_get_pressure(engine_t *engine); -#endif // ENGINE_H - +#endif /* ENGINE_H */ diff --git a/include/height_sim.h b/include/height_sim.h index 8265baa..17a1dbc 100644 --- a/include/height_sim.h +++ b/include/height_sim.h @@ -3,7 +3,7 @@ #include "design_func.h" -// Function prototypes +/* Function prototypes */ int TREL_EXPORT trel_run_height_sim_iterations(trel_rocket_t** rocket); -#endif //HEIGHT_SIM_H +#endif /*HEIGHT_SIM_H */ diff --git a/include/rendimientos.h b/include/rendimientos.h index c4b6cb5..10e6574 100644 --- a/include/rendimientos.h +++ b/include/rendimientos.h @@ -11,9 +11,9 @@ #include "comp_tiempo.h" #include "height_sim.h" -// TODO: cambiar list[] por nombres mas descriptivos +/* TODO: cambiar list[] por nombres mas descriptivos */ -// Function prototypes +/* Function prototypes */ double TREL_EXPORT volumen_combustible(engine_t *engine); double TREL_EXPORT masa_combustible(engine_t* engine); double TREL_EXPORT Tiempo_quemado_combustion(engine_t* engine); @@ -29,4 +29,4 @@ double TREL_EXPORT I_tot_teo(engine_t* engine); double TREL_EXPORT I_tot_esp(engine_t* engine); double TREL_EXPORT Delta_V_Esp(trel_rocket_t* rocket); -#endif //RENDIMIENTOS_H +#endif /*RENDIMIENTOS_H */ diff --git a/include/rocket.h b/include/rocket.h index 5cf136c..ed5cd89 100644 --- a/include/rocket.h +++ b/include/rocket.h @@ -8,9 +8,9 @@ #include "design_func.h" #include "structs.h" -// TODO: free library structs function +/* TODO: free library structs function */ -// Function prototypes +/* Function prototypes */ trel_rocket_t TREL_EXPORT* trel_rocket_init( engine_t* engine, double telemetry_mass, @@ -23,4 +23,4 @@ trel_rocket_t TREL_EXPORT* trel_rocket_init( double drag_coefficient ); -#endif//ROCKET_H +#endif/*ROCKET_H */ diff --git a/include/structs.h b/include/structs.h index 2ad2e87..45be08b 100644 --- a/include/structs.h +++ b/include/structs.h @@ -4,12 +4,12 @@ #include "design_constants.h" #include "DLLDefines.h" -// Comp area calculations +/* Comp area calculations */ typedef struct { double avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; } TREL_EXPORT comp_area_t; -// Propellent grains structure +/* Propellent grains structure */ typedef struct { unsigned int amount; double init_inter_radius, extern_radius, longitude, grain_separation; @@ -20,7 +20,7 @@ typedef struct { double const_burn_rate, pressure_exponent, density, burn_rate; } TREL_EXPORT fuel_t; -// Estructura para tuberia +/* Estructura para tuberia */ typedef struct tubing { char material[TREL_MAX_STR_LEN]; @@ -32,7 +32,7 @@ typedef struct tubing double nozzle_efficiency; } TREL_EXPORT tubing_t; -// Estructura para tornillos +/* Estructura para tornillos */ typedef struct screws { double diameter, dist_center_wall; @@ -42,27 +42,27 @@ typedef struct screws double width_cutting_segment; } TREL_EXPORT screws_t; -// Estructura del engine +/* Estructura del engine */ typedef struct engine { double engine_mass; - double pressure; // chamber pressure in psi + double pressure; /* chamber pressure in psi */ double escape_vel, temperature; double width_condition, margin_of_safety, max_stress, radial_stress; double tangencial_stress, longitudinal_stress, max_pressure; double nozzle_efficiency; - grains_t *grains; // ptr to intialized struct + grains_t *grains; /* ptr to intialized struct */ fuel_t *fuel; - tubing_t *tube; // ptr to intialized struct - screws_t *screws; // ptr to initialized struct - comp_area_t *comp_area_values; // ptr to initialized struct + tubing_t *tube; /* ptr to intialized struct */ + screws_t *screws; /* ptr to initialized struct */ + comp_area_t *comp_area_values; /* ptr to initialized struct */ } TREL_EXPORT engine_t; -// Struct for height simulation results +/* Struct for height simulation results */ typedef struct trel_height_sim_values { double rocket_position[TREL_MAX_ITERATIONS]; - double rocket_speed[TREL_MAX_ITERATIONS]; // m/s + double rocket_speed[TREL_MAX_ITERATIONS]; /* m/s */ double rocket_acceleration[TREL_MAX_ITERATIONS]; double rocket_force_balance[TREL_MAX_ITERATIONS]; double rocket_drag[TREL_MAX_ITERATIONS]; @@ -72,20 +72,20 @@ typedef struct trel_height_sim_values double time[TREL_MAX_ITERATIONS]; double max_rocket_height; double max_rocket_position; - double max_rocket_speed; // m/s + double max_rocket_speed; /* m/s */ double max_rocket_acceleration; double max_rocket_force_balance; double max_rocket_drag; } TREL_EXPORT trel_height_sim_t; -// Struct for the rocket +/* Struct for the rocket */ typedef struct trel_rocket { - engine_t *engine; // ptr to initialized struct + engine_t *engine; /* ptr to initialized struct */ double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; - double avg_thrust, max_thrust, delta_v, max_pressure; // Comportamiento en el Tiempo!AD3511 + double avg_thrust, max_thrust, delta_v, max_pressure; /* Comportamiento en el Tiempo!AD3511 */ double sim_latitude, max_sim_height, body_diameter, drag_coefficient; trel_height_sim_t *sim_values; } TREL_EXPORT trel_rocket_t; -#endif //TREL_STRUCTS_H +#endif /*TREL_STRUCTS_H */ diff --git a/include/val_termod.h b/include/val_termod.h index 03486c6..5916929 100644 --- a/include/val_termod.h +++ b/include/val_termod.h @@ -8,18 +8,18 @@ #include "design_func.h" #include "structs.h" -// Prototipos de funciones +/* Prototipos de funciones */ double TREL_EXPORT vol_esp_cam_combustion(engine_t* engine); double TREL_EXPORT vol_esp_garganta(engine_t* engine); double TREL_EXPORT vol_esp_escape(engine_t* engine); double TREL_EXPORT presion_garganta(engine_t* engine); -//double TREL_EXPORT presion_escape(engine_t* engine); // TODO +/*double TREL_EXPORT presion_escape(engine_t* engine); TODO */ double TREL_EXPORT trel_temper_garganta(engine_t *engine); -//double TREL_EXPORT temper_escape(engine_t* engine); +/*double TREL_EXPORT temper_escape(engine_t* engine); */ double TREL_EXPORT velocidad_garganta(engine_t* engine); double TREL_EXPORT vel_sonica_local_at(engine_t* engine); double TREL_EXPORT vel_numero_mach_mt(engine_t* engine); double TREL_EXPORT vel_sonica_local_a2(engine_t* engine); double TREL_EXPORT vel_numero_mach_m2(engine_t* engine); double TREL_EXPORT vel_escape_caract(engine_t* engine); -#endif // VAL_TERMOD_FUNC_H +#endif /* VAL_TERMOD_FUNC_H */ diff --git a/src/comp_area.c b/src/comp_area.c index f96dd40..d2184f4 100644 --- a/src/comp_area.c +++ b/src/comp_area.c @@ -1,24 +1,24 @@ #include "comp_area.h" #include "stdio.h" -// TODO: Evaluate if we need dynamic allocation for this iteration +/* TODO: Evaluate if we need dynamic allocation for this iteration */ int trel_run_area_comp_iterations(engine_t **engine) { - // iterate over given iterations and step size + /* iterate over given iterations and step size */ const double max_iterations = 1000.0f; const double burn_time = Tiempo_quemado_combustion(*engine); const double step = burn_time/max_iterations; - double time = 0.0f; // time in seconds + double time = 0.0f; /* time in seconds */ - // burn rate value from propellant + /* burn rate value from propellant */ const double burn_rate = br_combustion(*engine); - // variables for iteration + /* variables for iteration */ double inst_radius = 0.0f; double inst_long = 0.0f; - // sums for eventual average value + /* sums for eventual average value */ double transversal_area = 0.0; double sum_transversal_area = 0.0; double longitudinal_area = 0.0; @@ -26,83 +26,90 @@ int trel_run_area_comp_iterations(engine_t **engine) double burn_area = 0.0; double sum_burn_area = 0.0; - // array for final values calculation + double lt_differences = 0.0f; /* lower */ + double gt_differences = 0.0f; /* greater */ + + /* calculate standard deviation */ + double burn_std_deviation = 0.0; + + /* array for final values calculation */ double burn_area_array[1000]; - // initialize array - for (int i = 0; i < 1000; i++) + /* initialize array */ + int i = 0; + int it = 0; + for (i = 0; i < 1000; i++) burn_area_array[i] = 0.0f; - // iteration loop - for (int it = 0; it < max_iterations; it++) + /* iteration loop */ + for (it = 0; it < max_iterations; it++) { - // instantaneous grain radius step + /* instantaneous grain radius step */ inst_radius = (*engine)->grains->init_inter_radius + burn_rate * time; - // instantaneous grain longitude step + /* instantaneous grain longitude step */ inst_long = (*engine)->grains->longitude - 2.0f * burn_rate * time; - // add to transversal area sum + /* add to transversal area sum */ transversal_area = TREL_PI * ((*engine)->grains->extern_radius*(*engine)->grains->extern_radius - inst_radius * inst_radius); sum_transversal_area += transversal_area; - // add to longitudinal area sum + /* add to longitudinal area sum */ if (!transversal_area) longitudinal_area = 0.0; else longitudinal_area = 2 * TREL_PI * inst_radius * inst_long; sum_longitudinal_area += longitudinal_area; - // add to burn area sum + /* add to burn area sum */ burn_area_array[it] = transversal_area * 2 + longitudinal_area; sum_burn_area += burn_area_array[it]; - // step time value + /* step time value */ if (time + step < burn_time) time += step; else time = 0; } - // calculate average transversal area + /* calculate average transversal area */ transversal_area = sum_transversal_area / max_iterations; (*engine)->comp_area_values->avg_trans_area = transversal_area; - // calculate average longitudinal area + /* calculate average longitudinal area */ longitudinal_area = sum_longitudinal_area / max_iterations; (*engine)->comp_area_values->avg_long_area = longitudinal_area; - // calculate average burn area + /* calculate average burn area */ burn_area = sum_burn_area / max_iterations; (*engine)->comp_area_values->avg_burn_area = burn_area; - // calculate the difference of each iteration's burn area and the average burn area - for (int i = 0; i < max_iterations; i++) + /* calculate the difference of each iteration's burn area and the average burn area */ + i = 0; + for (i = 0; i < max_iterations; i++) burn_area_array[i] = burn_area_array[i] - burn_area; - // calculate standard deviation - double burn_std_deviation = 0.0; - // sum the squared burn area differences - for (int i = 0; i < max_iterations; i++) burn_std_deviation += burn_area_array[i] * burn_area_array[i]; - // calculate the average of the squared differences + /* sum the squared burn area differences */ + i = 0; + for (i = 0; i < max_iterations; i++) burn_std_deviation += burn_area_array[i] * burn_area_array[i]; + /* calculate the average of the squared differences */ burn_std_deviation /= 1000.0; - // get the square root of the avg + /* get the square root of the avg */ burn_std_deviation = sqrt(burn_std_deviation); - // store the value in the engine + /* store the value in the engine */ (*engine)->comp_area_values->burn_std_deviation = burn_std_deviation; - // then sum all the greater than zero differences, store them to a variable, and - // sum all the lower than zero differences, store them to another variable - double lt_differences = 0.0f; // lower - double gt_differences = 0.0f; // greater + /* then sum all the greater than zero differences, store them to a variable, and */ + /* sum all the lower than zero differences, store them to another variable */ - for (int i = 0; i < max_iterations; i++) + i = 0; + for (i = 0; i < max_iterations; i++) if (burn_area_array[i] > 0) gt_differences += burn_area_array[i]; else lt_differences += burn_area_array[i]; - // calculate the difference of burn area by adding the two sums + /* calculate the difference of burn area by adding the two sums */ (*engine)->comp_area_values->burn_sum_diff = gt_differences + lt_differences; - return(0); // everything went well + return(0); /* everything went well */ } diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index 023c94f..826da49 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -1,97 +1,98 @@ #include "comp_tiempo.h" -//Comportamiento en el Tiempo!AD3511 +/*Comportamiento en el Tiempo!AD3511 */ int trel_run_time_comp_iterations(trel_rocket_t* rocket) { engine_t* engine = rocket->engine; - // iterate over given iterations and step size + /* iterate over given iterations and step size */ const double max_iterations = 1000.0; const double burn_time = Tiempo_quemado_combustion(engine); - //const double step = burn_time / max_iterations; // DEBUG + /*const double step = burn_time / max_iterations; DEBUG */ const double step = 0.15999891; - double time = 0.0; // time in seconds + double time = 0.0; /* time in seconds */ double last_force = 0.0; - // variables for iteration + /* variables for iteration */ double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemetry_mass + engine->engine_mass + rocket->payload_mass; const double delta_t = step; double pressure_abs = TREL_ATMOSPHERIC_PRESSURE; - //double pressure = pressure_abs / 6894.757f; - //double pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; + /*double pressure = pressure_abs / 6894.757f; */ + /*double pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; */ double burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; double inst_radius = engine->grains->init_inter_radius; double inst_long = engine->grains->longitude; - //double trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; - //double long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; - //double burn_area = trans_area * 2 + long_area; - //double total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; + /*double trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; */ + /*double long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; */ + /*double burn_area = trans_area * 2 + long_area; */ + /*double total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; */ double fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; double fuel_mass = engine->fuel->density * fuel_vol; double produced_mass = 0.0; - //double produced_massic_flux = 0.0f; + /*double produced_massic_flux = 0.0f; */ double escape_massic_flux = 0.0; - //double flux_diff = produced_massic_flux - escape_massic_flux; + /*double flux_diff = produced_massic_flux - escape_massic_flux; */ double escape_delta_m = 0.0f; double free_vol = port_area(engine) - fuel_vol; double internal_mass = (free_vol * pressure_abs) / (long_secc_combus(engine) * engine->fuel->const_burn_rate); - //double areas_ratio = total_burn_area / engine->grains->longitude; + /*double areas_ratio = total_burn_area / engine->grains->longitude; */ double virtual_escape_pressure = pressure_abs / pow((1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0))); - //double real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); + /*double real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ double force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO))))), 0.5) + (((virtual_escape_pressure - TREL_ATMOSPHERIC_PRESSURE) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); double adjusted_cf = force_coeff * engine->tube->nozzle_efficiency; double force = adjusted_cf * pressure_abs * engine->grains->longitude > 0 ? adjusted_cf * pressure_abs * engine->grains->longitude : 0.0; double total_thrust = 0.0; double delta_v = total_thrust / (fuel_mass + (double)dead_mass); - // sums for eventual result values + /* sums for eventual result values */ double sum_delta_v = delta_v; double sum_avg_thrust = force; - // variable for max thrust + /* variable for max thrust */ double max_thrust = force; double max_pressure = pressure_abs; - // iteration loop - for (int it = 0; it < max_iterations; it++) + /* iteration loop */ + int it = 0; + for (it = 0; it < max_iterations; it++) { pressure_abs = engine->fuel->const_burn_rate * long_secc_combus(engine) * internal_mass / free_vol; - //pressure = pressure_abs / 6894.757f; - //pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; + /*pressure = pressure_abs / 6894.757f; */ + /*pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; */ burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; inst_radius = inst_radius > 0 && (inst_radius + burn_rate * delta_t) < engine->grains->extern_radius ? inst_radius + burn_rate * delta_t : 0; inst_long = inst_radius > 0 ? inst_long - burn_rate * delta_t * 2 : 0; - //trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; - //long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; - //burn_area = trans_area * 2 + long_area; - //total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; + /*trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; */ + /*long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; */ + /*burn_area = trans_area * 2 + long_area; */ + /*total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; */ fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - (inst_radius * inst_radius)) * inst_long; free_vol = port_area(engine) - fuel_vol; produced_mass = fuel_mass - engine->fuel->density * fuel_vol; fuel_mass = engine->fuel->density * fuel_vol; - //produced_massic_flux = produced_mass / delta_t; + /*produced_massic_flux = produced_mass / delta_t; */ escape_massic_flux = pressure_abs > TREL_ATMOSPHERIC_PRESSURE ? engine->grains->longitude * pressure_abs * TREL_HEAT_CAP_RATIO * (sqrt(pow((2.0f / (TREL_HEAT_CAP_RATIO + 1.0f)), ((TREL_HEAT_CAP_RATIO + 1.0f) / (TREL_HEAT_CAP_RATIO - 1.0f))))) / pow((TREL_HEAT_CAP_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; - //flux_diff = produced_massic_flux - escape_massic_flux; + /*flux_diff = produced_massic_flux - escape_massic_flux; */ escape_delta_m = escape_massic_flux * delta_t; internal_mass = produced_mass + internal_mass - escape_delta_m > 0.0f ? produced_mass + internal_mass - escape_delta_m : 0.0f; - //areas_ratio = total_burn_area / engine->grains->longitude; + /*areas_ratio = total_burn_area / engine->grains->longitude; */ virtual_escape_pressure = pressure_abs / pow((1.0f + ((TREL_HEAT_CAP_RATIO - 1.0f) / 2.0f) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0f))); - //real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); + /*real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO))))), 0.5) + (((virtual_escape_pressure - TREL_ATMOSPHERIC_PRESSURE) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); adjusted_cf = force_coeff * desv_est_grains(engine); - //total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2.0f)) * delta_t; + /*total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2.0f)) * delta_t; */ last_force = force; force = adjusted_cf * pressure_abs * engine->grains->longitude > 0 ? adjusted_cf * pressure_abs * engine->grains->longitude : 0.0f; total_thrust = (force + last_force) / 2.0 * delta_t; delta_v = total_thrust / (fuel_mass + dead_mass); - // step time value + /* step time value */ if (time + step < burn_time) time += step; else time = 0; - // add to delta_v sum and avg thrust + /* add to delta_v sum and avg thrust */ sum_delta_v += delta_v; sum_avg_thrust += force; @@ -102,11 +103,11 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) max_pressure = pressure_abs; } - // storing values + /* storing values */ rocket->delta_v = sum_delta_v; rocket->max_thrust = max_thrust; rocket->max_pressure = max_pressure; - // calculate avg thrust + /* calculate avg thrust */ rocket->avg_thrust = sum_avg_thrust / max_iterations; return(0); diff --git a/src/design_func.c b/src/design_func.c index 61b6476..8500d3e 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -1,55 +1,55 @@ #include "design_func.h" -/*Hoja de Excel: Diseño*/ +/*Hoja de Excel: Diseño */ /*Functions*/ -// E25 returns chamber pressure in pascals +/* E25 returns chamber pressure in pascals */ double trel_get_pressure_pa(engine_t* engine) { return(trel_psi_to_pa(engine->pressure)); } -//E34 returns the grain standard deviation +/*E34 returns the grain standard deviation */ double desv_est_grains(engine_t *engine) { return((double)(engine->comp_area_values->burn_std_deviation)*10000.0f); } -//E35 Volumen unitario granos +/*E35 Volumen unitario granos */ double volumen_unitario(engine_t *engine) { return(TREL_PI*(0.1)*((engine->grains->extern_radius)*(engine->grains->extern_radius)-(0.01)*(0.01))); } -// E36 Masa unitaria grano +/* E36 Masa unitaria grano */ double masa_unitaria(engine_t* engine) { return(volumen_unitario(engine)*1000.0f*(engine->fuel->density)); } -// E37 Longitud total con combustible +/* E37 Longitud total con combustible */ double longitudt_ccombustible(engine_t *engine) { return((engine->grains->amount)*(engine->grains->longitude)); } -// E38 Port Area +/* E38 Port Area */ double port_area(engine_t* engine) { return(TREL_PI*(engine->grains->init_inter_radius)*(engine->grains->init_inter_radius)); } -// E39 +/* E39 */ double long_secc_combus(engine_t* engine) { return((engine->grains->grain_separation)*((engine->grains->amount)-1.0)+((engine->grains->longitude)*(engine->grains->amount))); } -// E49 Volumen especifico camara +/* E49 Volumen especifico camara */ double volumen_camara(engine_t *engine) { return(engine->temperature*TREL_GAS_CONST/trel_get_pressure_pa(engine)); } -// E62 Velocidad de +/* E62 Velocidad de */ double calc_escape_vel(engine_t* engine) { @@ -58,118 +58,118 @@ double calc_escape_vel(engine_t* engine) } /* Determinacion de fuerza sobre tapas*/ -// E140 Area interna +/* E140 Area interna */ double area_interna(engine_t *engine) { return(TREL_PI*pow(engine->tube->internal_radius,2)); } -// E141 Fuerza maxima +/* E141 Fuerza maxima */ double fuerza_maxima(engine_t *engine) { return(engine->max_pressure*area_interna(engine)); } -// E142 Fuerza por tornillo +/* E142 Fuerza por tornillo */ double fuerza_tornillo(engine_t *engine) { return(fuerza_maxima(engine)/engine->screws->amount); } /*Esfuerzo circunferencial*/ -// E144 Tension +/* E144 Tension */ double tension(engine_t *engine) { return(fuerza_maxima(engine)/engine->tube->material_area); } -// E145 Margen de seguridad +/* E145 Margen de seguridad */ double margen_de_seguridad(engine_t *engine) { return(engine->tube->shear_stress_tension/tension(engine)); } /*Esfuerzo longitudinal*/ -// E146 Area de cortante +/* E146 Area de cortante */ double area_de_cortante(engine_t *engine) { return(engine->screws->width_cutting_segment*engine->tube->wall_thickness); } -// E147 Cortante promedio +/* E147 Cortante promedio */ double cortante_promedio(engine_t *engine) { return(fuerza_tornillo(engine)/area_de_cortante(engine)); } -// E148 Margen de seguridad cortante +/* E148 Margen de seguridad cortante */ double margen_de_seguridad_cortante(engine_t *engine) { return(engine->tube->ult_stress_pressure/cortante_promedio(engine)); } /*Esfuerzo de aplastamiento*/ -// E149 Aplastamiento +/* E149 Aplastamiento */ double aplastamiento(engine_t *engine) { return(fuerza_tornillo(engine)/engine->screws->area_per_screw); } -// E150 Margen de seguridad aplaztamiento +/* E150 Margen de seguridad aplaztamiento */ double margen_de_seguridad_aplaztamiento(engine_t *engine) { return(engine->tube->shear_stress_tension/aplastamiento(engine)); } /*Deformacion circunferencial*/ -// E152 Deformacion unitaria +/* E152 Deformacion unitaria */ double deformacion_unitaria(engine_t *engine) { return(engine->tangencial_stress/engine->tube->young_module); } -// E153 Circunferencia interna total +/* E153 Circunferencia interna total */ double circunferencia_interna_total(engine_t *engine) { return(2*TREL_PI*engine->tube->internal_radius); } -// E154 Deformacion de la circunferencia interna +/* E154 Deformacion de la circunferencia interna */ double deformacion_circunferencia_interna(engine_t *engine) { return(deformacion_unitaria(engine)*circunferencia_interna_total(engine)*1000.0f); } -// E155 Circunferencia externa inicial +/* E155 Circunferencia externa inicial */ double circunferencia_externa_inicial(engine_t *engine) { return(TREL_PI*engine->tube->diameter_ext); } -// E156 Deformacion de circunferencia externa +/* E156 Deformacion de circunferencia externa */ double deformacion_circunferencia_externa(engine_t *engine) { return(circunferencia_externa_inicial(engine)*deformacion_unitaria(engine)*1000); } -// E157 Radio interno final -// radio_final(circunferencia_interna_total(),deformacion_circunferencia_interna()); +/* E157 Radio interno final */ +/* radio_final(circunferencia_interna_total(),deformacion_circunferencia_interna()); */ double radio_final(double circ_total,double deform_circ) { return(circ_total*(deform_circ/1000.0f)/(2.0f*TREL_PI)); } -// E158 Diferencia de radio interno -//double engine->tube->internal_radius = 1; // Dependencia E15 +/* E158 Diferencia de radio interno */ +/*double engine->tube->internal_radius = 1; Dependencia E15 */ double diferencia_radio_interno(engine_t *engine) { return(radio_final(circunferencia_interna_total(engine),deformacion_circunferencia_interna(engine))-engine->tube->internal_radius*1000.0f); } -// E159 es la misma ecuacion de E157 -// radio_final(circunferencia_interna_inicial(),deformacion_circunferencia_externa()); -// E160 Diferencia radio externo +/* E159 es la misma ecuacion de E157 */ +/* radio_final(circunferencia_interna_inicial(),deformacion_circunferencia_externa()); */ +/* E160 Diferencia radio externo */ double diferencia_radio_externo(engine_t *engine) { return((radio_final(circunferencia_externa_inicial(engine),deformacion_circunferencia_externa(engine))-(engine->tube->diameter_ext/2.0f))*1000.0f); } /*Escape*/ -// E88 +/* E88 */ double throat_area(engine_t *engine) { return(mg_combustion(engine) * vol_esp_garganta(engine) / velocidad_garganta(engine)); } -// E89 +/* E89 */ double escape_area(engine_t* engine) { return(mg_combustion(engine) * vol_esp_escape(engine) / calc_escape_vel(engine)); } -// E101 engine->nozzle_efficiency +/* E101 engine->nozzle_efficiency */ void set_nozzle_efficiency(engine_t* engine, double value) { engine->nozzle_efficiency = value; diff --git a/src/design_resist.c b/src/design_resist.c index 5deaa6e..aa58d8a 100644 --- a/src/design_resist.c +++ b/src/design_resist.c @@ -5,36 +5,36 @@ Implementacion original por Kevin, en Python Inicio de traduccion a C por lross2k ******/ -// Variables necesarias de partes anteriores -//double temp_E13 = 0.0730f; -//double temp_E14 = 0.0052f; -//double temp_E15 = 0.0314f; +/* Variables necesarias de partes anteriores */ +/*double temp_E13 = 0.0730f; */ +/*double temp_E14 = 0.0052f; */ +/*double temp_E15 = 0.0314f; */ -// Resistencia de materiales E115 +/* Resistencia de materiales E115 */ void trel_mean_tubing_diameter(engine_t *engine) { engine->tube->mean_tubing_diameter = engine->tube->diameter_ext-engine->tube->wall_thickness; } -// condición de espesor del tubo E116 +/* condición de espesor del tubo E116 */ void trel_width_condition(engine_t *engine) { engine->width_condition = engine->tube->mean_tubing_diameter/engine->tube->wall_thickness; } -// Presión maxima E118 +/* Presión maxima E118 */ void trel_engine_max_pressure(engine_t *engine) { - // TODO: max_pressure = get_max(double *sim_data, int n_sim_data) - engine->max_pressure = 5.95f*pow(10,6); // Pascal - //engine.max_pressure = 5.95*e**6 # Pascales (Obtenido de simulación) - //engine.pmax = 863.01 #PSI + /* TODO: max_pressure = get_max(double *sim_data, int n_sim_data) */ + engine->max_pressure = 5.95f*pow(10,6); /* Pascal */ + /*engine.max_pressure = 5.95*e**6 # Pascales (Obtenido de simulación) */ + /*engine.pmax = 863.01 #PSI */ } /* Esfuerzo por presión */ -// Esfuezo Tangencial E121 +/* Esfuezo Tangencial E121 */ void trel_tangencial_stress(engine_t *engine) { - // TODO: implement into engine - // could be done branchless + /* TODO: implement into engine */ + /* could be done branchless */ if (engine->width_condition> 20.0f) { engine->tangencial_stress = engine->max_pressure*engine->tube->internal_radius/engine->tube->wall_thickness; @@ -44,7 +44,7 @@ void trel_tangencial_stress(engine_t *engine) engine->tangencial_stress = engine->max_pressure*(pow((engine->tube->diameter_ext/2.0f),2.0f)+pow(engine->tube->internal_radius,2.0f))/(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)); } } -// Esfuerzo Longitudinal E122 +/* Esfuerzo Longitudinal E122 */ void trel_logitudinal_stress(engine_t *engine) { if (engine->width_condition > 20.0f) @@ -56,7 +56,7 @@ void trel_logitudinal_stress(engine_t *engine) engine->longitudinal_stress = engine->max_pressure*pow(engine->tube->internal_radius,2.0f)/(pow(engine->tube->diameter_ext,2.0f)-pow(engine->tube->internal_radius,2.0f)); } } -// Esfuerzo Radial E123 +/* Esfuerzo Radial E123 */ void trel_radial_stress(engine_t *engine) { if (engine->width_condition > 20.0f) @@ -68,7 +68,7 @@ void trel_radial_stress(engine_t *engine) engine->radial_stress = -engine->max_pressure; } } -// Esfuerzo Maximo E125 +/* Esfuerzo Maximo E125 */ void trel_max_stress(engine_t *engine) { if (engine->tangencial_stress > engine->radial_stress) @@ -80,7 +80,7 @@ void trel_max_stress(engine_t *engine) engine->max_stress = engine->radial_stress; } } -// Margen de Seguridad E127 +/* Margen de Seguridad E127 */ void trel_margin_of_safety(engine_t *engine) { engine->margin_of_safety = engine->tube->shear_stress_tension/engine->max_stress; @@ -88,38 +88,38 @@ void trel_margin_of_safety(engine_t *engine) /* Determinación de Areas afectadas por tornillos */ -// engine_t has screws_t *screws to initialize at allocation -//engine->screws->n_screws = 6; -//engine->screws->diameter = 0.009f; -//engine->screws->dist_center_wall = 0.01f; +/* engine_t has screws_t *screws to initialize at allocation */ +/*engine->screws->n_screws = 6; */ +/*engine->screws->diameter = 0.009f; */ +/*engine->screws->dist_center_wall = 0.01f; */ -// Area_Transversal_tubo E133 -// engine_t has tubing_t *tube +/* Area_Transversal_tubo E133 */ +/* engine_t has tubing_t *tube */ void trel_transversal_area_tube(engine_t *engine) { engine->tube->transversal_area = (double)TREL_PI*(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)); } -// Angulo de sector circular E134 +/* Angulo de sector circular E134 */ void trel_ang_circular_sector(engine_t *engine) { engine->tube->sector_angle = (180.0/TREL_PI)*asin((engine->screws->diameter/2)/engine->tube->internal_radius); } -// Area transversal de 1 tornillo E135 +/* Area transversal de 1 tornillo E135 */ void trel_area_per_screw(engine_t *engine) { engine->screws->area_per_screw = (double)(engine->tube->sector_angle/2.0f)*(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)-pow(engine->tube->wall_thickness,2.0f)); } -// Area transversal ocupada por los tornillos E136 +/* Area transversal ocupada por los tornillos E136 */ void trel_screw_occupied_area(engine_t *engine) { engine->screws->screw_occupied_area = engine->screws->area_per_screw*engine->screws->amount; } -// Area de material E137 +/* Area de material E137 */ void trel_tube_mateial_area(engine_t *engine) { engine->tube->material_area = engine->tube->transversal_area-engine->screws->screw_occupied_area; } -// Espesor del segmento cortante E138 +/* Espesor del segmento cortante E138 */ void trel_width_cutting_segment(engine_t *engine) { engine->screws->width_cutting_segment = engine->screws->dist_center_wall-(engine->screws->diameter/2.0f); diff --git a/src/engine.c b/src/engine.c index 6390890..f07ceae 100644 --- a/src/engine.c +++ b/src/engine.c @@ -57,9 +57,9 @@ tubing_t* trel_tubing_init( printf("Tubing initialization failed\n"); exit(1); } -#if defined (_WIN32) // Take advantage of TR 24731-1 from MSVC +#if defined (_WIN32) /* Take advantage of TR 24731-1 from MSVC */ strcpy_s(tube->material, sizeof(char) * TREL_MAX_STR_LEN, material); -#else // Take the most portable approach for NIX +#else /* Take the most portable approach for NIX */ strncpy(tube->material, material, TREL_MAX_STR_LEN); #endif tube->diameter_ext = diameter; @@ -90,9 +90,9 @@ screws_t* trel_screws_init( printf("Screws initialization failed\n"); exit(1); } -#if defined (_WIN32) // Take advantage of TR 24731-1 from MSVC +#if defined (_WIN32) /* Take advantage of TR 24731-1 from MSVC */ strcpy_s(screws->material, sizeof(char) * TREL_MAX_STR_LEN, material); -#else // Take the most portable approach for NIX +#else /* Take the most portable approach for NIX */ strncpy(screws->material, material, TREL_MAX_STR_LEN); #endif screws->diameter = diameter; @@ -115,6 +115,8 @@ engine_t* trel_engine_init( screws_t *screws) { comp_area_t *comp_area_values = (comp_area_t*) malloc(sizeof(comp_area_t)); + engine_t *engine = (engine_t *) malloc(sizeof(engine_t)); + if (!comp_area_values) { printf("Engine initialization failed due to comp_area_values\n"); exit(1); @@ -125,7 +127,6 @@ engine_t* trel_engine_init( comp_area_values->burn_std_deviation = 0.0f; comp_area_values->burn_sum_diff = 0.0f; - engine_t *engine = (engine_t *) malloc(sizeof(engine_t)); if (!engine) { printf("Engine initialization failed\n"); exit(1); @@ -153,9 +154,9 @@ engine_t* trel_engine_init( trel_width_cutting_segment(engine); trel_area_per_screw(engine); trel_tangencial_stress(engine); - if (trel_run_area_comp_iterations(&engine)) // 1 means there was an error + if (trel_run_area_comp_iterations(&engine)) /* 1 means there was an error */ { - return(NULL); // NULL means the engine couldn't be properly generated + return(NULL); /* NULL means the engine couldn't be properly generated */ } return(engine); } diff --git a/src/height_sim.c b/src/height_sim.c index d289ef8..4b2c3e4 100644 --- a/src/height_sim.c +++ b/src/height_sim.c @@ -117,16 +117,17 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) { engine_t *engine = (*rocket)->engine; trel_height_sim_t *res = (*rocket)->sim_values; - res->time[0] = 0; - // iterate over given iterations and step size + /* iterate over given iterations and step size */ const unsigned int max_iterations = TREL_MAX_ITERATIONS; const double burn_time = Tiempo_quemado_combustion((*rocket)->engine); const double step = burn_time / TREL_MAX_ITERATIONS; - // variables for iteration + unsigned int it = 0; + + /* variables for iteration */ double rocket_height = 0.0; - //double rocket_total_thrust = 0.0; + /*double rocket_total_thrust = 0.0; */ double fuel_volume = 0.0; double gravitational_accel = 0.0; double force_coeff = 0.0; @@ -171,13 +172,13 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) res->rocket_force[0] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; res->rocket_force_balance[0] = calc_force_balance(res->rocket_force[0], res->rocket_position[0], res->rocket_speed[0], res->rocket_drag[0], res->rocket_weight[0]); - // iteration loop - for (unsigned int it = 1; it < max_iterations; it++) + /* iteration loop */ + for (it = 1; it < max_iterations; it++) { res->time[it] = res->time[it - 1] + step; gravitational_accel = calc_iter_grav_accel(rocket, rocket_height); - atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); // ACA SE COLAPSA EL TITANIC + atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); /* ACA SE COLAPSA EL TITANIC */ pressure = calc_iter_pressure(engine, intern_gas_mass, free_volume, atmospheric_pressure); inst_radius = calc_inst_radius(inst_radius, burn_rate, step, (*rocket)->engine->grains->extern_radius); inst_long = calc_inst_long(inst_radius, inst_long, burn_rate, step); @@ -205,7 +206,7 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) res->rocket_position[it] = (res->rocket_speed[it] * step + res->rocket_position[it - 1]) * (res->rocket_speed[it] * step + res->rocket_position[it - 1] > 0.0); rocket_height = (*rocket)->initial_height + res->rocket_position[it]; - // Check for new max values + /* Check for new max values */ if (rocket_height > res->max_rocket_height) res->max_rocket_height = rocket_height; if (res->rocket_position[it] > res->max_rocket_position) @@ -220,5 +221,5 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) res->max_rocket_drag = res->rocket_drag[it]; } - return(0); // everything went well + return(0); /* everything went well */ } diff --git a/src/rocket.c b/src/rocket.c index b40ab7b..20f32bd 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -13,12 +13,13 @@ trel_rocket_t *trel_rocket_init( double drag_coefficient) { trel_rocket_t* rocket = (trel_rocket_t*)malloc(sizeof(trel_rocket_t)); + trel_height_sim_t* sim_values = (trel_height_sim_t*)malloc(sizeof(trel_height_sim_t)); + unsigned int i = 0; if (!rocket) { printf("Rocket initialization failed\n"); exit(1); } - trel_height_sim_t* sim_values = (trel_height_sim_t*)malloc(sizeof(trel_height_sim_t)); if (!sim_values) { printf("Engine initialization failed due to sim_values\n"); exit(1); @@ -30,7 +31,7 @@ trel_rocket_t *trel_rocket_init( sim_values->max_rocket_position = 0.0; sim_values->max_rocket_speed = 0.0; - for (unsigned int i = 0; i < TREL_MAX_ITERATIONS; i++) + for (i = 0; i < TREL_MAX_ITERATIONS; i++) { sim_values->rocket_position[i] = 0.0; sim_values->rocket_speed[i] = 0.0; @@ -54,13 +55,13 @@ trel_rocket_t *trel_rocket_init( rocket->body_diameter = body_diameter; rocket->drag_coefficient = drag_coefficient; rocket->sim_values = sim_values; - if (trel_run_time_comp_iterations(rocket)) // 1 means there was an error + if (trel_run_time_comp_iterations(rocket)) /* 1 means there was an error */ { - return(NULL); // NULL means the engine couldn't be properly generated + return(NULL); /* NULL means the engine couldn't be properly generated */ } - if (trel_run_height_sim_iterations(&rocket)) // 1 means there was an error + if (trel_run_height_sim_iterations(&rocket)) /* 1 means there was an error */ { - return(NULL); // NULL means the engine couldn't be properly generated + return(NULL); /* NULL means the engine couldn't be properly generated */ } return(rocket); } diff --git a/src/val_termod.c b/src/val_termod.c index 2a5b872..fc4f766 100644 --- a/src/val_termod.c +++ b/src/val_termod.c @@ -1,84 +1,84 @@ #include "val_termod.h" + +/* Volumenes */ -// Volumenes - -// E49 Volumen Especifico Camara de Combustible +/* E49 Volumen Especifico Camara de Combustible */ double vol_esp_cam_combustion(engine_t *engine) { return (engine->temperature * TREL_GAS_CONST) / trel_get_pressure_pa(engine); } -// E50 Volumen Especifico de Garganta +/* E50 Volumen Especifico de Garganta */ double vol_esp_garganta(engine_t *engine) { return vol_esp_cam_combustion(engine) * pow((TREL_HEAT_CAP_RATIO + 1.0) / 2.0 , (1.0 / (TREL_HEAT_CAP_RATIO - 1.0))); } -// E51 Volumen Especifico Escape +/* E51 Volumen Especifico Escape */ double vol_esp_escape(engine_t *engine) { return vol_esp_cam_combustion(engine) * pow((trel_get_pressure_pa(engine) / TREL_ATMOSPHERIC_PRESSURE), (1 / TREL_HEAT_CAP_RATIO)); } -// Presiones +/* Presiones */ -// E53 presion de garganta +/* E53 presion de garganta */ double presion_garganta(engine_t *engine) { return trel_get_pressure_pa(engine) * pow((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)), (TREL_HEAT_CAP_RATIO)); } -// Temperaturas +/* Temperaturas */ -// E56 temperatura de garganta +/* E56 temperatura de garganta */ double trel_temper_garganta(engine_t *engine) { return engine->temperature * pow((vol_esp_cam_combustion(engine) / vol_esp_garganta(engine)),(TREL_HEAT_CAP_RATIO - 1)); } -// E57 temperatura de escape +/* E57 temperatura de escape */ double presion_escape(engine_t *engine) { return engine->temperature * (pow((TREL_ATMOSPHERIC_PRESSURE / trel_get_pressure_pa(engine)),((TREL_HEAT_CAP_RATIO - 1) / TREL_HEAT_CAP_RATIO))); } -// Velocidades +/* Velocidades */ -// E59 velocidad garganta +/* E59 velocidad garganta */ double velocidad_garganta(engine_t *engine) { return pow(((2.0f * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO + 1.0f) * TREL_GAS_CONST * engine->temperature),(0.5f)); } -// E60 Velocidad Sonica Local at +/* E60 Velocidad Sonica Local at */ double vel_sonica_local_at(engine_t *engine) { return pow((TREL_HEAT_CAP_RATIO * TREL_GAS_CONST * presion_garganta(engine)),(0.5)); } -// E61 Numero Mach Mt +/* E61 Numero Mach Mt */ double vel_numero_mach_mt(engine_t *engine) { return velocidad_garganta(engine) / vel_sonica_local_at(engine); } -// E63 Velocidad Sonica Local a2 +/* E63 Velocidad Sonica Local a2 */ double vel_sonica_local_a2(engine_t *engine) { return pow((TREL_HEAT_CAP_RATIO * TREL_GAS_CONST * presion_escape(engine)),(0.5)); } -// E64 Numero Mach M2 +/* E64 Numero Mach M2 */ double vel_numero_mach_m2(engine_t *engine) { return calc_escape_vel(engine) / vel_sonica_local_a2(engine); } -// E65 Velocidad de escape caracteristica +/* E65 Velocidad de escape caracteristica */ double vel_escape_caract(engine_t *engine) { return pow(pow((TREL_GAS_CONST * engine->temperature) / (TREL_HEAT_CAP_RATIO *(2 / (TREL_HEAT_CAP_RATIO + 1))),((TREL_HEAT_CAP_RATIO + 1) / (TREL_HEAT_CAP_RATIO - 1))), (0.5)); From fb3907f57148b78b17b39f12da073f00094e7717 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 22 Apr 2023 08:49:47 -0600 Subject: [PATCH 23/42] Changed main.c, moved to tests/ Now the tests will be implemented in their own directory ~lross2k~ --- CMakeLists.txt | 14 +++---- {include => tests}/TREL.h | 0 main.c => tests/manual_test.c | 77 ++++++----------------------------- 3 files changed, 19 insertions(+), 72 deletions(-) rename {include => tests}/TREL.h (100%) rename main.c => tests/manual_test.c (59%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94b8e36..a7dbb87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ PROJECT (structure_test VERSION 0.0.1 DESCRIPTION "Basic structure for compilati OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" ON) # Developer can choose to build the CLI testapp -OPTION (BUILD_CLI_TESTAPP "Build testapp" ON) +OPTION (BUILD_MANUAL_TEST "Build testapp" ON) # Used to generate header with macros for the library #INCLUDE (GenerateExportHeader) @@ -67,13 +67,13 @@ SET_TARGET_PROPERTIES (TREL PROPERTIES COMPILE_FLAGS -DLIB_STATIC_DEFINE) SET_PROPERTY(TARGET TREL PROPERTY C_STANDARD 90) # Additional function calls for building the testapp -IF (BUILD_CLI_TESTAPP) +IF (BUILD_MANUAL_TEST) # Adding an executable to test the library - ADD_EXECUTABLE (testapp "main.c") + ADD_EXECUTABLE (manualtest "tests/manual_test.c") # Declaring a link between the executable and the library - TARGET_LINK_LIBRARIES (testapp TREL) + TARGET_LINK_LIBRARIES (manualtest TREL) #target_link_libraries(testapp PRIVATE ) - TARGET_INCLUDE_DIRECTORIES (testapp PUBLIC "include") + TARGET_INCLUDE_DIRECTORIES (manualtest PUBLIC "tests") # Additional arguments for the excecutable compilation - TARGET_COMPILE_FEATURES (testapp PUBLIC c_std_90) -ENDIF (BUILD_CLI_TESTAPP) + TARGET_COMPILE_FEATURES (manualtest PUBLIC c_std_90) +ENDIF (BUILD_MANUAL_TEST) diff --git a/include/TREL.h b/tests/TREL.h similarity index 100% rename from include/TREL.h rename to tests/TREL.h diff --git a/main.c b/tests/manual_test.c similarity index 59% rename from main.c rename to tests/manual_test.c index 46fe297..8f7a7e4 100644 --- a/main.c +++ b/tests/manual_test.c @@ -1,5 +1,4 @@ #include -#include // Header for all the library functions #include @@ -26,10 +25,6 @@ void print_debug_example() 0.007466, 0.01 ); - - //printf("Probando %i y %s\n", created_screw->amount, created_screw->material); - printf("Probando %i\n", created_screw->amount); - // Se inicializa la tuberia tubing_t* created_tube = trel_tubing_init( "Aluminio 6061-T6", @@ -80,17 +75,17 @@ void print_debug_example() //printf("\nLa temperatura en garganta es de %lf\n", trel_temper_garganta(testing_engine)); // Probando valores generados por comp_area.c - //printf("\nArea longitudinal promedio: %lf\n", testing_engine->comp_area_values->avg_long_area); - //printf("Area quemado promedio: %lf\n", testing_engine->comp_area_values->avg_burn_area); - //printf("Area transversal promedio: %lf\n", testing_engine->comp_area_values->avg_trans_area); - //printf("Suma de diferencia quemado: %lf\n", testing_engine->comp_area_values->burn_sum_diff); - //printf("Desviacion estandar area de quemado: %lf\n", testing_engine->comp_area_values->burn_std_deviation); + printf("\nArea longitudinal promedio: %lf\n", testing_engine->comp_area_values->avg_long_area); + printf("Area quemado promedio: %lf\n", testing_engine->comp_area_values->avg_burn_area); + printf("Area transversal promedio: %lf\n", testing_engine->comp_area_values->avg_trans_area); + printf("Suma de diferencia quemado: %lf\n", testing_engine->comp_area_values->burn_sum_diff); + printf("Desviacion estandar area de quemado: %lf\n", testing_engine->comp_area_values->burn_std_deviation); // Probando valores generados por comp_tiempo.c - //printf("\nDelta V total: %lf\n", testing_rocket->delta_v); - //printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); - //printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); - //printf("Maxima presion: %lf\n", testing_rocket->max_pressure); + printf("\nDelta V total: %lf\n", testing_rocket->delta_v); + printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); + printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); + printf("Maxima presion: %lf\n", testing_rocket->max_pressure); // Printing rocket values printf("\nRocket data\n"); @@ -119,61 +114,13 @@ void print_debug_example() printf("time %lf\n", testing_rocket->time); */ - printf("Tubing's material name: %s\n", created_tube->material); - printf("Screws' material name: %s\n", created_screw->material); + //printf("Tubing's material name: %s\n", created_tube->material); + //printf("Screws' material name: %s\n", created_screw->material); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica int main(int argc, char *argv[]) { - printf("* * * * * * * * * * * * * * * * * * * * * * * *\n"); - printf("* TECSpace Rocket Engine Lib *\n"); - printf("* v 0.2d *\n"); - printf("* * * * * * * * * * * * * * * * * * * * * * * *\n\n"); - if (argc <= 1) - { - printf( "Usage: -f [options]\n" - "Check help with : --help or -h\n"); - - printf("\n* * *\nShowing debug example, gonna remove this for final CLI build\n* * *\n\n"); - - print_debug_example(); - } - else - { - int i; - for (i = 1; i < argc; i++) { - printf("Parameter %s\n", argv[i]); - if (strncmp(argv[i],"-f",2) == 0) { - if (i+1 < argc) { - //-f toma el siguiente argumento - if (strncmp(argv[i+1],"debug",6) == 0) { - printf("\'-f debug\' showing the debugging example\n"); - print_debug_example(); - } - else { - printf("%s is not a valid file for -f\nRead --help\n", argv[i+1]); - } - i++; - } - else - { - printf("Missing arguments\n"); - } - } - else if (strncmp(argv[i],"--help",7) == 0 || strncmp(argv[i],"-h",3) == 0) { - printf("This app hasn't been released yet, expect bugs\n\n"); - printf("--help (-h)\nShows this message\n\n"); - printf("-f [options]\nSelects the engine file to read and searches for options\n" - "options should be given as [opt1,opt2,opt3] with no spaces\n" - "there aren\'t avaliable options yet\n"); - } - else - { - printf("Unknown parameter %s\n", argv[i]); - } - printf("\n"); - } - } + print_debug_example(); return(0); } From ac7514e3d8da892f2b1e3431a708d9d8c0ecbfaf Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 22 Apr 2023 13:57:23 -0600 Subject: [PATCH 24/42] Fixed comp_tiempo.c issues Learned GDB in the process, cool ~lross2k~ --- .gitignore | 2 ++ include/height_sim.h | 3 ++ main.c | 80 +++++++------------------------------------- src/comp_tiempo.c | 35 ++++++++++--------- src/engine.c | 8 ++--- 5 files changed, 40 insertions(+), 88 deletions(-) diff --git a/.gitignore b/.gitignore index 3ec1797..592d626 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ CMakeSettings.json out/ # Recommended build directory build/ +Debug/ +Release/ # Binary files leftover *.exe *.so diff --git a/include/height_sim.h b/include/height_sim.h index 17a1dbc..0134399 100644 --- a/include/height_sim.h +++ b/include/height_sim.h @@ -5,5 +5,8 @@ /* Function prototypes */ int TREL_EXPORT trel_run_height_sim_iterations(trel_rocket_t** rocket); +double calc_iter_virt_escape_pressure(double pressure, engine_t *engine); +double calc_iter_force_coeff(double virt_escape_pressure, double pressure, double atmospheric_pressure, engine_t *engine); +double calc_iter_escape_massic_flux(double pressure, double atmospheric_pressure, engine_t *engine); #endif /*HEIGHT_SIM_H */ diff --git a/main.c b/main.c index 46fe297..5a559ab 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,6 @@ void print_debug_example() { // Se inicializan los tornillos - printf("Inicializando structs\n"); grains_t* created_grains = trel_grains_init( 4, 0.01, @@ -27,9 +26,6 @@ void print_debug_example() 0.01 ); - //printf("Probando %i y %s\n", created_screw->amount, created_screw->material); - printf("Probando %i\n", created_screw->amount); - // Se inicializa la tuberia tubing_t* created_tube = trel_tubing_init( "Aluminio 6061-T6", @@ -87,22 +83,18 @@ void print_debug_example() //printf("Desviacion estandar area de quemado: %lf\n", testing_engine->comp_area_values->burn_std_deviation); // Probando valores generados por comp_tiempo.c - //printf("\nDelta V total: %lf\n", testing_rocket->delta_v); - //printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); - //printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); - //printf("Maxima presion: %lf\n", testing_rocket->max_pressure); + printf("\nDelta V total: %lf\n", testing_rocket->delta_v); + printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); + printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); + printf("Maxima presion: %lf\n\n", testing_rocket->max_pressure); // Printing rocket values - printf("\nRocket data\n"); - printf("telemetry_mass %lf\n", testing_rocket->telemetry_mass); - printf("parachute_mass %lf\n", testing_rocket->parachute_mass); - printf("fuselage_mass %lf\n", testing_rocket->fuselage_mass); - printf("payload_mass %lf\n", testing_rocket->payload_mass); + //printf("\nRocket data\n"); + //printf("telemetry_mass %lf\n", testing_rocket->telemetry_mass); + //printf("parachute_mass %lf\n", testing_rocket->parachute_mass); + //printf("fuselage_mass %lf\n", testing_rocket->fuselage_mass); + //printf("payload_mass %lf\n", testing_rocket->payload_mass); printf("initial_height %lf\n", testing_rocket->initial_height); - printf("avg_thrust %lf\n", testing_rocket->avg_thrust); - printf("max_thrust %lf\n", testing_rocket->max_thrust); - printf("delta_v %lf\n", testing_rocket->delta_v); - printf("max_pressure %lf\n", testing_rocket->max_pressure); printf("sim_latitude %lf\n", testing_rocket->sim_latitude); printf("max_sim_height %lf\n", testing_rocket->max_sim_height); printf("body_diameter %lf\n", testing_rocket->body_diameter); @@ -119,61 +111,13 @@ void print_debug_example() printf("time %lf\n", testing_rocket->time); */ - printf("Tubing's material name: %s\n", created_tube->material); - printf("Screws' material name: %s\n", created_screw->material); + //printf("Tubing's material name: %s\n", created_tube->material); + //printf("Screws' material name: %s\n", created_screw->material); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica int main(int argc, char *argv[]) { - printf("* * * * * * * * * * * * * * * * * * * * * * * *\n"); - printf("* TECSpace Rocket Engine Lib *\n"); - printf("* v 0.2d *\n"); - printf("* * * * * * * * * * * * * * * * * * * * * * * *\n\n"); - if (argc <= 1) - { - printf( "Usage: -f [options]\n" - "Check help with : --help or -h\n"); - - printf("\n* * *\nShowing debug example, gonna remove this for final CLI build\n* * *\n\n"); - - print_debug_example(); - } - else - { - int i; - for (i = 1; i < argc; i++) { - printf("Parameter %s\n", argv[i]); - if (strncmp(argv[i],"-f",2) == 0) { - if (i+1 < argc) { - //-f toma el siguiente argumento - if (strncmp(argv[i+1],"debug",6) == 0) { - printf("\'-f debug\' showing the debugging example\n"); - print_debug_example(); - } - else { - printf("%s is not a valid file for -f\nRead --help\n", argv[i+1]); - } - i++; - } - else - { - printf("Missing arguments\n"); - } - } - else if (strncmp(argv[i],"--help",7) == 0 || strncmp(argv[i],"-h",3) == 0) { - printf("This app hasn't been released yet, expect bugs\n\n"); - printf("--help (-h)\nShows this message\n\n"); - printf("-f [options]\nSelects the engine file to read and searches for options\n" - "options should be given as [opt1,opt2,opt3] with no spaces\n" - "there aren\'t avaliable options yet\n"); - } - else - { - printf("Unknown parameter %s\n", argv[i]); - } - printf("\n"); - } - } + print_debug_example(); return(0); } diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index 826da49..e1b6d6b 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -1,17 +1,20 @@ #include "comp_tiempo.h" +#define POW2(VAR) (VAR * VAR) + /*Comportamiento en el Tiempo!AD3511 */ int trel_run_time_comp_iterations(trel_rocket_t* rocket) { engine_t* engine = rocket->engine; /* iterate over given iterations and step size */ - const double max_iterations = 1000.0; + const double max_iterations = TREL_MAX_ITERATIONS; const double burn_time = Tiempo_quemado_combustion(engine); - /*const double step = burn_time / max_iterations; DEBUG */ - const double step = 0.15999891; + const double step = burn_time / 3000.0; double time = 0.0; /* time in seconds */ double last_force = 0.0; + const double total_volume = TREL_PI * POW2(engine->tube->internal_radius) * + (engine->grains->grain_separation * (engine->grains->amount - 1) + engine->grains->longitude * engine->grains->amount); /* variables for iteration */ double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemetry_mass + engine->engine_mass + rocket->payload_mass; @@ -26,19 +29,19 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /*double long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; */ /*double burn_area = trans_area * 2 + long_area; */ /*double total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; */ - double fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius) * inst_long; + double fuel_vol = engine->grains->amount * TREL_PI * (POW2(engine->grains->extern_radius) - POW2(inst_radius)) * inst_long; double fuel_mass = engine->fuel->density * fuel_vol; double produced_mass = 0.0; /*double produced_massic_flux = 0.0f; */ double escape_massic_flux = 0.0; /*double flux_diff = produced_massic_flux - escape_massic_flux; */ double escape_delta_m = 0.0f; - double free_vol = port_area(engine) - fuel_vol; - double internal_mass = (free_vol * pressure_abs) / (long_secc_combus(engine) * engine->fuel->const_burn_rate); + double free_vol = total_volume - fuel_vol; + double internal_mass = (free_vol * pressure_abs) / (engine->temperature * TREL_GAS_CONST); /*double areas_ratio = total_burn_area / engine->grains->longitude; */ - double virtual_escape_pressure = pressure_abs / pow((1.0 + ((TREL_HEAT_CAP_RATIO - 1.0) / 2.0) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0))); + double virtual_escape_pressure = calc_iter_virt_escape_pressure(pressure_abs, engine); /*double real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ - double force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO))))), 0.5) + (((virtual_escape_pressure - TREL_ATMOSPHERIC_PRESSURE) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); + double force_coeff = calc_iter_force_coeff(virtual_escape_pressure, pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); double adjusted_cf = force_coeff * engine->tube->nozzle_efficiency; double force = adjusted_cf * pressure_abs * engine->grains->longitude > 0 ? adjusted_cf * pressure_abs * engine->grains->longitude : 0.0; double total_thrust = 0.0; @@ -56,7 +59,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) int it = 0; for (it = 0; it < max_iterations; it++) { - pressure_abs = engine->fuel->const_burn_rate * long_secc_combus(engine) * internal_mass / free_vol; + pressure_abs = TREL_GAS_CONST * engine->temperature * internal_mass / free_vol; /*pressure = pressure_abs / 6894.757f; */ /*pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; */ burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; @@ -66,23 +69,23 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /*long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; */ /*burn_area = trans_area * 2 + long_area; */ /*total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; */ - fuel_vol = engine->grains->extern_radius * TREL_PI * (engine->grains->extern_radius * engine->grains->extern_radius - (inst_radius * inst_radius)) * inst_long; - free_vol = port_area(engine) - fuel_vol; + fuel_vol = engine->grains->amount * TREL_PI * (POW2(engine->grains->extern_radius) - POW2(inst_radius)) * inst_long; + free_vol = total_volume - fuel_vol; produced_mass = fuel_mass - engine->fuel->density * fuel_vol; fuel_mass = engine->fuel->density * fuel_vol; /*produced_massic_flux = produced_mass / delta_t; */ - escape_massic_flux = pressure_abs > TREL_ATMOSPHERIC_PRESSURE ? engine->grains->longitude * pressure_abs * TREL_HEAT_CAP_RATIO * (sqrt(pow((2.0f / (TREL_HEAT_CAP_RATIO + 1.0f)), ((TREL_HEAT_CAP_RATIO + 1.0f) / (TREL_HEAT_CAP_RATIO - 1.0f))))) / pow((TREL_HEAT_CAP_RATIO * engine->fuel->const_burn_rate * long_secc_combus(engine)), 0.5f) : 0.0f; + escape_massic_flux = calc_iter_escape_massic_flux(pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); /*flux_diff = produced_massic_flux - escape_massic_flux; */ escape_delta_m = escape_massic_flux * delta_t; internal_mass = produced_mass + internal_mass - escape_delta_m > 0.0f ? produced_mass + internal_mass - escape_delta_m : 0.0f; /*areas_ratio = total_burn_area / engine->grains->longitude; */ - virtual_escape_pressure = pressure_abs / pow((1.0f + ((TREL_HEAT_CAP_RATIO - 1.0f) / 2.0f) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1.0f))); + virtual_escape_pressure = calc_iter_virt_escape_pressure(pressure_abs, engine); /*real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ - force_coeff = pow((((2.0 * TREL_HEAT_CAP_RATIO * TREL_HEAT_CAP_RATIO) / (TREL_HEAT_CAP_RATIO - 1.0)) * pow((2.0 / (TREL_HEAT_CAP_RATIO + 1.0)), ((TREL_HEAT_CAP_RATIO + 1.0) / (TREL_HEAT_CAP_RATIO - 1.0))) * ((1.0 - pow((virtual_escape_pressure / pressure_abs), ((TREL_HEAT_CAP_RATIO - 1.0) / TREL_HEAT_CAP_RATIO))))), 0.5) + (((virtual_escape_pressure - TREL_ATMOSPHERIC_PRESSURE) / (pressure_abs)) * (engine->grains->grain_separation / engine->grains->longitude)); - adjusted_cf = force_coeff * desv_est_grains(engine); + force_coeff = calc_iter_force_coeff(virtual_escape_pressure, pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); + adjusted_cf = force_coeff * engine->tube->nozzle_efficiency; /*total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2.0f)) * delta_t; */ last_force = force; - force = adjusted_cf * pressure_abs * engine->grains->longitude > 0 ? adjusted_cf * pressure_abs * engine->grains->longitude : 0.0f; + force = adjusted_cf * pressure_abs * throat_area(engine) > 0 ? adjusted_cf * pressure_abs * throat_area(engine) : 0.0; total_thrust = (force + last_force) / 2.0 * delta_t; delta_v = total_thrust / (fuel_mass + dead_mass); diff --git a/src/engine.c b/src/engine.c index f07ceae..7109aba 100644 --- a/src/engine.c +++ b/src/engine.c @@ -58,9 +58,9 @@ tubing_t* trel_tubing_init( exit(1); } #if defined (_WIN32) /* Take advantage of TR 24731-1 from MSVC */ - strcpy_s(tube->material, sizeof(char) * TREL_MAX_STR_LEN, material); + strcpy_s(tube->material, sizeof(char) * TREL_MAX_STR_LEN - 1, material); #else /* Take the most portable approach for NIX */ - strncpy(tube->material, material, TREL_MAX_STR_LEN); + strncpy(tube->material, material, TREL_MAX_STR_LEN - 1); #endif tube->diameter_ext = diameter; tube->wall_thickness = thickness; @@ -91,9 +91,9 @@ screws_t* trel_screws_init( exit(1); } #if defined (_WIN32) /* Take advantage of TR 24731-1 from MSVC */ - strcpy_s(screws->material, sizeof(char) * TREL_MAX_STR_LEN, material); + strcpy_s(screws->material, sizeof(char) * TREL_MAX_STR_LEN - 1, material); #else /* Take the most portable approach for NIX */ - strncpy(screws->material, material, TREL_MAX_STR_LEN); + strncpy(screws->material, material, TREL_MAX_STR_LEN - 1); #endif screws->diameter = diameter; screws->amount = amount; From 8b50db423f5085457e79a46a7d6deb42a8fd9e3b Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 22 Apr 2023 14:43:40 -0600 Subject: [PATCH 25/42] Finally found the comp_tiempo.c bug It's done, ...that part at leas ~lross2k~ --- src/comp_tiempo.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index e1b6d6b..1a55c2d 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -2,6 +2,16 @@ #define POW2(VAR) (VAR * VAR) +double calc_iter_internal_mass(double produced_mass, double internal_mass, double escape_delta_m) +{ + double val = 0.0; + if (produced_mass + internal_mass - escape_delta_m > 0.0) + { + val = produced_mass + internal_mass - escape_delta_m; + } + return(val); +} + /*Comportamiento en el Tiempo!AD3511 */ int trel_run_time_comp_iterations(trel_rocket_t* rocket) { @@ -77,7 +87,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) escape_massic_flux = calc_iter_escape_massic_flux(pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); /*flux_diff = produced_massic_flux - escape_massic_flux; */ escape_delta_m = escape_massic_flux * delta_t; - internal_mass = produced_mass + internal_mass - escape_delta_m > 0.0f ? produced_mass + internal_mass - escape_delta_m : 0.0f; + internal_mass = calc_iter_internal_mass(produced_mass, internal_mass, escape_delta_m); /*areas_ratio = total_burn_area / engine->grains->longitude; */ virtual_escape_pressure = calc_iter_virt_escape_pressure(pressure_abs, engine); /*real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ @@ -86,7 +96,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /*total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2.0f)) * delta_t; */ last_force = force; force = adjusted_cf * pressure_abs * throat_area(engine) > 0 ? adjusted_cf * pressure_abs * throat_area(engine) : 0.0; - total_thrust = (force + last_force) / 2.0 * delta_t; + total_thrust = ((force + last_force) / 2.0) * delta_t; delta_v = total_thrust / (fuel_mass + dead_mass); /* step time value */ @@ -97,10 +107,10 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /* add to delta_v sum and avg thrust */ sum_delta_v += delta_v; - sum_avg_thrust += force; + sum_avg_thrust += total_thrust; - if (force > max_thrust) - max_thrust = force; + if (total_thrust > max_thrust) + max_thrust = total_thrust; if (pressure_abs > max_pressure) max_pressure = pressure_abs; From cce1f622d01debbcc1dc35c6bafd9df8adb06ff0 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 22 Apr 2023 16:18:33 -0600 Subject: [PATCH 26/42] Changed the values displayed in the main.c app ~lross2k~ --- include/TREL.h | 2 +- include/structs.h | 2 +- main.c | 20 +++++++++++++------- src/rocket.c | 1 - 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/include/TREL.h b/include/TREL.h index f49f35d..3b42695 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -81,7 +81,7 @@ typedef struct trel_rocket engine_t* engine; /* ptr to initialized struct */ double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; double avg_thrust, max_thrust, delta_v, max_pressure; /* Comportamiento en el Tiempo!AD3511 */ - double sim_latitude, max_sim_height, body_diameter, drag_coefficient; + double sim_latitude, body_diameter, drag_coefficient; trel_height_sim_t* sim_values; } trel_rocket_t; diff --git a/include/structs.h b/include/structs.h index 45be08b..05087e1 100644 --- a/include/structs.h +++ b/include/structs.h @@ -84,7 +84,7 @@ typedef struct trel_rocket engine_t *engine; /* ptr to initialized struct */ double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; double avg_thrust, max_thrust, delta_v, max_pressure; /* Comportamiento en el Tiempo!AD3511 */ - double sim_latitude, max_sim_height, body_diameter, drag_coefficient; + double sim_latitude, body_diameter, drag_coefficient; trel_height_sim_t *sim_values; } TREL_EXPORT trel_rocket_t; diff --git a/main.c b/main.c index 5a559ab..01efde5 100644 --- a/main.c +++ b/main.c @@ -88,18 +88,17 @@ void print_debug_example() printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); printf("Maxima presion: %lf\n\n", testing_rocket->max_pressure); + /* // Printing rocket values - //printf("\nRocket data\n"); - //printf("telemetry_mass %lf\n", testing_rocket->telemetry_mass); - //printf("parachute_mass %lf\n", testing_rocket->parachute_mass); - //printf("fuselage_mass %lf\n", testing_rocket->fuselage_mass); - //printf("payload_mass %lf\n", testing_rocket->payload_mass); + printf("\nRocket data\n"); + printf("telemetry_mass %lf\n", testing_rocket->telemetry_mass); + printf("parachute_mass %lf\n", testing_rocket->parachute_mass); + printf("fuselage_mass %lf\n", testing_rocket->fuselage_mass); + printf("payload_mass %lf\n", testing_rocket->payload_mass); printf("initial_height %lf\n", testing_rocket->initial_height); printf("sim_latitude %lf\n", testing_rocket->sim_latitude); - printf("max_sim_height %lf\n", testing_rocket->max_sim_height); printf("body_diameter %lf\n", testing_rocket->body_diameter); printf("drag_coefficient %lf\n", testing_rocket->drag_coefficient); - /* printf("rocket_position %lf\n", testing_rocket->rocket_position); printf("rocket_speed %lf\n", testing_rocket->rocket_speed); printf("rocket_acceleration %lf\n", testing_rocket->rocket_acceleration); @@ -111,6 +110,13 @@ void print_debug_example() printf("time %lf\n", testing_rocket->time); */ + printf("max_rocket_height %lf\n", testing_rocket->sim_values->max_rocket_height); + printf("max_rocket_position %lf\n", testing_rocket->sim_values->max_rocket_position); + printf("max_rocket_speed %lf\n", testing_rocket->sim_values->max_rocket_speed); /* m/s */ + printf("max_rocket_acceleration %lf\n", testing_rocket->sim_values->max_rocket_acceleration); + printf("max_rocket_force_balance %lf\n", testing_rocket->sim_values->max_rocket_force_balance); + printf("max_rocket_drag %lf\n", testing_rocket->sim_values->max_rocket_drag); + //printf("Tubing's material name: %s\n", created_tube->material); //printf("Screws' material name: %s\n", created_screw->material); } diff --git a/src/rocket.c b/src/rocket.c index 20f32bd..7b602ac 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -51,7 +51,6 @@ trel_rocket_t *trel_rocket_init( rocket->engine = engine; rocket->initial_height = initial_height; rocket->sim_latitude = sim_latitude; - rocket->max_sim_height = 0.0; rocket->body_diameter = body_diameter; rocket->drag_coefficient = drag_coefficient; rocket->sim_values = sim_values; From eb7fa117a9489e90b866ed66d8ffea06c61cec71 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sat, 22 Apr 2023 23:18:29 -0600 Subject: [PATCH 27/42] Changed pointer managment for the TREL structs Before this, memory free functions where a problem because deleting a pointer could end up in a dangling pointer, as the intent of TREL is to be as family friendly as possible, this has to change ~lross2k~ --- include/TREL.h | 8 ++++++- include/engine.h | 11 +++++----- include/structs.h | 2 +- main.c | 8 +++++-- src/comp_area.c | 6 +++--- src/comp_tiempo.c | 28 ++++++++++++------------ src/design_func.c | 11 ++++++---- src/engine.c | 53 ++++++++++++++++++++++++++++++++++++++++++++-- src/height_sim.c | 12 +++++------ src/rendimientos.c | 8 +++---- 10 files changed, 105 insertions(+), 42 deletions(-) diff --git a/include/TREL.h b/include/TREL.h index 3b42695..6890f69 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -119,7 +119,7 @@ engine_t *trel_engine_init( double pressure, /* E24 */ double temperature, /* E26 */ double engine_mass, - grains_t* grains, + grains_t **grains, fuel_t* fuel, tubing_t* tube, screws_t* screws @@ -146,4 +146,10 @@ int trel_run_area_comp_iterations(engine_t **engine); int trel_run_time_comp_iterations(trel_rocket_t* rocket); int trel_run_height_sim_iterations(trel_rocket_t** rocket); +void trel_grains_free(grains_t** grains); +void trel_fuel_free(fuel_t** fuel); +void trel_tubing_free(tubing_t** tubing); +void trel_screws_free(screws_t** screws); +void trel_engine_free(engine_t** engine); + #endif /*TREL_H */ diff --git a/include/engine.h b/include/engine.h index 11153d0..288db09 100644 --- a/include/engine.h +++ b/include/engine.h @@ -9,10 +9,6 @@ #include "design_resist.h" #include "structs.h" -/* TODO: free library structs function */ - - - /* Function prototypes */ grains_t TREL_EXPORT *trel_grains_init( unsigned int amount, /* E29 */ @@ -47,7 +43,7 @@ engine_t TREL_EXPORT* trel_engine_init( double pressure, /* E24 */ double temperature, /* E26 */ double engine_mass, - grains_t* grains, + grains_t **grains, fuel_t* fuel, tubing_t* tube, screws_t* screws @@ -57,5 +53,10 @@ void TREL_EXPORT trel_set_pressure(engine_t *engine, double pressure); void TREL_EXPORT trel_set_escape_vel(engine_t *engine, double vel); double TREL_EXPORT trel_get_escape_vel(engine_t *engine); double TREL_EXPORT trel_get_pressure(engine_t *engine); +void trel_grains_free(grains_t** grains); +void trel_fuel_free(fuel_t** fuel); +void trel_tubing_free(tubing_t** tubing); +void trel_screws_free(screws_t** screws); +void trel_engine_free(engine_t** engine); #endif /* ENGINE_H */ diff --git a/include/structs.h b/include/structs.h index 05087e1..f0e9839 100644 --- a/include/structs.h +++ b/include/structs.h @@ -51,7 +51,7 @@ typedef struct engine double width_condition, margin_of_safety, max_stress, radial_stress; double tangencial_stress, longitudinal_stress, max_pressure; double nozzle_efficiency; - grains_t *grains; /* ptr to intialized struct */ + grains_t **grains; /* ptr to intialized struct */ fuel_t *fuel; tubing_t *tube; /* ptr to intialized struct */ screws_t *screws; /* ptr to initialized struct */ diff --git a/main.c b/main.c index 01efde5..fabc4eb 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,7 @@ void print_debug_example() { // Se inicializan los tornillos - grains_t* created_grains = trel_grains_init( + grains_t *created_grains = trel_grains_init( 4, 0.01, 0.0304, @@ -43,7 +43,7 @@ void print_debug_example() 800, 1710, 1.5, - created_grains, + &created_grains, created_fuel, created_tube, created_screw @@ -119,6 +119,10 @@ void print_debug_example() //printf("Tubing's material name: %s\n", created_tube->material); //printf("Screws' material name: %s\n", created_screw->material); + //trel_grains_free(&created_grains); + //printf("%p\n", &testing_engine->grains); + //printf("%p\n", created_grains); + //trel_engine_free(&testing_engine); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica diff --git a/src/comp_area.c b/src/comp_area.c index d2184f4..b2aec6b 100644 --- a/src/comp_area.c +++ b/src/comp_area.c @@ -44,13 +44,13 @@ int trel_run_area_comp_iterations(engine_t **engine) for (it = 0; it < max_iterations; it++) { /* instantaneous grain radius step */ - inst_radius = (*engine)->grains->init_inter_radius + burn_rate * time; + inst_radius = (*(*engine)->grains)->init_inter_radius + burn_rate * time; /* instantaneous grain longitude step */ - inst_long = (*engine)->grains->longitude - 2.0f * burn_rate * time; + inst_long = (*(*engine)->grains)->longitude - 2.0f * burn_rate * time; /* add to transversal area sum */ - transversal_area = TREL_PI * ((*engine)->grains->extern_radius*(*engine)->grains->extern_radius - + transversal_area = TREL_PI * ((*(*engine)->grains)->extern_radius*(*(*engine)->grains)->extern_radius - inst_radius * inst_radius); sum_transversal_area += transversal_area; diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index 1a55c2d..44daaaf 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -24,7 +24,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) double time = 0.0; /* time in seconds */ double last_force = 0.0; const double total_volume = TREL_PI * POW2(engine->tube->internal_radius) * - (engine->grains->grain_separation * (engine->grains->amount - 1) + engine->grains->longitude * engine->grains->amount); + ((*engine->grains)->grain_separation * ((*engine->grains)->amount - 1) + (*engine->grains)->longitude * (*engine->grains)->amount); /* variables for iteration */ double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemetry_mass + engine->engine_mass + rocket->payload_mass; @@ -33,13 +33,13 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /*double pressure = pressure_abs / 6894.757f; */ /*double pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; */ double burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; - double inst_radius = engine->grains->init_inter_radius; - double inst_long = engine->grains->longitude; - /*double trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; */ + double inst_radius = (*engine->grains)->init_inter_radius; + double inst_long = (*engine->grains)->longitude; + /*double trans_area = inst_radius > 0 ? TREL_PI * (*engine->grains)->extern_radius * (*engine->grains)->extern_radius - inst_radius * inst_radius : 0.0f; */ /*double long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; */ /*double burn_area = trans_area * 2 + long_area; */ - /*double total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; */ - double fuel_vol = engine->grains->amount * TREL_PI * (POW2(engine->grains->extern_radius) - POW2(inst_radius)) * inst_long; + /*double total_burn_area = burn_area > 0 ? burn_area * (*engine->grains)->extern_radius : 0.0f; */ + double fuel_vol = (*engine->grains)->amount * TREL_PI * (POW2((*engine->grains)->extern_radius) - POW2(inst_radius)) * inst_long; double fuel_mass = engine->fuel->density * fuel_vol; double produced_mass = 0.0; /*double produced_massic_flux = 0.0f; */ @@ -48,12 +48,12 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) double escape_delta_m = 0.0f; double free_vol = total_volume - fuel_vol; double internal_mass = (free_vol * pressure_abs) / (engine->temperature * TREL_GAS_CONST); - /*double areas_ratio = total_burn_area / engine->grains->longitude; */ + /*double areas_ratio = total_burn_area / (*engine->grains)->longitude; */ double virtual_escape_pressure = calc_iter_virt_escape_pressure(pressure_abs, engine); /*double real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ double force_coeff = calc_iter_force_coeff(virtual_escape_pressure, pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); double adjusted_cf = force_coeff * engine->tube->nozzle_efficiency; - double force = adjusted_cf * pressure_abs * engine->grains->longitude > 0 ? adjusted_cf * pressure_abs * engine->grains->longitude : 0.0; + double force = adjusted_cf * pressure_abs * (*engine->grains)->longitude > 0 ? adjusted_cf * pressure_abs * (*engine->grains)->longitude : 0.0; double total_thrust = 0.0; double delta_v = total_thrust / (fuel_mass + (double)dead_mass); @@ -73,13 +73,13 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /*pressure = pressure_abs / 6894.757f; */ /*pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; */ burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; - inst_radius = inst_radius > 0 && (inst_radius + burn_rate * delta_t) < engine->grains->extern_radius ? inst_radius + burn_rate * delta_t : 0; + inst_radius = inst_radius > 0 && (inst_radius + burn_rate * delta_t) < (*engine->grains)->extern_radius ? inst_radius + burn_rate * delta_t : 0; inst_long = inst_radius > 0 ? inst_long - burn_rate * delta_t * 2 : 0; - /*trans_area = inst_radius > 0 ? TREL_PI * engine->grains->extern_radius * engine->grains->extern_radius - inst_radius * inst_radius : 0.0f; */ + /*trans_area = inst_radius > 0 ? TREL_PI * (*engine->grains)->extern_radius * (*engine->grains)->extern_radius - inst_radius * inst_radius : 0.0f; */ /*long_area = trans_area == 0 ? 0.0f : 2 * TREL_PI * inst_radius * inst_long; */ /*burn_area = trans_area * 2 + long_area; */ - /*total_burn_area = burn_area > 0 ? burn_area * engine->grains->extern_radius : 0.0f; */ - fuel_vol = engine->grains->amount * TREL_PI * (POW2(engine->grains->extern_radius) - POW2(inst_radius)) * inst_long; + /*total_burn_area = burn_area > 0 ? burn_area * (*engine->grains)->extern_radius : 0.0f; */ + fuel_vol = (*engine->grains)->amount * TREL_PI * (POW2((*engine->grains)->extern_radius) - POW2(inst_radius)) * inst_long; free_vol = total_volume - fuel_vol; produced_mass = fuel_mass - engine->fuel->density * fuel_vol; fuel_mass = engine->fuel->density * fuel_vol; @@ -88,12 +88,12 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /*flux_diff = produced_massic_flux - escape_massic_flux; */ escape_delta_m = escape_massic_flux * delta_t; internal_mass = calc_iter_internal_mass(produced_mass, internal_mass, escape_delta_m); - /*areas_ratio = total_burn_area / engine->grains->longitude; */ + /*areas_ratio = total_burn_area / (*engine->grains)->longitude; */ virtual_escape_pressure = calc_iter_virt_escape_pressure(pressure_abs, engine); /*real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ force_coeff = calc_iter_force_coeff(virtual_escape_pressure, pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); adjusted_cf = force_coeff * engine->tube->nozzle_efficiency; - /*total_thrust = ((force + adjusted_cr * pressure_abs * engine->grains->longitude > 0 ? adjusted_cr * pressure_abs * engine->grains->longitude : 0.0f) / (2.0f)) * delta_t; */ + /*total_thrust = ((force + adjusted_cr * pressure_abs * (*engine->grains)->longitude > 0 ? adjusted_cr * pressure_abs * (*engine->grains)->longitude : 0.0f) / (2.0f)) * delta_t; */ last_force = force; force = adjusted_cf * pressure_abs * throat_area(engine) > 0 ? adjusted_cf * pressure_abs * throat_area(engine) : 0.0; total_thrust = ((force + last_force) / 2.0) * delta_t; diff --git a/src/design_func.c b/src/design_func.c index 8500d3e..122d980 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -1,6 +1,8 @@ #include "design_func.h" /*Hoja de Excel: Diseño */ +#define TREL_GRAINS engine->(*grains) + /*Functions*/ /* E25 returns chamber pressure in pascals */ double trel_get_pressure_pa(engine_t* engine) @@ -17,7 +19,7 @@ double desv_est_grains(engine_t *engine) /*E35 Volumen unitario granos */ double volumen_unitario(engine_t *engine) { - return(TREL_PI*(0.1)*((engine->grains->extern_radius)*(engine->grains->extern_radius)-(0.01)*(0.01))); + return(TREL_PI*(0.1)*(((*engine->grains)->extern_radius)*((*engine->grains)->extern_radius)-(0.01)*(0.01))); } /* E36 Masa unitaria grano */ @@ -29,19 +31,20 @@ double masa_unitaria(engine_t* engine) /* E37 Longitud total con combustible */ double longitudt_ccombustible(engine_t *engine) { - return((engine->grains->amount)*(engine->grains->longitude)); + return(((*engine->grains)->amount)*((*engine->grains)->longitude)); } /* E38 Port Area */ double port_area(engine_t* engine) { - return(TREL_PI*(engine->grains->init_inter_radius)*(engine->grains->init_inter_radius)); + return(TREL_PI*((*engine->grains)->init_inter_radius)*((*engine->grains)->init_inter_radius)); } /* E39 */ double long_secc_combus(engine_t* engine) { - return((engine->grains->grain_separation)*((engine->grains->amount)-1.0)+((engine->grains->longitude)*(engine->grains->amount))); + return(((*engine->grains)->grain_separation)*(((*engine->grains)->amount)-1.0)+ + (((*engine->grains)->longitude)*((*engine->grains)->amount))); } /* E49 Volumen especifico camara */ diff --git a/src/engine.c b/src/engine.c index 7109aba..1c2727f 100644 --- a/src/engine.c +++ b/src/engine.c @@ -2,7 +2,7 @@ #include "engine.h" /* Initializes all variables in the grains struct */ -grains_t* trel_grains_init( +grains_t *trel_grains_init( unsigned int amount, double internal_radius, double external_radius, @@ -109,7 +109,7 @@ engine_t* trel_engine_init( double pressure, double temperature, double engine_mass, - grains_t *grains, + grains_t **grains, fuel_t *fuel, tubing_t *tube, screws_t *screws) @@ -195,3 +195,52 @@ int trel_set_engine_mass(engine_t* engine, double mass) engine->engine_mass = mass; return(0); } + +void trel_grains_free(grains_t** grains) +{ + free(*grains); + *grains = NULL; +} + +void trel_fuel_free(fuel_t** fuel) +{ + free(*fuel); + *fuel = NULL; +} + +void trel_tubing_free(tubing_t** tubing) +{ + free(*tubing); + *tubing = NULL; +} + +void trel_screws_free(screws_t** screws) +{ + free(*screws); + *screws = NULL; +} + +void trel_comp_area_free(comp_area_t** values) +{ + free(*values); + *values = NULL; +} + +void trel_engine_free(engine_t** engine) +{ + if ((*engine)->comp_area_values != NULL) + trel_comp_area_free(&(*engine)->comp_area_values); + if ((*engine)->screws != NULL) + trel_screws_free(&(*engine)->screws); + if ((*engine)->tube != NULL) + trel_tubing_free(&(*engine)->tube); + if ((*engine)->fuel != NULL) + trel_fuel_free(&(*engine)->fuel); + if ((*engine)->grains != NULL) + trel_grains_free((*engine)->grains); + if (*engine != NULL) + { + free(*engine); + *engine = NULL; + } +} diff --git a/src/height_sim.c b/src/height_sim.c index 4b2c3e4..9c757d5 100644 --- a/src/height_sim.c +++ b/src/height_sim.c @@ -148,11 +148,11 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) double virt_escape_pressure = 0.0; double delta_m = 0.0; const double total_volume = TREL_PI * POW2(engine->tube->internal_radius) * - (engine->grains->grain_separation * (engine->grains->amount - 1) + engine->grains->longitude * engine->grains->amount); + ((*engine->grains)->grain_separation * ((*engine->grains)->amount - 1) + (*engine->grains)->longitude * (*engine->grains)->amount); - inst_long = engine->grains->longitude; - inst_radius = engine->grains->init_inter_radius; - fuel_volume = engine->grains->amount * TREL_PI * (POW2(engine->grains->extern_radius) - POW2(inst_radius)) * inst_long; + inst_long = (*engine->grains)->longitude; + inst_radius = (*engine->grains)->init_inter_radius; + fuel_volume = (*engine->grains)->amount * TREL_PI * (POW2((*engine->grains)->extern_radius) - POW2(inst_radius)) * inst_long; fuel_mass = fuel_volume * engine->fuel->density; prev_fuel_mass = fuel_mass; free_volume = total_volume - fuel_volume; @@ -180,12 +180,12 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) gravitational_accel = calc_iter_grav_accel(rocket, rocket_height); atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); /* ACA SE COLAPSA EL TITANIC */ pressure = calc_iter_pressure(engine, intern_gas_mass, free_volume, atmospheric_pressure); - inst_radius = calc_inst_radius(inst_radius, burn_rate, step, (*rocket)->engine->grains->extern_radius); + inst_radius = calc_inst_radius(inst_radius, burn_rate, step, (*(*rocket)->engine->grains)->extern_radius); inst_long = calc_inst_long(inst_radius, inst_long, burn_rate, step); burn_rate = calc_burn_rate(engine, pressure); atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); - fuel_volume = (*rocket)->engine->grains->amount * TREL_PI * (((*rocket)->engine->grains->extern_radius * (*rocket)->engine->grains->extern_radius) - inst_radius * inst_radius) * inst_long; + fuel_volume = (*(*rocket)->engine->grains)->amount * TREL_PI * (((*(*rocket)->engine->grains)->extern_radius * (*(*rocket)->engine->grains)->extern_radius) - inst_radius * inst_radius) * inst_long; escape_massic_flux = calc_iter_escape_massic_flux(pressure, atmospheric_pressure, engine); fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; produced_mass = prev_fuel_mass - fuel_mass; diff --git a/src/rendimientos.c b/src/rendimientos.c index 48d558e..c2c817b 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -13,8 +13,8 @@ double br_combustion(engine_t *engine) /*E67 Volumen_Combustible */ double volumen_combustible(engine_t *engine) { - return(double)(TREL_PI*engine->grains->longitude*engine->grains->amount*(pow( - engine->grains->extern_radius,2.0f)-pow(engine->grains->init_inter_radius,2.0f))); + return(double)(TREL_PI*(*engine->grains)->longitude*(*engine->grains)->amount*(pow( + (*engine->grains)->extern_radius,2.0f)-pow((*engine->grains)->init_inter_radius,2.0f))); } /*E68 Masa_Combustible */ @@ -28,13 +28,13 @@ double masa_combustible(engine_t *engine) /*E70 Tiempo de quemado total_Combustible*/ double Tiempo_quemado_combustion(engine_t *engine) { - return (engine->grains->extern_radius-engine->grains->init_inter_radius)/br_combustion(engine); + return ((*engine->grains)->extern_radius-(*engine->grains)->init_inter_radius)/br_combustion(engine); } /*E71 Área de quemado */ double Ab_combustion(engine_t *engine) { - return (engine->comp_area_values->avg_burn_area*engine->grains->amount); + return (engine->comp_area_values->avg_burn_area*(*engine->grains)->amount); } /*E72 Flujo másico*/ From bc671ab2794e90dbeddda31cf69ca17898d59aa8 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sun, 23 Apr 2023 08:09:19 -0600 Subject: [PATCH 28/42] Found the solution to freeing pointers from engine **p ~lross2k~ --- include/TREL.h | 14 +++++++------- include/structs.h | 6 +++--- main.c | 10 +++++----- src/engine.c | 16 ++++++---------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/include/TREL.h b/include/TREL.h index 6890f69..c00ffa4 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -48,10 +48,10 @@ typedef struct engine double width_condition, margin_of_safety, max_stress, radial_stress; double tangencial_stress, longitudinal_stress, max_pressure; double nozzle_efficiency; - grains_t *grains; /* ptr to intialized struct */ - fuel_t *fuel; - tubing_t *tube; /* ptr to intialized struct */ - screws_t *screws; /* ptr to initialized struct */ + grains_t **grains; /* ptr to intialized struct */ + fuel_t **fuel; + tubing_t **tube; /* ptr to intialized struct */ + screws_t **screws; /* ptr to initialized struct */ comp_area_t *comp_area_values; /* ptr to initialized struct */ } engine_t; @@ -120,9 +120,9 @@ engine_t *trel_engine_init( double temperature, /* E26 */ double engine_mass, grains_t **grains, - fuel_t* fuel, - tubing_t* tube, - screws_t* screws + fuel_t **fuel, + tubing_t **tube, + screws_t **screws ); trel_rocket_t *trel_rocket_init( engine_t* engine, diff --git a/include/structs.h b/include/structs.h index f0e9839..6e940ff 100644 --- a/include/structs.h +++ b/include/structs.h @@ -52,9 +52,9 @@ typedef struct engine double tangencial_stress, longitudinal_stress, max_pressure; double nozzle_efficiency; grains_t **grains; /* ptr to intialized struct */ - fuel_t *fuel; - tubing_t *tube; /* ptr to intialized struct */ - screws_t *screws; /* ptr to initialized struct */ + fuel_t **fuel; + tubing_t **tube; /* ptr to intialized struct */ + screws_t **screws; /* ptr to initialized struct */ comp_area_t *comp_area_values; /* ptr to initialized struct */ } TREL_EXPORT engine_t; diff --git a/main.c b/main.c index fabc4eb..40b9624 100644 --- a/main.c +++ b/main.c @@ -38,12 +38,13 @@ void print_debug_example() 205000000, 0.85 ); + grains_t **p = &created_grains; // Se inicializa el motor usando los tornillos y tuberia creados engine_t* testing_engine = trel_engine_init( 800, 1710, 1.5, - &created_grains, + p, created_fuel, created_tube, created_screw @@ -119,10 +120,9 @@ void print_debug_example() //printf("Tubing's material name: %s\n", created_tube->material); //printf("Screws' material name: %s\n", created_screw->material); - //trel_grains_free(&created_grains); - //printf("%p\n", &testing_engine->grains); - //printf("%p\n", created_grains); - //trel_engine_free(&testing_engine); + + trel_grains_free(&created_grains); + trel_engine_free(&testing_engine); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica diff --git a/src/engine.c b/src/engine.c index 1c2727f..b038b8d 100644 --- a/src/engine.c +++ b/src/engine.c @@ -228,16 +228,12 @@ void trel_comp_area_free(comp_area_t** values) void trel_engine_free(engine_t** engine) { - if ((*engine)->comp_area_values != NULL) - trel_comp_area_free(&(*engine)->comp_area_values); - if ((*engine)->screws != NULL) - trel_screws_free(&(*engine)->screws); - if ((*engine)->tube != NULL) - trel_tubing_free(&(*engine)->tube); - if ((*engine)->fuel != NULL) - trel_fuel_free(&(*engine)->fuel); - if ((*engine)->grains != NULL) - trel_grains_free((*engine)->grains); + if ((*(*engine)->grains) != NULL) + { + free((*(*engine)->grains)); + (*(*engine)->grains) = NULL; + } + (*engine)->grains = NULL; if (*engine != NULL) { free(*engine); From cad725d5d5a21e6cec73f0f9cf9a3e44ce49fa9f Mon Sep 17 00:00:00 2001 From: lross2k Date: Sun, 23 Apr 2023 08:33:10 -0600 Subject: [PATCH 29/42] Implemented memory deallocation functions Now I'm only missing the free for trel_rocket_t ~lross2k~ --- include/engine.h | 6 +++--- main.c | 12 +++++++----- src/comp_tiempo.c | 16 ++++++++-------- src/design_func.c | 30 +++++++++++++++--------------- src/design_resist.c | 32 ++++++++++++++++---------------- src/engine.c | 35 +++++++++++++++++++++++++++-------- src/height_sim.c | 14 +++++++------- src/rendimientos.c | 10 +++++----- 8 files changed, 88 insertions(+), 67 deletions(-) diff --git a/include/engine.h b/include/engine.h index 288db09..512c671 100644 --- a/include/engine.h +++ b/include/engine.h @@ -44,9 +44,9 @@ engine_t TREL_EXPORT* trel_engine_init( double temperature, /* E26 */ double engine_mass, grains_t **grains, - fuel_t* fuel, - tubing_t* tube, - screws_t* screws + fuel_t **fuel, + tubing_t **tube, + screws_t **screws ); double TREL_EXPORT trel_psi_to_pa(double psi); void TREL_EXPORT trel_set_pressure(engine_t *engine, double pressure); diff --git a/main.c b/main.c index 40b9624..023dfc3 100644 --- a/main.c +++ b/main.c @@ -38,16 +38,15 @@ void print_debug_example() 205000000, 0.85 ); - grains_t **p = &created_grains; // Se inicializa el motor usando los tornillos y tuberia creados engine_t* testing_engine = trel_engine_init( 800, 1710, 1.5, - p, - created_fuel, - created_tube, - created_screw + &created_grains, + &created_fuel, + &created_tube, + &created_screw ); // Se inicializa el cohete trel_rocket_t* testing_rocket = trel_rocket_init( @@ -122,6 +121,9 @@ void print_debug_example() //printf("Screws' material name: %s\n", created_screw->material); trel_grains_free(&created_grains); + trel_fuel_free(&created_fuel); + trel_tubing_free(&created_tube); + trel_screws_free(&created_screw); trel_engine_free(&testing_engine); } diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index 44daaaf..c839de4 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -23,7 +23,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) const double step = burn_time / 3000.0; double time = 0.0; /* time in seconds */ double last_force = 0.0; - const double total_volume = TREL_PI * POW2(engine->tube->internal_radius) * + const double total_volume = TREL_PI * POW2((*engine->tube)->internal_radius) * ((*engine->grains)->grain_separation * ((*engine->grains)->amount - 1) + (*engine->grains)->longitude * (*engine->grains)->amount); /* variables for iteration */ @@ -32,7 +32,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) double pressure_abs = TREL_ATMOSPHERIC_PRESSURE; /*double pressure = pressure_abs / 6894.757f; */ /*double pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; */ - double burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; + double burn_rate = (*engine->fuel)->const_burn_rate * pow(pressure_abs * 0.000145038f, (*engine->fuel)->pressure_exponent) * 0.0254f; double inst_radius = (*engine->grains)->init_inter_radius; double inst_long = (*engine->grains)->longitude; /*double trans_area = inst_radius > 0 ? TREL_PI * (*engine->grains)->extern_radius * (*engine->grains)->extern_radius - inst_radius * inst_radius : 0.0f; */ @@ -40,7 +40,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /*double burn_area = trans_area * 2 + long_area; */ /*double total_burn_area = burn_area > 0 ? burn_area * (*engine->grains)->extern_radius : 0.0f; */ double fuel_vol = (*engine->grains)->amount * TREL_PI * (POW2((*engine->grains)->extern_radius) - POW2(inst_radius)) * inst_long; - double fuel_mass = engine->fuel->density * fuel_vol; + double fuel_mass = (*engine->fuel)->density * fuel_vol; double produced_mass = 0.0; /*double produced_massic_flux = 0.0f; */ double escape_massic_flux = 0.0; @@ -52,7 +52,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) double virtual_escape_pressure = calc_iter_virt_escape_pressure(pressure_abs, engine); /*double real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ double force_coeff = calc_iter_force_coeff(virtual_escape_pressure, pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); - double adjusted_cf = force_coeff * engine->tube->nozzle_efficiency; + double adjusted_cf = force_coeff * (*engine->tube)->nozzle_efficiency; double force = adjusted_cf * pressure_abs * (*engine->grains)->longitude > 0 ? adjusted_cf * pressure_abs * (*engine->grains)->longitude : 0.0; double total_thrust = 0.0; double delta_v = total_thrust / (fuel_mass + (double)dead_mass); @@ -72,7 +72,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) pressure_abs = TREL_GAS_CONST * engine->temperature * internal_mass / free_vol; /*pressure = pressure_abs / 6894.757f; */ /*pressure_man = pressure_abs - TREL_ATMOSPHERIC_PRESSURE; */ - burn_rate = engine->fuel->const_burn_rate * pow(pressure_abs * 0.000145038f, engine->fuel->pressure_exponent) * 0.0254f; + burn_rate = (*engine->fuel)->const_burn_rate * pow(pressure_abs * 0.000145038f, (*engine->fuel)->pressure_exponent) * 0.0254f; inst_radius = inst_radius > 0 && (inst_radius + burn_rate * delta_t) < (*engine->grains)->extern_radius ? inst_radius + burn_rate * delta_t : 0; inst_long = inst_radius > 0 ? inst_long - burn_rate * delta_t * 2 : 0; /*trans_area = inst_radius > 0 ? TREL_PI * (*engine->grains)->extern_radius * (*engine->grains)->extern_radius - inst_radius * inst_radius : 0.0f; */ @@ -81,8 +81,8 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) /*total_burn_area = burn_area > 0 ? burn_area * (*engine->grains)->extern_radius : 0.0f; */ fuel_vol = (*engine->grains)->amount * TREL_PI * (POW2((*engine->grains)->extern_radius) - POW2(inst_radius)) * inst_long; free_vol = total_volume - fuel_vol; - produced_mass = fuel_mass - engine->fuel->density * fuel_vol; - fuel_mass = engine->fuel->density * fuel_vol; + produced_mass = fuel_mass - (*engine->fuel)->density * fuel_vol; + fuel_mass = (*engine->fuel)->density * fuel_vol; /*produced_massic_flux = produced_mass / delta_t; */ escape_massic_flux = calc_iter_escape_massic_flux(pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); /*flux_diff = produced_massic_flux - escape_massic_flux; */ @@ -92,7 +92,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) virtual_escape_pressure = calc_iter_virt_escape_pressure(pressure_abs, engine); /*real_escape_pressure = pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))) < TREL_ATMOSPHERIC_PRESSURE ? TREL_ATMOSPHERIC_PRESSURE : pressure_abs / pow((1 + ((TREL_HEAT_CAP_RATIO - 1) / 2) * volumen_unitario(engine) * volumen_unitario(engine)), (TREL_HEAT_CAP_RATIO / (TREL_HEAT_CAP_RATIO - 1))); */ force_coeff = calc_iter_force_coeff(virtual_escape_pressure, pressure_abs, TREL_ATMOSPHERIC_PRESSURE, engine); - adjusted_cf = force_coeff * engine->tube->nozzle_efficiency; + adjusted_cf = force_coeff * (*engine->tube)->nozzle_efficiency; /*total_thrust = ((force + adjusted_cr * pressure_abs * (*engine->grains)->longitude > 0 ? adjusted_cr * pressure_abs * (*engine->grains)->longitude : 0.0f) / (2.0f)) * delta_t; */ last_force = force; force = adjusted_cf * pressure_abs * throat_area(engine) > 0 ? adjusted_cf * pressure_abs * throat_area(engine) : 0.0; diff --git a/src/design_func.c b/src/design_func.c index 122d980..60f857a 100644 --- a/src/design_func.c +++ b/src/design_func.c @@ -25,7 +25,7 @@ double volumen_unitario(engine_t *engine) /* E36 Masa unitaria grano */ double masa_unitaria(engine_t* engine) { - return(volumen_unitario(engine)*1000.0f*(engine->fuel->density)); + return(volumen_unitario(engine)*1000.0f*((*engine->fuel)->density)); } /* E37 Longitud total con combustible */ @@ -64,7 +64,7 @@ double calc_escape_vel(engine_t* engine) /* E140 Area interna */ double area_interna(engine_t *engine) { - return(TREL_PI*pow(engine->tube->internal_radius,2)); + return(TREL_PI*pow((*engine->tube)->internal_radius,2)); } /* E141 Fuerza maxima */ double fuerza_maxima(engine_t *engine) @@ -74,24 +74,24 @@ double fuerza_maxima(engine_t *engine) /* E142 Fuerza por tornillo */ double fuerza_tornillo(engine_t *engine) { - return(fuerza_maxima(engine)/engine->screws->amount); + return(fuerza_maxima(engine)/(*engine->screws)->amount); } /*Esfuerzo circunferencial*/ /* E144 Tension */ double tension(engine_t *engine) { - return(fuerza_maxima(engine)/engine->tube->material_area); + return(fuerza_maxima(engine)/(*engine->tube)->material_area); } /* E145 Margen de seguridad */ double margen_de_seguridad(engine_t *engine) { - return(engine->tube->shear_stress_tension/tension(engine)); + return((*engine->tube)->shear_stress_tension/tension(engine)); } /*Esfuerzo longitudinal*/ /* E146 Area de cortante */ double area_de_cortante(engine_t *engine) { - return(engine->screws->width_cutting_segment*engine->tube->wall_thickness); + return((*engine->screws)->width_cutting_segment*(*engine->tube)->wall_thickness); } /* E147 Cortante promedio */ double cortante_promedio(engine_t *engine) @@ -101,29 +101,29 @@ double cortante_promedio(engine_t *engine) /* E148 Margen de seguridad cortante */ double margen_de_seguridad_cortante(engine_t *engine) { - return(engine->tube->ult_stress_pressure/cortante_promedio(engine)); + return((*engine->tube)->ult_stress_pressure/cortante_promedio(engine)); } /*Esfuerzo de aplastamiento*/ /* E149 Aplastamiento */ double aplastamiento(engine_t *engine) { - return(fuerza_tornillo(engine)/engine->screws->area_per_screw); + return(fuerza_tornillo(engine)/(*engine->screws)->area_per_screw); } /* E150 Margen de seguridad aplaztamiento */ double margen_de_seguridad_aplaztamiento(engine_t *engine) { - return(engine->tube->shear_stress_tension/aplastamiento(engine)); + return((*engine->tube)->shear_stress_tension/aplastamiento(engine)); } /*Deformacion circunferencial*/ /* E152 Deformacion unitaria */ double deformacion_unitaria(engine_t *engine) { - return(engine->tangencial_stress/engine->tube->young_module); + return(engine->tangencial_stress/(*engine->tube)->young_module); } /* E153 Circunferencia interna total */ double circunferencia_interna_total(engine_t *engine) { - return(2*TREL_PI*engine->tube->internal_radius); + return(2*TREL_PI*(*engine->tube)->internal_radius); } /* E154 Deformacion de la circunferencia interna */ double deformacion_circunferencia_interna(engine_t *engine) @@ -133,7 +133,7 @@ double deformacion_circunferencia_interna(engine_t *engine) /* E155 Circunferencia externa inicial */ double circunferencia_externa_inicial(engine_t *engine) { - return(TREL_PI*engine->tube->diameter_ext); + return(TREL_PI*(*engine->tube)->diameter_ext); } /* E156 Deformacion de circunferencia externa */ double deformacion_circunferencia_externa(engine_t *engine) @@ -147,17 +147,17 @@ double radio_final(double circ_total,double deform_circ) return(circ_total*(deform_circ/1000.0f)/(2.0f*TREL_PI)); } /* E158 Diferencia de radio interno */ -/*double engine->tube->internal_radius = 1; Dependencia E15 */ +/*double (*engine->tube)->internal_radius = 1; Dependencia E15 */ double diferencia_radio_interno(engine_t *engine) { - return(radio_final(circunferencia_interna_total(engine),deformacion_circunferencia_interna(engine))-engine->tube->internal_radius*1000.0f); + return(radio_final(circunferencia_interna_total(engine),deformacion_circunferencia_interna(engine))-(*engine->tube)->internal_radius*1000.0f); } /* E159 es la misma ecuacion de E157 */ /* radio_final(circunferencia_interna_inicial(),deformacion_circunferencia_externa()); */ /* E160 Diferencia radio externo */ double diferencia_radio_externo(engine_t *engine) { - return((radio_final(circunferencia_externa_inicial(engine),deformacion_circunferencia_externa(engine))-(engine->tube->diameter_ext/2.0f))*1000.0f); + return((radio_final(circunferencia_externa_inicial(engine),deformacion_circunferencia_externa(engine))-((*engine->tube)->diameter_ext/2.0f))*1000.0f); } /*Escape*/ /* E88 */ diff --git a/src/design_resist.c b/src/design_resist.c index aa58d8a..b71aee2 100644 --- a/src/design_resist.c +++ b/src/design_resist.c @@ -13,12 +13,12 @@ Inicio de traduccion a C por lross2k /* Resistencia de materiales E115 */ void trel_mean_tubing_diameter(engine_t *engine) { - engine->tube->mean_tubing_diameter = engine->tube->diameter_ext-engine->tube->wall_thickness; + (*engine->tube)->mean_tubing_diameter = (*engine->tube)->diameter_ext-(*engine->tube)->wall_thickness; } /* condición de espesor del tubo E116 */ void trel_width_condition(engine_t *engine) { - engine->width_condition = engine->tube->mean_tubing_diameter/engine->tube->wall_thickness; + engine->width_condition = (*engine->tube)->mean_tubing_diameter/(*engine->tube)->wall_thickness; } /* Presión maxima E118 */ void trel_engine_max_pressure(engine_t *engine) @@ -37,11 +37,11 @@ void trel_tangencial_stress(engine_t *engine) /* could be done branchless */ if (engine->width_condition> 20.0f) { - engine->tangencial_stress = engine->max_pressure*engine->tube->internal_radius/engine->tube->wall_thickness; + engine->tangencial_stress = engine->max_pressure*(*engine->tube)->internal_radius/(*engine->tube)->wall_thickness; } else { - engine->tangencial_stress = engine->max_pressure*(pow((engine->tube->diameter_ext/2.0f),2.0f)+pow(engine->tube->internal_radius,2.0f))/(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)); + engine->tangencial_stress = engine->max_pressure*(pow(((*engine->tube)->diameter_ext/2.0f),2.0f)+pow((*engine->tube)->internal_radius,2.0f))/(pow((*engine->tube)->diameter_ext/2.0f,2.0f)-pow((*engine->tube)->internal_radius,2.0f)); } } /* Esfuerzo Longitudinal E122 */ @@ -49,11 +49,11 @@ void trel_logitudinal_stress(engine_t *engine) { if (engine->width_condition > 20.0f) { - engine->longitudinal_stress = engine->max_pressure*engine->tube->internal_radius/2.0f*engine->tube->wall_thickness; + engine->longitudinal_stress = engine->max_pressure*(*engine->tube)->internal_radius/2.0f*(*engine->tube)->wall_thickness; } else { - engine->longitudinal_stress = engine->max_pressure*pow(engine->tube->internal_radius,2.0f)/(pow(engine->tube->diameter_ext,2.0f)-pow(engine->tube->internal_radius,2.0f)); + engine->longitudinal_stress = engine->max_pressure*pow((*engine->tube)->internal_radius,2.0f)/(pow((*engine->tube)->diameter_ext,2.0f)-pow((*engine->tube)->internal_radius,2.0f)); } } /* Esfuerzo Radial E123 */ @@ -83,44 +83,44 @@ void trel_max_stress(engine_t *engine) /* Margen de Seguridad E127 */ void trel_margin_of_safety(engine_t *engine) { - engine->margin_of_safety = engine->tube->shear_stress_tension/engine->max_stress; + engine->margin_of_safety = (*engine->tube)->shear_stress_tension/engine->max_stress; } /* Determinación de Areas afectadas por tornillos */ /* engine_t has screws_t *screws to initialize at allocation */ -/*engine->screws->n_screws = 6; */ -/*engine->screws->diameter = 0.009f; */ -/*engine->screws->dist_center_wall = 0.01f; */ +/*(*engine->screws)->n_screws = 6; */ +/*(*engine->screws)->diameter = 0.009f; */ +/*(*engine->screws)->dist_center_wall = 0.01f; */ /* Area_Transversal_tubo E133 */ /* engine_t has tubing_t *tube */ void trel_transversal_area_tube(engine_t *engine) { - engine->tube->transversal_area = (double)TREL_PI*(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)); + (*engine->tube)->transversal_area = (double)TREL_PI*(pow((*engine->tube)->diameter_ext/2.0f,2.0f)-pow((*engine->tube)->internal_radius,2.0f)); } /* Angulo de sector circular E134 */ void trel_ang_circular_sector(engine_t *engine) { - engine->tube->sector_angle = (180.0/TREL_PI)*asin((engine->screws->diameter/2)/engine->tube->internal_radius); + (*engine->tube)->sector_angle = (180.0/TREL_PI)*asin(((*engine->screws)->diameter/2)/(*engine->tube)->internal_radius); } /* Area transversal de 1 tornillo E135 */ void trel_area_per_screw(engine_t *engine) { - engine->screws->area_per_screw = (double)(engine->tube->sector_angle/2.0f)*(pow(engine->tube->diameter_ext/2.0f,2.0f)-pow(engine->tube->internal_radius,2.0f)-pow(engine->tube->wall_thickness,2.0f)); + (*engine->screws)->area_per_screw = (double)((*engine->tube)->sector_angle/2.0f)*(pow((*engine->tube)->diameter_ext/2.0f,2.0f)-pow((*engine->tube)->internal_radius,2.0f)-pow((*engine->tube)->wall_thickness,2.0f)); } /* Area transversal ocupada por los tornillos E136 */ void trel_screw_occupied_area(engine_t *engine) { - engine->screws->screw_occupied_area = engine->screws->area_per_screw*engine->screws->amount; + (*engine->screws)->screw_occupied_area = (*engine->screws)->area_per_screw*(*engine->screws)->amount; } /* Area de material E137 */ void trel_tube_mateial_area(engine_t *engine) { - engine->tube->material_area = engine->tube->transversal_area-engine->screws->screw_occupied_area; + (*engine->tube)->material_area = (*engine->tube)->transversal_area-(*engine->screws)->screw_occupied_area; } /* Espesor del segmento cortante E138 */ void trel_width_cutting_segment(engine_t *engine) { - engine->screws->width_cutting_segment = engine->screws->dist_center_wall-(engine->screws->diameter/2.0f); + (*engine->screws)->width_cutting_segment = (*engine->screws)->dist_center_wall-((*engine->screws)->diameter/2.0f); } diff --git a/src/engine.c b/src/engine.c index b038b8d..bad72c0 100644 --- a/src/engine.c +++ b/src/engine.c @@ -110,9 +110,9 @@ engine_t* trel_engine_init( double temperature, double engine_mass, grains_t **grains, - fuel_t *fuel, - tubing_t *tube, - screws_t *screws) + fuel_t **fuel, + tubing_t **tube, + screws_t **screws) { comp_area_t *comp_area_values = (comp_area_t*) malloc(sizeof(comp_area_t)); engine_t *engine = (engine_t *) malloc(sizeof(engine_t)); @@ -228,15 +228,34 @@ void trel_comp_area_free(comp_area_t** values) void trel_engine_free(engine_t** engine) { + if (*engine != NULL) + { + if ((*engine)->comp_area_values != NULL) + { + free((*engine)->comp_area_values); + (*engine)->comp_area_values = NULL; + } + if ((*(*engine)->screws) != NULL) + { + free((*(*engine)->screws)); + (*(*engine)->screws) = NULL; + } + if ((*(*engine)->tube) != NULL) + { + free((*(*engine)->tube)); + (*(*engine)->tube) = NULL; + } + if ((*(*engine)->fuel) != NULL) + { + free((*(*engine)->fuel)); + (*(*engine)->fuel) = NULL; + } if ((*(*engine)->grains) != NULL) { free((*(*engine)->grains)); (*(*engine)->grains) = NULL; } - (*engine)->grains = NULL; - if (*engine != NULL) - { - free(*engine); - *engine = NULL; + free(*engine); + *engine = NULL; } } diff --git a/src/height_sim.c b/src/height_sim.c index 9c757d5..12d4304 100644 --- a/src/height_sim.c +++ b/src/height_sim.c @@ -45,7 +45,7 @@ double calc_inst_radius(double inst_radius, double burn_rate, double step, doubl double calc_burn_rate(engine_t *engine, double pressure) { - return(engine->fuel->const_burn_rate * pow(pressure * 0.000145038, engine->fuel->pressure_exponent) * 0.0254); + return((*engine->fuel)->const_burn_rate * pow(pressure * 0.000145038, (*engine->fuel)->pressure_exponent) * 0.0254); } double calc_inst_long(double inst_radius, double inst_long, double burn_rate, double step) @@ -147,13 +147,13 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) double burn_rate = 0.0; double virt_escape_pressure = 0.0; double delta_m = 0.0; - const double total_volume = TREL_PI * POW2(engine->tube->internal_radius) * + const double total_volume = TREL_PI * POW2((*engine->tube)->internal_radius) * ((*engine->grains)->grain_separation * ((*engine->grains)->amount - 1) + (*engine->grains)->longitude * (*engine->grains)->amount); inst_long = (*engine->grains)->longitude; inst_radius = (*engine->grains)->init_inter_radius; fuel_volume = (*engine->grains)->amount * TREL_PI * (POW2((*engine->grains)->extern_radius) - POW2(inst_radius)) * inst_long; - fuel_mass = fuel_volume * engine->fuel->density; + fuel_mass = fuel_volume * (*engine->fuel)->density; prev_fuel_mass = fuel_mass; free_volume = total_volume - fuel_volume; rocket_height = (*rocket)->initial_height; @@ -164,11 +164,11 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) burn_rate = calc_burn_rate(engine, pressure); atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); - res->rocket_mass[0] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*rocket)->engine->fuel->density * fuel_volume; + res->rocket_mass[0] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*(*rocket)->engine->fuel)->density * fuel_volume; res->rocket_weight[0] = res->rocket_mass[0] * gravitational_accel; virt_escape_pressure = calc_iter_virt_escape_pressure(pressure, engine); force_coeff = calc_iter_force_coeff(virt_escape_pressure, pressure, atmospheric_pressure, engine); - adjusted_cf = force_coeff * (*rocket)->engine->tube->nozzle_efficiency; + adjusted_cf = force_coeff * (*(*rocket)->engine->tube)->nozzle_efficiency; res->rocket_force[0] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; res->rocket_force_balance[0] = calc_force_balance(res->rocket_force[0], res->rocket_position[0], res->rocket_speed[0], res->rocket_drag[0], res->rocket_weight[0]); @@ -187,7 +187,7 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); fuel_volume = (*(*rocket)->engine->grains)->amount * TREL_PI * (((*(*rocket)->engine->grains)->extern_radius * (*(*rocket)->engine->grains)->extern_radius) - inst_radius * inst_radius) * inst_long; escape_massic_flux = calc_iter_escape_massic_flux(pressure, atmospheric_pressure, engine); - fuel_mass = fuel_volume * (*rocket)->engine->fuel->density; + fuel_mass = fuel_volume * (*(*rocket)->engine->fuel)->density; produced_mass = prev_fuel_mass - fuel_mass; prev_fuel_mass = fuel_mass; delta_m = escape_massic_flux * step; @@ -195,7 +195,7 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) free_volume = total_volume - fuel_volume; virt_escape_pressure = calc_iter_virt_escape_pressure(pressure, engine); force_coeff = calc_iter_force_coeff(virt_escape_pressure, pressure, atmospheric_pressure, engine); - adjusted_cf = force_coeff * (*rocket)->engine->tube->nozzle_efficiency; + adjusted_cf = force_coeff * (*(*rocket)->engine->tube)->nozzle_efficiency; res->rocket_mass[it] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + fuel_mass; res->rocket_weight[it] = res->rocket_mass[it] * gravitational_accel; res->rocket_drag[it] = 0.5 * atmospheric_density * (*rocket)->drag_coefficient * TRANSVERSAL_AREA((*rocket)->body_diameter) * res->rocket_speed[it - 1] * res->rocket_speed[it - 1]; diff --git a/src/rendimientos.c b/src/rendimientos.c index c2c817b..6e6f110 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -4,8 +4,8 @@ /*E43 */ double br_combustion(engine_t *engine) { - return (engine->fuel->const_burn_rate* pow(trel_get_pressure_pa(engine) * - FACT_CONV_BR, engine->fuel->pressure_exponent) * FACT_CONV_M_TO_I); + return ((*engine->fuel)->const_burn_rate* pow(trel_get_pressure_pa(engine) * + FACT_CONV_BR, (*engine->fuel)->pressure_exponent) * FACT_CONV_M_TO_I); } /*Combustible */ @@ -20,7 +20,7 @@ double volumen_combustible(engine_t *engine) /*E68 Masa_Combustible */ double masa_combustible(engine_t *engine) { - return (volumen_combustible(engine) *engine->fuel->density); + return (volumen_combustible(engine) *(*engine->fuel)->density); } /*Combustión*/ @@ -40,7 +40,7 @@ double Ab_combustion(engine_t *engine) /*E72 Flujo másico*/ double mg_combustion(engine_t *engine) { - return Ab_combustion(engine)*engine->fuel->density*br_combustion(engine); + return Ab_combustion(engine)*(*engine->fuel)->density*br_combustion(engine); } /*Fuerzas*/ @@ -76,7 +76,7 @@ double I_sp_teo(engine_t *engine) /*E82 Impulso específico esperado*/ double I_sp_esperado(engine_t* engine) { - return(engine->fuel->density); + return((*engine->fuel)->density); } /*E83 Impulso total teórico*/ From 7207260a52e1cce730594ecf197237fd47cd21b6 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sun, 23 Apr 2023 09:31:05 -0600 Subject: [PATCH 30/42] Finished memory managment functions, TREL is now fully functional ~lross2k~ --- TODO.md | 7 +++---- include/TREL.h | 5 +++-- include/rocket.h | 4 +++- include/structs.h | 2 +- main.c | 3 ++- src/comp_tiempo.c | 2 +- src/height_sim.c | 24 ++++++++++++------------ src/rocket.c | 23 ++++++++++++++++++++++- 8 files changed, 47 insertions(+), 23 deletions(-) diff --git a/TODO.md b/TODO.md index 373bcd0..df14679 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ # TODO list for the project -1. Implement memory free functions, and destroy all pointers -2. Define and apply standard function convention -3. Define and apply standard style guide -4. Implement Python library interface +1. Define and apply standard function convention +2. Define and apply standard style guide +3. Implement Python library interface diff --git a/include/TREL.h b/include/TREL.h index c00ffa4..50411a3 100644 --- a/include/TREL.h +++ b/include/TREL.h @@ -78,7 +78,7 @@ typedef struct trel_height_sim_values /* Struct for the rocket */ typedef struct trel_rocket { - engine_t* engine; /* ptr to initialized struct */ + engine_t **engine; /* ptr to initialized struct */ double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; double avg_thrust, max_thrust, delta_v, max_pressure; /* Comportamiento en el Tiempo!AD3511 */ double sim_latitude, body_diameter, drag_coefficient; @@ -125,7 +125,7 @@ engine_t *trel_engine_init( screws_t **screws ); trel_rocket_t *trel_rocket_init( - engine_t* engine, + engine_t **engine, double telemetry_mass, double parachute_mass, double fuselage_mass, @@ -151,5 +151,6 @@ void trel_fuel_free(fuel_t** fuel); void trel_tubing_free(tubing_t** tubing); void trel_screws_free(screws_t** screws); void trel_engine_free(engine_t** engine); +void trel_rocket_free(trel_rocket_t **rocket); #endif /*TREL_H */ diff --git a/include/rocket.h b/include/rocket.h index ed5cd89..99024ce 100644 --- a/include/rocket.h +++ b/include/rocket.h @@ -12,7 +12,7 @@ /* Function prototypes */ trel_rocket_t TREL_EXPORT* trel_rocket_init( - engine_t* engine, + engine_t **engine, double telemetry_mass, double parachute_mass, double fuselage_mass, @@ -23,4 +23,6 @@ trel_rocket_t TREL_EXPORT* trel_rocket_init( double drag_coefficient ); +void trel_rocket_free(trel_rocket_t **rocket); + #endif/*ROCKET_H */ diff --git a/include/structs.h b/include/structs.h index 6e940ff..4d7fa04 100644 --- a/include/structs.h +++ b/include/structs.h @@ -81,7 +81,7 @@ typedef struct trel_height_sim_values /* Struct for the rocket */ typedef struct trel_rocket { - engine_t *engine; /* ptr to initialized struct */ + engine_t **engine; /* ptr to initialized struct */ double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; double avg_thrust, max_thrust, delta_v, max_pressure; /* Comportamiento en el Tiempo!AD3511 */ double sim_latitude, body_diameter, drag_coefficient; diff --git a/main.c b/main.c index 023dfc3..2c3e02b 100644 --- a/main.c +++ b/main.c @@ -50,7 +50,7 @@ void print_debug_example() ); // Se inicializa el cohete trel_rocket_t* testing_rocket = trel_rocket_init( - testing_engine, // engine + &testing_engine, // engine 0.3, // telemetry_mass 0.05, // parachute_mass 5.0, // fuselage_mass @@ -125,6 +125,7 @@ void print_debug_example() trel_tubing_free(&created_tube); trel_screws_free(&created_screw); trel_engine_free(&testing_engine); + trel_rocket_free(&testing_rocket); } // Para hacer pruebas de la lib se tiene la aplicacion de CLI basica diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index c839de4..3e1a8bc 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -15,7 +15,7 @@ double calc_iter_internal_mass(double produced_mass, double internal_mass, doubl /*Comportamiento en el Tiempo!AD3511 */ int trel_run_time_comp_iterations(trel_rocket_t* rocket) { - engine_t* engine = rocket->engine; + engine_t* engine = (*rocket->engine); /* iterate over given iterations and step size */ const double max_iterations = TREL_MAX_ITERATIONS; diff --git a/src/height_sim.c b/src/height_sim.c index 12d4304..967c766 100644 --- a/src/height_sim.c +++ b/src/height_sim.c @@ -115,12 +115,12 @@ double calc_iter_escape_massic_flux(double pressure, double atmospheric_pressure int trel_run_height_sim_iterations(trel_rocket_t** rocket) { - engine_t *engine = (*rocket)->engine; + engine_t *engine = (*(*rocket)->engine); trel_height_sim_t *res = (*rocket)->sim_values; /* iterate over given iterations and step size */ const unsigned int max_iterations = TREL_MAX_ITERATIONS; - const double burn_time = Tiempo_quemado_combustion((*rocket)->engine); + const double burn_time = Tiempo_quemado_combustion((*(*rocket)->engine)); const double step = burn_time / TREL_MAX_ITERATIONS; unsigned int it = 0; @@ -160,16 +160,16 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) gravitational_accel = TREL_ECUATOR_GRAV_ACCEL * (1.0 + (TREL_GRAV_CRUSH * (sin((*rocket)->sim_latitude * TREL_PI / 180.0)) * (sin((*rocket)->sim_latitude * TREL_PI / 180.0))) - (TREL_GRAV_CRUSH_4 * (sin((2.0 * (*rocket)->sim_latitude) * TREL_PI / 180.0)) * (sin((2.0 * (*rocket)->sim_latitude) * TREL_PI / 180.0)))) - (TREL_GRAV_A * rocket_height); atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); pressure = atmospheric_pressure; - intern_gas_mass = pressure * free_volume / (TREL_GAS_CONST * (*rocket)->engine->temperature); + intern_gas_mass = pressure * free_volume / (TREL_GAS_CONST * (*(*rocket)->engine)->temperature); burn_rate = calc_burn_rate(engine, pressure); atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); - res->rocket_mass[0] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + (*(*rocket)->engine->fuel)->density * fuel_volume; + res->rocket_mass[0] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*(*rocket)->engine)->engine_mass + (*(*(*rocket)->engine)->fuel)->density * fuel_volume; res->rocket_weight[0] = res->rocket_mass[0] * gravitational_accel; virt_escape_pressure = calc_iter_virt_escape_pressure(pressure, engine); force_coeff = calc_iter_force_coeff(virt_escape_pressure, pressure, atmospheric_pressure, engine); - adjusted_cf = force_coeff * (*(*rocket)->engine->tube)->nozzle_efficiency; - res->rocket_force[0] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; + adjusted_cf = force_coeff * (*(*(*rocket)->engine)->tube)->nozzle_efficiency; + res->rocket_force[0] = adjusted_cf * pressure * throat_area((*(*rocket)->engine)) > 0 ? adjusted_cf * pressure * throat_area((*(*rocket)->engine)) : 0; res->rocket_force_balance[0] = calc_force_balance(res->rocket_force[0], res->rocket_position[0], res->rocket_speed[0], res->rocket_drag[0], res->rocket_weight[0]); /* iteration loop */ @@ -180,14 +180,14 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) gravitational_accel = calc_iter_grav_accel(rocket, rocket_height); atmospheric_pressure = TREL_ATMOSPHERIC_PRESSURE * pow(1.0 - TREL_ADIABATIC_GRADIENT * rocket_height / TREL_STANDARD_TEMP, gravitational_accel * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * TREL_ADIABATIC_GRADIENT)); /* ACA SE COLAPSA EL TITANIC */ pressure = calc_iter_pressure(engine, intern_gas_mass, free_volume, atmospheric_pressure); - inst_radius = calc_inst_radius(inst_radius, burn_rate, step, (*(*rocket)->engine->grains)->extern_radius); + inst_radius = calc_inst_radius(inst_radius, burn_rate, step, (*(*(*rocket)->engine)->grains)->extern_radius); inst_long = calc_inst_long(inst_radius, inst_long, burn_rate, step); burn_rate = calc_burn_rate(engine, pressure); atmospheric_temp = TREL_STANDARD_TEMP - (TREL_ADIABATIC_GRADIENT * rocket_height); atmospheric_density = atmospheric_pressure * TREL_DRY_AIR_MOLAR_MASS / (TREL_IDEAL_GAS_CONST * atmospheric_temp); - fuel_volume = (*(*rocket)->engine->grains)->amount * TREL_PI * (((*(*rocket)->engine->grains)->extern_radius * (*(*rocket)->engine->grains)->extern_radius) - inst_radius * inst_radius) * inst_long; + fuel_volume = (*(*(*rocket)->engine)->grains)->amount * TREL_PI * (((*(*(*rocket)->engine)->grains)->extern_radius * (*(*(*rocket)->engine)->grains)->extern_radius) - inst_radius * inst_radius) * inst_long; escape_massic_flux = calc_iter_escape_massic_flux(pressure, atmospheric_pressure, engine); - fuel_mass = fuel_volume * (*(*rocket)->engine->fuel)->density; + fuel_mass = fuel_volume * (*(*(*rocket)->engine)->fuel)->density; produced_mass = prev_fuel_mass - fuel_mass; prev_fuel_mass = fuel_mass; delta_m = escape_massic_flux * step; @@ -195,11 +195,11 @@ int trel_run_height_sim_iterations(trel_rocket_t** rocket) free_volume = total_volume - fuel_volume; virt_escape_pressure = calc_iter_virt_escape_pressure(pressure, engine); force_coeff = calc_iter_force_coeff(virt_escape_pressure, pressure, atmospheric_pressure, engine); - adjusted_cf = force_coeff * (*(*rocket)->engine->tube)->nozzle_efficiency; - res->rocket_mass[it] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*rocket)->engine->engine_mass + fuel_mass; + adjusted_cf = force_coeff * (*(*(*rocket)->engine)->tube)->nozzle_efficiency; + res->rocket_mass[it] = (*rocket)->telemetry_mass + (*rocket)->parachute_mass + (*rocket)->fuselage_mass + (*rocket)->payload_mass + (*(*rocket)->engine)->engine_mass + fuel_mass; res->rocket_weight[it] = res->rocket_mass[it] * gravitational_accel; res->rocket_drag[it] = 0.5 * atmospheric_density * (*rocket)->drag_coefficient * TRANSVERSAL_AREA((*rocket)->body_diameter) * res->rocket_speed[it - 1] * res->rocket_speed[it - 1]; - res->rocket_force[it] = adjusted_cf * pressure * throat_area((*rocket)->engine) > 0 ? adjusted_cf * pressure * throat_area((*rocket)->engine) : 0; + res->rocket_force[it] = adjusted_cf * pressure * throat_area((*(*rocket)->engine)) > 0 ? adjusted_cf * pressure * throat_area((*(*rocket)->engine)) : 0; res->rocket_force_balance[it] = calc_force_balance(res->rocket_force[it], res->rocket_position[it - 1], res->rocket_speed[it - 1], res->rocket_drag[it], res->rocket_weight[it]); res->rocket_acceleration[it] = res->rocket_force_balance[it] / res->rocket_mass[it]; res->rocket_speed[it] = res->rocket_acceleration[it] * step + res->rocket_speed[it - 1]; diff --git a/src/rocket.c b/src/rocket.c index 7b602ac..d77cea4 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -2,7 +2,7 @@ /* Initializes all the variables in the rocket struct */ trel_rocket_t *trel_rocket_init( - engine_t* engine, + engine_t **engine, double telemetry_mass, double parachute_mass, double fuselage_mass, @@ -64,3 +64,24 @@ trel_rocket_t *trel_rocket_init( } return(rocket); } + +void trel_height_sim_free(trel_height_sim_t **values) +{ + free(*values); + *values = NULL; +} + +void trel_rocket_free(trel_rocket_t **rocket) +{ + if (*rocket != NULL) + if ((*rocket)->sim_values != NULL) + { + free((*rocket)->sim_values); + (*rocket)->sim_values = NULL; + } + trel_engine_free((*rocket)->engine); + { + free(*rocket); + *rocket = NULL; + } +} From 1351728aaaea8ea98d4036801452a919372002ee Mon Sep 17 00:00:00 2001 From: lross2k Date: Sun, 23 Apr 2023 10:00:44 -0600 Subject: [PATCH 31/42] Cleaned a bit of everything Major code refactoring is pending, but it won't catch the official release, too bad ~lross2k~ --- .gitignore | 5 ++- {python => Bindings/Python}/TREL.py | 0 {python => Bindings/Python}/main.py | 0 README.md | 66 +++-------------------------- misc/MAKE_BUILD.md | 18 ++++++++ misc/MANUAL_BUILD.md | 35 +++++++++++++++ misc/VISUAL_STUDIO_BUILD.md | 12 ++++++ 7 files changed, 75 insertions(+), 61 deletions(-) rename {python => Bindings/Python}/TREL.py (100%) rename {python => Bindings/Python}/main.py (100%) create mode 100644 misc/MAKE_BUILD.md create mode 100644 misc/MANUAL_BUILD.md create mode 100644 misc/VISUAL_STUDIO_BUILD.md diff --git a/.gitignore b/.gitignore index 3ec1797..fecabf5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,8 @@ CMakeSettings.json out/ # Recommended build directory -build/ +Release/ +Debug/ # Binary files leftover *.exe *.so @@ -16,5 +17,5 @@ build/ # Compressed files for whatever reason *.zip *.7z -# Python interpreter leftovers +# Bindings stuff __pycache__/ diff --git a/python/TREL.py b/Bindings/Python/TREL.py similarity index 100% rename from python/TREL.py rename to Bindings/Python/TREL.py diff --git a/python/main.py b/Bindings/Python/main.py similarity index 100% rename from python/main.py rename to Bindings/Python/main.py diff --git a/README.md b/README.md index 85d8ad5..6487c73 100644 --- a/README.md +++ b/README.md @@ -1,64 +1,12 @@ -# TREL -## TECSpace Rocket Engine Library -This library is still in early development, only debug compiling options for x64 have been configured and tested for Windows and Unix like systems. - -## Roadmap -Read the [TODO](https://github.com/Coheteria-TECSpace/TREL/blob/dev/TODO.md) and try to -understand what to do just by reading that. +# TECSpace Rocket Engine Library (TREL) +ANSI C89/C90 library for transient simulations of solid propellant rocket engines' performance, easily run simulations for rocket engines or entire rocket systems with just a few lines of code. ## Portability -Given that this library expects to calculate data for rockets in many ways, allowing the same functionality to be easily ported to any high level programming language having the basis at ANSI C (C89/C90) will make the initial effort more scalable as it could even be used to interface straight from embedded systems that gather data and compare it to the one generated in real time from the library, or also using the basic funtions to create an advanced GUI +Given that this library expects to calculate data for rockets in many ways, allowing the same functionality to be easily ported to any high level programming language having the basis at ANSI C (C89/C90) will make the initial effort more scalable as it could even be used to interface straight from embedded systems that gather data and compare it to the one generated in real time from the library, or even using the basic funtions to create an advanced GUI. -# Build process +## Building the library The current build method uses CMake, modifying the CMakeLists.txt file allows the user to generate a static or dynamic library by changing `"Build Shared Libraries" ON to OFF` depending on what is desired, just as well as `"Build app" ON` can be toggled to generate the testing app which links automatically to the library. -## Usinc CMake - -### With GNU make -``` -mkdir build && cd build -cmake .. -make -``` - -### With MSVC in Visual Studio -Open the directory in VS, let CMake generate the project, then select on *Build project* at the top of VS and then the final build will be created, currently only Debug x64 exists - -*CMake adds `/W3` by deffault so some warning for overriding with `/W4` will appear, currently looking for a solution* - -### With MSVC for other IDEs/Text editors with Visual Studio avaliable -Guessing you have a `build/`, run cmake from there with `cmake ..` - -When the project is built, there will be a `.sln` file which once it's oppened on Visual Studio it will allow to build de project, then leaving the library and app if chosen. - -## Using only GCC and make -In case you want to stay away from CMake, this should work for all Unix like systems, but it isn't the recommended route - -### Compiling the shared object from a subdirectory build -``` -mkdir build -cd build -gcc -c -Wall -Wextra -pedantic -Werror -fpic ../src/*.c -I../include/ -gcc -shared -o libTREL.so ./*.o -rm *.o -``` -### Building main.c and linking to the lib from the build subdirectory -``` -gcc -o app ../main.c -Wall -Wextra -pedantic -Werror -I../include/ -L. -lTREL -lm -export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH -``` - -## Using only GCC and make (MINGW) -In case you can't use (or don't want to use) WSL, this is the less Unix-y way of doing it in Windows - -### Compiling the shared object from a subdirectory build -``` -md build && cd build -gcc -c -Wall -Wextra -pedantic -Werror -fpic ../src/*.c -I../include/ -gcc -shared -o libTREL.dll ./*.o -del *.o -``` -### Building main.c and linking to the lib from the build subdirectory -``` -gcc -o app ../main.c -Wall -Wextra -pedantic -Werror -I../include/ -L. -lTREL -lm -``` +## Roadmap +Read the [TODO](https://github.com/Coheteria-TECSpace/TREL/blob/dev/TODO.md) and try to +understand what to do just by reading that. diff --git a/misc/MAKE_BUILD.md b/misc/MAKE_BUILD.md new file mode 100644 index 0000000..c01e986 --- /dev/null +++ b/misc/MAKE_BUILD.md @@ -0,0 +1,18 @@ +# Make building TREL +When using Make the process should be fairly straightforward, the latest release of TREL will always be tested with GNU Linux, at least in a Debian 11+ distro, however, CMake's generated Makefile should be compatible with many OSes. + +## Release +Do this if you only need TREL to run simulations +``` +mkdir Release && cd Release +cmake -DCMAKE_BUILD_TYPE=Release .. +make +``` + +## Debug +Do this if you want to test and contribute code to TREl, allowing debug symbols to be bundled in the executable, **expect lower performance than Release** due to the lack of compiler optimizations +``` +mkdir Debug && cd Debug +cmake -DCMAKE_BUILD_TYPE=Debug .. +make +``` diff --git a/misc/MANUAL_BUILD.md b/misc/MANUAL_BUILD.md new file mode 100644 index 0000000..3c857dd --- /dev/null +++ b/misc/MANUAL_BUILD.md @@ -0,0 +1,35 @@ +# Manual building TREL +This isn't the recommended way of compiling TREL, so it's highly discouraged, however, if your system doesn't have CMake for some reason, this **old** instructions are left as a draft to what you might need to do + +## Using only GCC and make +In case you want to stay away from CMake, this should work for all Unix like systems, but it isn't the recommended route + +### Compiling the shared object from a subdirectory build +``` +mkdir build +cd build +gcc -c -Wall -Wextra -pedantic -Werror -fpic ../src/*.c -I../include/ +gcc -shared -o libTREL.so ./*.o +rm *.o +``` +### Building main.c and linking to the lib from the build subdirectory +``` +gcc -o app ../main.c -Wall -Wextra -pedantic -Werror -I../include/ -L. -lTREL -lm +export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH +``` + +## Using only GCC and make (MINGW) +In case you can't use (or don't want to use) WSL, this is the less Unix-y way of doing it in Windows + +### Compiling the shared object from a subdirectory build +``` +md build && cd build +gcc -c -Wall -Wextra -pedantic -Werror -fpic ../src/*.c -I../include/ +gcc -shared -o libTREL.dll ./*.o +del *.o +``` +### Building main.c and linking to the lib from the build subdirectory +``` +gcc -o app ../main.c -Wall -Wextra -pedantic -Werror -I../include/ -L. -lTREL -lm +``` + diff --git a/misc/VISUAL_STUDIO_BUILD.md b/misc/VISUAL_STUDIO_BUILD.md new file mode 100644 index 0000000..b4ff2f7 --- /dev/null +++ b/misc/VISUAL_STUDIO_BUILD.md @@ -0,0 +1,12 @@ +# MSVC building TREL +The latest release of TREL will **always** be tested for compilation and execution both in Unix and Windows, for the latter, Visual Studio Community 2022 is the officcial IDE, but in theory many other methods are supported + +## With MSVC in Visual Studio +Open the directory in VS, let CMake generate the project, then select on *Build project* at the top of VS and then the final build will be created, currently only Debug x64 exists + +*CMake adds `/W3` by deffault so some warning for overriding with `/W4` will appear, currently looking for a solution* + +# With MSVC for other IDEs/Text editors with Visual Studio avaliable +Guessing you have a `build/`, run cmake from there with `cmake ..` + +When the project is built, there will be a `.sln` file which once it's oppened on Visual Studio it will allow to build de project, then leaving the library and app if chosen. From 9ac3d4a68c0eb1f0e75a3bbe39f30e69fd186c87 Mon Sep 17 00:00:00 2001 From: lross2k Date: Sun, 23 Apr 2023 10:54:06 -0600 Subject: [PATCH 32/42] Tested Windows compilation ~lross2k~ --- .gitignore | 1 + CMakeLists.txt | 3 --- include/engine.h | 10 +++++----- include/rocket.h | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index fecabf5..5a00e8f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .vs/ .vscode/ CMakeSettings.json +CMakePresets.json out/ # Recommended build directory Release/ diff --git a/CMakeLists.txt b/CMakeLists.txt index a7dbb87..0e9b570 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,6 @@ # @version 0.0.1 # CMake allows for a wide range of platforms and IDEs/build tools # This project structure was designed with Win32 and UNIX like OS in mind -# Some terminal arguments should allow for specific builds with make -# cmake -DCMAKE_BUILD_TYPE=Debug path/to/source -# cmake -DCMAKE_BUILD_TYPE=Release path/to/source # Probably works on 3.12, hasn't been tested yet CMAKE_MINIMUM_REQUIRED (VERSION 3.13.4) diff --git a/include/engine.h b/include/engine.h index 512c671..279dcca 100644 --- a/include/engine.h +++ b/include/engine.h @@ -53,10 +53,10 @@ void TREL_EXPORT trel_set_pressure(engine_t *engine, double pressure); void TREL_EXPORT trel_set_escape_vel(engine_t *engine, double vel); double TREL_EXPORT trel_get_escape_vel(engine_t *engine); double TREL_EXPORT trel_get_pressure(engine_t *engine); -void trel_grains_free(grains_t** grains); -void trel_fuel_free(fuel_t** fuel); -void trel_tubing_free(tubing_t** tubing); -void trel_screws_free(screws_t** screws); -void trel_engine_free(engine_t** engine); +void TREL_EXPORT trel_grains_free(grains_t** grains); +void TREL_EXPORT trel_fuel_free(fuel_t** fuel); +void TREL_EXPORT trel_tubing_free(tubing_t** tubing); +void TREL_EXPORT trel_screws_free(screws_t** screws); +void TREL_EXPORT trel_engine_free(engine_t** engine); #endif /* ENGINE_H */ diff --git a/include/rocket.h b/include/rocket.h index 99024ce..589ff31 100644 --- a/include/rocket.h +++ b/include/rocket.h @@ -23,6 +23,6 @@ trel_rocket_t TREL_EXPORT* trel_rocket_init( double drag_coefficient ); -void trel_rocket_free(trel_rocket_t **rocket); +void TREL_EXPORT trel_rocket_free(trel_rocket_t **rocket); #endif/*ROCKET_H */ From 8e7d0d2e47a03b90472a35206881f701313c2e27 Mon Sep 17 00:00:00 2001 From: lross2k Date: Tue, 25 Apr 2023 17:08:14 -0600 Subject: [PATCH 33/42] Updated the interface to generate a whole rocket TODO: - Call memory free functions - Write a better TREL class so it's more high level (remove ctypes stuff) --- Interfaces/Python/TREL.py | 204 ++++++++++++++++++++++++++++++++++++-- Interfaces/Python/main.py | 51 ++++++++-- 2 files changed, 237 insertions(+), 18 deletions(-) diff --git a/Interfaces/Python/TREL.py b/Interfaces/Python/TREL.py index 7641665..d3c5cbd 100644 --- a/Interfaces/Python/TREL.py +++ b/Interfaces/Python/TREL.py @@ -1,10 +1,21 @@ -import ctypes +import ctypes import os +TREL_MAX_STR_LEN = 100 +TREL_MAX_ITERATIONS = 1000 + if os.name == 'posix': - TREL = ctypes.CDLL("./libTREL.so") + TREL = ctypes.CDLL("../build/libTREL.so") elif os.name == 'nt': - TREL = ctypes.CDLL("./TREL.dll") + TREL = ctypes.CDLL("../../out/build/x64-Release/TREL.dll") + +# Struct for comp area simulation +class TREL_COMP_AREA(ctypes.Structure): + _fields_ = [("avg_long_area", ctypes.c_double), + ("avg_trans_area", ctypes.c_double), + ("avg_burn_area", ctypes.c_double), + ("burn_std_deviation", ctypes.c_double), + ("burn_sum_diff", ctypes.c_double)] # Struct and constructor for propellent grains class TREL_GRAINS(ctypes.Structure): @@ -39,20 +50,23 @@ class TREL_FUEL(ctypes.Structure): class TREL_SCREWS(ctypes.Structure): _fields_ = [("diameter", ctypes.c_double), ("dist_center_wall", ctypes.c_double), + ("amount", ctypes.c_uint), ("area_per_screw", ctypes.c_double), ("screw_occupied_area", ctypes.c_double), - ("width_cutting_segment", ctypes.c_double), - ("amount", ctypes.c_uint)] + ("material", TREL_MAX_STR_LEN * ctypes.c_char), + ("width_cutting_segment", ctypes.c_double)] trel_screws_init = TREL.trel_screws_init -trel_screws_init.argtypes = [ctypes.c_uint, +trel_screws_init.argtypes = [ctypes.c_char_p, + ctypes.c_uint, ctypes.c_double, ctypes.c_double] trel_screws_init.restype = ctypes.c_void_p # Struct and constructor for tubing class TREL_TUBING(ctypes.Structure): - _fields_ = [("diameter_ext", ctypes.c_double), + _fields_ = [("material", TREL_MAX_STR_LEN * ctypes.c_char), + ("diameter_ext", ctypes.c_double), ("wall_thickness", ctypes.c_double), ("internal_radius", ctypes.c_double), ("young_module", ctypes.c_double), @@ -60,11 +74,16 @@ class TREL_TUBING(ctypes.Structure): ("mean_tubing_diameter", ctypes.c_double), ("shear_stress_tension", ctypes.c_double), ("shear_stress_pressure", ctypes.c_double), + ("ult_stress_tension", ctypes.c_double), + ("ult_stress_pressure", ctypes.c_double), ("transversal_area", ctypes.c_double), - ("material_area", ctypes.c_double)] + ("material_area", ctypes.c_double), + ("nozzle_efficiency", ctypes.c_double)] trel_tubing_init = TREL.trel_tubing_init -trel_tubing_init.argtypes = [ctypes.c_double, +trel_tubing_init.argtypes = [ctypes.c_char_p, + ctypes.c_double, + ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, @@ -73,3 +92,170 @@ class TREL_TUBING(ctypes.Structure): ctypes.c_double] trel_tubing_init.restype = ctypes.c_void_p +# Struct and constructor for engine +class TREL_ENGINE(ctypes.Structure): + _fields_ = [("engine_mass", ctypes.c_double), + ("pressure", ctypes.c_double), + ("escape_vel", ctypes.c_double), + ("temperature", ctypes.c_double), + ("width_condition", ctypes.c_double), + ("margin_of_safety", ctypes.c_double), + ("max_stress", ctypes.c_double), + ("radial_stress", ctypes.c_double), + ("tangencial_stress", ctypes.c_double), + ("longitudinal_stress", ctypes.c_double), + ("max_pressue", ctypes.c_double), + ("nozzle_efficiency", ctypes.c_double), + ("grains", ctypes.POINTER(ctypes.POINTER(TREL_GRAINS))), + ("fuel", ctypes.POINTER(ctypes.POINTER(TREL_FUEL))), + ("tube", ctypes.POINTER(ctypes.POINTER(TREL_TUBING))), + ("screws", ctypes.POINTER(ctypes.POINTER(TREL_SCREWS))), + ("comp_area_values", ctypes.POINTER(TREL_COMP_AREA))] + +trel_engine_init = TREL.trel_engine_init +trel_engine_init.argtypes = [ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.POINTER(ctypes.POINTER(TREL_GRAINS)), + ctypes.POINTER(ctypes.POINTER(TREL_FUEL)), + ctypes.POINTER(ctypes.POINTER(TREL_TUBING)), + ctypes.POINTER(ctypes.POINTER(TREL_SCREWS))] +trel_engine_init.restype = ctypes.c_void_p + +# Struct for sim values +class TREL_HEIGHT_SIM(ctypes.Structure): + _fields_ = [("position", TREL_MAX_ITERATIONS * ctypes.c_double), + ("speed", TREL_MAX_ITERATIONS * ctypes.c_double), + ("acceleration", TREL_MAX_ITERATIONS * ctypes.c_double), + ("force_balance", TREL_MAX_ITERATIONS * ctypes.c_double), + ("drag", TREL_MAX_ITERATIONS * ctypes.c_double), + ("weight", TREL_MAX_ITERATIONS * ctypes.c_double), + ("mass", TREL_MAX_ITERATIONS * ctypes.c_double), + ("force", TREL_MAX_ITERATIONS * ctypes.c_double), + ("time", TREL_MAX_ITERATIONS * ctypes.c_double), + ("max_height", ctypes.c_double), + ("max_position", ctypes.c_double), + ("max_speed", ctypes.c_double), + ("max_acceleration", ctypes.c_double), + ("max_force_balance", ctypes.c_double), + ("max_drag", ctypes.c_double)] + +# Struct and constructor for rocket +class TREL_ROCKET(ctypes.Structure): + _fields_ = [("engine", ctypes.POINTER(ctypes.POINTER(TREL_ENGINE))), + ("telemetry_mass", ctypes.c_double), + ("parachute_mass", ctypes.c_double), + ("fuselage_mass", ctypes.c_double), + ("payload_mass", ctypes.c_double), + ("initial_height", ctypes.c_double), + ("avg_thrust", ctypes.c_double), + ("max_thrust", ctypes.c_double), + ("delta_v", ctypes.c_double), + ("max_pressure", ctypes.c_double), + ("sim_latitude", ctypes.c_double), + ("body_diameter", ctypes.c_double), + ("drag_coefficient", ctypes.c_double), + ("sim_values", ctypes.POINTER(TREL_HEIGHT_SIM))] + +trel_rocket_init = TREL.trel_rocket_init +trel_rocket_init.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ENGINE)), + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double] +trel_rocket_init.restype = ctypes.c_void_p + +def trel_print_rocket(rocket): + print("\nGrains data") + print("amount", rocket.engine.contents.contents.grains.contents.contents.amount) + print("init_inter_radius", rocket.engine.contents.contents.grains.contents.contents.init_inter_radius) + print("extern_radius", rocket.engine.contents.contents.grains.contents.contents.extern_radius) + print("longitude", rocket.engine.contents.contents.grains.contents.contents.longitude) + print("grain_separation", rocket.engine.contents.contents.grains.contents.contents.grain_separation) + + print("\nFuel data") + print("const_burn_rate", rocket.engine.contents.contents.fuel.contents.contents.const_burn_rate) + print("pressure_exponent", rocket.engine.contents.contents.fuel.contents.contents.pressure_exponent) + print("density", rocket.engine.contents.contents.fuel.contents.contents.density) + print("burn_rate", rocket.engine.contents.contents.fuel.contents.contents.burn_rate) + + print("\nScrews data") + print("diameter", rocket.engine.contents.contents.screws.contents.contents.diameter) + print("dist_center_wall", rocket.engine.contents.contents.screws.contents.contents.dist_center_wall) + print("area_per_screw", rocket.engine.contents.contents.screws.contents.contents.area_per_screw) + print("screw_occupied_area", rocket.engine.contents.contents.screws.contents.contents.screw_occupied_area) + print("width_cutting_segment", rocket.engine.contents.contents.screws.contents.contents.width_cutting_segment) + print("material", rocket.engine.contents.contents.screws.contents.contents.material.decode('ASCII')) + print("amount", rocket.engine.contents.contents.screws.contents.contents.amount) + + print("\nTubing data") + print("material", rocket.engine.contents.contents.tube.contents.contents.material.decode('ASCII')) + print("diameter_ext", rocket.engine.contents.contents.tube.contents.contents.diameter_ext) + print("wall_thickness", rocket.engine.contents.contents.tube.contents.contents.wall_thickness) + print("internal_radius", rocket.engine.contents.contents.tube.contents.contents.internal_radius) + print("young_module", rocket.engine.contents.contents.tube.contents.contents.young_module) + print("sector_angle", rocket.engine.contents.contents.tube.contents.contents.sector_angle) + print("mean_tubing_diameter", rocket.engine.contents.contents.tube.contents.contents.mean_tubing_diameter) + print("shear_stress_tension", rocket.engine.contents.contents.tube.contents.contents.shear_stress_tension) + print("shear_stress_pressure", rocket.engine.contents.contents.tube.contents.contents.shear_stress_pressure) + print("ult_stress_tension", rocket.engine.contents.contents.tube.contents.contents.ult_stress_tension) + print("ult_stress_pressure", rocket.engine.contents.contents.tube.contents.contents.ult_stress_pressure) + print("transversal_area", rocket.engine.contents.contents.tube.contents.contents.transversal_area) + print("material_area", rocket.engine.contents.contents.tube.contents.contents.material_area) + print("nozzle_efficiency", rocket.engine.contents.contents.tube.contents.contents.nozzle_efficiency) + + print("\nEngine data") + print("engine_mass", rocket.engine.contents.contents.engine_mass) + print("pressure", rocket.engine.contents.contents.pressure) + print("escape_vel", rocket.engine.contents.contents.escape_vel) + print("temperature", rocket.engine.contents.contents.temperature) + print("width_condition", rocket.engine.contents.contents.width_condition) + print("margin_of_safety", rocket.engine.contents.contents.margin_of_safety) + print("max_stress", rocket.engine.contents.contents.max_stress) + print("radial_stress", rocket.engine.contents.contents.radial_stress) + print("tangencial_stress", rocket.engine.contents.contents.tangencial_stress) + print("longitudinal_stress", rocket.engine.contents.contents.longitudinal_stress) + print("max_pressue", rocket.engine.contents.contents.max_pressue) + print("nozzle_efficiency", rocket.engine.contents.contents.nozzle_efficiency) + + print("\nComp area data") + print("avg_long_area", rocket.engine.contents.contents.comp_area_values.contents.avg_long_area) + print("avg_trans_area", rocket.engine.contents.contents.comp_area_values.contents.avg_trans_area) + print("avg_burn_area", rocket.engine.contents.contents.comp_area_values.contents.avg_burn_area) + print("burn_std_deviation", rocket.engine.contents.contents.comp_area_values.contents.burn_std_deviation) + print("burn_sum_diff", rocket.engine.contents.contents.comp_area_values.contents.burn_sum_diff) + + print("\nRocket data") + print("telemetry_mass", rocket.telemetry_mass) + print("parachute_mass", rocket.parachute_mass) + print("fuselage_mass", rocket.fuselage_mass) + print("payload_mass", rocket.payload_mass) + print("initial_height", rocket.initial_height) + print("avg_thrust", rocket.avg_thrust) + print("max_thrust", rocket.max_thrust) + print("delta_v", rocket.delta_v) + print("max_pressure", rocket.max_pressure) + print("sim_latitude", rocket.sim_latitude) + print("body_diameter", rocket.body_diameter) + print("drag_coefficient", rocket.drag_coefficient) + + print("\nHeight sim data") + print("rocket_position list with %i items" % (len(list(rocket.sim_values.contents.position)))) + print("rocket_speed list with %i items" % (len(list(rocket.sim_values.contents.speed)))) + print("rocket_acceleration list with %i items" % (len(list(rocket.sim_values.contents.acceleration)))) + print("rocket_force_balance list with %i items" % (len(list(rocket.sim_values.contents.force_balance)))) + print("rocket_drag list with %i items" % (len(list(rocket.sim_values.contents.drag)))) + print("rocket_weight list with %i items" % (len(list(rocket.sim_values.contents.weight)))) + print("rocket_mass list with %i items" % (len(list(rocket.sim_values.contents.mass)))) + print("rocket_force list with %i items" % (len(list(rocket.sim_values.contents.force)))) + print("time list with %i items" % (len(list(rocket.sim_values.contents.time)))) + print("max_height", rocket.sim_values.contents.max_height) + print("max_position", rocket.sim_values.contents.max_position) + print("max_speed", rocket.sim_values.contents.max_speed) + print("max_acceleration", rocket.sim_values.contents.max_acceleration) + print("max_force_balance", rocket.sim_values.contents.max_force_balance) + print("max_drag", rocket.sim_values.contents.max_drag) diff --git a/Interfaces/Python/main.py b/Interfaces/Python/main.py index 3b2a2d7..f6d65ec 100644 --- a/Interfaces/Python/main.py +++ b/Interfaces/Python/main.py @@ -1,4 +1,4 @@ -from TREL import * +from TREL import * created_grains = TREL_GRAINS.from_address(trel_grains_init( 4, @@ -7,24 +7,57 @@ 0.1, 0.001 )) +p_created_grains = ctypes.pointer(created_grains) +p_p_created_grains = ctypes.pointer(p_created_grains) created_fuel = TREL_FUEL.from_address(trel_fuel_init( - 0.005, - 0.688, - 1859)) + 0.005, + 0.688, + 1859)) +p_created_fuel = ctypes.pointer(created_fuel) +p_p_created_fuel = ctypes.pointer(p_created_fuel) created_screw = TREL_SCREWS.from_address(trel_screws_init( - 6, - 0.007466, - 0.01)) + b"Acero", + 6, + 0.007466, + 0.01)) +p_created_screw = ctypes.pointer(created_screw) +p_p_created_screw = ctypes.pointer(p_created_screw) created_tube = TREL_TUBING.from_address(trel_tubing_init( + b"Aluminio 6061-T6", 0.073, 0.0052, 68900000000.0, 275000000.0, -1, 310000000, - 205000000)) + 205000000, + 0.85)) +p_created_tube = ctypes.pointer(created_tube) +p_p_created_tube = ctypes.pointer(p_created_tube) -print(created_tube.wall_thickness) +created_engine = TREL_ENGINE.from_address(trel_engine_init( + 800.0, + 1710.0, + 1.5, + p_p_created_grains, + p_p_created_fuel, + p_p_created_tube, + p_p_created_screw)) +p_created_engine = ctypes.pointer(created_engine) +p_p_created_engine = ctypes.pointer(p_created_engine) + +created_rocket = TREL_ROCKET.from_address(trel_rocket_init( + p_p_created_engine, + 0.3, + 0.05, + 5.0, + 1.5, + 1207.0, + 9.936111, + 0.13, + 0.4)) + +trel_print_rocket(created_rocket) From 0b1db7a6d7cfafc4c5ad5fec731c7d9e30b6c093 Mon Sep 17 00:00:00 2001 From: lross2k Date: Tue, 25 Apr 2023 17:45:59 -0600 Subject: [PATCH 34/42] Implemented memory functions ~lross2k~ --- Interfaces/Python/TREL.py | 33 ++++++++++++++++++++ Interfaces/Python/{main.py => manualtest.py} | 29 +++++++++++++++++ 2 files changed, 62 insertions(+) rename Interfaces/Python/{main.py => manualtest.py} (65%) diff --git a/Interfaces/Python/TREL.py b/Interfaces/Python/TREL.py index d3c5cbd..8a483a3 100644 --- a/Interfaces/Python/TREL.py +++ b/Interfaces/Python/TREL.py @@ -169,6 +169,39 @@ class TREL_ROCKET(ctypes.Structure): ctypes.c_double] trel_rocket_init.restype = ctypes.c_void_p +trel_run_area_comp_iterations = TREL.trel_run_area_comp_iterations +trel_run_area_comp_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ENGINE))] + +trel_run_time_comp_iterations = TREL.trel_run_time_comp_iterations +trel_run_time_comp_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] + +trel_run_height_sim_iterations = TREL.trel_run_height_sim_iterations +trel_run_height_sim_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] + +trel_grains_free = TREL.trel_grains_free +trel_grains_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_GRAINS))] +trel_grains_free.restype = ctypes.c_void_p + +trel_fuel_free = TREL.trel_fuel_free +trel_fuel_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_FUEL))] +trel_fuel_free.restype = ctypes.c_void_p + +trel_tubing_free = TREL.trel_tubing_free +trel_tubing_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_TUBING))] +trel_tubing_free.restype = ctypes.c_void_p + +trel_screws_free = TREL.trel_screws_free +trel_screws_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_SCREWS))] +trel_screws_free.restype = ctypes.c_void_p + +trel_engine_free = TREL.trel_engine_free +trel_engine_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ENGINE))] +trel_engine_free.restype = ctypes.c_void_p + +trel_rocket_free = TREL.trel_rocket_free +trel_rocket_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] +trel_rocket_free.restype = ctypes.c_void_p + def trel_print_rocket(rocket): print("\nGrains data") print("amount", rocket.engine.contents.contents.grains.contents.contents.amount) diff --git a/Interfaces/Python/main.py b/Interfaces/Python/manualtest.py similarity index 65% rename from Interfaces/Python/main.py rename to Interfaces/Python/manualtest.py index f6d65ec..185eff4 100644 --- a/Interfaces/Python/main.py +++ b/Interfaces/Python/manualtest.py @@ -59,5 +59,34 @@ 9.936111, 0.13, 0.4)) +p_created_rocket = ctypes.pointer(created_rocket) +p_p_created_rocket = ctypes.pointer(p_created_rocket) + +#trel_print_rocket(created_rocket) + +trel_run_area_comp_iterations(p_p_created_engine) +trel_run_time_comp_iterations(p_p_created_rocket) +trel_run_height_sim_iterations(p_p_created_rocket) + +#trel_grains_free(p_p_created_grains) +#trel_fuel_free(p_p_created_fuel) +#trel_tubing_free(p_p_created_tube) +#trel_screws_free(p_p_created_screw) +#trel_engine_free(p_p_created_engine) +#trel_rocket_free(p_p_created_rocket) + +for x in range(0,10000): + print(x) + trel_rocket_init( + p_p_created_engine, + 0.3, + 0.05, + 5.0, + 1.5, + 1207.0, + 9.936111, + 0.13, + 0.4 + ) trel_print_rocket(created_rocket) From 894ceaa5400f274a5042effe6586fabb403674f2 Mon Sep 17 00:00:00 2001 From: lross2k Date: Tue, 25 Apr 2023 17:47:27 -0600 Subject: [PATCH 35/42] Changed an insignificant funtion parameter Maybe insignificant, but this could mean lots of trouble in the future, so I tought it'd be better to do it now Passing trel_rocket_t ** instead of trel_rocket_t * ~lross2k~ --- include/comp_tiempo.h | 2 +- src/comp_tiempo.c | 15 ++++++++------- src/rendimientos.c | 2 +- src/rocket.c | 20 +++++++++++--------- tests/TREL.h | 4 ++-- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/include/comp_tiempo.h b/include/comp_tiempo.h index 7acb16f..513e534 100644 --- a/include/comp_tiempo.h +++ b/include/comp_tiempo.h @@ -3,6 +3,6 @@ #include "design_func.h" -int TREL_EXPORT trel_run_time_comp_iterations(trel_rocket_t* rocket); +int TREL_EXPORT trel_run_time_comp_iterations(trel_rocket_t **rocket); #endif /*COMP_TIEMPO_H */ diff --git a/src/comp_tiempo.c b/src/comp_tiempo.c index 3e1a8bc..6e16bad 100644 --- a/src/comp_tiempo.c +++ b/src/comp_tiempo.c @@ -13,9 +13,10 @@ double calc_iter_internal_mass(double produced_mass, double internal_mass, doubl } /*Comportamiento en el Tiempo!AD3511 */ -int trel_run_time_comp_iterations(trel_rocket_t* rocket) +int trel_run_time_comp_iterations(trel_rocket_t **rocket) { - engine_t* engine = (*rocket->engine); + trel_rocket_t *p_rocket = *rocket; + engine_t *engine = (*p_rocket->engine); /* iterate over given iterations and step size */ const double max_iterations = TREL_MAX_ITERATIONS; @@ -27,7 +28,7 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) ((*engine->grains)->grain_separation * ((*engine->grains)->amount - 1) + (*engine->grains)->longitude * (*engine->grains)->amount); /* variables for iteration */ - double dead_mass = rocket->fuselage_mass + rocket->parachute_mass + rocket->telemetry_mass + engine->engine_mass + rocket->payload_mass; + double dead_mass = p_rocket->fuselage_mass + p_rocket->parachute_mass + p_rocket->telemetry_mass + engine->engine_mass + p_rocket->payload_mass; const double delta_t = step; double pressure_abs = TREL_ATMOSPHERIC_PRESSURE; /*double pressure = pressure_abs / 6894.757f; */ @@ -117,11 +118,11 @@ int trel_run_time_comp_iterations(trel_rocket_t* rocket) } /* storing values */ - rocket->delta_v = sum_delta_v; - rocket->max_thrust = max_thrust; - rocket->max_pressure = max_pressure; + p_rocket->delta_v = sum_delta_v; + p_rocket->max_thrust = max_thrust; + p_rocket->max_pressure = max_pressure; /* calculate avg thrust */ - rocket->avg_thrust = sum_avg_thrust / max_iterations; + p_rocket->avg_thrust = sum_avg_thrust / max_iterations; return(0); } diff --git a/src/rendimientos.c b/src/rendimientos.c index 6e6f110..456f116 100644 --- a/src/rendimientos.c +++ b/src/rendimientos.c @@ -55,7 +55,7 @@ double emp_teo(engine_t *engine) double emp_prom(trel_rocket_t* rocket) { if (!rocket->avg_thrust) - trel_run_time_comp_iterations(rocket); + trel_run_time_comp_iterations(&rocket); return(rocket->avg_thrust); } diff --git a/src/rocket.c b/src/rocket.c index d77cea4..847239f 100644 --- a/src/rocket.c +++ b/src/rocket.c @@ -54,7 +54,7 @@ trel_rocket_t *trel_rocket_init( rocket->body_diameter = body_diameter; rocket->drag_coefficient = drag_coefficient; rocket->sim_values = sim_values; - if (trel_run_time_comp_iterations(rocket)) /* 1 means there was an error */ + if (trel_run_time_comp_iterations(&rocket)) /* 1 means there was an error */ { return(NULL); /* NULL means the engine couldn't be properly generated */ } @@ -74,14 +74,16 @@ void trel_height_sim_free(trel_height_sim_t **values) void trel_rocket_free(trel_rocket_t **rocket) { if (*rocket != NULL) - if ((*rocket)->sim_values != NULL) { - free((*rocket)->sim_values); - (*rocket)->sim_values = NULL; - } - trel_engine_free((*rocket)->engine); - { - free(*rocket); - *rocket = NULL; + if ((*rocket)->sim_values != NULL) + { + free((*rocket)->sim_values); + (*rocket)->sim_values = NULL; + } + trel_engine_free((*rocket)->engine); + { + free(*rocket); + *rocket = NULL; + } } } diff --git a/tests/TREL.h b/tests/TREL.h index 50411a3..2d3c508 100644 --- a/tests/TREL.h +++ b/tests/TREL.h @@ -143,8 +143,8 @@ double trel_get_pressure(engine_t* engine); double trel_temper_garganta(engine_t *engine); double br_combustion(engine_t *engine); int trel_run_area_comp_iterations(engine_t **engine); -int trel_run_time_comp_iterations(trel_rocket_t* rocket); -int trel_run_height_sim_iterations(trel_rocket_t** rocket); +int trel_run_time_comp_iterations(trel_rocket_t **rocket); +int trel_run_height_sim_iterations(trel_rocket_t **rocket); void trel_grains_free(grains_t** grains); void trel_fuel_free(fuel_t** fuel); From 181058185cbe72e115f919fd30d5c522a1fe3924 Mon Sep 17 00:00:00 2001 From: lross2k Date: Tue, 25 Apr 2023 19:34:27 -0600 Subject: [PATCH 36/42] Many changes, no time to loose anything HIGH level of abstraction in progress ~lross2k~ --- Interfaces/Python/HIGH_TREL.py | 75 +++++++++++++++++++++++++++++ Interfaces/Python/TREL.py | 72 +++++++++++++-------------- Interfaces/Python/highmanualtest.py | 28 +++++++++++ Interfaces/Python/manualtest.py | 17 +------ 4 files changed, 141 insertions(+), 51 deletions(-) create mode 100644 Interfaces/Python/HIGH_TREL.py create mode 100644 Interfaces/Python/highmanualtest.py diff --git a/Interfaces/Python/HIGH_TREL.py b/Interfaces/Python/HIGH_TREL.py new file mode 100644 index 0000000..0ecd3b0 --- /dev/null +++ b/Interfaces/Python/HIGH_TREL.py @@ -0,0 +1,75 @@ +import ctypes +from TREL import _trel_grains_init, _TREL_GRAINS_T, _trel_grains_free +from TREL import _trel_fuel_init, _TREL_FUEL_T, _trel_fuel_free +from TREL import _trel_screws_init, _TREL_SCREWS_T, _trel_screws_free +from TREL import _trel_tubing_init, _TREL_TUBING_T, _trel_tubing_free + +class TREL_GRAINS(): + def __init__(self, amount, internal_radius, external_radius, longitude, separation): + self.values = _TREL_GRAINS_T.from_address(_trel_grains_init( + amount, internal_radius, external_radius, longitude, separation)) + self._p_grains_instance = ctypes.pointer(self.values) + self._p_p_grains_instance = ctypes.pointer(self._p_grains_instance) + self.allocated = True + def free(self): + _trel_grains_free(self._p_p_grains_instance) + self.allocated = False + def __repr__(self): + return('Amount: %f\nInternal radius: %f\nExternal radius: %f\nLongitude: %f\nSeparation: %f' % ( + self.values.amount, self.values.internal_radius, self.values.external_radius, self.values.longitude, self.values.separation)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_FUEL(): + def __init__(self, c_burn_rate, pressure_exponent, density): + self.values = _TREL_FUEL_T.from_address(_trel_fuel_init( + c_burn_rate, pressure_exponent, density)) + self._p_fuel_instance = ctypes.pointer(self.values) + self._p_p_fuel_instance = ctypes.pointer(self._p_fuel_instance) + self.allocated = True + def free(self): + _trel_fuel_free(self._p_p_fuel_instance) + self.allocated = False + def __repr__(self): + return('Const Burn Rate: %f\nPressure Exponent: %f\nDensity: %f\nBurn Rate: %f' % ( + self.values.c_burn_rate, self.values.pressure_exponent, self.values.density, self.values.burn_rate)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_SCREWS(): + def __init__(self, material_name, amount, diameter, dist_center_wall): + if len(material_name) > 99: + print("ERROR: TREL's materials can't take more than 99 characters") + return(None) + self.values = _TREL_SCREWS_T.from_address(_trel_screws_init( + bytes(material_name, 'utf8'), amount, diameter, dist_center_wall)) + self._p_screws_instance = ctypes.pointer(self.values) + self._p_p_screws_instance = ctypes.pointer(self._p_screws_instance) + self.allocated = True + def free(self): + _trel_screws_free(self._p_p_screws_instance) + self.allocated = False + def __repr__(self): + return('Material: %s\nAmount: %f\nDiameter: %f\nWall Center Distance: %f\nArea Per Screw:%f\nScrew Occupied Area: %f\nWidth Cutting Segment %f' % ( + self.values.material_name, self.values.amount, self.values.diameter, self.values.dist_center_wall, self.values.area_per_screw, self.values.screw_occupied_area, self.values.width_cutting_segment)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_TUBING(): + def __init__(self, material_name, external_diameter, thickness, young_module, shear_tension, ult_tension, ult_pressure, nozzle_efficiency): + if len(material_name) > 99: + print("ERROR: TREL's materials can't take more than 99 characters") + return(None) + self.valores = _TREL_TUBING_T.from_address(_trel_tubing_init( + bytes(material_name, 'utf8'), external_diameter, thickness, young_module, shear_tension, ult_tension, ult_pressure, nozzle_efficiency)) + self._p_tubing_instance = ctypes.pointer(self.valores) + self._p_p_tubing_instance = ctypes.pointer(self._p_tubing_instance) + self.allocated = True + def free(self): + _trel_tubing_free(self._p_p_tubing_instance) + self.allocated = False + def __repr__(self): + return('Material: %s\nExternal Diameter: %f\nWall Thickness: %f\nInternal Radius: %f\n' % ( + self.material, )) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') diff --git a/Interfaces/Python/TREL.py b/Interfaces/Python/TREL.py index 8a483a3..d78a55b 100644 --- a/Interfaces/Python/TREL.py +++ b/Interfaces/Python/TREL.py @@ -18,36 +18,36 @@ class TREL_COMP_AREA(ctypes.Structure): ("burn_sum_diff", ctypes.c_double)] # Struct and constructor for propellent grains -class TREL_GRAINS(ctypes.Structure): +class _TREL_GRAINS_T(ctypes.Structure): _fields_ = [("amount", ctypes.c_uint), ("init_inter_radius", ctypes.c_double), ("extern_radius", ctypes.c_double), ("longitude", ctypes.c_double), ("grain_separation", ctypes.c_double)] -trel_grains_init = TREL.trel_grains_init -trel_grains_init.argtypes = [ctypes.c_int, +_trel_grains_init = TREL.trel_grains_init +_trel_grains_init.argtypes = [ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double] -trel_grains_init.restype = ctypes.c_void_p +_trel_grains_init.restype = ctypes.c_void_p # Struct and constructor for fuel -class TREL_FUEL(ctypes.Structure): +class _TREL_FUEL_T(ctypes.Structure): _fields_ = [("const_burn_rate", ctypes.c_double), ("pressure_exponent", ctypes.c_double), ("density", ctypes.c_double), ("burn_rate", ctypes.c_double)] -trel_fuel_init = TREL.trel_fuel_init -trel_fuel_init.argtypes = [ctypes.c_double, +_trel_fuel_init = TREL.trel_fuel_init +_trel_fuel_init.argtypes = [ctypes.c_double, ctypes.c_double, ctypes.c_double] -trel_fuel_init.restype = ctypes.c_void_p +_trel_fuel_init.restype = ctypes.c_void_p # Struct and constructor for screws -class TREL_SCREWS(ctypes.Structure): +class _TREL_SCREWS_T(ctypes.Structure): _fields_ = [("diameter", ctypes.c_double), ("dist_center_wall", ctypes.c_double), ("amount", ctypes.c_uint), @@ -56,15 +56,15 @@ class TREL_SCREWS(ctypes.Structure): ("material", TREL_MAX_STR_LEN * ctypes.c_char), ("width_cutting_segment", ctypes.c_double)] -trel_screws_init = TREL.trel_screws_init -trel_screws_init.argtypes = [ctypes.c_char_p, +_trel_screws_init = TREL.trel_screws_init +_trel_screws_init.argtypes = [ctypes.c_char_p, ctypes.c_uint, ctypes.c_double, ctypes.c_double] -trel_screws_init.restype = ctypes.c_void_p +_trel_screws_init.restype = ctypes.c_void_p # Struct and constructor for tubing -class TREL_TUBING(ctypes.Structure): +class _TREL_TUBING_T(ctypes.Structure): _fields_ = [("material", TREL_MAX_STR_LEN * ctypes.c_char), ("diameter_ext", ctypes.c_double), ("wall_thickness", ctypes.c_double), @@ -80,8 +80,8 @@ class TREL_TUBING(ctypes.Structure): ("material_area", ctypes.c_double), ("nozzle_efficiency", ctypes.c_double)] -trel_tubing_init = TREL.trel_tubing_init -trel_tubing_init.argtypes = [ctypes.c_char_p, +_trel_tubing_init = TREL.trel_tubing_init +_trel_tubing_init.argtypes = [ctypes.c_char_p, ctypes.c_double, ctypes.c_double, ctypes.c_double, @@ -90,7 +90,7 @@ class TREL_TUBING(ctypes.Structure): ctypes.c_double, ctypes.c_double, ctypes.c_double] -trel_tubing_init.restype = ctypes.c_void_p +_trel_tubing_init.restype = ctypes.c_void_p # Struct and constructor for engine class TREL_ENGINE(ctypes.Structure): @@ -106,20 +106,20 @@ class TREL_ENGINE(ctypes.Structure): ("longitudinal_stress", ctypes.c_double), ("max_pressue", ctypes.c_double), ("nozzle_efficiency", ctypes.c_double), - ("grains", ctypes.POINTER(ctypes.POINTER(TREL_GRAINS))), - ("fuel", ctypes.POINTER(ctypes.POINTER(TREL_FUEL))), - ("tube", ctypes.POINTER(ctypes.POINTER(TREL_TUBING))), - ("screws", ctypes.POINTER(ctypes.POINTER(TREL_SCREWS))), + ("grains", ctypes.POINTER(ctypes.POINTER(_TREL_GRAINS_T))), + ("fuel", ctypes.POINTER(ctypes.POINTER(_TREL_FUEL_T))), + ("tube", ctypes.POINTER(ctypes.POINTER(_TREL_TUBING_T))), + ("screws", ctypes.POINTER(ctypes.POINTER(_TREL_SCREWS_T))), ("comp_area_values", ctypes.POINTER(TREL_COMP_AREA))] trel_engine_init = TREL.trel_engine_init trel_engine_init.argtypes = [ctypes.c_double, ctypes.c_double, ctypes.c_double, - ctypes.POINTER(ctypes.POINTER(TREL_GRAINS)), - ctypes.POINTER(ctypes.POINTER(TREL_FUEL)), - ctypes.POINTER(ctypes.POINTER(TREL_TUBING)), - ctypes.POINTER(ctypes.POINTER(TREL_SCREWS))] + ctypes.POINTER(ctypes.POINTER(_TREL_GRAINS_T)), + ctypes.POINTER(ctypes.POINTER(_TREL_FUEL_T)), + ctypes.POINTER(ctypes.POINTER(_TREL_TUBING_T)), + ctypes.POINTER(ctypes.POINTER(_TREL_SCREWS_T))] trel_engine_init.restype = ctypes.c_void_p # Struct for sim values @@ -178,21 +178,21 @@ class TREL_ROCKET(ctypes.Structure): trel_run_height_sim_iterations = TREL.trel_run_height_sim_iterations trel_run_height_sim_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] -trel_grains_free = TREL.trel_grains_free -trel_grains_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_GRAINS))] -trel_grains_free.restype = ctypes.c_void_p +_trel_grains_free = TREL.trel_grains_free +_trel_grains_free.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_GRAINS_T))] +_trel_grains_free.restype = ctypes.c_void_p -trel_fuel_free = TREL.trel_fuel_free -trel_fuel_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_FUEL))] -trel_fuel_free.restype = ctypes.c_void_p +_trel_fuel_free = TREL.trel_fuel_free +_trel_fuel_free.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_FUEL_T))] +_trel_fuel_free.restype = ctypes.c_void_p -trel_tubing_free = TREL.trel_tubing_free -trel_tubing_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_TUBING))] -trel_tubing_free.restype = ctypes.c_void_p +_trel_tubing_free = TREL.trel_tubing_free +_trel_tubing_free.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_TUBING_T))] +_trel_tubing_free.restype = ctypes.c_void_p -trel_screws_free = TREL.trel_screws_free -trel_screws_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_SCREWS))] -trel_screws_free.restype = ctypes.c_void_p +_trel_screws_free = TREL.trel_screws_free +_trel_screws_free.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_SCREWS_T))] +_trel_screws_free.restype = ctypes.c_void_p trel_engine_free = TREL.trel_engine_free trel_engine_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ENGINE))] diff --git a/Interfaces/Python/highmanualtest.py b/Interfaces/Python/highmanualtest.py new file mode 100644 index 0000000..0521518 --- /dev/null +++ b/Interfaces/Python/highmanualtest.py @@ -0,0 +1,28 @@ +from HIGH_TREL import * + +engine_grains = TREL_GRAINS( + 4, + 0.01, + 0.0304, + 0.1, + 0.001 +) +engine_fuel = TREL_FUEL( + 0.005, + 0.688, + 1859 +) +engine_screws = TREL_SCREWS( + 'Acero', + 6, + 0.007466, + 0.01 +) + +print(f'Grains\n{engine_grains}') +print(f'Fuel\n{engine_fuel}') +print(f'Screws\n{engine_screws}') + +engine_grains.free() +engine_fuel.free() +engine_screws.free() diff --git a/Interfaces/Python/manualtest.py b/Interfaces/Python/manualtest.py index 185eff4..9580f6f 100644 --- a/Interfaces/Python/manualtest.py +++ b/Interfaces/Python/manualtest.py @@ -1,6 +1,7 @@ from TREL import * +from TREL import _TREL_GRAINS_T -created_grains = TREL_GRAINS.from_address(trel_grains_init( +created_grains = _TREL_GRAINS_T.from_address(trel_grains_init( 4, 0.01, 0.0304, @@ -75,18 +76,4 @@ #trel_engine_free(p_p_created_engine) #trel_rocket_free(p_p_created_rocket) -for x in range(0,10000): - print(x) - trel_rocket_init( - p_p_created_engine, - 0.3, - 0.05, - 5.0, - 1.5, - 1207.0, - 9.936111, - 0.13, - 0.4 - ) - trel_print_rocket(created_rocket) From 117ce85f70367b23153a5a1a0d743767aad2fd01 Mon Sep 17 00:00:00 2001 From: lross2k Date: Tue, 25 Apr 2023 20:09:48 -0600 Subject: [PATCH 37/42] Almost done with the Python interface Found an issue, a field in engine is max_pressue instead of max_pressure ~lross2k~ --- Interfaces/Python/HIGH_TREL.py | 57 +++++++++++++++++++---------- Interfaces/Python/TREL.py | 20 +++++----- Interfaces/Python/highmanualtest.py | 24 ++++++++++++ 3 files changed, 71 insertions(+), 30 deletions(-) diff --git a/Interfaces/Python/HIGH_TREL.py b/Interfaces/Python/HIGH_TREL.py index 0ecd3b0..d25a87d 100644 --- a/Interfaces/Python/HIGH_TREL.py +++ b/Interfaces/Python/HIGH_TREL.py @@ -3,20 +3,21 @@ from TREL import _trel_fuel_init, _TREL_FUEL_T, _trel_fuel_free from TREL import _trel_screws_init, _TREL_SCREWS_T, _trel_screws_free from TREL import _trel_tubing_init, _TREL_TUBING_T, _trel_tubing_free +from TREL import _trel_engine_init, _TREL_ENGINE_T, _trel_engine_free class TREL_GRAINS(): def __init__(self, amount, internal_radius, external_radius, longitude, separation): self.values = _TREL_GRAINS_T.from_address(_trel_grains_init( amount, internal_radius, external_radius, longitude, separation)) - self._p_grains_instance = ctypes.pointer(self.values) - self._p_p_grains_instance = ctypes.pointer(self._p_grains_instance) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) self.allocated = True def free(self): - _trel_grains_free(self._p_p_grains_instance) + _trel_grains_free(self._p_p) self.allocated = False def __repr__(self): return('Amount: %f\nInternal radius: %f\nExternal radius: %f\nLongitude: %f\nSeparation: %f' % ( - self.values.amount, self.values.internal_radius, self.values.external_radius, self.values.longitude, self.values.separation)) + self.values.amount, self.values.init_inter_radius, self.values.extern_radius, self.values.longitude, self.values.grain_separation)) def __str__(self): return(self.__repr__() if self.allocated else 'NULL') @@ -24,15 +25,15 @@ class TREL_FUEL(): def __init__(self, c_burn_rate, pressure_exponent, density): self.values = _TREL_FUEL_T.from_address(_trel_fuel_init( c_burn_rate, pressure_exponent, density)) - self._p_fuel_instance = ctypes.pointer(self.values) - self._p_p_fuel_instance = ctypes.pointer(self._p_fuel_instance) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) self.allocated = True def free(self): - _trel_fuel_free(self._p_p_fuel_instance) + _trel_fuel_free(self._p_p) self.allocated = False def __repr__(self): return('Const Burn Rate: %f\nPressure Exponent: %f\nDensity: %f\nBurn Rate: %f' % ( - self.values.c_burn_rate, self.values.pressure_exponent, self.values.density, self.values.burn_rate)) + self.values.const_burn_rate, self.values.pressure_exponent, self.values.density, self.values.burn_rate)) def __str__(self): return(self.__repr__() if self.allocated else 'NULL') @@ -43,33 +44,49 @@ def __init__(self, material_name, amount, diameter, dist_center_wall): return(None) self.values = _TREL_SCREWS_T.from_address(_trel_screws_init( bytes(material_name, 'utf8'), amount, diameter, dist_center_wall)) - self._p_screws_instance = ctypes.pointer(self.values) - self._p_p_screws_instance = ctypes.pointer(self._p_screws_instance) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) self.allocated = True def free(self): - _trel_screws_free(self._p_p_screws_instance) + _trel_screws_free(self._p_p) self.allocated = False def __repr__(self): return('Material: %s\nAmount: %f\nDiameter: %f\nWall Center Distance: %f\nArea Per Screw:%f\nScrew Occupied Area: %f\nWidth Cutting Segment %f' % ( - self.values.material_name, self.values.amount, self.values.diameter, self.values.dist_center_wall, self.values.area_per_screw, self.values.screw_occupied_area, self.values.width_cutting_segment)) + self.values.material, self.values.amount, self.values.diameter, self.values.dist_center_wall, self.values.area_per_screw, self.values.screw_occupied_area, self.values.width_cutting_segment)) def __str__(self): return(self.__repr__() if self.allocated else 'NULL') class TREL_TUBING(): - def __init__(self, material_name, external_diameter, thickness, young_module, shear_tension, ult_tension, ult_pressure, nozzle_efficiency): + def __init__(self, material_name, external_diameter, thickness, young_module, shear_tension, shear_pressure, ult_tension, ult_pressure, nozzle_efficiency): if len(material_name) > 99: print("ERROR: TREL's materials can't take more than 99 characters") return(None) - self.valores = _TREL_TUBING_T.from_address(_trel_tubing_init( - bytes(material_name, 'utf8'), external_diameter, thickness, young_module, shear_tension, ult_tension, ult_pressure, nozzle_efficiency)) - self._p_tubing_instance = ctypes.pointer(self.valores) - self._p_p_tubing_instance = ctypes.pointer(self._p_tubing_instance) + self.values = _TREL_TUBING_T.from_address(_trel_tubing_init( + bytes(material_name, 'utf8'), external_diameter, thickness, young_module, shear_tension, shear_pressure, ult_tension, ult_pressure, nozzle_efficiency)) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) self.allocated = True def free(self): - _trel_tubing_free(self._p_p_tubing_instance) + _trel_tubing_free(self._p_p) self.allocated = False def __repr__(self): - return('Material: %s\nExternal Diameter: %f\nWall Thickness: %f\nInternal Radius: %f\n' % ( - self.material, )) + return('Material: %s\nExternal Diameter: %f\nWall Thickness: %f\nInternal Radius: %f\nYoung Module: %f\nSector Angle: %f\nMean Tubing Diameter: %f\nShear Stress Tension: %f\nShear Stress Pressure: %f\nUltimate Stress Tension: %f\nUltimate Stress Pressure: %f\nTransversal Area: %f\nMaterial Area %f\nNozzle Efficiency %f' % ( + self.values.material, self.values.diameter_ext, self.values.wall_thickness, self.values.internal_radius, self.values.young_module, self.values.sector_angle, self.values.mean_tubing_diameter, self.values.shear_stress_tension, self.values.shear_stress_pressure, self.values.ult_stress_tension, self.values.ult_stress_pressure, self.values.transversal_area, self.values.material_area, self.values.nozzle_efficiency)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_ENGINE(): + def __init__(self, pressure, temperature, mass, grains, fuel, tubing, screws): + self.values = _TREL_ENGINE_T.from_address(_trel_engine_init( + pressure, temperature, mass, grains._p_p, fuel._p_p, tubing._p_p, screws._p_p)) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) + self.allocated = True + def free(self): + _trel_engine_free(self._p_p) + self.allocated = False + def __repr__(self): + return('Engine Mass: %f\nPressue: %f\nEscape Velocity: %f\nTemperature %f\nWidth Condition: %f\nMargin of Safety: %f\nMaximum Stress: %f\nRadial Stress: %f\nTangencial Stress: %f\nLongitudinal Stress: %f\nMaximum Pressure: %f\nNozzle Efficiency: %f' % ( + self.values.engine_mass, self.values.pressure, self.values.escape_vel, self.values.temperature, self.values.width_condition, self.values.margin_of_safety, self.values.max_stress, self.values.radial_stress, self.values.tangencial_stress, self.values.longitudinal_stress, self.values.max_pressue, self.values.nozzle_efficiency)) def __str__(self): return(self.__repr__() if self.allocated else 'NULL') diff --git a/Interfaces/Python/TREL.py b/Interfaces/Python/TREL.py index d78a55b..8f7ffe2 100644 --- a/Interfaces/Python/TREL.py +++ b/Interfaces/Python/TREL.py @@ -93,7 +93,7 @@ class _TREL_TUBING_T(ctypes.Structure): _trel_tubing_init.restype = ctypes.c_void_p # Struct and constructor for engine -class TREL_ENGINE(ctypes.Structure): +class _TREL_ENGINE_T(ctypes.Structure): _fields_ = [("engine_mass", ctypes.c_double), ("pressure", ctypes.c_double), ("escape_vel", ctypes.c_double), @@ -112,15 +112,15 @@ class TREL_ENGINE(ctypes.Structure): ("screws", ctypes.POINTER(ctypes.POINTER(_TREL_SCREWS_T))), ("comp_area_values", ctypes.POINTER(TREL_COMP_AREA))] -trel_engine_init = TREL.trel_engine_init -trel_engine_init.argtypes = [ctypes.c_double, +_trel_engine_init = TREL.trel_engine_init +_trel_engine_init.argtypes = [ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.POINTER(ctypes.POINTER(_TREL_GRAINS_T)), ctypes.POINTER(ctypes.POINTER(_TREL_FUEL_T)), ctypes.POINTER(ctypes.POINTER(_TREL_TUBING_T)), ctypes.POINTER(ctypes.POINTER(_TREL_SCREWS_T))] -trel_engine_init.restype = ctypes.c_void_p +_trel_engine_init.restype = ctypes.c_void_p # Struct for sim values class TREL_HEIGHT_SIM(ctypes.Structure): @@ -142,7 +142,7 @@ class TREL_HEIGHT_SIM(ctypes.Structure): # Struct and constructor for rocket class TREL_ROCKET(ctypes.Structure): - _fields_ = [("engine", ctypes.POINTER(ctypes.POINTER(TREL_ENGINE))), + _fields_ = [("engine", ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T)))), ("telemetry_mass", ctypes.c_double), ("parachute_mass", ctypes.c_double), ("fuselage_mass", ctypes.c_double), @@ -158,7 +158,7 @@ class TREL_ROCKET(ctypes.Structure): ("sim_values", ctypes.POINTER(TREL_HEIGHT_SIM))] trel_rocket_init = TREL.trel_rocket_init -trel_rocket_init.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ENGINE)), +trel_rocket_init.argtypes = [ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T))), ctypes.c_double, ctypes.c_double, ctypes.c_double, @@ -170,7 +170,7 @@ class TREL_ROCKET(ctypes.Structure): trel_rocket_init.restype = ctypes.c_void_p trel_run_area_comp_iterations = TREL.trel_run_area_comp_iterations -trel_run_area_comp_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ENGINE))] +trel_run_area_comp_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T)))] trel_run_time_comp_iterations = TREL.trel_run_time_comp_iterations trel_run_time_comp_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] @@ -194,9 +194,9 @@ class TREL_ROCKET(ctypes.Structure): _trel_screws_free.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_SCREWS_T))] _trel_screws_free.restype = ctypes.c_void_p -trel_engine_free = TREL.trel_engine_free -trel_engine_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ENGINE))] -trel_engine_free.restype = ctypes.c_void_p +_trel_engine_free = TREL.trel_engine_free +_trel_engine_free.argtypes = [ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T)))] +_trel_engine_free.restype = ctypes.c_void_p trel_rocket_free = TREL.trel_rocket_free trel_rocket_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] diff --git a/Interfaces/Python/highmanualtest.py b/Interfaces/Python/highmanualtest.py index 0521518..b5d3234 100644 --- a/Interfaces/Python/highmanualtest.py +++ b/Interfaces/Python/highmanualtest.py @@ -18,11 +18,35 @@ 0.007466, 0.01 ) +engine_tubing = TREL_TUBING( + 'Aluminio 6061-T6', + 0.073, + 0.0052, + 68900000000.0, + 275000000.0, + -1, + 310000000, + 205000000, + 0.85 +) +engine = TREL_ENGINE( + 800.0, + 1710.0, + 1.5, + engine_grains, + engine_fuel, + engine_tubing, + engine_screws +) print(f'Grains\n{engine_grains}') print(f'Fuel\n{engine_fuel}') print(f'Screws\n{engine_screws}') +print(f'Tubing\n{engine_tubing}') +print(f'Engine\n{engine}') engine_grains.free() engine_fuel.free() engine_screws.free() +engine_tubing.free() +engine.free() From fb69a0a3c3ec0d53483400797237b5f0f54e9220 Mon Sep 17 00:00:00 2001 From: lross2k Date: Tue, 25 Apr 2023 20:40:07 -0600 Subject: [PATCH 38/42] It's done ~lross2k~ --- Interfaces/Python/HIGH_TREL.py | 92 ------------ Interfaces/Python/TREL.py | 213 +++++++++++++++------------- Interfaces/Python/highmanualtest.py | 16 ++- Interfaces/Python/manualtest.py | 79 ----------- 4 files changed, 128 insertions(+), 272 deletions(-) delete mode 100644 Interfaces/Python/HIGH_TREL.py delete mode 100644 Interfaces/Python/manualtest.py diff --git a/Interfaces/Python/HIGH_TREL.py b/Interfaces/Python/HIGH_TREL.py deleted file mode 100644 index d25a87d..0000000 --- a/Interfaces/Python/HIGH_TREL.py +++ /dev/null @@ -1,92 +0,0 @@ -import ctypes -from TREL import _trel_grains_init, _TREL_GRAINS_T, _trel_grains_free -from TREL import _trel_fuel_init, _TREL_FUEL_T, _trel_fuel_free -from TREL import _trel_screws_init, _TREL_SCREWS_T, _trel_screws_free -from TREL import _trel_tubing_init, _TREL_TUBING_T, _trel_tubing_free -from TREL import _trel_engine_init, _TREL_ENGINE_T, _trel_engine_free - -class TREL_GRAINS(): - def __init__(self, amount, internal_radius, external_radius, longitude, separation): - self.values = _TREL_GRAINS_T.from_address(_trel_grains_init( - amount, internal_radius, external_radius, longitude, separation)) - self._p = ctypes.pointer(self.values) - self._p_p = ctypes.pointer(self._p) - self.allocated = True - def free(self): - _trel_grains_free(self._p_p) - self.allocated = False - def __repr__(self): - return('Amount: %f\nInternal radius: %f\nExternal radius: %f\nLongitude: %f\nSeparation: %f' % ( - self.values.amount, self.values.init_inter_radius, self.values.extern_radius, self.values.longitude, self.values.grain_separation)) - def __str__(self): - return(self.__repr__() if self.allocated else 'NULL') - -class TREL_FUEL(): - def __init__(self, c_burn_rate, pressure_exponent, density): - self.values = _TREL_FUEL_T.from_address(_trel_fuel_init( - c_burn_rate, pressure_exponent, density)) - self._p = ctypes.pointer(self.values) - self._p_p = ctypes.pointer(self._p) - self.allocated = True - def free(self): - _trel_fuel_free(self._p_p) - self.allocated = False - def __repr__(self): - return('Const Burn Rate: %f\nPressure Exponent: %f\nDensity: %f\nBurn Rate: %f' % ( - self.values.const_burn_rate, self.values.pressure_exponent, self.values.density, self.values.burn_rate)) - def __str__(self): - return(self.__repr__() if self.allocated else 'NULL') - -class TREL_SCREWS(): - def __init__(self, material_name, amount, diameter, dist_center_wall): - if len(material_name) > 99: - print("ERROR: TREL's materials can't take more than 99 characters") - return(None) - self.values = _TREL_SCREWS_T.from_address(_trel_screws_init( - bytes(material_name, 'utf8'), amount, diameter, dist_center_wall)) - self._p = ctypes.pointer(self.values) - self._p_p = ctypes.pointer(self._p) - self.allocated = True - def free(self): - _trel_screws_free(self._p_p) - self.allocated = False - def __repr__(self): - return('Material: %s\nAmount: %f\nDiameter: %f\nWall Center Distance: %f\nArea Per Screw:%f\nScrew Occupied Area: %f\nWidth Cutting Segment %f' % ( - self.values.material, self.values.amount, self.values.diameter, self.values.dist_center_wall, self.values.area_per_screw, self.values.screw_occupied_area, self.values.width_cutting_segment)) - def __str__(self): - return(self.__repr__() if self.allocated else 'NULL') - -class TREL_TUBING(): - def __init__(self, material_name, external_diameter, thickness, young_module, shear_tension, shear_pressure, ult_tension, ult_pressure, nozzle_efficiency): - if len(material_name) > 99: - print("ERROR: TREL's materials can't take more than 99 characters") - return(None) - self.values = _TREL_TUBING_T.from_address(_trel_tubing_init( - bytes(material_name, 'utf8'), external_diameter, thickness, young_module, shear_tension, shear_pressure, ult_tension, ult_pressure, nozzle_efficiency)) - self._p = ctypes.pointer(self.values) - self._p_p = ctypes.pointer(self._p) - self.allocated = True - def free(self): - _trel_tubing_free(self._p_p) - self.allocated = False - def __repr__(self): - return('Material: %s\nExternal Diameter: %f\nWall Thickness: %f\nInternal Radius: %f\nYoung Module: %f\nSector Angle: %f\nMean Tubing Diameter: %f\nShear Stress Tension: %f\nShear Stress Pressure: %f\nUltimate Stress Tension: %f\nUltimate Stress Pressure: %f\nTransversal Area: %f\nMaterial Area %f\nNozzle Efficiency %f' % ( - self.values.material, self.values.diameter_ext, self.values.wall_thickness, self.values.internal_radius, self.values.young_module, self.values.sector_angle, self.values.mean_tubing_diameter, self.values.shear_stress_tension, self.values.shear_stress_pressure, self.values.ult_stress_tension, self.values.ult_stress_pressure, self.values.transversal_area, self.values.material_area, self.values.nozzle_efficiency)) - def __str__(self): - return(self.__repr__() if self.allocated else 'NULL') - -class TREL_ENGINE(): - def __init__(self, pressure, temperature, mass, grains, fuel, tubing, screws): - self.values = _TREL_ENGINE_T.from_address(_trel_engine_init( - pressure, temperature, mass, grains._p_p, fuel._p_p, tubing._p_p, screws._p_p)) - self._p = ctypes.pointer(self.values) - self._p_p = ctypes.pointer(self._p) - self.allocated = True - def free(self): - _trel_engine_free(self._p_p) - self.allocated = False - def __repr__(self): - return('Engine Mass: %f\nPressue: %f\nEscape Velocity: %f\nTemperature %f\nWidth Condition: %f\nMargin of Safety: %f\nMaximum Stress: %f\nRadial Stress: %f\nTangencial Stress: %f\nLongitudinal Stress: %f\nMaximum Pressure: %f\nNozzle Efficiency: %f' % ( - self.values.engine_mass, self.values.pressure, self.values.escape_vel, self.values.temperature, self.values.width_condition, self.values.margin_of_safety, self.values.max_stress, self.values.radial_stress, self.values.tangencial_stress, self.values.longitudinal_stress, self.values.max_pressue, self.values.nozzle_efficiency)) - def __str__(self): - return(self.__repr__() if self.allocated else 'NULL') diff --git a/Interfaces/Python/TREL.py b/Interfaces/Python/TREL.py index 8f7ffe2..1c84396 100644 --- a/Interfaces/Python/TREL.py +++ b/Interfaces/Python/TREL.py @@ -141,7 +141,7 @@ class TREL_HEIGHT_SIM(ctypes.Structure): ("max_drag", ctypes.c_double)] # Struct and constructor for rocket -class TREL_ROCKET(ctypes.Structure): +class _TREL_ROCKET_T(ctypes.Structure): _fields_ = [("engine", ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T)))), ("telemetry_mass", ctypes.c_double), ("parachute_mass", ctypes.c_double), @@ -157,8 +157,8 @@ class TREL_ROCKET(ctypes.Structure): ("drag_coefficient", ctypes.c_double), ("sim_values", ctypes.POINTER(TREL_HEIGHT_SIM))] -trel_rocket_init = TREL.trel_rocket_init -trel_rocket_init.argtypes = [ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T))), +_trel_rocket_init = TREL.trel_rocket_init +_trel_rocket_init.argtypes = [ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T))), ctypes.c_double, ctypes.c_double, ctypes.c_double, @@ -167,16 +167,16 @@ class TREL_ROCKET(ctypes.Structure): ctypes.c_double, ctypes.c_double, ctypes.c_double] -trel_rocket_init.restype = ctypes.c_void_p +_trel_rocket_init.restype = ctypes.c_void_p trel_run_area_comp_iterations = TREL.trel_run_area_comp_iterations trel_run_area_comp_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T)))] trel_run_time_comp_iterations = TREL.trel_run_time_comp_iterations -trel_run_time_comp_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] +trel_run_time_comp_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_ROCKET_T))] trel_run_height_sim_iterations = TREL.trel_run_height_sim_iterations -trel_run_height_sim_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] +trel_run_height_sim_iterations.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_ROCKET_T))] _trel_grains_free = TREL.trel_grains_free _trel_grains_free.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_GRAINS_T))] @@ -198,97 +198,110 @@ class TREL_ROCKET(ctypes.Structure): _trel_engine_free.argtypes = [ctypes.POINTER(ctypes.POINTER((_TREL_ENGINE_T)))] _trel_engine_free.restype = ctypes.c_void_p -trel_rocket_free = TREL.trel_rocket_free -trel_rocket_free.argtypes = [ctypes.POINTER(ctypes.POINTER(TREL_ROCKET))] -trel_rocket_free.restype = ctypes.c_void_p - -def trel_print_rocket(rocket): - print("\nGrains data") - print("amount", rocket.engine.contents.contents.grains.contents.contents.amount) - print("init_inter_radius", rocket.engine.contents.contents.grains.contents.contents.init_inter_radius) - print("extern_radius", rocket.engine.contents.contents.grains.contents.contents.extern_radius) - print("longitude", rocket.engine.contents.contents.grains.contents.contents.longitude) - print("grain_separation", rocket.engine.contents.contents.grains.contents.contents.grain_separation) - - print("\nFuel data") - print("const_burn_rate", rocket.engine.contents.contents.fuel.contents.contents.const_burn_rate) - print("pressure_exponent", rocket.engine.contents.contents.fuel.contents.contents.pressure_exponent) - print("density", rocket.engine.contents.contents.fuel.contents.contents.density) - print("burn_rate", rocket.engine.contents.contents.fuel.contents.contents.burn_rate) - - print("\nScrews data") - print("diameter", rocket.engine.contents.contents.screws.contents.contents.diameter) - print("dist_center_wall", rocket.engine.contents.contents.screws.contents.contents.dist_center_wall) - print("area_per_screw", rocket.engine.contents.contents.screws.contents.contents.area_per_screw) - print("screw_occupied_area", rocket.engine.contents.contents.screws.contents.contents.screw_occupied_area) - print("width_cutting_segment", rocket.engine.contents.contents.screws.contents.contents.width_cutting_segment) - print("material", rocket.engine.contents.contents.screws.contents.contents.material.decode('ASCII')) - print("amount", rocket.engine.contents.contents.screws.contents.contents.amount) - - print("\nTubing data") - print("material", rocket.engine.contents.contents.tube.contents.contents.material.decode('ASCII')) - print("diameter_ext", rocket.engine.contents.contents.tube.contents.contents.diameter_ext) - print("wall_thickness", rocket.engine.contents.contents.tube.contents.contents.wall_thickness) - print("internal_radius", rocket.engine.contents.contents.tube.contents.contents.internal_radius) - print("young_module", rocket.engine.contents.contents.tube.contents.contents.young_module) - print("sector_angle", rocket.engine.contents.contents.tube.contents.contents.sector_angle) - print("mean_tubing_diameter", rocket.engine.contents.contents.tube.contents.contents.mean_tubing_diameter) - print("shear_stress_tension", rocket.engine.contents.contents.tube.contents.contents.shear_stress_tension) - print("shear_stress_pressure", rocket.engine.contents.contents.tube.contents.contents.shear_stress_pressure) - print("ult_stress_tension", rocket.engine.contents.contents.tube.contents.contents.ult_stress_tension) - print("ult_stress_pressure", rocket.engine.contents.contents.tube.contents.contents.ult_stress_pressure) - print("transversal_area", rocket.engine.contents.contents.tube.contents.contents.transversal_area) - print("material_area", rocket.engine.contents.contents.tube.contents.contents.material_area) - print("nozzle_efficiency", rocket.engine.contents.contents.tube.contents.contents.nozzle_efficiency) - - print("\nEngine data") - print("engine_mass", rocket.engine.contents.contents.engine_mass) - print("pressure", rocket.engine.contents.contents.pressure) - print("escape_vel", rocket.engine.contents.contents.escape_vel) - print("temperature", rocket.engine.contents.contents.temperature) - print("width_condition", rocket.engine.contents.contents.width_condition) - print("margin_of_safety", rocket.engine.contents.contents.margin_of_safety) - print("max_stress", rocket.engine.contents.contents.max_stress) - print("radial_stress", rocket.engine.contents.contents.radial_stress) - print("tangencial_stress", rocket.engine.contents.contents.tangencial_stress) - print("longitudinal_stress", rocket.engine.contents.contents.longitudinal_stress) - print("max_pressue", rocket.engine.contents.contents.max_pressue) - print("nozzle_efficiency", rocket.engine.contents.contents.nozzle_efficiency) - - print("\nComp area data") - print("avg_long_area", rocket.engine.contents.contents.comp_area_values.contents.avg_long_area) - print("avg_trans_area", rocket.engine.contents.contents.comp_area_values.contents.avg_trans_area) - print("avg_burn_area", rocket.engine.contents.contents.comp_area_values.contents.avg_burn_area) - print("burn_std_deviation", rocket.engine.contents.contents.comp_area_values.contents.burn_std_deviation) - print("burn_sum_diff", rocket.engine.contents.contents.comp_area_values.contents.burn_sum_diff) - - print("\nRocket data") - print("telemetry_mass", rocket.telemetry_mass) - print("parachute_mass", rocket.parachute_mass) - print("fuselage_mass", rocket.fuselage_mass) - print("payload_mass", rocket.payload_mass) - print("initial_height", rocket.initial_height) - print("avg_thrust", rocket.avg_thrust) - print("max_thrust", rocket.max_thrust) - print("delta_v", rocket.delta_v) - print("max_pressure", rocket.max_pressure) - print("sim_latitude", rocket.sim_latitude) - print("body_diameter", rocket.body_diameter) - print("drag_coefficient", rocket.drag_coefficient) - - print("\nHeight sim data") - print("rocket_position list with %i items" % (len(list(rocket.sim_values.contents.position)))) - print("rocket_speed list with %i items" % (len(list(rocket.sim_values.contents.speed)))) - print("rocket_acceleration list with %i items" % (len(list(rocket.sim_values.contents.acceleration)))) - print("rocket_force_balance list with %i items" % (len(list(rocket.sim_values.contents.force_balance)))) - print("rocket_drag list with %i items" % (len(list(rocket.sim_values.contents.drag)))) - print("rocket_weight list with %i items" % (len(list(rocket.sim_values.contents.weight)))) - print("rocket_mass list with %i items" % (len(list(rocket.sim_values.contents.mass)))) - print("rocket_force list with %i items" % (len(list(rocket.sim_values.contents.force)))) - print("time list with %i items" % (len(list(rocket.sim_values.contents.time)))) - print("max_height", rocket.sim_values.contents.max_height) - print("max_position", rocket.sim_values.contents.max_position) - print("max_speed", rocket.sim_values.contents.max_speed) - print("max_acceleration", rocket.sim_values.contents.max_acceleration) - print("max_force_balance", rocket.sim_values.contents.max_force_balance) - print("max_drag", rocket.sim_values.contents.max_drag) +_trel_rocket_free = TREL.trel_rocket_free +_trel_rocket_free.argtypes = [ctypes.POINTER(ctypes.POINTER(_TREL_ROCKET_T))] +_trel_rocket_free.restype = ctypes.c_void_p + +class TREL_GRAINS(): + def __init__(self, amount, internal_radius, external_radius, longitude, separation): + self.values = _TREL_GRAINS_T.from_address(_trel_grains_init( + amount, internal_radius, external_radius, longitude, separation)) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) + self.allocated = True + def free(self): + _trel_grains_free(self._p_p) + self.allocated = False + def __repr__(self): + return('Amount: %f\nInternal radius: %f\nExternal radius: %f\nLongitude: %f\nSeparation: %f' % ( + self.values.amount, self.values.init_inter_radius, self.values.extern_radius, self.values.longitude, self.values.grain_separation)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_FUEL(): + def __init__(self, c_burn_rate, pressure_exponent, density): + self.values = _TREL_FUEL_T.from_address(_trel_fuel_init( + c_burn_rate, pressure_exponent, density)) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) + self.allocated = True + def free(self): + _trel_fuel_free(self._p_p) + self.allocated = False + def __repr__(self): + return('Const Burn Rate: %f\nPressure Exponent: %f\nDensity: %f\nBurn Rate: %f' % ( + self.values.const_burn_rate, self.values.pressure_exponent, self.values.density, self.values.burn_rate)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_SCREWS(): + def __init__(self, material_name, amount, diameter, dist_center_wall): + if len(material_name) > 99: + print("ERROR: TREL's materials can't take more than 99 characters") + return(None) + self.values = _TREL_SCREWS_T.from_address(_trel_screws_init( + bytes(material_name, 'utf8'), amount, diameter, dist_center_wall)) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) + self.allocated = True + def free(self): + _trel_screws_free(self._p_p) + self.allocated = False + def __repr__(self): + return('Material: %s\nAmount: %f\nDiameter: %f\nWall Center Distance: %f\nArea Per Screw:%f\nScrew Occupied Area: %f\nWidth Cutting Segment %f' % ( + self.values.material, self.values.amount, self.values.diameter, self.values.dist_center_wall, self.values.area_per_screw, self.values.screw_occupied_area, self.values.width_cutting_segment)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_TUBING(): + def __init__(self, material_name, external_diameter, thickness, young_module, shear_tension, shear_pressure, ult_tension, ult_pressure, nozzle_efficiency): + if len(material_name) > 99: + print("ERROR: TREL's materials can't take more than 99 characters") + return(None) + self.values = _TREL_TUBING_T.from_address(_trel_tubing_init( + bytes(material_name, 'utf8'), external_diameter, thickness, young_module, shear_tension, shear_pressure, ult_tension, ult_pressure, nozzle_efficiency)) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) + self.allocated = True + def free(self): + _trel_tubing_free(self._p_p) + self.allocated = False + def __repr__(self): + return('Material: %s\nExternal Diameter: %f\nWall Thickness: %f\nInternal Radius: %f\nYoung Module: %f\nSector Angle: %f\nMean Tubing Diameter: %f\nShear Stress Tension: %f\nShear Stress Pressure: %f\nUltimate Stress Tension: %f\nUltimate Stress Pressure: %f\nTransversal Area: %f\nMaterial Area %f\nNozzle Efficiency %f' % ( + self.values.material, self.values.diameter_ext, self.values.wall_thickness, self.values.internal_radius, self.values.young_module, self.values.sector_angle, self.values.mean_tubing_diameter, self.values.shear_stress_tension, self.values.shear_stress_pressure, self.values.ult_stress_tension, self.values.ult_stress_pressure, self.values.transversal_area, self.values.material_area, self.values.nozzle_efficiency)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_ENGINE(): + def __init__(self, pressure, temperature, mass, grains, fuel, tubing, screws): + self.values = _TREL_ENGINE_T.from_address(_trel_engine_init( + pressure, temperature, mass, grains._p_p, fuel._p_p, tubing._p_p, screws._p_p)) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) + self.allocated = True + def free(self): + _trel_engine_free(self._p_p) + self.allocated = False + def __repr__(self): + return('Engine Mass: %f\nPressue: %f\nEscape Velocity: %f\nTemperature %f\nWidth Condition: %f\nMargin of Safety: %f\nMaximum Stress: %f\nRadial Stress: %f\nTangencial Stress: %f\nLongitudinal Stress: %f\nMaximum Pressure: %f\nNozzle Efficiency: %f' % ( + self.values.engine_mass, self.values.pressure, self.values.escape_vel, self.values.temperature, self.values.width_condition, self.values.margin_of_safety, self.values.max_stress, self.values.radial_stress, self.values.tangencial_stress, self.values.longitudinal_stress, self.values.max_pressue, self.values.nozzle_efficiency)) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') + +class TREL_ROCKET(): + def __init__(self, engine, telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height, sim_latitude, body_diameter, drag_coefficient): + self.values = _TREL_ROCKET_T.from_address(_trel_rocket_init( + engine._p_p, telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height, sim_latitude, body_diameter, drag_coefficient)) + self._p = ctypes.pointer(self.values) + self._p_p = ctypes.pointer(self._p) + self.allocated = True + def free(self): + _trel_rocket_free(self._p_p) + self.allocated = False + def __repr__(self): + return('Telemetry Mass: %f\nParachute Mass: %f\nFuselage Mass: %f\nPayload Mass: %f\nInitial Height: %f\nAverage Thrust: %f\nMaximum Thrust: %f\nDelta V: %f\nMaximum Pressure: %f\nSimulation Latitude: %f\nBody Diameter: %f\nDrag Coefficient: %f' % ( + self.values.telemetry_mass, self.values.parachute_mass, self.values.fuselage_mass, self.values.payload_mass, self.values.initial_height, self.values.avg_thrust, self.values.max_thrust, self.values.delta_v, self.values.max_pressure, self.values.sim_latitude, self.values.body_diameter, self.values.drag_coefficient)) + def get_sim_values(self): + return(self.values.sim_values.contents) + def __str__(self): + return(self.__repr__() if self.allocated else 'NULL') diff --git a/Interfaces/Python/highmanualtest.py b/Interfaces/Python/highmanualtest.py index b5d3234..81b9335 100644 --- a/Interfaces/Python/highmanualtest.py +++ b/Interfaces/Python/highmanualtest.py @@ -1,4 +1,4 @@ -from HIGH_TREL import * +from TREL import * engine_grains = TREL_GRAINS( 4, @@ -38,15 +38,29 @@ engine_tubing, engine_screws ) +rocket = TREL_ROCKET( + engine, + 0.3, + 0.05, + 5.0, + 1.5, + 1207.0, + 9.936111, + 0.13, + 0.4 +) print(f'Grains\n{engine_grains}') print(f'Fuel\n{engine_fuel}') print(f'Screws\n{engine_screws}') print(f'Tubing\n{engine_tubing}') print(f'Engine\n{engine}') +print(f'Rocket\n{rocket}') +print(list(rocket.get_sim_values().position)) engine_grains.free() engine_fuel.free() engine_screws.free() engine_tubing.free() engine.free() +rocket.free() diff --git a/Interfaces/Python/manualtest.py b/Interfaces/Python/manualtest.py deleted file mode 100644 index 9580f6f..0000000 --- a/Interfaces/Python/manualtest.py +++ /dev/null @@ -1,79 +0,0 @@ -from TREL import * -from TREL import _TREL_GRAINS_T - -created_grains = _TREL_GRAINS_T.from_address(trel_grains_init( - 4, - 0.01, - 0.0304, - 0.1, - 0.001 - )) -p_created_grains = ctypes.pointer(created_grains) -p_p_created_grains = ctypes.pointer(p_created_grains) - -created_fuel = TREL_FUEL.from_address(trel_fuel_init( - 0.005, - 0.688, - 1859)) -p_created_fuel = ctypes.pointer(created_fuel) -p_p_created_fuel = ctypes.pointer(p_created_fuel) - -created_screw = TREL_SCREWS.from_address(trel_screws_init( - b"Acero", - 6, - 0.007466, - 0.01)) -p_created_screw = ctypes.pointer(created_screw) -p_p_created_screw = ctypes.pointer(p_created_screw) - -created_tube = TREL_TUBING.from_address(trel_tubing_init( - b"Aluminio 6061-T6", - 0.073, - 0.0052, - 68900000000.0, - 275000000.0, - -1, - 310000000, - 205000000, - 0.85)) -p_created_tube = ctypes.pointer(created_tube) -p_p_created_tube = ctypes.pointer(p_created_tube) - -created_engine = TREL_ENGINE.from_address(trel_engine_init( - 800.0, - 1710.0, - 1.5, - p_p_created_grains, - p_p_created_fuel, - p_p_created_tube, - p_p_created_screw)) -p_created_engine = ctypes.pointer(created_engine) -p_p_created_engine = ctypes.pointer(p_created_engine) - -created_rocket = TREL_ROCKET.from_address(trel_rocket_init( - p_p_created_engine, - 0.3, - 0.05, - 5.0, - 1.5, - 1207.0, - 9.936111, - 0.13, - 0.4)) -p_created_rocket = ctypes.pointer(created_rocket) -p_p_created_rocket = ctypes.pointer(p_created_rocket) - -#trel_print_rocket(created_rocket) - -trel_run_area_comp_iterations(p_p_created_engine) -trel_run_time_comp_iterations(p_p_created_rocket) -trel_run_height_sim_iterations(p_p_created_rocket) - -#trel_grains_free(p_p_created_grains) -#trel_fuel_free(p_p_created_fuel) -#trel_tubing_free(p_p_created_tube) -#trel_screws_free(p_p_created_screw) -#trel_engine_free(p_p_created_engine) -#trel_rocket_free(p_p_created_rocket) - -trel_print_rocket(created_rocket) From 597a7dbf1c8971786686aeaac247bb29bd692bd8 Mon Sep 17 00:00:00 2001 From: lross2k Date: Wed, 26 Apr 2023 16:10:03 -0600 Subject: [PATCH 39/42] About to release I guess ~lross2k~ --- Interfaces/Python/TREL.py | 6 +- Interfaces/Python/basic_example.py | 95 +++++++++++++++++++++++++++++ Interfaces/Python/highmanualtest.py | 66 -------------------- 3 files changed, 99 insertions(+), 68 deletions(-) create mode 100644 Interfaces/Python/basic_example.py delete mode 100644 Interfaces/Python/highmanualtest.py diff --git a/Interfaces/Python/TREL.py b/Interfaces/Python/TREL.py index 1c84396..c18a79b 100644 --- a/Interfaces/Python/TREL.py +++ b/Interfaces/Python/TREL.py @@ -104,7 +104,7 @@ class _TREL_ENGINE_T(ctypes.Structure): ("radial_stress", ctypes.c_double), ("tangencial_stress", ctypes.c_double), ("longitudinal_stress", ctypes.c_double), - ("max_pressue", ctypes.c_double), + ("max_pressure", ctypes.c_double), ("nozzle_efficiency", ctypes.c_double), ("grains", ctypes.POINTER(ctypes.POINTER(_TREL_GRAINS_T))), ("fuel", ctypes.POINTER(ctypes.POINTER(_TREL_FUEL_T))), @@ -284,7 +284,9 @@ def free(self): self.allocated = False def __repr__(self): return('Engine Mass: %f\nPressue: %f\nEscape Velocity: %f\nTemperature %f\nWidth Condition: %f\nMargin of Safety: %f\nMaximum Stress: %f\nRadial Stress: %f\nTangencial Stress: %f\nLongitudinal Stress: %f\nMaximum Pressure: %f\nNozzle Efficiency: %f' % ( - self.values.engine_mass, self.values.pressure, self.values.escape_vel, self.values.temperature, self.values.width_condition, self.values.margin_of_safety, self.values.max_stress, self.values.radial_stress, self.values.tangencial_stress, self.values.longitudinal_stress, self.values.max_pressue, self.values.nozzle_efficiency)) + self.values.engine_mass, self.values.pressure, self.values.escape_vel, self.values.temperature, self.values.width_condition, self.values.margin_of_safety, self.values.max_stress, self.values.radial_stress, self.values.tangencial_stress, self.values.longitudinal_stress, self.values.max_pressure, self.values.nozzle_efficiency)) + def get_area_behavior(self): + return(self.values.comp_area_values.contents) def __str__(self): return(self.__repr__() if self.allocated else 'NULL') diff --git a/Interfaces/Python/basic_example.py b/Interfaces/Python/basic_example.py new file mode 100644 index 0000000..4fe77b4 --- /dev/null +++ b/Interfaces/Python/basic_example.py @@ -0,0 +1,95 @@ +from TREL import * + +# Create a rocket structure +engine_grains = TREL_GRAINS( + 4, + 0.01, + 0.0304, + 0.1, + 0.001 +) +engine_fuel = TREL_FUEL( + 0.005, + 0.688, + 1859 +) +engine_screws = TREL_SCREWS( + 'Acero', + 6, + 0.007466, + 0.01 +) +engine_tubing = TREL_TUBING( + 'Aluminio 6061-T6', + 0.073, + 0.0052, + 68900000000.0, + 275000000.0, + -1, + 310000000, + 205000000, + 0.85 +) +engine = TREL_ENGINE( + 800.0, + 1710.0, + 1.5, + engine_grains, + engine_fuel, + engine_tubing, + engine_screws +) +rocket = TREL_ROCKET( + engine, + 0.3, + 0.05, + 5.0, + 1.5, + 1207.0, + 9.936111, + 0.13, + 0.4 +) + +# Visualize the created objetcs +print(f'Grains\n{engine_grains}\n') +print(f'Fuel\n{engine_fuel}\n') +print(f'Screws\n{engine_screws}\n') +print(f'Tubing\n{engine_tubing}\n') +print(f'Engine\n{engine}\n') +print(f'Rocket\n{rocket}\n') + +# Access area behavior data +print('Area behavior data') +print(f'Average Longitudinal Area: {engine.get_area_behavior().avg_long_area}') +print(f'Average Transversal Area: {engine.get_area_behavior().avg_trans_area}') +print(f'Average Burn Area: {engine.get_area_behavior().avg_burn_area}') +print(f'Burn Standard Deviation: {engine.get_area_behavior().burn_std_deviation}') +print(f'Brun Sum Differential: {engine.get_area_behavior().burn_sum_diff}') + +# Access height simulation data +print('\nHeight simulation data') +print(f'Position {len(list(rocket.get_sim_values().position))} items') +print(f'Speed {len(list(rocket.get_sim_values().speed))} items') +print(f'Acceleration {len(list(rocket.get_sim_values().acceleration))} items') +print(f'Force Balance {len(list(rocket.get_sim_values().force_balance))} items') +print(f'Drag {len(list(rocket.get_sim_values().drag))} items') +print(f'Weight {len(list(rocket.get_sim_values().weight))} items') +print(f'Mass {len(list(rocket.get_sim_values().mass))} items') +print(f'Force {len(list(rocket.get_sim_values().force))} items') +print(f'Time {len(list(rocket.get_sim_values().time))} items') +print(f'Maximum Height: {rocket.get_sim_values().max_height}') +print(f'Maximum Position: {rocket.get_sim_values().max_position}') +print(f'Maximum Speed: {rocket.get_sim_values().max_speed}') +print(f'Maximum Acceleration: {rocket.get_sim_values().max_acceleration}') +print(f'Maximum Force Balance: {rocket.get_sim_values().max_force_balance}') +print(f'Maximum Position: {rocket.get_sim_values().max_position}') +print(f'Maximum Drag: {rocket.get_sim_values().max_drag}') + +# Delete the objects created **ALWAYS DO AFTER YOU DON'T NEED THEM** +engine_grains.free() +engine_fuel.free() +engine_screws.free() +engine_tubing.free() +engine.free() +rocket.free() diff --git a/Interfaces/Python/highmanualtest.py b/Interfaces/Python/highmanualtest.py deleted file mode 100644 index 81b9335..0000000 --- a/Interfaces/Python/highmanualtest.py +++ /dev/null @@ -1,66 +0,0 @@ -from TREL import * - -engine_grains = TREL_GRAINS( - 4, - 0.01, - 0.0304, - 0.1, - 0.001 -) -engine_fuel = TREL_FUEL( - 0.005, - 0.688, - 1859 -) -engine_screws = TREL_SCREWS( - 'Acero', - 6, - 0.007466, - 0.01 -) -engine_tubing = TREL_TUBING( - 'Aluminio 6061-T6', - 0.073, - 0.0052, - 68900000000.0, - 275000000.0, - -1, - 310000000, - 205000000, - 0.85 -) -engine = TREL_ENGINE( - 800.0, - 1710.0, - 1.5, - engine_grains, - engine_fuel, - engine_tubing, - engine_screws -) -rocket = TREL_ROCKET( - engine, - 0.3, - 0.05, - 5.0, - 1.5, - 1207.0, - 9.936111, - 0.13, - 0.4 -) - -print(f'Grains\n{engine_grains}') -print(f'Fuel\n{engine_fuel}') -print(f'Screws\n{engine_screws}') -print(f'Tubing\n{engine_tubing}') -print(f'Engine\n{engine}') -print(f'Rocket\n{rocket}') -print(list(rocket.get_sim_values().position)) - -engine_grains.free() -engine_fuel.free() -engine_screws.free() -engine_tubing.free() -engine.free() -rocket.free() From 467205f6915cf76c7d275ce43929e4c95a3411be Mon Sep 17 00:00:00 2001 From: lross2k Date: Wed, 26 Apr 2023 16:44:08 -0600 Subject: [PATCH 40/42] Minor formatting improvements ~lross2k~ --- Interfaces/Python/basic_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Interfaces/Python/basic_example.py b/Interfaces/Python/basic_example.py index 4fe77b4..ac737a0 100644 --- a/Interfaces/Python/basic_example.py +++ b/Interfaces/Python/basic_example.py @@ -51,7 +51,7 @@ 0.4 ) -# Visualize the created objetcs +# Visualize the created objects print(f'Grains\n{engine_grains}\n') print(f'Fuel\n{engine_fuel}\n') print(f'Screws\n{engine_screws}\n') @@ -65,7 +65,7 @@ print(f'Average Transversal Area: {engine.get_area_behavior().avg_trans_area}') print(f'Average Burn Area: {engine.get_area_behavior().avg_burn_area}') print(f'Burn Standard Deviation: {engine.get_area_behavior().burn_std_deviation}') -print(f'Brun Sum Differential: {engine.get_area_behavior().burn_sum_diff}') +print(f'Burn Sum Differential: {engine.get_area_behavior().burn_sum_diff}') # Access height simulation data print('\nHeight simulation data') From e9bf1bfe01841b8e871392d78f226215c2b5959b Mon Sep 17 00:00:00 2001 From: lross2k Date: Wed, 26 Apr 2023 16:49:05 -0600 Subject: [PATCH 41/42] More small changes, vanity ~lross2k~ --- CMakeLists.txt | 18 +++--- tests/TREL.h | 79 +++++++++++------------ tests/advanced_example.c | 14 ++++ tests/basic_example.c | 109 +++++++++++++++++++++++++++++++ tests/manual_test.c | 136 --------------------------------------- 5 files changed, 171 insertions(+), 185 deletions(-) create mode 100644 tests/advanced_example.c create mode 100644 tests/basic_example.c delete mode 100644 tests/manual_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e9b570..7004435 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ PROJECT (structure_test VERSION 0.0.1 DESCRIPTION "Basic structure for compilati OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" ON) # Developer can choose to build the CLI testapp -OPTION (BUILD_MANUAL_TEST "Build testapp" ON) +OPTION (BUILD_BASIC_EXAMPLE "Build example" ON) # Used to generate header with macros for the library #INCLUDE (GenerateExportHeader) @@ -64,13 +64,13 @@ SET_TARGET_PROPERTIES (TREL PROPERTIES COMPILE_FLAGS -DLIB_STATIC_DEFINE) SET_PROPERTY(TARGET TREL PROPERTY C_STANDARD 90) # Additional function calls for building the testapp -IF (BUILD_MANUAL_TEST) +IF (BUILD_BASIC_EXAMPLE) # Adding an executable to test the library - ADD_EXECUTABLE (manualtest "tests/manual_test.c") + ADD_EXECUTABLE (basic_example "tests/basic_example.c") # Declaring a link between the executable and the library - TARGET_LINK_LIBRARIES (manualtest TREL) - #target_link_libraries(testapp PRIVATE ) - TARGET_INCLUDE_DIRECTORIES (manualtest PUBLIC "tests") - # Additional arguments for the excecutable compilation - TARGET_COMPILE_FEATURES (manualtest PUBLIC c_std_90) -ENDIF (BUILD_MANUAL_TEST) + TARGET_LINK_LIBRARIES (basic_example TREL) + TARGET_INCLUDE_DIRECTORIES (basic_example PUBLIC "tests") + # Additional arguments for the executable compilation + TARGET_COMPILE_FEATURES (basic_example PUBLIC c_std_90) + SET_PROPERTY(TARGET basic_example PROPERTY C_STANDARD 90) +ENDIF (BUILD_BASIC_EXAMPLE) diff --git a/tests/TREL.h b/tests/TREL.h index 2d3c508..12668b6 100644 --- a/tests/TREL.h +++ b/tests/TREL.h @@ -3,23 +3,23 @@ /* Types */ /* Comp area calculations */ -typedef struct { +typedef struct trel_comp_area { double avg_long_area, avg_trans_area, avg_burn_area, burn_std_deviation, burn_sum_diff; -} comp_area_t; +} trel_comp_area_t; /* Propellent grains structure */ -typedef struct { +typedef struct trel_grains { unsigned int amount; double init_inter_radius, extern_radius, longitude, grain_separation; -} grains_t; +} trel_grains_t; /* Fuel structure */ -typedef struct { +typedef struct trel_fuel { double const_burn_rate, pressure_exponent, density, burn_rate; -} fuel_t; +} trel_fuel_t; /* Estructura para tuberia */ -typedef struct tubing +typedef struct trel_tubing { char material[100]; double diameter_ext, wall_thickness, internal_radius; @@ -27,20 +27,20 @@ typedef struct tubing double shear_stress_tension, shear_stress_pressure; double ult_stress_tension, ult_stress_pressure; double transversal_area, material_area; -} tubing_t; +} trel_tubing_t; /* Estructura para tornillos */ -typedef struct screws +typedef struct trel_screws { double diameter, dist_center_wall; unsigned int amount; double area_per_screw, screw_occupied_area; char material[100]; double width_cutting_segment; -} screws_t; +} trel_screws_t; /* Estructura del engine */ -typedef struct engine +typedef struct trel_engine { double engine_mass; double pressure; /* chamber pressure in psi */ @@ -48,12 +48,12 @@ typedef struct engine double width_condition, margin_of_safety, max_stress, radial_stress; double tangencial_stress, longitudinal_stress, max_pressure; double nozzle_efficiency; - grains_t **grains; /* ptr to intialized struct */ - fuel_t **fuel; - tubing_t **tube; /* ptr to intialized struct */ - screws_t **screws; /* ptr to initialized struct */ - comp_area_t *comp_area_values; /* ptr to initialized struct */ -} engine_t; + trel_grains_t **grains; /* ptr to intialized struct */ + trel_fuel_t **fuel; + trel_tubing_t **tube; /* ptr to intialized struct */ + trel_screws_t **screws; /* ptr to initialized struct */ + trel_comp_area_t *comp_area_values; /* ptr to initialized struct */ +} trel_engine_t; /* Struct for height simulation results */ typedef struct trel_height_sim_values @@ -78,7 +78,7 @@ typedef struct trel_height_sim_values /* Struct for the rocket */ typedef struct trel_rocket { - engine_t **engine; /* ptr to initialized struct */ + trel_engine_t **engine; /* ptr to initialized struct */ double telemetry_mass, parachute_mass, fuselage_mass, payload_mass, initial_height; double avg_thrust, max_thrust, delta_v, max_pressure; /* Comportamiento en el Tiempo!AD3511 */ double sim_latitude, body_diameter, drag_coefficient; @@ -86,19 +86,19 @@ typedef struct trel_rocket } trel_rocket_t; /* Function prototypes */ -grains_t *trel_grains_init( +trel_grains_t *trel_grains_init( unsigned int amount, /* E29 */ double internal_radius, /* E30 */ double external_radius, /* E31 */ double longitude, /* E32 */ double grain_separation /* E33 */ ); -fuel_t *trel_fuel_init( +trel_fuel_t *trel_fuel_init( double const_burn_rate, /* E41 */ double pressure_exponent,/* E42 */ double density /* E44 */ ); -tubing_t *trel_tubing_init( +trel_tubing_t *trel_tubing_init( char* material, /* E16 & E17 */ double diameter, /* E13 */ double thickness, /* E14 */ @@ -109,23 +109,23 @@ tubing_t *trel_tubing_init( double ult_pressure, /* E21 */ double nozzle_efficiency ); -screws_t *trel_screws_init( +trel_screws_t *trel_screws_init( char* material, unsigned int amount, double diameter, double dist_center_wall ); -engine_t *trel_engine_init( +trel_engine_t *trel_engine_init( double pressure, /* E24 */ double temperature, /* E26 */ double engine_mass, - grains_t **grains, - fuel_t **fuel, - tubing_t **tube, - screws_t **screws + trel_grains_t **grains, + trel_fuel_t **fuel, + trel_tubing_t **tube, + trel_screws_t **screws ); trel_rocket_t *trel_rocket_init( - engine_t **engine, + trel_engine_t **engine, double telemetry_mass, double parachute_mass, double fuselage_mass, @@ -136,21 +136,20 @@ trel_rocket_t *trel_rocket_init( double drag_coefficient ); double trel_psi_to_pa(double psi); -void trel_set_pressure(engine_t* engine, double pressure); -void trel_set_escape_vel(engine_t* engine, double vel); -double trel_get_escape_vel(engine_t* engine); -double trel_get_pressure(engine_t* engine); -double trel_temper_garganta(engine_t *engine); -double br_combustion(engine_t *engine); -int trel_run_area_comp_iterations(engine_t **engine); +void trel_set_pressure(trel_engine_t* engine, double pressure); +void trel_set_escape_vel(trel_engine_t* engine, double vel); +double trel_get_escape_vel(trel_engine_t* engine); +double trel_get_pressure(trel_engine_t* engine); +double trel_temper_garganta(trel_engine_t *engine); +int trel_run_area_comp_iterations(trel_engine_t **engine); int trel_run_time_comp_iterations(trel_rocket_t **rocket); int trel_run_height_sim_iterations(trel_rocket_t **rocket); -void trel_grains_free(grains_t** grains); -void trel_fuel_free(fuel_t** fuel); -void trel_tubing_free(tubing_t** tubing); -void trel_screws_free(screws_t** screws); -void trel_engine_free(engine_t** engine); +void trel_grains_free(trel_grains_t** grains); +void trel_fuel_free(trel_fuel_t** fuel); +void trel_tubing_free(trel_tubing_t** tubing); +void trel_screws_free(trel_screws_t** screws); +void trel_engine_free(trel_engine_t** engine); void trel_rocket_free(trel_rocket_t **rocket); #endif /*TREL_H */ diff --git a/tests/advanced_example.c b/tests/advanced_example.c new file mode 100644 index 0000000..d1a3694 --- /dev/null +++ b/tests/advanced_example.c @@ -0,0 +1,14 @@ + // Accessing to specific memory + //printf("La presion del motor dada es %lf psi\n",trel_get_pressure(testing_engine)); + //printf("\nLa velocidad de escape automaticamente inicializada es %lf\n", trel_get_escape_vel(testing_engine)); + + // Modificar valores de memoria con funciones + //trel_set_escape_vel(testing_engine, 666.420); + //printf("\nLa velocidad de escape modificada con tsel_set_escape_vel() es %lf\n", trel_get_escape_vel(testing_engine)); + + // Usando una funcion de rendimientos.c + //printf("\nEl valor de combuistion es %lf\n", br_combustion(testing_engine)); + + // Usando una funcion de val_termod.c + //printf("\nLa temperatura en garganta es de %lf\n", trel_temper_garganta(testing_engine)); + diff --git a/tests/basic_example.c b/tests/basic_example.c new file mode 100644 index 0000000..ab457e4 --- /dev/null +++ b/tests/basic_example.c @@ -0,0 +1,109 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + // Create a rocket structure + trel_grains_t *engine_grains = trel_grains_init( + 4, + 0.01, + 0.0304, + 0.1, + 0.001 + ); + trel_fuel_t *engine_fuel = trel_fuel_init( + 0.005, + 0.688, + 1859 + ); + trel_screws_t *engine_screws = trel_screws_init( + "Acero", + 6, + 0.007466, + 0.01 + ); + trel_tubing_t *engine_tube = trel_tubing_init( + "Aluminio 6061-T6", + 0.073, + 0.0052, + 68900000000.0, + 275000000.0, + -1, + 310000000, + 205000000, + 0.85 + ); + trel_engine_t *engine = trel_engine_init( + 800, + 1710, + 1.5, + &engine_grains, + &engine_fuel, + &engine_tube, + &engine_screws + ); + trel_rocket_t* rocket = trel_rocket_init( + &engine, // engine + 0.3, // telemetry_mass + 0.05, // parachute_mass + 5.0, // fuselage_mass + 1.5, // payload_mass + 1207.0, // initial_height + 9.936111, // sim_latitude + 0.13, // body_diameter + 0.4 // drag_coefficient + ); + + // Visualize the created objects + // trel_print_grains(engine_grains); // We don't have such nice features yet + // trel_print_fuel(engine_fuel); + // trel_print_screws(engine_screws); + // trel_print_tubing(engine_tubing); + // trel_print_engine(engine); + // trel_print_rocket(rocket); + + // Manually visualizing rocket values + printf("Rocket\n"); + printf("telemetry_mass %lf\n", rocket->telemetry_mass); + printf("parachute_mass %lf\n", rocket->parachute_mass); + printf("fuselage_mass %lf\n", rocket->fuselage_mass); + printf("payload_mass %lf\n", rocket->payload_mass); + printf("initial_height %lf\n", rocket->initial_height); + printf("sim_latitude %lf\n", rocket->sim_latitude); + printf("body_diameter %lf\n", rocket->body_diameter); + printf("drag_coefficient %lf\n", rocket->drag_coefficient); + + // Manually visualizing a few values from the rocket time behavior + printf("\nTotal Delta V: %lf\n", rocket->delta_v); + printf("Maximum Thrust: %lf\n", rocket->max_thrust); + printf("Average Thrust: %lf\n", rocket->avg_thrust); + printf("Maximum Pressure: %lf\n\n", rocket->max_pressure); + + // Access area behavior data + printf("Average Longitudinal Area: %lf\n", engine->comp_area_values->avg_long_area); + printf("Average Burn Area: %lf\n", engine->comp_area_values->avg_burn_area); + printf("Average Transversal Area: %lf\n", engine->comp_area_values->avg_trans_area); + printf("Burn Standard Deviation: %lf\n", engine->comp_area_values->burn_std_deviation); + printf("Burn Sum Differential: %lf\n", engine->comp_area_values->burn_sum_diff); + + // Access height simulation data + printf("Max Rocket Height %lf\n", rocket->sim_values->max_rocket_height); + printf("Max Rocket Position %lf\n", rocket->sim_values->max_rocket_position); + printf("Max Rocket Speed %lf\n", rocket->sim_values->max_rocket_speed); + printf("Max Rocket Acceleration %lf\n", rocket->sim_values->max_rocket_acceleration); + printf("Max Rocket Force Balance %lf\n", rocket->sim_values->max_rocket_force_balance); + printf("Max Rocket Drag %lf\n", rocket->sim_values->max_rocket_drag); + + // String memory test + printf("\nScrews' material name: %s\n", engine_screws->material); + + // Freeing allocated memory + trel_grains_free(&engine_grains); + trel_fuel_free(&engine_fuel); + trel_tubing_free(&engine_tube); + trel_screws_free(&engine_screws); + trel_engine_free(&engine); + trel_rocket_free(&rocket); + + return(0); +} diff --git a/tests/manual_test.c b/tests/manual_test.c deleted file mode 100644 index 2c3e02b..0000000 --- a/tests/manual_test.c +++ /dev/null @@ -1,136 +0,0 @@ -#include -#include -// Header for all the library functions -#include - -// Example of how to use the library functions -void print_debug_example() -{ - // Se inicializan los tornillos - grains_t *created_grains = trel_grains_init( - 4, - 0.01, - 0.0304, - 0.1, - 0.001 - ); - fuel_t* created_fuel = trel_fuel_init( - 0.005, - 0.688, - 1859 - ); - screws_t* created_screw = trel_screws_init( - "Acero", - 6, - 0.007466, - 0.01 - ); - - // Se inicializa la tuberia - tubing_t* created_tube = trel_tubing_init( - "Aluminio 6061-T6", - 0.073, - 0.0052, - 68900000000.0, - 275000000.0, - -1, - 310000000, - 205000000, - 0.85 - ); - // Se inicializa el motor usando los tornillos y tuberia creados - engine_t* testing_engine = trel_engine_init( - 800, - 1710, - 1.5, - &created_grains, - &created_fuel, - &created_tube, - &created_screw - ); - // Se inicializa el cohete - trel_rocket_t* testing_rocket = trel_rocket_init( - &testing_engine, // engine - 0.3, // telemetry_mass - 0.05, // parachute_mass - 5.0, // fuselage_mass - 1.5, // payload_mass - 1207.0, // initial_height - 9.936111, // sim_latitude - 0.13, // body_diameter - 0.4 // drag_coefficient - ); - - // Acceder a memoria inicializada - //printf("La presion del motor dada es %lf psi\n",trel_get_pressure(testing_engine)); - //printf("\nLa velocidad de escape automaticamente inicializada es %lf\n", trel_get_escape_vel(testing_engine)); - - // Modificar valores de memoria con funciones - //trel_set_escape_vel(testing_engine, 666.420); - //printf("\nLa velocidad de escape modificada con tsel_set_escape_vel() es %lf\n", trel_get_escape_vel(testing_engine)); - - // Usando una funcion de rendimientos.c - //printf("\nEl valor de combuistion es %lf\n", br_combustion(testing_engine)); - - // Usando una funcion de val_termod.c - //printf("\nLa temperatura en garganta es de %lf\n", trel_temper_garganta(testing_engine)); - - // Probando valores generados por comp_area.c - //printf("\nArea longitudinal promedio: %lf\n", testing_engine->comp_area_values->avg_long_area); - //printf("Area quemado promedio: %lf\n", testing_engine->comp_area_values->avg_burn_area); - //printf("Area transversal promedio: %lf\n", testing_engine->comp_area_values->avg_trans_area); - //printf("Suma de diferencia quemado: %lf\n", testing_engine->comp_area_values->burn_sum_diff); - //printf("Desviacion estandar area de quemado: %lf\n", testing_engine->comp_area_values->burn_std_deviation); - - // Probando valores generados por comp_tiempo.c - printf("\nDelta V total: %lf\n", testing_rocket->delta_v); - printf("Maximo impulso: %lf\n", testing_rocket->max_thrust); - printf("Impulso promedio: %lf\n", testing_rocket->avg_thrust); - printf("Maxima presion: %lf\n\n", testing_rocket->max_pressure); - - /* - // Printing rocket values - printf("\nRocket data\n"); - printf("telemetry_mass %lf\n", testing_rocket->telemetry_mass); - printf("parachute_mass %lf\n", testing_rocket->parachute_mass); - printf("fuselage_mass %lf\n", testing_rocket->fuselage_mass); - printf("payload_mass %lf\n", testing_rocket->payload_mass); - printf("initial_height %lf\n", testing_rocket->initial_height); - printf("sim_latitude %lf\n", testing_rocket->sim_latitude); - printf("body_diameter %lf\n", testing_rocket->body_diameter); - printf("drag_coefficient %lf\n", testing_rocket->drag_coefficient); - printf("rocket_position %lf\n", testing_rocket->rocket_position); - printf("rocket_speed %lf\n", testing_rocket->rocket_speed); - printf("rocket_acceleration %lf\n", testing_rocket->rocket_acceleration); - printf("rocket_force_balance %lf\n", testing_rocket->rocket_force_balance); - printf("rocket_drag %lf\n", testing_rocket->rocket_drag); - printf("rocket_weight %lf\n", testing_rocket->rocket_weight); - printf("rocket_mass %lf\n", testing_rocket->rocket_mass); - printf("rocket_force %lf\n", testing_rocket->rocket_force); - printf("time %lf\n", testing_rocket->time); - */ - - printf("max_rocket_height %lf\n", testing_rocket->sim_values->max_rocket_height); - printf("max_rocket_position %lf\n", testing_rocket->sim_values->max_rocket_position); - printf("max_rocket_speed %lf\n", testing_rocket->sim_values->max_rocket_speed); /* m/s */ - printf("max_rocket_acceleration %lf\n", testing_rocket->sim_values->max_rocket_acceleration); - printf("max_rocket_force_balance %lf\n", testing_rocket->sim_values->max_rocket_force_balance); - printf("max_rocket_drag %lf\n", testing_rocket->sim_values->max_rocket_drag); - - //printf("Tubing's material name: %s\n", created_tube->material); - //printf("Screws' material name: %s\n", created_screw->material); - - trel_grains_free(&created_grains); - trel_fuel_free(&created_fuel); - trel_tubing_free(&created_tube); - trel_screws_free(&created_screw); - trel_engine_free(&testing_engine); - trel_rocket_free(&testing_rocket); -} - -// Para hacer pruebas de la lib se tiene la aplicacion de CLI basica -int main(int argc, char *argv[]) -{ - print_debug_example(); - return(0); -} From 7ae6a7f6618edd6622a966004d6acb6d065c1173 Mon Sep 17 00:00:00 2001 From: lross2k Date: Wed, 26 Apr 2023 20:05:09 -0600 Subject: [PATCH 42/42] Python interface readyyy --- Interfaces/Python/TREL.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Interfaces/Python/TREL.py b/Interfaces/Python/TREL.py index c18a79b..6bdbb23 100644 --- a/Interfaces/Python/TREL.py +++ b/Interfaces/Python/TREL.py @@ -5,9 +5,9 @@ TREL_MAX_ITERATIONS = 1000 if os.name == 'posix': - TREL = ctypes.CDLL("../build/libTREL.so") + TREL = ctypes.CDLL("./libTREL.so") elif os.name == 'nt': - TREL = ctypes.CDLL("../../out/build/x64-Release/TREL.dll") + TREL = ctypes.CDLL("./TREL.dll") # Struct for comp area simulation class TREL_COMP_AREA(ctypes.Structure): @@ -249,7 +249,7 @@ def free(self): self.allocated = False def __repr__(self): return('Material: %s\nAmount: %f\nDiameter: %f\nWall Center Distance: %f\nArea Per Screw:%f\nScrew Occupied Area: %f\nWidth Cutting Segment %f' % ( - self.values.material, self.values.amount, self.values.diameter, self.values.dist_center_wall, self.values.area_per_screw, self.values.screw_occupied_area, self.values.width_cutting_segment)) + self.values.material.decode('utf8'), self.values.amount, self.values.diameter, self.values.dist_center_wall, self.values.area_per_screw, self.values.screw_occupied_area, self.values.width_cutting_segment)) def __str__(self): return(self.__repr__() if self.allocated else 'NULL') @@ -268,7 +268,7 @@ def free(self): self.allocated = False def __repr__(self): return('Material: %s\nExternal Diameter: %f\nWall Thickness: %f\nInternal Radius: %f\nYoung Module: %f\nSector Angle: %f\nMean Tubing Diameter: %f\nShear Stress Tension: %f\nShear Stress Pressure: %f\nUltimate Stress Tension: %f\nUltimate Stress Pressure: %f\nTransversal Area: %f\nMaterial Area %f\nNozzle Efficiency %f' % ( - self.values.material, self.values.diameter_ext, self.values.wall_thickness, self.values.internal_radius, self.values.young_module, self.values.sector_angle, self.values.mean_tubing_diameter, self.values.shear_stress_tension, self.values.shear_stress_pressure, self.values.ult_stress_tension, self.values.ult_stress_pressure, self.values.transversal_area, self.values.material_area, self.values.nozzle_efficiency)) + self.values.material.decode('utf8'), self.values.diameter_ext, self.values.wall_thickness, self.values.internal_radius, self.values.young_module, self.values.sector_angle, self.values.mean_tubing_diameter, self.values.shear_stress_tension, self.values.shear_stress_pressure, self.values.ult_stress_tension, self.values.ult_stress_pressure, self.values.transversal_area, self.values.material_area, self.values.nozzle_efficiency)) def __str__(self): return(self.__repr__() if self.allocated else 'NULL')