underworldcode / underworld3

https://underworldcode.github.io/underworld3/
Other
21 stars 10 forks source link

AttributeError: 'tuple' object has no attribute 'reshape' when using swarm.getField("DMSwarmPIC_coor") #257

Closed NengLu closed 5 days ago

NengLu commented 1 month ago

Branch tested: development PETSc version: 3.15.0; 3.22.0 Python version: 3.11; 3.12 Issue description:

Error:

AttributeError                            Traceback (most recent call last)
     PIC_coords = tempSwarm.getField("DMSwarmPIC_coor").reshape(-1, self.dim)
     PIC_cellid = tempSwarm.getField("DMSwarm_cellid")
AttributeError: 'tuple' object has no attribute 'reshape'

The swarm field data obtained from the function .getField() in FETSC used to be an array([]), but in the new version of PETSC, it becomes a tuple (array([]), dim). Easy to be fixed in related scripts (swam.py and discretisation.py) as:

getField("DMSwarm_cellid")[0]
getField("DMSwarmPIC_coor")[0]

Code to test:

import petsc4py.PETSc as PETSc
import underworld3 as uw
from underworld3.swarm import Swarm, SwarmPICLayout

mesh = uw.meshing.StructuredQuadBox(elementRes=(4,4), minCoords=(0, 0), maxCoords=(1, 1)) 
tempSwarm = PETSc.DMSwarm().create()
tempSwarm.setDimension(mesh.dim)
tempSwarm.setCellDM(mesh.dm)
tempSwarm.setType(PETSc.DMSwarm.Type.PIC)
tempSwarm.finalizeFieldRegister()
tempSwarm.insertPointUsingCellDM(PETSc.DMSwarm.PICLayoutType.LAYOUT_GAUSS, 2)

PIC_coords = tempSwarm.getField("DMSwarmPIC_coor").reshape(-1, self.dim)
PIC_cellid = tempSwarm.getField("DMSwarm_cellid")
NengLu commented 5 days ago

move to https://github.com/underworldcode/underworld3/issues/268

knepley commented 5 days ago

So, I had complaints about this and was going to go back to a simple array with the dimensions already in it so you do not have to reshape. I will try and mail once that lands so we can switch back to the sensible interface. Sorry about the churn.

NengLu commented 5 days ago

So, I had complaints about this and was going to go back to a simple array with the dimensions already in it so you do not have to reshape. I will try and mail once that lands so we can switch back to the sensible interface. Sorry about the churn.

Thanks for the update and your efforts to resolve the concerns.

lmoresi commented 4 days ago

Great ...

Neng, many ways around this so that we can cope with users installing any version they like. Catch the return value and see if it is a tuple, or try / except, or use the PETSc version to see what needs to be done.