summaryrefslogtreecommitdiffstats
path: root/src/ballistics.c
blob: 418318a767c4dc9adafd990a67444eaaee28006e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <math.h>

#include "ballistics.h"
#include "constants.h"
#include "context.h"

static inline double speed_of_sound(double temperature);
static inline double calculate_retard(DragModel drag_function,
									  double ballistic_coefficient,
									  double velocity,
									  double mach);

double atmospheric_correction(double ballistic_coefficient,
							  double temperature,
							  double pressure,
							  double relative_humidity,
							  AtmosphereModel atmosphere_model) {
	double air_density = calculate_air_density(temperature, pressure, relative_humidity);

	if (atmosphere_model == ICAO)
		return (STD_AIR_DENSITY_ICAO / air_density) * ballistic_coefficient;
	else
		return (STD_AIR_DENSITY_ASM / air_density) * ballistic_coefficient;
}

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 vapor_pressure = relative_humidity * p_sat;
        double partial_pressure = pressure - vapor_pressure;

        return fma(partial_pressure,
				   DRY_AIR_MOLAR_MASS,
                   vapor_pressure * WATOR_VAPOR_MOLAR_MASS)
               / (UNIVERSAL_GAS_CONSTANT * temperature);
	}
	else
		return pressure / (SPECIFIC_GAST_CONSTANT_DRY_AIR * 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,
				 double ballistic_coefficient,
                 double velocity,
                 double mach)
{
	double m = velocity / mach;
for (int i = 0
}

double retard(DragModel ballistic_model,
			  double ballistic_coefficient,
			  double velocity,
			  double temperature)
{
	return calculate_retard(ballistic_model,
							ballistic_coefficient,
							velocity,
							speed_of_sound(temperature));
}