From 17eaf66bb7713378b95cce0fccd2cf32bde213a6 Mon Sep 17 00:00:00 2001 From: hybrid Date: Sat, 18 Jul 2026 11:44:43 +0300 Subject: add: tmp --- Makefile | 2 +- include/ballistics.h | 3 +- include/constants.h | 1 + include/context.h | 14 +---- include/drag_model.h | 23 +++++++ include/vector.h | 9 +++ src/atragmx.c | 15 ++++- src/ballistics.c | 83 +++++++++++++++++++------ src/drag_model.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 282 insertions(+), 35 deletions(-) create mode 100644 include/vector.h create mode 100644 src/drag_model.c diff --git a/Makefile b/Makefile index 42c9ac3..ba28c77 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC := /usr/bin/gcc -CFLAGS = -Wall -Wpedantic -I$(INC_DIR) +CFLAGS = -Wall -Wpedantic -I$(INC_DIR) -O2 LDLIBS := -lm # DIRS diff --git a/include/ballistics.h b/include/ballistics.h index 84a883d..ab68c3f 100644 --- a/include/ballistics.h +++ b/include/ballistics.h @@ -2,6 +2,7 @@ #define ACE_BALLISTICS_H #include "context.h" +#include "drag_model.h" double atmospheric_correction(double ballistic_coefficient, double temperature, @@ -13,7 +14,7 @@ double calculate_air_density(double temperature, double pressure, double relative_humidity); -double retard(DragModel ballistic_model, +double retard(DragFunction drag_function, double ballistic_coefficient, double velocity, double temperature); diff --git a/include/constants.h b/include/constants.h index cfc9c80..026ff7b 100644 --- a/include/constants.h +++ b/include/constants.h @@ -9,5 +9,6 @@ static const double UNIVERSAL_GAS_CONSTANT = 8.31432; static const double SPECIFIC_GAST_CONSTANT_DRY_AIR = 287.057036320950; static const double STD_AIR_DENSITY_ICAO = 1.22498; static const double STD_AIR_DENSITY_ASM = 1.20886; +static const double BC_CONVERSION_FACTOR = 0.00068418; #endif diff --git a/include/context.h b/include/context.h index 65b4585..5dfad31 100644 --- a/include/context.h +++ b/include/context.h @@ -6,17 +6,7 @@ #include #include "ace.h" - -typedef enum { - BC_UNKNOWN = 0, - BC_G1, - BC_G2, - BC_G3, - BC_G4, - BC_G5, - BC_G6, - BC_G7, -} DragModel; +#include "drag_model.h" typedef enum { ICAO, @@ -29,7 +19,7 @@ typedef struct { double bore_height; double muzzle_velocity; double ballistic_coef; - size_t drag_model; + DragFunction drag_model; double twist_dir; } GunConfig; diff --git a/include/drag_model.h b/include/drag_model.h index baf5f72..8c18bed 100644 --- a/include/drag_model.h +++ b/include/drag_model.h @@ -1,6 +1,29 @@ #ifndef DRAG_MODEL_H #define DRAG_MODEL_H +#include +typedef enum { + DRAG_G1, + DRAG_G2, + DRAG_G5, + DRAG_G6, + DRAG_G7, + DRAG_G8, + DRAG_COUNT +} DragFunction; + +typedef struct { + size_t num_points; + const double *mach; + const double *cd; +} DragModel; + +extern const DragModel drag_models[DRAG_COUNT]; + +static inline const DragModel* get_drag_model(DragFunction func) { + if (func >= DRAG_COUNT) return NULL; + return &drag_models[func]; +} #endif diff --git a/include/vector.h b/include/vector.h new file mode 100644 index 0000000..5b0d2de --- /dev/null +++ b/include/vector.h @@ -0,0 +1,9 @@ +#pragma once +#include "vector.h" +#include + +typedef struct vector_t vector_t; + +vector_t* vector_new(void); +void vector_ctr(vector_t* obj); +void vector_dtr(vector_t* obj); diff --git a/src/atragmx.c b/src/atragmx.c index ff684c3..c9cebae 100644 --- a/src/atragmx.c +++ b/src/atragmx.c @@ -128,17 +128,28 @@ ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) { // calculate magnitude of bullet.velocity bullet.speed = hypot(bullet.velocity[0], hypot(bullet.velocity[1], bullet.velocity[2])); + memcpy(local_solution.true_velocity, (double[3]){bullet.velocity[0] - wind1[0], bullet.velocity[1] - wind1[1], bullet.velocity[2] - wind1[2]}, sizeof(local_solution.true_velocity)); + local_solution.true_speed = hypot(local_solution.true_velocity[0], hypot(local_solution.true_velocity[1], local_solution.true_velocity[2])); - if (ace->ballistics->enabled) - + if (ace->ballistics->enabled) { + double data = retard(ctx->gun.drag_model, + ctx->gun.ballistic_coef, + ctx->gun.muzzle_velocity, + ctx->atmosphere.temperature); + memcpy(bullet.accel, + (double[3]){bullet.accel[0] != 0 ? -data : 0, + bullet.accel[1] != 0 ? -data : 0, + bullet.accel[2] != 0 ? -data : 0}, + + } } return sln; diff --git a/src/ballistics.c b/src/ballistics.c index 418318a..d009cd4 100644 --- a/src/ballistics.c +++ b/src/ballistics.c @@ -3,14 +3,16 @@ #include "ballistics.h" #include "constants.h" #include "context.h" +#include "drag_model.h" static inline double speed_of_sound(double temperature); -static inline double calculate_retard(DragModel drag_function, +static inline double calculate_retard(DragFunction drag_function, double ballistic_coefficient, double velocity, double mach); -double atmospheric_correction(double ballistic_coefficient, +double +atmospheric_correction(double ballistic_coefficient, double temperature, double pressure, double relative_humidity, @@ -23,14 +25,17 @@ double atmospheric_correction(double ballistic_coefficient, return (STD_AIR_DENSITY_ASM / air_density) * ballistic_coefficient; } -double calculate_air_density(double temperature, - double pressure, - double relative_humidity) { +double +calculate_air_density(double temperature, + double pressure, + double relative_humidity) { pressure = pressure * 100; - + if (relative_humidity > 0.0) { - double p_sat = 610.78 * pow(10, (7.5 * temperature - 273.15) / - (temperature - 273.15 + 237.3)); + double p_sat = 610.78 + * pow(10, + (7.5 * temperature - 273.15) + / (temperature - 273.15 + 237.3)); double vapor_pressure = relative_humidity * p_sat; double partial_pressure = pressure - vapor_pressure; @@ -43,26 +48,66 @@ double calculate_air_density(double temperature, return pressure / (SPECIFIC_GAST_CONSTANT_DRY_AIR * temperature); } -static inline double speed_of_sound(double temperature) { +static inline double +speed_of_sound(double temperature) { return sqrt(331.3 * (1.0 + temperature - 273.15 / 273.15)); } static inline double -calculate_retard(DragModel ballistic_model, +calculate_retard(DragFunction drag_function, double ballistic_coefficient, - double velocity, - double mach) + double velocity, + double mach) { - double m = velocity / mach; -for (int i = 0 + const DragModel *model = get_drag_model(drag_function); + if (model == NULL) { + return 0.0; + } + + const double *model_mach = model->mach; + const double *model_cd = model->cd; + size_t n = model->num_points; + + // border cases + if (mach <= model_mach[0]) + return model_cd[0]; + + if (mach >= model_mach[n - 1]) + return model_cd[n - 1]; + + double m = velocity / mach; + size_t left = 0, right = n - 1; + while (left < right) { + size_t mid = left + (right - left) / 2; + if (model_mach[mid] >= m) { + right = mid; + } else { + left = mid + 1; + } + } + size_t i = left; + + double m_prev = model_mach[i - 1]; + double m_curr = model_mach[i]; + double cd_prev = model_cd[i - 1]; + double cd_curr = model_cd[i]; + + double cd = cd_prev + + (cd_curr - cd_prev) + * (m - m_prev) + / (m_curr - m_prev); + return BC_CONVERSION_FACTOR + * (cd / ballistic_coefficient) + * pow(velocity, 2); } -double retard(DragModel ballistic_model, - double ballistic_coefficient, - double velocity, - double temperature) +double +retard(DragFunction drag_function, + double ballistic_coefficient, + double velocity, + double temperature) { - return calculate_retard(ballistic_model, + return calculate_retard(drag_function, ballistic_coefficient, velocity, speed_of_sound(temperature)); diff --git a/src/drag_model.c b/src/drag_model.c new file mode 100644 index 0000000..9099d16 --- /dev/null +++ b/src/drag_model.c @@ -0,0 +1,167 @@ +#include "drag_model.h" + +static const double g1_mach[] = { + 0.00, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, + 0.50, 0.55, 0.60, 0.70, 0.725, 0.75, 0.775, 0.80, 0.825, 0.85, + 0.875, 0.90, 0.925, 0.95, 0.975, 1.0, 1.025, 1.05, 1.075, 1.10, + 1.125, 1.15, 1.20, 1.25, 1.30, 1.35, 1.40, 1.45, 1.50, 1.55, + 1.60, 1.65, 1.70, 1.75, 1.80, 1.85, 1.90, 1.95, 2.00, 2.05, + 2.10, 2.15, 2.20, 2.25, 2.30, 2.35, 2.40, 2.45, 2.50, 2.60, + 2.70, 2.80, 2.90, 3.00, 3.10, 3.20, 3.30, 3.40, 3.50, 3.60, + 3.70, 3.80, 3.90, 4.00, 4.20, 4.40, 4.60, 4.80, 5.00, +}; +static const double g1_cd[] = { + 0.2629, 0.2558, 0.2487, 0.2413, 0.2344, 0.2278, 0.2214, 0.2155, 0.2104, + 0.2061, 0.2032, 0.2020, 0.2034, 0.2165, 0.2230, 0.2313, 0.2417, 0.2546, + 0.2706, 0.2901, 0.3136, 0.3415, 0.3734, 0.4084, 0.4448, 0.4805, 0.5136, + 0.5427, 0.5677, 0.5883, 0.6053, 0.6191, 0.6393, 0.6518, 0.6589, 0.6621, + 0.6625, 0.6607, 0.6573, 0.6528, 0.6474, 0.6413, 0.6347, 0.6280, 0.6210, + 0.6141, 0.6072, 0.6003, 0.5934, 0.5867, 0.5804, 0.5743, 0.5685, 0.5630, + 0.5577, 0.5527, 0.5481, 0.5438, 0.5397, 0.5325, 0.5264, 0.5211, 0.5168, + 0.5133, 0.5105, 0.5084, 0.5067, 0.5054, 0.5040, 0.5030, 0.5022, 0.5016, + 0.5010, 0.5006, 0.4998, 0.4995, 0.4992, 0.4990, 0.4988, +}; + +static const double g2_mach[] = { + 0.00, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, + 0.55, 0.60, 0.65, 0.70, 0.75, 0.775, 0.80, 0.825, 0.85, 0.875, 0.90, + 0.925, 0.95, 0.975, 1.0, 1.025, 1.05, 1.075, 1.10, 1.125, 1.15, 1.175, + 1.20, 1.25, 1.30, 1.35, 1.40, 1.45, 1.50, 1.55, 1.60, 1.65, 1.70, + 1.75, 1.80, 1.85, 1.90, 1.95, 2.00, 2.05, 2.10, 2.15, 2.20, 2.25, + 2.30, 2.35, 2.40, 2.45, 2.50, 2.55, 2.60, 2.65, 2.70, 2.75, 2.80, + 2.85, 2.90, 2.95, 3.00, 3.10, 3.20, 3.30, 3.40, 3.50, 3.60, 3.70, + 3.80, 3.90, 4.00, 4.20, 4.40, 4.60, 4.80, 5.00, +}; +static const double g2_cd[] = { + 0.2303, 0.2298, 0.2287, 0.2271, 0.2251, 0.2227, 0.2196, 0.2156, 0.2107, + 0.2048, 0.1980, 0.1905, 0.1828, 0.1758, 0.1702, 0.1669, 0.1664, 0.1667, + 0.1682, 0.1711, 0.1761, 0.1831, 0.2004, 0.2589, 0.3492, 0.3983, 0.4075, + 0.4103, 0.4114, 0.4106, 0.4089, 0.4068, 0.4046, 0.4021, 0.3966, 0.3904, + 0.3835, 0.3759, 0.3678, 0.3594, 0.3512, 0.3432, 0.3356, 0.3282, 0.3213, + 0.3149, 0.3089, 0.3033, 0.2982, 0.2933, 0.2889, 0.2846, 0.2806, 0.2768, + 0.2731, 0.2696, 0.2663, 0.2632, 0.2602, 0.2572, 0.2543, 0.2515, 0.2487, + 0.2460, 0.2433, 0.2408, 0.2382, 0.2357, 0.2333, 0.2309, 0.2262, 0.2217, + 0.2173, 0.2132, 0.2091, 0.2052, 0.2014, 0.1978, 0.1944, 0.1912, 0.1851, + 0.1794, 0.1741, 0.1693, 0.1648, +}; + +static const double g5_mach[] = { + 0.00, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, + 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.875, 0.90, 0.925, 0.95, + 0.975, 1.0, 1.025, 1.05, 1.075, 1.10, 1.15, 1.20, 1.25, 1.30, 1.35, + 1.40, 1.45, 1.50, 1.55, 1.60, 1.65, 1.70, 1.75, 1.80, 1.85, 1.90, + 1.95, 2.00, 2.05, 2.10, 2.15, 2.20, 2.25, 2.30, 2.35, 2.40, 2.45, + 2.50, 2.60, 2.70, 2.80, 2.90, 3.00, 3.10, 3.20, 3.30, 3.40, 3.50, + 3.60, 3.70, 3.80, 3.90, 4.00, 4.20, 4.40, 4.60, 4.80, 5.00, +}; +static const double g5_cd[] = { + 0.1710, 0.1719, 0.1727, 0.1732, 0.1734, 0.1730, 0.1718, 0.1696, 0.1668, + 0.1637, 0.1603, 0.1566, 0.1529, 0.1497, 0.1473, 0.1463, 0.1489, 0.1583, + 0.1672, 0.1815, 0.2051, 0.2413, 0.2884, 0.3379, 0.3785, 0.4032, 0.4147, + 0.4201, 0.4278, 0.4338, 0.4373, 0.4392, 0.4403, 0.4406, 0.4401, 0.4386, + 0.4362, 0.4328, 0.4286, 0.4237, 0.4182, 0.4121, 0.4057, 0.3991, 0.3926, + 0.3861, 0.3800, 0.3741, 0.3684, 0.3630, 0.3578, 0.3529, 0.3481, 0.3435, + 0.3391, 0.3349, 0.3269, 0.3194, 0.3125, 0.3060, 0.2999, 0.2942, 0.2889, + 0.2838, 0.2790, 0.2745, 0.2703, 0.2662, 0.2624, 0.2588, 0.2553, 0.2488, + 0.2429, 0.2376, 0.2326, 0.2280, +}; + +static const double g6_mach[] = { + 0.00, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, + 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.875, 0.90, + 0.925, 0.95, 0.975, 1.0, 1.025, 1.05, 1.075, 1.10, 1.125, 1.15, + 1.175, 1.20, 1.225, 1.25, 1.30, 1.35, 1.40, 1.45, 1.50, 1.55, + 1.60, 1.65, 1.70, 1.75, 1.80, 1.85, 1.90, 1.95, 2.00, 2.05, + 2.10, 2.15, 2.20, 2.25, 2.30, 2.35, 2.40, 2.45, 2.50, 2.60, + 2.70, 2.80, 2.90, 3.00, 3.10, 3.20, 3.30, 3.40, 3.50, 3.60, + 3.70, 3.80, 3.90, 4.00, 4.20, 4.40, 4.60, 4.80, 5.00, +}; +static const double g6_cd[] = { + 0.2617, 0.2553, 0.2491, 0.2432, 0.2376, 0.2324, 0.2278, 0.2238, 0.2205, + 0.2177, 0.2155, 0.2138, 0.2126, 0.2121, 0.2122, 0.2132, 0.2154, 0.2194, + 0.2229, 0.2297, 0.2449, 0.2732, 0.3141, 0.3597, 0.3994, 0.4261, 0.4402, + 0.4465, 0.4490, 0.4497, 0.4494, 0.4482, 0.4464, 0.4441, 0.4390, 0.4336, + 0.4279, 0.4221, 0.4162, 0.4102, 0.4042, 0.3981, 0.3919, 0.3855, 0.3788, + 0.3721, 0.3652, 0.3583, 0.3515, 0.3447, 0.3381, 0.3314, 0.3249, 0.3185, + 0.3122, 0.3060, 0.3000, 0.2941, 0.2883, 0.2772, 0.2668, 0.2574, 0.2487, + 0.2407, 0.2333, 0.2265, 0.2202, 0.2144, 0.2089, 0.2039, 0.1991, 0.1947, + 0.1905, 0.1866, 0.1794, 0.1730, 0.1673, 0.1621, 0.1574, +}; + +static const double g7_mach[] = { + 0.0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, + 0.55, 0.60, 0.65, 0.70, 0.725, 0.75, 0.775, 0.80, 0.825, 0.85, 0.875, + 0.90, 0.925, 0.95, 0.975, 1.0, 1.025, 1.05, 1.075, 1.10, 1.125, 1.15, + 1.20, 1.25, 1.30, 1.35, 1.40, 1.50, 1.55, 1.60, 1.65, 1.70, 1.75, + 1.80, 1.85, 1.90, 1.95, 2.00, 2.05, 2.10, 2.15, 2.20, 2.25, 2.30, + 2.35, 2.40, 2.45, 2.50, 2.55, 2.60, 2.65, 2.70, 2.75, 2.80, 2.85, + 2.90, 2.95, 3.00, 3.10, 3.20, 3.30, 3.40, 3.50, 3.60, 3.70, 3.80, + 3.90, 4.00, 4.20, 4.40, 4.60, 4.80, 5.00, +}; +static const double g7_cd[] = { + 0.1198, 0.1197, 0.1196, 0.1194, 0.1193, 0.1194, 0.1194, 0.1194, 0.1193, + 0.1193, 0.1194, 0.1193, 0.1194, 0.1197, 0.1202, 0.1207, 0.1215, 0.1226, + 0.1242, 0.1266, 0.1306, 0.1368, 0.1464, 0.1660, 0.2054, 0.2993, 0.3803, + 0.4015, 0.4043, 0.4034, 0.4014, 0.3987, 0.3955, 0.3884, 0.3810, 0.3732, + 0.3657, 0.3580, 0.3440, 0.3376, 0.3315, 0.3260, 0.3209, 0.3160, 0.3117, + 0.3078, 0.3042, 0.3010, 0.2980, 0.2951, 0.2922, 0.2892, 0.2864, 0.2835, + 0.2807, 0.2779, 0.2752, 0.2725, 0.2697, 0.2670, 0.2643, 0.2615, 0.2588, + 0.2561, 0.2533, 0.2506, 0.2479, 0.2451, 0.2424, 0.2368, 0.2313, 0.2258, + 0.2205, 0.2154, 0.2106, 0.2060, 0.2017, 0.1975, 0.1935, 0.1861, 0.1793, + 0.1730, 0.1672, 0.1618, +}; + +static const double g8_mach[] = { + 0.00, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, + 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.825, 0.85, 0.875, + 0.90, 0.925, 0.95, 0.975, 1.0, 1.025, 1.05, 1.075, 1.10, 1.125, + 1.15, 1.20, 1.25, 1.30, 1.35, 1.40, 1.45, 1.50, 1.55, 1.60, + 1.65, 1.70, 1.75, 1.80, 1.85, 1.90, 1.95, 2.00, 2.05, 2.10, + 2.15, 2.20, 2.25, 2.30, 2.35, 2.40, 2.45, 2.50, 2.60, 2.70, + 2.80, 2.90, 3.00, 3.10, 3.20, 3.30, 3.40, 3.50, 3.60, 3.70, + 3.80, 3.90, 4.00, 4.20, 4.40, 4.60, 4.80, 5.00, +}; +static const double g8_cd[] = { + 0.2105, 0.2105, 0.2104, 0.2104, 0.2103, 0.2103, 0.2103, 0.2103, 0.2103, + 0.2102, 0.2102, 0.2102, 0.2102, 0.2102, 0.2103, 0.2103, 0.2104, 0.2104, + 0.2105, 0.2106, 0.2109, 0.2183, 0.2571, 0.3358, 0.4068, 0.4378, 0.4476, + 0.4493, 0.4477, 0.4450, 0.4419, 0.4353, 0.4283, 0.4208, 0.4133, 0.4059, + 0.3986, 0.3915, 0.3845, 0.3777, 0.3710, 0.3645, 0.3581, 0.3519, 0.3458, + 0.3400, 0.3343, 0.3288, 0.3234, 0.3182, 0.3131, 0.3081, 0.3032, 0.2983, + 0.2937, 0.2891, 0.2845, 0.2802, 0.2720, 0.2642, 0.2569, 0.2499, 0.2432, + 0.2368, 0.2308, 0.2251, 0.2197, 0.2147, 0.2101, 0.2058, 0.2019, 0.1983, + 0.1950, 0.1890, 0.1837, 0.1791, 0.1750, 0.1713, +}; + +const DragModel drag_models[DRAG_COUNT] = { + [DRAG_G1] = { + .num_points = sizeof(g1_mach) / sizeof(g1_mach[0]), + .mach = g1_mach, + .cd = g1_cd + }, + [DRAG_G2] = { + .num_points = sizeof(g2_mach) / sizeof(g2_mach[0]), + .mach = g2_mach, + .cd = g2_cd + }, + [DRAG_G5] = { + .num_points = sizeof(g5_mach) / sizeof(g5_mach[0]), + .mach = g5_mach, + .cd = g5_cd + }, + [DRAG_G6] = { + .num_points = sizeof(g6_mach) / sizeof(g6_mach[0]), + .mach = g6_mach, + .cd = g6_cd + }, + [DRAG_G7] = { + .num_points = sizeof(g7_mach) / sizeof(g7_mach[0]), + .mach = g7_mach, + .cd = g7_cd + }, + [DRAG_G8] = { + .num_points = sizeof(g8_mach) / sizeof(g8_mach[0]), + .mach = g8_mach, + .cd = g8_cd + }, +}; -- cgit v1.3.1