# Using Output as Input in FEniCSx and Solving Potential Equation

**URL:** <https://fenicsproject.discourse.group/t/using-output-as-input-in-fenicsx-and-solving-potential-equation/16023>\
**Category:** General\
**Tags:** mesh\
**Created:** [October 11, 2024, 11:14am UTC](https://fenicsproject.discourse.group/t/using-output-as-input-in-fenicsx-and-solving-potential-equation/16023 "2024-10-11T11:14:01Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 14, 2024, 8:06am UTC](https://fenicsproject.discourse.group/t/using-output-as-input-in-fenicsx-and-solving-potential-equation/16023/6 "2024-10-14T08:06:40Z")

</div>

There are various ways of reading this, see for instance:

> <https://github.com/jorgensd/adios4dolfinx/issues/16>
>
> quick and "dirty" python version of reading data at geometry nodes.
> Should be p…roperly implemented with ADIOS2.
> \`\`\`python
> from pathlib import Path
> 
> import adios4dolfinx
> import dolfinx
> import numpy as np
> from mpi4py import MPI
> 
> with dolfinx.io.XDMFFile(MPI.COMM\_WORLD, "idealized\_LV\_ref\_0.xdmf", "r") as xdmf:
> mesh = xdmf.read\_mesh(name="mesh", xpath="Xdmf/Domain/Grid")
> 
> import basix
> import h5py
> 
> 
> def read\_mesh\_data(file\_path:Path, mesh: dolfinx.mesh.Mesh, data\_path:str):
> assert file\_path.is\_file(), f"File {file\_path} does not exist"
> infile = h5py.File(file\_path, "r", driver="mpio", comm=mesh.comm)
> num\_nodes\_global = mesh.geometry.index\_map().size\_global
> assert data\_path in infile.keys(), f"Data {data\_path} does not exist"
> dataset = infile\[data\_path\]
> shape = dataset.shape
> assert shape\[0\] == num\_nodes\_global, f"Got data of shape {shape}, expected {num\_nodes\_global, shape\[1\]}"
> dtype = dataset.dtype
> # Read data locally on each process
> local\_input\_range = adios4dolfinx.comm\_helpers.compute\_local\_range(mesh.comm, num\_nodes\_global)    
> local\_input\_data = dataset\[local\_input\_range\[0\]:local\_input\_range\[1\]\]
> 
> # Create appropriate function space (based on coordinate map)
> assert len(mesh.geometry.cmaps) == 1, "Mixed cell-type meshes not supported"
> element = basix.ufl.element(
> basix.ElementFamily.P,
> mesh.topology.cell\_name(),
> mesh.geometry.cmaps\[0\].degree,
> mesh.geometry.cmaps\[0\].variant,
> shape=(shape\[1\],),
> gdim=mesh.geometry.dim)
> 
> # Assumption: Same doflayout for geometry and function space, cannot test in python    
> V = dolfinx.fem.FunctionSpace(mesh, element)
> uh = dolfinx.fem.Function(V, name=data\_path)
> # Assume that mesh is first order for now
> assert mesh.geometry.cmaps\[0\].degree == 1, "Only linear meshes supported"
> x\_dofmap = mesh.geometry.dofmap
> igi = np.array(mesh.geometry.input\_global\_indices, dtype=np.int64)
> global\_geom\_input = igi\[x\_dofmap\]
> global\_geom\_owner = adios4dolfinx.utils.index\_owner(mesh.comm, global\_geom\_input.reshape(-1), num\_nodes\_global)
> for i in range(shape\[1\]):
> arr\_i = adios4dolfinx.comm\_helpers.send\_dofs\_and\_recv\_values(global\_geom\_input.reshape(-1), global\_geom\_owner, mesh.comm, local\_input\_data\[:,i\], local\_input\_range\[0\])
> dof\_pos = x\_dofmap.reshape(-1)\*shape\[1\]+i
> uh.x.array\[dof\_pos\] = arr\_i
> infile.close()
> return uh
> infile = Path("idealized\_LV\_ref\_0.h5")
> 
> 
> for data in \["f0", "n0", "phi", "psi", "s0"\]:
> uh = read\_mesh\_data(infile, mesh, data)
> with dolfinx.io.XDMFFile(mesh.comm, f"{data}.xdmf", "w") as xdmf:
> xdmf.write\_mesh(mesh)
> xdmf.write\_function(uh)
> \`\`\`
> 
> ref:
> https://fenicsproject.discourse.group/t/i-o-from-xdmf-hdf5-files-in-dolfin-x/3122/47

and

> [@I/O from XDMF/HDF5 files in dolfin-x](https://fenicsproject.discourse.group/t/i-o-from-xdmf-hdf5-files-in-dolfin-x/3122/48):
>
> @rosilho I had some spare time today and made a “quick and dirty” solution to this relying on my adios4dolfinx library ([GitHub - jorgensd/adios4dolfinx: Interface of ADIOS2 for DOLFINx](https://github.com/jorgensd/adios4dolfinx/)) and h5py. I tested it with the file you supplied (in serial and parallel), and got the expected results: from pathlib import Path import adios4dolfinx import dolfinx import numpy as np from mpi4py import MPI with dolfinx.io.XDMFFile(MPI.COMM\_WORLD, "idealized\_LV\_ref\_0.xdmf", "r") as xdmf: mesh = xdmf.read…

---

_[View the full topic](https://fenicsproject.discourse.group/t/using-output-as-input-in-fenicsx-and-solving-potential-equation/16023)._
