Skip to content

Commit 39b7252

Browse files
authored
Merge pull request #63 from underworldcode/BK-xdmfFixes
Fix for xdmf output
2 parents b6c37eb + 51784ed commit 39b7252

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

src/underworld3/discretisation/enhanced_variables.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ def _gvec(self):
386386
"""Global PETSc vector."""
387387
return self._base_var._gvec
388388

389+
def _sync_lvec_to_gvec(self):
390+
"""Synchronize local PETSc vector to global PETSc vector."""
391+
return self._base_var._sync_lvec_to_gvec()
392+
389393
# === MATHEMATICAL SYMBOL ===
390394

391395
@property

src/underworld3/utilities/uw_petsc_gen_xdmf.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -401,24 +401,26 @@ def write(
401401
self.writeSpaceGridFooter(fp)
402402
self.writeSpaceGridHeader(fp, numCells, numCorners, cellDim, spaceDim)
403403
for vf in vfields:
404+
vpath = vf[2] if len(vf) > 2 else "/vertex_fields/" + vf[0]
404405
self.writeField(
405406
fp,
406407
len(time),
407408
t,
408409
cellDim,
409410
spaceDim,
410-
"/vertex_fields/" + vf[0],
411+
vpath,
411412
vf,
412413
"Node",
413414
)
414415
for cf in cfields:
416+
cpath = cf[2] if len(cf) > 2 else "/cell_fields/" + cf[0]
415417
self.writeField(
416418
fp,
417419
len(time),
418420
t,
419421
cellDim,
420422
spaceDim,
421-
"/cell_fields/" + cf[0],
423+
cpath,
422424
cf,
423425
"Cell",
424426
)
@@ -442,6 +444,40 @@ def write(
442444
return
443445

444446

447+
def _collect_mesh_fields(h5, numVertices, numCells):
448+
"""Collect mesh fields from HDF5 in either new (/fields) or legacy layouts."""
449+
vfields = []
450+
cfields = []
451+
452+
# New layout: /fields/<name>, with /fields/coordinates reserved for point locations
453+
if "fields" in h5:
454+
for name, dataset in h5["fields"].items():
455+
if name == "coordinates":
456+
continue
457+
458+
dims = tuple(dataset.shape)
459+
has_vertices_dim = numVertices in dims
460+
has_cells_dim = numCells in dims
461+
462+
item = (name, dataset, f"/fields/{name}")
463+
464+
if has_cells_dim and not has_vertices_dim:
465+
cfields.append(item)
466+
else:
467+
# Default to node-centered when ambiguous
468+
vfields.append(item)
469+
470+
return vfields, cfields
471+
472+
# Legacy layout
473+
if "vertex_fields" in h5:
474+
vfields = list(h5["vertex_fields"].items())
475+
if "cell_fields" in h5:
476+
cfields = list(h5["cell_fields"].items())
477+
478+
return vfields, cfields
479+
480+
445481
def generateXdmf(hdfFilename, xdmfFilename=None):
446482
if xdmfFilename is None:
447483
xdmfFilename = os.path.splitext(hdfFilename)[0] + ".xmf"
@@ -486,19 +522,13 @@ def generateXdmf(hdfFilename, xdmfFilename=None):
486522
time = np.array(h5["time"]).flatten()
487523
else:
488524
time = [-1]
489-
vfields = []
490-
cfields = []
491-
pfields = []
525+
vfields, cfields = _collect_mesh_fields(h5, numVertices, numCells)
492526
pfields = []
493-
if "vertex_fields" in h5:
494-
vfields = h5["vertex_fields"].items()
495-
if "cell_fields" in h5:
496-
cfields = h5["cell_fields"].items()
497527
numParticles = 0
498528
if "particles" in h5:
499529
numParticles = h5["particles"]["coordinates"].shape[0]
500530
if "particle_fields" in h5:
501-
pfields = h5["particle_fields"].items()
531+
pfields = list(h5["particle_fields"].items())
502532

503533
# Write Xdmf
504534
Xdmf(xdmfFilename).write(
@@ -568,19 +598,13 @@ def generate_uw_Xdmf(hdfFilename, xdmfFilename=None):
568598
time = np.array(h5["time"]).flatten()
569599
else:
570600
time = [-1]
571-
vfields = []
572-
cfields = []
601+
vfields, cfields = _collect_mesh_fields(h5, numVertices, numCells)
573602
pfields = []
574-
pfields = []
575-
if "vertex_fields" in h5:
576-
vfields = h5["vertex_fields"].items()
577-
if "cell_fields" in h5:
578-
cfields = h5["cell_fields"].items()
579603
numParticles = 0
580604
if "particles" in h5:
581605
numParticles = h5["particles"]["coordinates"].shape[0]
582606
if "particle_fields" in h5:
583-
pfields = h5["particle_fields"].items()
607+
pfields = list(h5["particle_fields"].items())
584608

585609
# Write Xdmf
586610
Xdmf(xdmfFilename).write(

0 commit comments

Comments
 (0)