Skip to content

Commit 30da55b

Browse files
aligned with the new material structure of libalea
1 parent 9e78f2c commit 30da55b

4 files changed

Lines changed: 51 additions & 7 deletions

File tree

src/aleathor/_binding/_bind_geometry.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,21 +641,50 @@ static PyObject* AleaTHORSystem_get_surface_nodes(AleaTHORSystemObject* self, Py
641641
static PyObject* AleaTHORSystem_add_cell(AleaTHORSystemObject* self, PyObject* args, PyObject* kwds) {
642642
int cell_id;
643643
unsigned long root_node;
644-
int material_id = 0;
644+
int material_index = -1; /* ALEA_MATERIAL_VOID */
645645
double density = 0.0;
646646
int universe_id = 0;
647-
static char* kwlist[] = {"cell_id", "root_node", "material_id", "density", "universe_id", NULL};
647+
static char* kwlist[] = {"cell_id", "root_node", "material_index", "density", "universe_id", NULL};
648648

649649
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ik|idi", kwlist,
650-
&cell_id, &root_node, &material_id, &density, &universe_id)) {
650+
&cell_id, &root_node, &material_index, &density, &universe_id)) {
651651
return NULL;
652652
}
653653
if (!self->sys) { PyErr_SetString(PyExc_RuntimeError, "System not initialized"); return NULL; }
654654

655-
int idx = alea_add_cell(self->sys, cell_id, (alea_node_id_t)root_node, material_id, density, universe_id);
655+
int idx = alea_add_cell(self->sys, cell_id, (alea_node_id_t)root_node, material_index, density, universe_id);
656656
if (idx < 0) {
657657
PyErr_SetString(PyExc_RuntimeError, "Failed to add cell");
658658
return NULL;
659659
}
660660
return PyLong_FromLong(idx);
661661
}
662+
663+
/* ============================================================================
664+
* AleaTHORSystem Methods - Material Registration
665+
* ============================================================================ */
666+
667+
static PyObject* AleaTHORSystem_add_material(AleaTHORSystemObject* self, PyObject* args) {
668+
int material_id;
669+
if (!PyArg_ParseTuple(args, "i", &material_id)) return NULL;
670+
if (!self->sys) { PyErr_SetString(PyExc_RuntimeError, "System not initialized"); return NULL; }
671+
672+
int idx = alea_add_material(self->sys, material_id);
673+
if (idx < 0) {
674+
PyErr_SetString(PyExc_RuntimeError, "Failed to add material");
675+
return NULL;
676+
}
677+
return PyLong_FromLong(idx);
678+
}
679+
680+
static PyObject* AleaTHORSystem_find_material_by_id(AleaTHORSystemObject* self, PyObject* args) {
681+
int material_id;
682+
if (!PyArg_ParseTuple(args, "i", &material_id)) return NULL;
683+
if (!self->sys) { PyErr_SetString(PyExc_RuntimeError, "System not initialized"); return NULL; }
684+
685+
int idx = alea_find_material_by_id(self->sys, material_id);
686+
if (idx < 0) {
687+
Py_RETURN_NONE;
688+
}
689+
return PyLong_FromLong(idx);
690+
}

src/aleathor/_binding/aleathor_binding.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,14 @@ static PyMethodDef AleaTHORSystem_methods[] = {
358358
/* Cell registration */
359359
{"add_cell", (PyCFunction)AleaTHORSystem_add_cell,
360360
METH_VARARGS | METH_KEYWORDS,
361-
"add_cell(cell_id, root_node, material_id=0, density=0.0, universe_id=0) -> index"},
361+
"add_cell(cell_id, root_node, material_index=-1, density=0.0, universe_id=0) -> index\n\n"
362+
"Register a cell. material_index is a material index from add_material(), or -1 for void."},
363+
364+
/* Material registration */
365+
{"add_material", (PyCFunction)AleaTHORSystem_add_material, METH_VARARGS,
366+
"add_material(material_id) -> index\n\nRegister a material by MCNP ID. Returns material index."},
367+
{"find_material_by_id", (PyCFunction)AleaTHORSystem_find_material_by_id, METH_VARARGS,
368+
"find_material_by_id(material_id) -> index or None\n\nFind material index by MCNP ID."},
362369

363370
/* Export */
364371
{"export_mcnp", (PyCFunction)AleaTHORSystem_export_mcnp,

src/aleathor/model.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,22 @@ def _rebuild_if_needed(self):
172172
self._sys.reset()
173173
self._halfspace_node_cache.clear()
174174

175+
# Register materials in C system and build id→index map
176+
material_index_map = {} # MCNP material_id → C material_index
177+
for cell in self._cells.values():
178+
mat_id = cell.material
179+
if mat_id != 0 and mat_id not in material_index_map:
180+
material_index_map[mat_id] = self._sys.add_material(mat_id)
181+
175182
# Build cells
176183
for cell in self._cells.values():
177184
node_id = cell.region._to_csg(self)
178185
# C expects signed density: negative = g/cm³, positive = atoms/b-cm
179186
signed_density = -cell.density if cell.density_unit == "g/cm3" else cell.density
187+
mat_index = material_index_map.get(cell.material, -1) # -1 = void
180188
self._sys.add_cell(
181189
cell.id, node_id,
182-
material_id=cell.material,
190+
material_index=mat_index,
183191
density=signed_density,
184192
universe_id=cell.universe
185193
)

0 commit comments

Comments
 (0)