Skip to content

Crystal in from Python arrays (WP3 slice 2) and the ASE bridge - #60

Merged
tproffen merged 3 commits into
claude-python-appfrom
claude/inspiring-brahmagupta-ne81m7
Nov 24, 2026
Merged

tproffen merged 3 commits into
claude-python-appfrom
claude/inspiring-brahmagupta-ne81m7

Conversation

@tproffen

@tproffen tproffen commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Targets claude-python-app (not master).

What

The second slice of step 4 / WP3: a crystal goes from Python into DISCUS without a file, plus the ASE bridge.

C interface (version 3): dc_set_crystal replaces DISCUS's crystal from arrays:

  • lattice and space group (recorded, not applied)
  • unit cells and atoms per cell
  • atom types: names, B, occupancy
  • atoms: positions as double[n][3], types, property flags

It follows the steps of read stru in structur.f90:

  1. rese_cr and allocation
  2. header
  3. atoms, set as struc_read_atoms sets them
  4. cell counts, as do_readstru_disk sets them
  5. setup_lattice, get_symmetry_matrices, prep_anis and the stack reset

On an error it calls errlist, so dc_last_error carries the message, for example an unknown space group.

Python

  • dc.make_crystal(lattice, names, positions, space_group=, b_iso=, occupancy=, cells=, types=)
    • B and occupancy as one value, one per name, or one per atom.
    • As in DISCUS, a type is name + B + occupancy. The optional types label keeps further splits, such as the per-site types a .cell file gives.
    • A crystal made this way is sent to DISCUS again if another structure was loaded meanwhile.
  • dc.from_ase(atoms) and Crystal.to_ase(). The ASE cell is the whole crystal; DISCUS names are kept in atoms.arrays["discus_names"].
  • Crystal.path may be None; new Crystal.label.
  • Fixed: Crystal's short repr was defined inside StructureView by mistake. It is now on Crystal, with the space group.

Packaging: ASE is a default dependency. The owner wants an inclusive default install, with extras such as .[no-jupyter] to leave parts out. Plan WP1 records how to get there:

  • PEP 771 default extras, not yet in a pip release
  • until then, plain dependencies plus a documented lean install
  • as a fallback, diffuse-core plus diffuse

The decision is deferred to the first release; Jupyter stays an extra until then.

Tests

New file tests/python/test_wp3_crystal_in.py:

  • Round trip file → arrays → make_crystal → arrays: identical for the nine golden structures.
  • Powder (Debye), Fourier plane and PDF of the crystal made from arrays equal those of the same structure read from its file (Ni, CuO, 3×3×3).
  • A crystal made from arrays is reloaded after another structure was read.
  • Type grouping, bad input, unknown space group.
  • ASE: a bulk crystal; to_ase() and back gives the same Debye pattern; ASE's CIF reader gives the same anatase cell as DISCUS, an independent check of the space-group expansion.

test_capi also calls dc_set_crystal, including a bad type and a bad space group.

Local results:

  • pytest tests/python: 168 passed, 34 skipped, 2 xfailed
  • Sphinx -W: OK
  • The new notebook also ran in a Jupyter kernel, 3D views included.

Docs and examples

  • New script crystals_from_python.py (gallery): a NumPy-built crystal, oxygen vacancies in anatase, and a gold nanoparticle built with ASE with its PDF.
  • New notebook crystals_and_ase.ipynb, committed without outputs:
    • what a Crystal holds, and view()
    • a random Cu₃Au alloy built with NumPy, its powder pattern against pure Cu
    • atom types
    • to_ase() with ASE's neighbour list (Ti–O bonds in anatase) and its file writers
    • from_ase() from a CIF read by ASE, and from gold icosahedron and octahedron clusters (Debye patterns)
    • the round trip
  • api.md, the install docs, the examples README, the binding decision and the plan (WP1 install note, WP3 progress).

🤖 Generated with Claude Code

https://claude.ai/code/session_018Yw8KkfPLawp8Po4f5Bc9v

…E bridge

dc_set_crystal (C interface version 3) replaces DISCUS's crystal from
arrays: lattice, space group (recorded, not applied), unit cells and atoms
per cell, atom types (names, B, occupancy) and atoms (positions as
double[n][3], types, property flags). It follows the steps of "read stru":
rese_cr, allocation, header, atoms as struc_read_atoms sets them (surface,
magnetic and value zero, as_* list), cell counts as do_readstru_disk, then
setup_lattice, get_symmetry_matrices, prep_anis, stack reset. Errors call
errlist, so dc_last_error has the message (e.g. an unknown space group).

Python:
* dc.make_crystal(lattice, names, positions, space_group=, b_iso=,
  occupancy=, cells=, types=): B and occupancy as one value, per name or
  per atom; as in DISCUS a type is name + B + occupancy (+ an optional
  label, e.g. the site: .cell files give each site its own type).
  A Crystal made this way is sent again when DISCUS holds another
  structure meanwhile.
* dc.from_ase(atoms) and Crystal.to_ase() (ASE cell = the whole crystal,
  DISCUS names kept in atoms.arrays["discus_names"]).
* Crystal.path may be None (made from arrays); Crystal.label; the short
  Crystal repr (it sat in StructureView by mistake) now includes the
  space group.
* ASE is a default dependency: inclusive default install (owner, Nov
  2026); the plan (WP1) records the options for a lean install.

Tests (tests/python/test_wp3_crystal_in.py, test_capi): the round trip
file -> arrays -> make_crystal -> arrays is identical for the nine golden
structures; powder, Fourier plane and PDF equal those of the structure
read from its file (Ni, CuO); reload after another structure; type
grouping; bad input; unknown space group; ASE bulk, to_ase and back (same
Debye pattern), and ASE's CIF reader gives the same anatase cell as
DISCUS's. Example crystals_from_python.py (NumPy crystal, O vacancies in
anatase, Au nanoparticle from ASE and its PDF); api.md, install docs,
binding decision and plan updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yw8KkfPLawp8Po4f5Bc9v
…terim and fallback

The owner's intent: pip install . installs everything, an extra such as
.[no-jupyter] leaves a part out. Standard extras only add; PEP 771 default
extras would do this but is not in a pip release yet (Nov 2026). Interim:
plain dependencies and a documented lean install; fallback at the first
release: diffuse-core + diffuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yw8KkfPLawp8Po4f5Bc9v
What a Crystal holds (lattice, space group, atoms(), atom_types(),
cartesian(), lattice_vectors(), view()); a random Cu3Au alloy built with
NumPy and make_crystal, powder pattern against pure Cu; atom types (name +
B + occupancy); to_ase() with ASE's neighbour list (Ti-O bonds of anatase)
and file writers (into a temporary folder); from_ase() from a CIF read by
ASE and from gold clusters (icosahedron vs octahedron, Debye patterns);
the round trip. Committed without outputs; runs as a script in the
notebook test (view cells tagged jupyter-only) and in a Jupyter kernel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yw8KkfPLawp8Po4f5Bc9v
@tproffen
tproffen merged commit 197e91b into claude-python-app Nov 24, 2026
6 checks passed
@tproffen
tproffen deleted the claude/inspiring-brahmagupta-ne81m7 branch November 24, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants