Skip to content

Commit

Permalink
Merge pull request #20 from o-murphy/interface_remake
Browse files Browse the repository at this point in the history
* @dbookstaber Added look_angle parameter for uphill/downhill shots. 
* Interface remake
  • Loading branch information
o-murphy authored Oct 13, 2023
2 parents 3f39370 + a468969 commit 5a012a2
Show file tree
Hide file tree
Showing 20 changed files with 945 additions and 312 deletions.
32 changes: 32 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Environment: **
- OS: [e.g. iOS]
- Python3 version: [e.g. 3.9, 3.11]
- Release [e.g. 1.0.12]

**Additional context**
Add any other context about the problem here.
9 changes: 7 additions & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ jobs:
# python setup.py build_ext --inplace
- name: Run unittest tests
run: |
# python -m unittest discover -s . -p "tests*.py"
pytest tests --no-header --no-summary -v
if pytest tests --no-header --no-summary -v; then
echo "Pytest succeeded."
else
echo "Pytest failed, running without capture"
# Add your additional commands here
pytest tests --no-header --no-summary -v -s
fi
2 changes: 1 addition & 1 deletion .github/workflows/python-publish-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
86 changes: 49 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
# BallisticCalculator
#### LGPL library for small arms ballistic calculations (Python 3.9+)

## Table of contents
* [Installation](#installation)
* [Usage](#usage)
LGPL library for small arms ballistic calculations (Python 3.9+)

### Table of contents
* **[Installation](#installation)**
* [Latest stable](#latest-stable-release-from-pypi)
* [From sources](#installing-from-sources)
* [Clone and build](#clone-and-build)
* **[Usage](#usage)**
* [Units of measure](#unit-manipulation-syntax)
* [An example of calculations](#an-example-of-calculations)
* [Output example](#example-of-the-formatted-output)
* [Contributors](#contributors)
* [About project](#about-project)

## Installation
**Stable release from pypi, installing from binaries**
* **[Older versions]()**
* [v1.0.x](https://github.com/o-murphy/py_ballisticcalc/tree/v1.0.12)
* **[Contributors](#contributors)**
* **[About project](#about-project)**

(Contains c-extensions which offer higher performance)
### Installation
#### Latest stable release from pypi**
```shell
pip install py-ballisticcalc
```

**Build wheel package for your interpreter version by pypi sdist**

Download and install MSVC or GCC depending on target platform
#### Installing from sources
**MSVC** or **GCC** required
* Download and install **MSVC** or **GCC** depending on target platform
* Use one of the references you need:
```shell
pip install Cython>=3.0.0a10
# no binary from PyPi
pip install py-ballisticcalc --no-binary :all:
```

**Also use `git clone` to build your own package**
# master brunch
pip install git+https://github.com/o-murphy/py_ballisticcalc

(Contains cython files to build your own c-extensions)
# specific branch
pip install git+https://github.com/o-murphy/py_ballisticcalc.git@<target_branch_name>
```

#### Clone and build
**MSVC** or **GCC** required
```shell
git clone https://github.com/o-murphy/py_ballisticcalc
```

cd py_ballisticcalc
python -m venv venv
. venv/bin/activate
pip install cython
python setup.py build_ext --inplace
```

## Usage

Expand Down Expand Up @@ -79,19 +91,21 @@ Distance.Meter(100) > 10 # >>> True, compare unit with float by raw value
#### An example of calculations

```python
from py_ballisticcalc import *
from py_ballisticcalc import Velocity, Temperature, Distance
from py_ballisticcalc import DragModel, TableG7
from py_ballisticcalc import Ammo, Atmo, Wind
from py_ballisticcalc import Weapon, Shot, Calculator
from py_ballisticcalc import Settings as Set


# set global library settings
Set.Units.Mach = Velocity.FPS
Set.Units.velocity = Velocity.FPS
Set.Units.temperature = Temperature.Celsius
Set.Units.distance = Distance.Meter
# Set.Units.distance = Distance.Meter
Set.Units.sight_height = Distance.Centimeter

# set maximum inner Calculator step size, larger is faster, but accuracy is going down
Set.set_max_calc_step_size(Distance.Foot(1)) # same as default
# enable muzzle velocity correction by powder temperature
Set.USE_POWDER_SENSITIVITY = True # default False
Set.set_max_calc_step_size(Distance.Foot(1))
Set.USE_POWDER_SENSITIVITY = True # enable muzzle velocity correction my powder temperature

# define params with default units
weight, diameter = 168, 0.308
Expand All @@ -104,21 +118,19 @@ dm = DragModel(0.223, TableG7, weight, diameter)
ammo = Ammo(dm, length, 2750, 15)
ammo.calc_powder_sens(2723, 0)

zero_atmo = Atmo.icao()
zero_atmo = Atmo.icao(100)

# defining calculator instance
calc = Calculator(weapon, ammo, zero_atmo)
calc.update_elevation()

shot = Shot(1500, 100)

current_atmo = Atmo(100, 1000, 15, 72)
winds = [Wind(2, Angular.OClock(3))]
current_atmo = Atmo(110, 1000, 15, 72)
current_winds = [Wind(2, 90)]
shot = Shot(1500, atmo=current_atmo, winds=current_winds)

data = calc.trajectory(shot, current_atmo, winds)
shot_result = calc.fire(shot, trajectory_step=Distance.Yard(100))

for p in data:
print(p.formatted())
for p in shot_result:
print(p.formatted())
```
#### Example of the formatted output:
```shell
Expand Down
2 changes: 1 addition & 1 deletion py_ballisticcalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__copyright__ = ("",)

__credits__ = ["o-murphy"]
__version__ = "1.1.0b7"
__version__ = "1.1.0b8"

from .drag_model import * # pylint: disable=import-error
from .drag_tables import *
Expand Down
37 changes: 27 additions & 10 deletions py_ballisticcalc/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass, field

from .settings import Settings as Set
from .unit import Distance, Velocity, Temperature, Pressure, is_unit, TypedUnits, Angular
from .unit import Distance, Velocity, Temperature, Pressure, TypedUnits, Angular

__all__ = ('Atmo', 'Wind', 'Shot')

Expand Down Expand Up @@ -61,7 +61,7 @@ def __post_init__(self):
@staticmethod
def icao(altitude: [float, Distance] = 0):
"""Creates Atmosphere with ICAO values"""
altitude = altitude if is_unit(altitude) else Distance(altitude, Set.Units.distance)
altitude = Set.Units.distance(altitude)
temperature = Temperature.Fahrenheit(
cIcaoStandardTemperatureR + (altitude >> Distance.Foot)
* cTemperatureGradient - cIcaoFreezingPointTemperatureR
Expand Down Expand Up @@ -142,17 +142,34 @@ def __post_init__(self):

@dataclass
class Shot(TypedUnits):
"""Stores shot parameters for the trajectory calculation"""
"""
Stores shot parameters for the trajectory calculation
:param max_range: Downrange distance to stop computing trajectory
:param step: Distance between each TrajectoryData row to record
:param zero_angle: The angle between the barrel and horizontal when zeroed
:param relative_angle: Elevation adjustment added to zero_angle for a particular shot
:param cant_angle: Rotation of gun around barrel axis, relative to position when zeroed.
(Only relevant when Weapon.sight_height != 0)
"""
max_range: [float, Distance] = field(default_factory=lambda: Set.Units.distance)
step: [float, Distance] = field(default_factory=lambda: Set.Units.distance)
shot_angle: [float, Angular] = field(default_factory=lambda: Set.Units.angular)
zero_angle: [float, Angular] = field(default_factory=lambda: Set.Units.angular)
relative_angle: [float, Angular] = field(default_factory=lambda: Set.Units.angular)
cant_angle: [float, Angular] = field(default_factory=lambda: Set.Units.angular)
sight_angle: [float, Angular] = field(default_factory=lambda: Set.Units.angular)

atmo: Atmo = field(default=None)
winds: list[Wind] = field(default=None)

def __post_init__(self):
if not self.shot_angle:
self.shot_angle = 0

if not self.relative_angle:
self.relative_angle = 0
if not self.cant_angle:
self.cant_angle = 0
if not self.sight_angle:
self.sight_angle = 0
if not self.zero_angle:
self.zero_angle = 0

if not self.atmo:
self.atmo = Atmo.icao()
if not self.winds:
self.winds = [Wind()]
4 changes: 2 additions & 2 deletions py_ballisticcalc/drag_model.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import typing
from libc.math cimport pow

from .settings import Settings as Set
from .unit import Weight, Distance, is_unit
from .unit import Weight, Distance
from .drag_tables import DragTablesSet

__all__ = ('DragModel', )
Expand Down Expand Up @@ -63,7 +63,7 @@ cdef class DragModel:
else:
raise ValueError('Wrong drag data')

self.weight = weight if is_unit(weight) else Set.Units.weight(weight)
self.weight = Set.Units.weight(weight)
self.diameter = Set.Units.diameter(diameter)
self.sectional_density = self._get_sectional_density()
self.form_factor = self._get_form_factor(self.value)
Expand Down
13 changes: 4 additions & 9 deletions py_ballisticcalc/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,12 @@

# defining calculator instance
calc = Calculator(weapon, ammo, zero_atmo)
calc.update_elevation()

shot = Shot(1500, 100)
print(shot.max_range)
print(zero_atmo.temperature)

current_atmo = Atmo(110, 1000, 15, 72)
winds = [Wind(2, 90)]
print(weapon.sight_height)
current_winds = [Wind(2, 90)]
shot = Shot(1500, atmo=current_atmo, winds=current_winds)

data = calc.trajectory(shot, current_atmo, winds)
shot_result = calc.fire(shot, Distance.Yard(100))

for p in data:
for p in shot_result:
print(p.formatted())
Loading

0 comments on commit 5a012a2

Please sign in to comment.