Skip to content

Public custom-compound config builder + flat forward/inverse/peak wrappers #123

@grzanka

Description

@grzanka

Part of #118 (dedx_extra.c migration) · Phase E. Depends on Phase C/D and a stoichiometry decision (below).

Problem

To evaluate a user-defined compound, dedx_web builds a dedx_config by hand in wasm/dedx_extra.c (dedx_internal_setup_custom_compound / _cleanup): allocate elements_id[] / elements_atoms[], set rho and optional i_value, set ion_a, then load/evaluate/free. The forward table, inverse STP/CSDA, and Bragg-peak for custom compounds are all flat wrappers around that. None of this is public libdedx API, so every binding re-implements the dedx_config plumbing (and can get the lifecycle/free wrong).

Proposed change

  1. Promote a public config-builder + matching teardown to dedx.h:
    int  dedx_config_set_compound(dedx_config *cfg, int program, int ion,
                                  const int *elements_z, const double *elements_atoms,
                                  int n_elements, double density, double i_value, int *err);
    void dedx_config_free_compound(dedx_config *cfg, int *err);
  2. Add flat one-call wrappers to dedx_wrappers.h: forward STP+CSDA table, inverse STP, inverse CSDA, Bragg-peak STP — each taking the compound spec directly.

Open decision — fractional stoichiometry

dedx_config.elements_atoms is currently int. The web wrapper accepts double atom counts and lround()s them (rejecting ≤0), which loses precision for non-integer formulas (e.g. H2.5O). Phase E is the moment to decide whether libdedx should accept fractional stoichiometry or mass fractions natively. This also intersects #6 (per-element I-value override) — decide whether the public builder takes per-element I-values.

Acceptance criteria

  • dedx_config_set_compound / dedx_config_free_compound in dedx.h, documented, with the int-vs-fractional decision recorded.
  • Flat custom-compound wrappers in dedx_wrappers.h (forward / inverse STP / inverse CSDA / Bragg-peak).
  • Tests building on examples/dedx_custom_compound.c; no leaks under the existing Valgrind CI.
  • dedx_web removes the custom-compound block from wasm/dedx_extra.c and updates wasm/contract-manifest.json + docs/06-wasm-api-contract.md. At this point dedx_extra.c is empty/removed.

Reference

dedx_web wasm/dedx_extra.c (lines ~463–808); recent custom-compound fixes #105, #107.


Filed via Claude Code as part of the dedx_extra.c → libdedx migration plan.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions