Skip to content

Commit 0ca3824

Browse files
committed
WIP: Debugging Cython zero functionality
1 parent 526f7da commit 0ca3824

14 files changed

Lines changed: 125 additions & 124 deletions

File tree

py_ballisticcalc.exts/py_ballisticcalc_exts/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
from .euler_engine import CythonizedEulerIntegrationEngine
1010
from .rk4_engine import CythonizedRK4IntegrationEngine
11-
from .trajectory_data import TrajectoryData
11+
# from .trajectory_data import TrajectoryDataT
1212

1313
__all__ = (
1414
'CythonizedEulerIntegrationEngine',
1515
'CythonizedRK4IntegrationEngine',
16-
'TrajectoryData',
16+
# 'TrajectoryDataT',
1717
)

py_ballisticcalc.exts/py_ballisticcalc_exts/base_engine.pxd

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ cdef class CythonizedBaseIntegrationEngine:
7171
# Python 'def' methods are not exposed in the C interface defined by a .pxd.
7272
# Only 'cdef' or 'cpdef' methods are declared here.
7373
cdef void _free_trajectory(CythonizedBaseIntegrationEngine self)
74-
cdef void _init_trajectory(CythonizedBaseIntegrationEngine self, object shot_info)
75-
cdef tuple _init_zero_calculation(CythonizedBaseIntegrationEngine self, object shot_info, object distance)
76-
cdef object _find_zero_angle(CythonizedBaseIntegrationEngine self, object shot_info, object distance, bint lofted)
77-
cdef object _zero_angle(CythonizedBaseIntegrationEngine self, object shot_info, object distance)
78-
cdef tuple _find_max_range(CythonizedBaseIntegrationEngine self, object shot_info, tuple angle_bracket_deg = *)
79-
cdef object _find_apex(CythonizedBaseIntegrationEngine self, object shot_info)
74+
cdef ShotProps_t* _init_trajectory(CythonizedBaseIntegrationEngine self, object shot_info)
75+
cdef tuple _init_zero_calculation(CythonizedBaseIntegrationEngine self, ShotProps_t *shot_props_ptr, double distance)
76+
cdef object _find_zero_angle(CythonizedBaseIntegrationEngine self, ShotProps_t *shot_props_ptr, double distance, bint lofted)
77+
cdef object _zero_angle(CythonizedBaseIntegrationEngine self, ShotProps_t *shot_props_ptr, double distance)
78+
cdef tuple _find_max_range(CythonizedBaseIntegrationEngine self, ShotProps_t *shot_props_ptr, tuple angle_bracket_deg = *)
79+
cdef object _find_apex(CythonizedBaseIntegrationEngine self, ShotProps_t *shot_props_ptr)
8080
# In contrast to Python engines, _integrate here returns (CBaseTrajSeq, Optional[error_str])
81-
cdef object _integrate(CythonizedBaseIntegrationEngine self,
81+
cdef object _integrate(CythonizedBaseIntegrationEngine self, ShotProps_t *shot_props_ptr,
8282
double range_limit_ft, double range_step_ft, double time_step, int filter_flags)
8383

8484

py_ballisticcalc.exts/py_ballisticcalc_exts/base_engine.pyx

Lines changed: 85 additions & 69 deletions
Large diffs are not rendered by default.

py_ballisticcalc.exts/py_ballisticcalc_exts/base_traj_seq.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ __all__ = ['CBaseTrajSeq', 'BaseTrajC']
4949

5050

5151
# Interpolation helper (pure C math; safe to call with or without GIL)
52-
cdef int _interpolate_nogil_raw(_CBaseTrajSeq_cview* seq, Py_ssize_t idx, int key_kind, double key_value, BaseTrajC* out) noexcept:
52+
cdef int _interpolate_nogil_raw(_CBaseTrajSeq_cview* seq, Py_ssize_t idx, int key_kind, double key_value, BaseTrajC* out) noexcept nogil:
5353
"""Interpolate at idx using points (idx-1, idx, idx+1) keyed by key_kind at key_value."""
5454
cdef BaseTrajC* buffer = seq._buffer
5555
cdef Py_ssize_t plength = <Py_ssize_t> seq._length
@@ -124,7 +124,7 @@ cdef int _interpolate_nogil_raw(_CBaseTrajSeq_cview* seq, Py_ssize_t idx, int ke
124124
return 1
125125

126126

127-
cdef inline double _key_val_from_kind_buf(BaseTrajC* p, int key_kind) noexcept:
127+
cdef inline double _key_val_from_kind_buf(BaseTrajC* p, int key_kind) noexcept nogil:
128128
if key_kind == <int>KEY_TIME:
129129
return p.time
130130
elif key_kind == <int>KEY_MACH:
@@ -143,10 +143,10 @@ cdef inline double _key_val_from_kind_buf(BaseTrajC* p, int key_kind) noexcept:
143143
return p.vz
144144
return <double>0.0
145145

146-
cdef inline double _slant_val_buf(BaseTrajC* p, double ca, double sa) noexcept:
146+
cdef inline double _slant_val_buf(BaseTrajC* p, double ca, double sa) noexcept nogil:
147147
return p.py * ca - p.px * sa
148148

149-
cdef Py_ssize_t _bisect_center_idx_buf(BaseTrajC* buf, size_t length, int key_kind, double key_value) noexcept:
149+
cdef Py_ssize_t _bisect_center_idx_buf(BaseTrajC* buf, size_t length, int key_kind, double key_value) noexcept nogil:
150150
cdef Py_ssize_t n = <Py_ssize_t>length
151151
if n < 3:
152152
return <Py_ssize_t>(-1)
@@ -176,7 +176,7 @@ cdef Py_ssize_t _bisect_center_idx_buf(BaseTrajC* buf, size_t length, int key_ki
176176
return n - 2
177177
return lo
178178

179-
cdef Py_ssize_t _bisect_center_idx_slant_buf(BaseTrajC* buf, size_t length, double ca, double sa, double value) noexcept:
179+
cdef Py_ssize_t _bisect_center_idx_slant_buf(BaseTrajC* buf, size_t length, double ca, double sa, double value) noexcept nogil:
180180
cdef Py_ssize_t n = <Py_ssize_t>length
181181
if n < 3:
182182
return <Py_ssize_t>(-1)

py_ballisticcalc.exts/py_ballisticcalc_exts/cy_bindings.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ cdef extern from "include/bclib.h" nogil:
7979
double calc_step
8080
double muzzle_velocity
8181
double stability_coefficient
82+
int filter_flags
8283
Atmosphere_t atmo
8384

8485
void ShotProps_t_free(ShotProps_t *shot_props_ptr)

py_ballisticcalc.exts/py_ballisticcalc_exts/euler_engine.pyx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from cython cimport final
22
from libc.math cimport fabs, sin, cos, tan, atan, atan2, fmin, fmax, pow
3-
from libc.stdlib cimport malloc, realloc, free
4-
from py_ballisticcalc_exts.trajectory_data cimport TrajFlag_t, BaseTrajDataT, TrajectoryData, BaseTrajDataT_create
53
from py_ballisticcalc_exts.cy_bindings cimport (
64
Config_t,
75
ShotProps_t,
@@ -10,16 +8,8 @@ from py_ballisticcalc_exts.cy_bindings cimport (
108
)
119
from py_ballisticcalc_exts.base_engine cimport (
1210
CythonizedBaseIntegrationEngine,
13-
TrajDataFilter_t,
14-
1511
WindSock_t_currentVector,
1612
WindSock_t_vectorForRange,
17-
18-
create_trajectory_row,
19-
20-
TrajDataFilter_t_create,
21-
TrajDataFilter_t_setup_seen_zero,
22-
TrajDataFilter_t_should_record,
2313
)
2414

2515
from py_ballisticcalc_exts.v3d cimport V3dT, add, sub, mag, mulS
@@ -46,7 +36,7 @@ cdef class CythonizedEulerIntegrationEngine(CythonizedBaseIntegrationEngine):
4636
"""Calculate time step based on current projectile speed."""
4737
return base_step / fmax(<double>1.0, velocity)
4838

49-
cdef object _integrate(CythonizedEulerIntegrationEngine self,
39+
cdef object _integrate(CythonizedEulerIntegrationEngine self, ShotProps_t *shot_props_ptr,
5040
double range_limit_ft, double range_step_ft,
5141
double time_step, int filter_flags):
5242
"""

py_ballisticcalc.exts/py_ballisticcalc_exts/include/bclib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef struct {
8080
double calc_step;
8181
double muzzle_velocity;
8282
double stability_coefficient;
83+
int filter_flags;
8384
Atmosphere_t atmo;
8485
} ShotProps_t;
8586

py_ballisticcalc.exts/py_ballisticcalc_exts/rk4_engine.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ cdef class CythonizedRK4IntegrationEngine(CythonizedBaseIntegrationEngine):
5555
"""Calculate the step size for integration."""
5656
return self.DEFAULT_TIME_STEP * CythonizedBaseIntegrationEngine.get_calc_step(self)
5757

58-
cdef object _integrate(CythonizedRK4IntegrationEngine self,
58+
cdef object _integrate(CythonizedRK4IntegrationEngine self, ShotProps_t *shot_props_ptr,
5959
double range_limit_ft, double range_step_ft,
6060
double time_step, int filter_flags):
6161
"""

py_ballisticcalc.exts/py_ballisticcalc_exts/trajectory_data.pxd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ cdef extern from "include/bclib.h":
2323
V3dT velocity
2424
double mach
2525

26-
# aliases
27-
# ctypedef BaseTrajDataT BaseTrajData # temporary undeclared
28-
2926
# Ensure signature places 'nogil' at the end to match Cython's recommendation
3027
cdef double lagrange_quadratic(double x, double x0, double y0, double x1, double y1, double x2, double y2) except -1.0 nogil
3128

@@ -36,7 +33,7 @@ cdef class BaseTrajDataT:
3633
readonly V3dT velocity
3734
readonly double mach
3835

39-
cdef class TrajectoryData:
36+
cdef class TrajectoryDataT:
4037
cdef:
4138
readonly double time
4239
readonly object distance
@@ -55,5 +52,8 @@ cdef class TrajectoryData:
5552
readonly object ogw
5653
readonly int flag
5754

55+
# @staticmethod
56+
# cdef from_BaseTrajDataT(BaseTrajDataT base_data)
57+
5858
# Factory helper exposed for use from other Cython modules
5959
cdef BaseTrajDataT BaseTrajDataT_create(double time, V3dT position, V3dT velocity, double mach)

py_ballisticcalc.exts/py_ballisticcalc_exts/trajectory_data.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ cdef BaseTrajDataT BaseTrajDataT_create(double time, V3dT position, V3dT velocit
143143
return BaseTrajDataT(time, position, velocity, mach)
144144

145145
@final
146-
cdef class TrajectoryData:
146+
cdef class TrajectoryDataT:
147147
__slots__ = ('time', 'distance', 'velocity',
148148
'mach', 'height', 'slant_height', 'drop_adj',
149149
'windage', 'windage_adj', 'slant_distance',
150150
'angle', 'density_ratio', 'drag', 'energy', 'ogw', 'flag')
151151

152152
_fields = __slots__
153153

154-
def __cinit__(TrajectoryData self,
154+
def __cinit__(TrajectoryDataT self,
155155
double time,
156156
object distance,
157157
object velocity,
@@ -191,7 +191,7 @@ cdef class TrajectoryData:
191191
"""Create a TrajectoryData instance from a BaseTrajData object."""
192192
cdef double velocity_mag = mag(&data.velocity)
193193

194-
return TrajectoryData(
194+
return TrajectoryDataT(
195195
time=data.time,
196196
distance=_new_feet(data.position.x),
197197
velocity=_new_fps(velocity_mag),
@@ -212,7 +212,7 @@ cdef class TrajectoryData:
212212

213213
@staticmethod
214214
def interpolate(str key_attribute, double key_value,
215-
TrajectoryData t0, TrajectoryData t1, TrajectoryData t2, int flag):
215+
TrajectoryDataT t0, TrajectoryDataT t1, TrajectoryDataT t2, int flag):
216216
"""
217217
Quadratic (Lagrange) interpolation of a TrajectoryData point.
218218
@@ -263,14 +263,14 @@ cdef class TrajectoryData:
263263
energy = _new_ft_lb(interp_scalar(t0.energy._ft_lb, t1.energy._ft_lb, t2.energy._ft_lb))
264264
ogw = _new_lb(interp_scalar(t0.ogw._lb, t1.ogw._lb, t2.ogw._lb))
265265

266-
return TrajectoryData(
266+
return TrajectoryDataT(
267267
time, distance, velocity, mach,
268268
height, slant_height, drop_adj,
269269
windage, windage_adj, slant_distance,
270270
angle, density_ratio, drag, energy, ogw, flag
271271
)
272272

273-
def formatted(TrajectoryData self) -> tuple[str, ...]:
273+
def formatted(TrajectoryDataT self) -> tuple[str, ...]:
274274
"""
275275
:return: matrix of formatted strings for each value of trajectory in default prefer_units
276276
"""
@@ -300,7 +300,7 @@ cdef class TrajectoryData:
300300
f"{self.flag}" # TODO: fix flag.name
301301
)
302302

303-
def in_def_units(TrajectoryData self) -> tuple[float, ...]:
303+
def in_def_units(TrajectoryDataT self) -> tuple[float, ...]:
304304
"""
305305
:return: matrix of floats of the trajectory in default prefer_units
306306
"""

0 commit comments

Comments
 (0)