Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
NengLu opened this issue Oct 22, 2024 · 4 comments

Comments

@NengLu
Copy link
Contributor

NengLu commented Oct 22, 2024

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
Copy link
Contributor Author

NengLu commented Nov 18, 2024

move to #268

@NengLu NengLu closed this as completed Nov 18, 2024
@knepley
Copy link
Collaborator

knepley commented Nov 18, 2024

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
Copy link
Contributor Author

NengLu commented Nov 18, 2024

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
Copy link
Member

lmoresi commented Nov 19, 2024

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.

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

No branches or pull requests

3 participants