petsc4py:从csc_matrix创建AIJ矩阵会导致TypeError [英] petsc4py: Creating AIJ Matrix from csc_matrix results in TypeError

查看:189
本文介绍了petsc4py:从csc_matrix创建AIJ矩阵会导致TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个已经存在的csc矩阵创建一个petsc矩阵.考虑到,我创建了以下示例代码:

import numpy as np
import scipy.sparse as sp
import math as math
from petsc4py import PETSc
n=100

A = sp.csc_matrix((n,n),dtype=np.complex128)
print A.shape
A[1:5,:]=1+1j*5*math.pi

p1=A.indptr
p2=A.indices
p3=A.data
petsc_mat = PETSc.Mat().createAIJ(size=A.shape,csr=(p1,p2,p3))

只要矩阵仅由实数值组成,此方法就可以很好地工作.当矩阵很复杂时,运行这段代码会导致 TypeError: Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'. 我试图找出错误的确切位置,但对回溯的理解却不大:

petsc_mat = PETSc.Mat().createAIJ(size=A.shape,csr=(p1,p2,p3))  File "Mat.pyx", line 265, in petsc4py.PETSc.Mat.createAIJ (src/petsc4py.PETSc.c:98970)
File "petscmat.pxi", line 662, in petsc4py.PETSc.Mat_AllocAIJ (src/petsc4py.PETSc.c:24264)
File "petscmat.pxi", line 633, in petsc4py.PETSc.Mat_AllocAIJ_CSR (src/petsc4py.PETSc.c:23858)
File "arraynpy.pxi", line 136, in petsc4py.PETSc.iarray_s (src/petsc4py.PETSc.c:8048)
File "arraynpy.pxi", line 117, in petsc4py.PETSc.iarray (src/petsc4py.PETSc.c:7771)

是否存在从复杂的scipy csc矩阵创建petsc矩阵(我想稍后再检索一些本征对)的有效方法?

如果你们能帮助我发现我的(希望不太明显)错误,我将非常高兴.

解决方案

我很难使PETSc正常工作,因此我不只对其进行了一次配置,在上一次运行中,我显然忘记了选项--with-scalar-type=complex.

这是我应该做的:

  • 要么检查日志文件$PETSC_DIR/arch-linux2-c-opt/conf/configure.log.

  • 或者看看reconfigure-arch-linux2-c-opt.py.

您可以在其中找到用于配置PETSc的所有选项.如果还使用SLEPc,则还需要重新编译它.现在,由于我在重新配置脚本中添加了选项(--with-scalar-type=complex)并运行了该脚本,因此一切正常.

I am trying to create a petsc-matrix form an already existing csc-matrix. With this in mind I created the following example code:

import numpy as np
import scipy.sparse as sp
import math as math
from petsc4py import PETSc
n=100

A = sp.csc_matrix((n,n),dtype=np.complex128)
print A.shape
A[1:5,:]=1+1j*5*math.pi

p1=A.indptr
p2=A.indices
p3=A.data
petsc_mat = PETSc.Mat().createAIJ(size=A.shape,csr=(p1,p2,p3))

This works perfectly well as long as the matrix only consist of real values. When the matrix is complex running this piece of code results in a TypeError: Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'. I tried to figure out where the error occurs exactly, but could not make much sense of the traceback:

petsc_mat = PETSc.Mat().createAIJ(size=A.shape,csr=(p1,p2,p3))  File "Mat.pyx", line 265, in petsc4py.PETSc.Mat.createAIJ (src/petsc4py.PETSc.c:98970)
File "petscmat.pxi", line 662, in petsc4py.PETSc.Mat_AllocAIJ (src/petsc4py.PETSc.c:24264)
File "petscmat.pxi", line 633, in petsc4py.PETSc.Mat_AllocAIJ_CSR (src/petsc4py.PETSc.c:23858)
File "arraynpy.pxi", line 136, in petsc4py.PETSc.iarray_s (src/petsc4py.PETSc.c:8048)
File "arraynpy.pxi", line 117, in petsc4py.PETSc.iarray (src/petsc4py.PETSc.c:7771)

Is there an efficient way of creating a petsc matrix (of which i want to retrieve some eigenpairs later) from a complex scipy csc matrix ?

I would be really happy if you guys could help me find my (hopefully not too obvious) mistake.

解决方案

I had troubles getting PETSc to work, so I configured it more than just once, and in the last run I obviously forgot the option --with-scalar-type=complex.

This is what I should have done:

  • Either check the log file $PETSC_DIR/arch-linux2-c-opt/conf/configure.log.

  • Or take a look at the reconfigure-arch-linux2-c-opt.py.

There you can find all options you used to configure PETSc. In case you use SLEPc as well, you also need to recompile it. Now since I added the option (--with-scalar-type=complex) to the reconfigure script and ran it, everything works perfectly fine.

这篇关于petsc4py:从csc_matrix创建AIJ矩阵会导致TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆