Skip to content

Commit

Permalink
Merge pull request #12891 from KratosMultiphysics/core/fix-numpy-depr…
Browse files Browse the repository at this point in the history
…ecation

[Core][Tests] Fixing numpy copy=false
  • Loading branch information
roigcarlo authored Nov 29, 2024
2 parents 1d7ec51 + 6c1f603 commit f957ad1
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ jobs:

- name: CI configuration
shell: bash
run: python3 kratos/python_scripts/testing/ci_utilities.py
run: |
python3 kratos/python_scripts/testing/ci_utilities.py
python3 -m pip install --upgrade numpy==1.26.4
- name: Build
shell: bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def AppendCurrentStepResiduals(self):
err_msg = f"Projection strategy \'{self.projection_strategy}\' for HROM is not supported."
raise Exception(err_msg)

np_res_mat = np.array(res_mat, copy=False)
np_res_mat = np.asarray(res_mat)
self.time_step_residual_matrix_container.append(np_res_mat)

def GetJacobianPhiMultiplication(self, computing_model_part):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def AppendCurrentStepProjectedSystem(self):
err_msg = "\'self.basis_strategy\' is not available. Select either 'jacobian' or 'residuals'."
raise Exception(err_msg)

np_snapshots_matrix = np.array(snapshots_matrix, copy=False)
np_snapshots_matrix = np.asarray(snapshots_matrix)
self.time_step_snapshots_matrix_container.append(np_snapshots_matrix)

def GetJacobianPhiMultiplication(self, computing_model_part):
Expand Down
2 changes: 1 addition & 1 deletion applications/RomApplication/python_scripts/rom_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def ModifyInitialGeometry(self):

print('Nodal variables: ', nodal_unknown_names)

s_default = np.array(s, copy=False)
s_default = np.asarray(s)
print(s_default.shape)

q, _ = nn_rom_interface.get_encode_function()(s_default)
Expand Down
2 changes: 1 addition & 1 deletion applications/RomApplication/python_scripts/rom_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ def Initialize(cls):

def GetNonconvergedSolutions(cls):
a,_ = cls._GetSolver()._GetSolutionStrategy().GetNonconvergedSolutions()
return np.array(a, copy=False)
return np.asarray(a)

simulation.Initialize = types.MethodType(Initialize, simulation)
simulation.GetNonconvergedSolutions = types.MethodType(GetNonconvergedSolutions, simulation)
Expand Down
12 changes: 6 additions & 6 deletions kratos/python_scripts/petsc_conversion_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ def __init__(self, A): #Kratos DistributedCsrMatrix
N = A.GetColNumbering().Size()

##pointers to the kratos arrays of values
self.a = np.array(A.GetDiagonalBlock().value_data(), copy=False)
self.oa = np.array(A.GetOffDiagonalBlock().value_data(), copy=False)
self.a = np.asarray(A.GetDiagonalBlock().value_data())
self.oa = np.asarray(A.GetOffDiagonalBlock().value_data())

if(type(m) == type(PETSc.IntType)): #we can avoid doing copies
#indices of diagonal block
self.i = np.array(A.GetDiagonalBlock().index1_data(), copy=False)
self.j = np.array(A.GetDiagonalBlock().index2_data(), copy=False)
self.i = np.asarray(A.GetDiagonalBlock().index1_data())
self.j = np.asarray(A.GetDiagonalBlock().index2_data())

#indices of offdiagonal block
self.oi = np.array(A.GetOffDiagonalBlock().index1_data(), copy=False)
self.oj = np.array(A.GetOffDiagonalIndex2DataInGlobalNumbering(), copy=False)
self.oi = np.asarray(A.GetOffDiagonalBlock().index1_data())
self.oj = np.asarray(A.GetOffDiagonalIndex2DataInGlobalNumbering())

else: #WE NEED TO COPY INDICES! since the size of the IndexType is not compatible between Kratos and PETSc
if(A.GetComm().Rank() == 0):
Expand Down
4 changes: 2 additions & 2 deletions kratos/tests/test_numpy_export_dense_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_numpy_export_dense_matrix_no_copying(self):
KratosMatrix.fill(1.0)

# Export it to numpy array (No copying is performed)
NumpyMatrix = np.array(KratosMatrix,copy=False)
NumpyMatrix = np.asarray(KratosMatrix)

# Test correct creation
for i in range(3):
Expand All @@ -35,7 +35,7 @@ def test_numpy_export_complex_dense_matrix_no_copying(self):
KratosMatrix.fill(1.0+1.0j)

# Export it to numpy array (No copying is performed)
NumpyMatrix = np.array(KratosMatrix,copy=False)
NumpyMatrix = np.asarray(KratosMatrix)

# Test correct creation
for i in range(3):
Expand Down
2 changes: 1 addition & 1 deletion kratos/tests/test_vectorized_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def testVectorizedInterpolation(self):

#obtaining a copy of database onto numpy array, and reshaping it into matrices
nnodes = len(self.mp.Nodes)
vdata = np.array(Kratos.VariableUtils().GetSolutionStepValuesVector(self.mp.Nodes, Kratos.VELOCITY, 0, 3),copy=False).reshape(nnodes,3)
vdata = np.asarray(Kratos.VariableUtils().GetSolutionStepValuesVector(self.mp.Nodes, Kratos.VELOCITY, 0, 3)).reshape(nnodes,3)
coords = np.array(Kratos.VariableUtils().GetCurrentPositionsVector(self.mp.Nodes,3)).reshape(nnodes,3)

##coordinates to search for
Expand Down

0 comments on commit f957ad1

Please sign in to comment.