summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile8
-rw-r--r--example/example.c72
-rw-r--r--include/ballistics.h17
-rw-r--r--include/context.h21
-rw-r--r--src/atragmx.c30
-rw-r--r--src/ballistics.c8
6 files changed, 118 insertions, 38 deletions
diff --git a/Makefile b/Makefile
index c7c7777..681768d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,6 @@
CC := /usr/bin/gcc
CFLAGS = -Wall -Wpedantic -I$(INC_DIR) -O2 -fPIC
+LDFLAGS := -shared
LDLIBS := -lm
# DIRS
@@ -18,17 +19,24 @@ EXAMPLE := $(BIN_DIR)/example
SOURCES := $(wildcard $(SRC_DIR)/*.c)
OBJECTS := $(SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
+TARGET := $(LIBBALLISTIC)
+
# ALL TARGET
all: $(TARGET)
# COMPILE MAIN TARGET
$(TARGET): $(OBJECTS) | $(BIN_DIR)
+ @mkdir -p lib
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
# COMPILE OBJECTS
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
+# COMPILE EXAMPLE
+$(EXAMPLE): $(EXAMPLE_DIR)/*.c
+ $(CC) $(CFLAGS) -L./lib -o $@ $^ -lballistic
+
# MAKE DIRS
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
diff --git a/example/example.c b/example/example.c
index 15615df..977a8ae 100644
--- a/example/example.c
+++ b/example/example.c
@@ -1,9 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
+#include <assert.h>
+
#include "atragmx.h"
int main(int argc, char** argv) {
- printf("Hello, %s\n", argv[0]);
+ ATragMXSolution *result = NULL;
+
+ ATragMXContext ctx = {
+ .atmosphere = {
+ .atmosphere_model = ICAO,
+ .barometric_pressure = 1096.0,
+ .relative_humidity = 50.0,
+ .temperature = 15.2,
+ .wind_dir = 4,
+ .wind_speed = { 2.3, 2.7 }
+ },
+ .gun = {
+ .air_friction = 0.0,
+ .ballistic_coef = 0.4,
+ .bore_height = 0.08,
+ .bullet_mass = 10.0,
+ .drag_model = DRAG_G1,
+ .muzzle_velocity = 850.0,
+ .scope_angle = 0.0,
+ .twist_dir = 1,
+ },
+ .target = {
+ .speed = 5.5,
+ .direction = 5,
+ .inclination_angle = 12.3,
+ .latitude = 45.0,
+ .range = 625.0,
+ },
+ .sim_steps = 100,
+ .stability_factor = 1,
+ .store_range_card = true,
+ };
+
+ ace_ballistics ctx_ace_ballistics = {
+ .enabled = true,
+ };
+ ace_config ctx_ace = {
+ .ballistics = &ctx_ace_ballistics,
+ };
+
+ result = ATragMXCalcSolution(&ctx, &ctx_ace);
+
+ if (!result) {
+ perror("Calculation failed");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("=== Solution ===\n");
+ printf("Elevation (radians): %.6f\n", result->elev);
+ printf("Windage W1: %.6f, W2: %.6f\n", result->windage.w1, result->windage.w2);
+ printf("Lead: %.6f\n", result->lead);
+ printf("Time of flight: %.4f s\n", result->time_of_flight);
+ printf("Remaining velocity: %.2f m/s\n", result->remaining_velocity);
+ printf("Remaining energy: %.2f J\n", result->remaining_energy);
+ printf("Coriolis vertical drift: %.6f\n", result->coriolis_vdrift);
+ printf("Coriolis horizontal drift: %.6f\n", result->coriolis_hdrift);
+ printf("Spin drift: %.6f\n", result->spin_drift);
+
+ assert(ctx.store_range_card);
+
+ assert(result->range_card != NULL);
+
+ if (ctx.store_range_card && result->range_card != NULL) {
+ printf("\nRange card (sim_steps = %zu):\n", ctx.sim_steps);
+ for (size_t i = 0; i < ctx.sim_steps; i++) {
+ printf(" шаг %3zu: %.3f\n", i, result->range_card->card->kinetic_energy);
+ }
+ }
+
exit(EXIT_SUCCESS);
}
diff --git a/include/ballistics.h b/include/ballistics.h
index 7ba4248..ab68c3f 100644
--- a/include/ballistics.h
+++ b/include/ballistics.h
@@ -19,21 +19,4 @@ double retard(DragFunction drag_function,
double velocity,
double temperature);
-typedef struct RangeCardRecord RangeCardRecord;
-struct RangeCardRecord {
- size_t n;
- double range;
- double elevation;
- Windage windage;
- double lead;
- double time_of_flight;
- double bullet_speed;
- double kinetic_energy;
-};
-typedef struct RangeCard RangeCard;
-struct RangeCard {
- RangeCardRecord *card;
- size_t records;
-};
-
#endif
diff --git a/include/context.h b/include/context.h
index dd207bb..ce5f913 100644
--- a/include/context.h
+++ b/include/context.h
@@ -5,7 +5,6 @@
#include <stdlib.h>
#include <stdbool.h>
-#include "ace.h"
#include "drag_model.h"
typedef enum {
@@ -55,6 +54,24 @@ typedef struct {
double w2;
} Windage;
+typedef struct RangeCardRecord RangeCardRecord;
+struct RangeCardRecord {
+ size_t n;
+ double range;
+ double elevation;
+ Windage windage;
+ double lead;
+ double time_of_flight;
+ double bullet_speed;
+ double kinetic_energy;
+};
+
+typedef struct RangeCard RangeCard;
+struct RangeCard {
+ RangeCardRecord *card;
+ size_t records;
+};
+
typedef struct {
double elev;
Windage windage;
@@ -65,7 +82,7 @@ typedef struct {
double coriolis_vdrift;
double coriolis_hdrift;
double spin_drift;
- double *range_card;
+ RangeCard *range_card;
} ATragMXSolution;
#endif
diff --git a/src/atragmx.c b/src/atragmx.c
index c797505..1b39dbc 100644
--- a/src/atragmx.c
+++ b/src/atragmx.c
@@ -70,11 +70,12 @@ ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) {
} ctx_range = {
.n = 0,
.range_factor = ctx->store_range_card ? 1.0936133 : 1,
- .range_card.records = (size_t) (rangeCardEndRange / rangeCardIncrement),
- .range_card.card = calloc(ctx_range.range_card.records,
- sizeof(RangeCardRecord)),
+ .range_card.records = (size_t) (rangeCardEndRange / rangeCardIncrement),
+ .range_card.card = NULL,
};
-
+
+ ctx_range.range_card.card = calloc(ctx_range.range_card.records,
+ sizeof(RangeCardRecord));
Vec3 wind1 = {
.x = cos(270 - ctx->atmosphere.wind_dir * 30)
@@ -240,25 +241,25 @@ ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) {
if (ctx_range.n < ctx_range.range_card.records) {
- ctx_range.range_card.card->n = ctx_range.n;
- ctx_range.range_card.card->range = ctx_range.range;
+ (ctx_range.range_card.card+ctx_range.n)->n = ctx_range.n;
+ (ctx_range.range_card.card+ctx_range.n)->range = ctx_range.range;
- ctx_range.range_card.card->elevation
+ (ctx_range.range_card.card+ctx_range.n)->elevation
= local_solution.elevation;
- ctx_range.range_card.card->windage.w1
+ (ctx_range.range_card.card+ctx_range.n)->windage.w1
= local_solution.windage.w1 * 60.0;
- ctx_range.range_card.card->windage.w2
+ (ctx_range.range_card.card+ctx_range.n)->windage.w2
= local_solution.windage.w2 * 60.0;
- ctx_range.range_card.card->lead = sln->lead;
+ (ctx_range.range_card.card+ctx_range.n)->lead = sln->lead;
- ctx_range.range_card.card->time_of_flight
+ (ctx_range.range_card.card+ctx_range.n)->time_of_flight
= local_solution.time_of_flight;
- ctx_range.range_card.card->bullet_speed = bullet.speed;
- ctx_range.range_card.card->kinetic_energy = kinetic_energy;
+ (ctx_range.range_card.card+ctx_range.n)->bullet_speed = bullet.speed;
+ (ctx_range.range_card.card+ctx_range.n)->kinetic_energy = kinetic_energy;
}
ctx_range.n++;
}
@@ -312,6 +313,7 @@ ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) {
sln->coriolis_vdrift = local_solution.v_coriolis * 60.0;
sln->coriolis_hdrift = local_solution.h_coriolis * 60.0;
sln->spin_drift = local_solution.spin_drift * 60.0;
-
+ sln->range_card = &ctx_range.range_card;
+
return sln;
}
diff --git a/src/ballistics.c b/src/ballistics.c
index d009cd4..918cc13 100644
--- a/src/ballistics.c
+++ b/src/ballistics.c
@@ -13,10 +13,10 @@ static inline double calculate_retard(DragFunction drag_function,
double
atmospheric_correction(double ballistic_coefficient,
- double temperature,
- double pressure,
- double relative_humidity,
- AtmosphereModel atmosphere_model) {
+ double temperature,
+ double pressure,
+ double relative_humidity,
+ AtmosphereModel atmosphere_model) {
double air_density = calculate_air_density(temperature, pressure, relative_humidity);
if (atmosphere_model == ICAO)