Metadata-Version: 2.4
Name: funnyhills-parameterization
Version: 0.1.0
Summary: Python bindings for the Brack et al. Funny-Hills {c, h, alpha} nuclear-shape parameterization library
License-Expression: MIT
License-File: LICENSE
Project-URL: Homepage, https://github.com/AleksanderAugustyn/funnyhills-parameterization
Project-URL: Repository, https://github.com/AleksanderAugustyn/funnyhills-parameterization
Project-URL: Changelog, https://github.com/AleksanderAugustyn/funnyhills-parameterization/blob/master/CHANGELOG.md
Requires-Python: >=3.9
Requires-Dist: numpy>=1.21
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Description-Content-Type: text/markdown

# funnyhills-parameterization

The Funny-Hills {c, h, α} nuclear shapes of Brack, Damgaard, Jensen, Pauli,
Strutinsky and Wong (Rev. Mod. Phys. 44 (1972) 320, §VII): a Fortran 2018
library with a C API and Python bindings. It takes `[c, h, alpha]` and
returns the axial surface: R(theta) and dR/dtheta at caller thetas, the
ρ(z) profile on a uniform z grid, the poles, the neck, the resolved
coefficients A and B with their branch, the (c, h)-plane reference lines,
and the diagnostics q2 and ρ_cm.

The library follows the two-tier shape parameterization contract shared with
[beta-parameterization](https://github.com/AleksanderAugustyn/beta-parameterization),
[fos-parameterization](https://github.com/AleksanderAugustyn/fos-parameterization),
[tqs-parameterization](https://github.com/AleksanderAugustyn/tqs-parameterization)
and [cassini-parameterization](https://github.com/AleksanderAugustyn/cassini-parameterization):
one-shot functions, plus a read-only cache that is built once and shared
across threads. Like tqs it is fixed-arity: exactly three parameters.

## The shape

In R0 = 1 units (volume 4π/3), with u ∈ [−1, 1], z = c u − z_cm and
ρ = c √P(u):

    B ≥ 0 (polynomial branch, eq. VII.4):    P = (1 − u²)(A + B u² + α u)
    B < 0 (exponential branch, eq. VII.5):   P = (1 − u²)(A + α u) exp(B c³ u²)
    B = 2h + (c − 1)/2                        (eq. VII.7)

The sphere is `[1, 0, 0]`. The line h = −(c − 1)/4 is the ellipsoid line
(B = 0, the branch switch); h = 0 approximately follows the liquid-drop
valley. A is fixed by exact volume conservation: A = 1/c³ − B/5 on the
polynomial branch (VII.7) and A = 4/(3 c³ K₀₁(B c³)) on the exponential one,
with K₀₁ a positive-term power series. This is the literal reading of the
paper (its eq. VII.6 is exact for VII.4 and "approximately equal" for VII.5):
c is the true half-length on both branches, so the length is exactly 2c
independently of α. The origin is the centre of mass of every connected
shape: z_cm = α c⁴/5 on the polynomial branch, (3/4) α c⁴ K₁₁(B c³) on the
exponential one (for separated shapes see below). Every
integral is closed form; the only root-finds are R(θ) per theta and the
between-node refinement of the star margin.

**What is accepted and what is flagged.**

- **Separated** (103 on the R(θ) paths, `shape` and the checked profile): on
  the polynomial branch with B > 0, f = A + αu + Bu² dips below zero at
  u* = −α/(2B) inside (−1, 1). The unchecked profile draws both fragments
  with ρ = 0 in the void; `geometry` reports the gap and `lobe_volume`. Past
  scission the paper's normalization integrates P over the whole interval,
  void included, so the fragments hold more than 4π/3 (5.7% more at
  (2.2, 0, 0), 10.8% at (1.9, 0.3, 0)); `lobe_volume` says by how much.
  The frame follows the paper's full-interval convention: z_cm = c ∫uP/∫P
  over the whole of [−1, 1], void included (α c⁴/5 on the polynomial
  branch, whatever B). For a symmetric separated shape that is the
  fragments' centre of mass; for an asymmetric one it is not, and the
  offset is large. SEPARATED_ASYM `[2.2, 0.3, 0.2]` puts the origin 0.40
  above the fragments' centre of mass. Once α c³ > 5 the origin lies
  beyond the north pole and `geometry` reports a negative `r_north`: at
  `[2.5, 0.6, 0.5]` the body spans z ∈ [−6.41, −1.41], `r_north = −1.41`,
  and the fragments' centre of mass is at −2.92. Physics paths are
  unaffected (every R(θ) output and `shape` report 103 there), so this
  matters only to a caller drawing or splitting separated shapes.
- **Pinched** (102 on the R(θ) paths): |f(u*)| ≤ 1e-12, the scission line.
- **Pole-negative** (101, every path but `coefficients`): f(±1) ≤ 0, i.e.
  A + B ≤ |α| (polynomial) or A ≤ |α| (exponential): ρ² < 0 next to a pole,
  outside the family.
- **c ≤ 0** (100, every path).

**The star gate.** R(θ) exists only where the surface is star-convex from
the centre of mass. The gate quantity is d, the signed distance from the
origin to the tangent line of the profile curve (1 on the sphere, the pole
radius at a pole). d is sampled on the scan in φ = arccos u, every sampled
local minimum is refined by golden section between its scan neighbours, and
the gate passes iff the refined minimum `d_min` ≥ 1e-3 (provisional,
inclusive). Sampling alone is not enough: `[2.13, −0.165, 0.2]` samples
d = +2.9e-3 on a 65-node scan, but its minimum is −8.4e-4 and the ray at
θ ≈ 3.0244 meets the surface three times. `d_min` is exported on every
resolvable shape.

**The neck** is exact: the critical points of P are the roots of the cubic
P'(u) = −4Bu³ − 3αu² + 2(B − A)u + α, and a neck exists iff all three are real
and inside (−1, 1). The exponential branch and the ellipsoid line never have
one (P is log-concave there). Pinched and separated shapes report the split
plane z = c u* − z_cm with ρ_neck = 0.

**ρ_cm** is half the distance between the mass centres of the halves u < 0
and u > 0 (sphere 3/8), as the paper's prose (p. 368) and figs. VII-3 and
VIII-3 use it. The printed eq. VII.17a is twice that (sphere 3/4); the
library follows the prose and the figures. **q2** is ⟨r² P2(cos θ)⟩ with the
reference normalization 3/(4π) in the library frame; for a separated shape it
is integrated over the fragments, not divided by `lobe_volume`.

## Two tiers

1. **One-shot.** One call computes a shape and keeps nothing.
2. **Read-only cache.** Build a cache once (the scan resolution `n_u_grid`
   and a primary theta set; `max_params` must be 3), then request any number
   of shapes against it. Nothing derived from a shape parameter is stored, so
   no call influences a later one, and any number of threads may compute on
   one cache at once.

The two tiers return the same status and bitwise-identical outputs.

| Output | Cached | One-shot |
|---|---|---|
| R(theta) at the primary thetas | `cache_radius_grid_s` | `compute_radius_grid_standalone_s` |
| R(theta) and dR/dtheta | `cache_radius_and_derivative_s` | `compute_radius_and_derivative_standalone_s` |
| R(theta) and dR/dtheta at caller thetas | `cache_radius_and_derivative_at_thetas_s` | (the one-shot forms take caller thetas) |
| z_shift and pole distances, with the R(theta) verdict | `cache_shape_s` | `compute_shape_standalone_s` |
| profile (z, rho, drho/dz) on uniform z, checked | `cache_rho_z_grid_s` | `compute_rho_z_grid_standalone_s` |
| profile, separated shapes included | `cache_rho_z_grid_unchecked_s` | `compute_rho_z_grid_unchecked_standalone_s` |
| neck position and radius | `cache_neck_s` | `compute_neck_standalone_s` |
| geometry: branch, A, B, z_shift, poles, gap, lobe_volume, d_min, flags | `cache_geometry_s` | `compute_geometry_standalone_s` |
| q2 and ρ_cm | `cache_moments_s` | `compute_moments_standalone_s` |
| A, B, branch, h_ellipsoid and h_scission at this c | `cache_coefficients_s` | `compute_coefficients_standalone_s` |

The C functions are `funnyhills_param_cache_<output>` and
`funnyhills_param_<output>`; the Python names are the methods of `Cache` and
the module-level functions.

### Fortran

```fortran
use funnyhills_parameterization_mod, only: cache_t, cache_init_s, cache_radius_grid_s, &
        compute_geometry_standalone_s, funnyhills_geometry_t

type(cache_t) :: cache
type(funnyhills_geometry_t) :: geometry
real(kind = rk) :: thetas(64), radii(64)
integer(kind = ik) :: status

! One-shot
call compute_geometry_standalone_s([1.6_rk, 0.3_rk, 0.2_rk], 2049_ik, geometry, status)

! Cached
call cache_init_s(cache, 3_ik, 2049_ik, thetas, status)
call cache_radius_grid_s(cache, [1.0_rk, 0.0_rk, 0.0_rk], radii, status)
```

### C

```c
#include "funnyhills_parameterization.h"

const double params[3] = {1.6, 0.3, 0.2};
int32_t status;
funnyhills_param_cache_t *cache = funnyhills_param_cache_create(3, 2049, thetas, n_thetas, &status);
status = funnyhills_param_cache_radius_grid(cache, params, 3, radii, n_thetas);
funnyhills_param_geometry_t geometry;
funnyhills_param_geometry(params, 3, 2049, &geometry, &status);   /* one-shot */
funnyhills_param_cache_destroy(cache);
```

### Python

```python
import funnyhills_parameterization as fp

print(fp.moments([1.0, 0.0, 0.0]))                 # one-shot: q2 = 0, rho_cm = 0.375

params = [1.6, 0.3, 0.2]
with fp.Cache(2049, fp.theta_grid(64)) as cache:   # cached
    res = cache.radius_and_derivative(params)
    if res.ok:
        print(res.radii, res.dr_dtheta)
    print(cache.geometry(params), cache.coefficients(params))
```

## Status codes

Every fallible routine reports an integer status; `0` is success. On any
nonzero status every output is zero-filled.

| Code | Name | Meaning |
|---|---|---|
| 1 | `too_many_params` | `max_params > 3`, or a one-shot call with more than 3 parameters |
| 2 | `cache_not_initialized` | compute on a cache that was not built |
| 3 | `invalid_grid` | no thetas, a theta outside `[0, pi]`, `n_u_grid < 65`, a profile under 3 nodes, or an output array of the wrong size |
| 4 | `wrong_param_count` | a cached call without exactly 3 parameters, or a one-shot call with fewer |
| 5 | `invalid_init` | `max_params < 3` (and, in C, a negative size argument to a one-shot call) |
| 100 | `invalid_c` | c ≤ 0 |
| 101 | `pole_negative` | ρ² negative next to a pole: not in the family |
| 102 | `not_star_convex` | R(theta) outputs and `shape` only: pinched, or star margin below 1e-3 |
| 103 | `separated` | R(theta) outputs, `shape` and the checked profile: two fragments |

Precedence on every path: usage codes, then 100, 101, 103, 102. `neck`,
`geometry`, `moments` and the unchecked profile fail only with usage codes,
100 and 101; `coefficients` only with usage codes and 100. Codes 1-5 are
shared with the other shape libraries. Inputs must be finite: the release
build cannot detect NaN.

## Build and install

**Python.** 0.1.0 is not on PyPI yet. Build the wheel from a checkout:

    python -m pip install build
    python -m build --wheel --outdir dist_raw
    python -m pip install dist_raw/funnyhills_parameterization-*.whl

**CMake.** Fetch the library into a Fortran or C/C++ project:

```cmake
include(FetchContent)
FetchContent_Declare(
        funnyhills-parameterization
        GIT_REPOSITORY https://github.com/AleksanderAugustyn/funnyhills-parameterization.git
        GIT_TAG 0.1.0
)
set(FUNNYHILLS_PARAM_BUILD_TESTS OFF)
FetchContent_MakeAvailable(funnyhills-parameterization)

target_link_libraries(my_target PRIVATE FunnyhillsParameterization::funnyhills_parameterization)
```

Targets: `FunnyhillsParameterization::funnyhills_parameterization` (static,
Fortran modules), `FunnyhillsParameterization::funnyhills_parameterization_shared`
(shared library) and `FunnyhillsParameterization::funnyhills_parameterization_cxx`
(the C header plus the shared library).

**From a checkout.**

    cmake -S . -B build/release -DCMAKE_BUILD_TYPE=Release
    cmake --build build/release -j
    ctest --test-dir build/release --output-on-failure -E geometry_sweep
    FUNNYHILLS_PARAM_LIB=$PWD/build/release/libfunnyhills_parameterization.so PYTHONPATH=$PWD/python \
        python -m pytest python/tests -q

The geometry sweep over the published window is its own pair of ctests
(`ctest -L sweep`: the default pass and `--full`). The Python loader prefers
`FUNNYHILLS_PARAM_LIB`, then the library inside an installed wheel, then
`build/release`: rebuild `build/release` after pulling, or a stale build can
shadow the source you are testing.

## Version pins

| Dependency | Version |
|---|---|
| [gcc-compiler-options](https://github.com/AleksanderAugustyn/gcc-compiler-options) | 2.0.0 |
| [fortran-foundations](https://github.com/AleksanderAugustyn/fortran-foundations) | 3.0.0 |

Both are fetched by CMake. The library needs gfortran 10 or newer; it is
developed with gfortran 13. Python 3.9 or newer, NumPy 1.21 or newer.

## Changelog and license

See the
[changelog](https://github.com/AleksanderAugustyn/funnyhills-parameterization/blob/master/CHANGELOG.md).
MIT license.
