学习poisson.c
static char help[] = "A structured-grid Poisson problem with DMDA+KSP.\n\n"; #include <petsc.h> extern PetscErrorCode formMatrix(DM, Mat);
extern PetscErrorCode formExact(DM, Vec);
extern PetscErrorCode formRHS(DM, Vec); //STARTMAIN
int main(int argc,char **args) {
PetscErrorCode ierr;
DM da;
Mat A;
Vec b,u,uexact;
KSP ksp;
double errnorm;
DMDALocalInfo info; PetscInitialize(&argc,&args,(char*),help); // default size (9 x 9) can be changed using -da_refine X or
// -da_grid_x M -da_grid_y N
ierr = DMDACreate2d(PETSC_COMM_WORLD,
DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_STAR,
,,PETSC_DECIDE,PETSC_DECIDE,,,NULL,NULL,&da); CHKERRQ(ierr); // create linear system matrix A
ierr = DMSetFromOptions(da); CHKERRQ(ierr);
ierr = DMSetUp(da); CHKERRQ(ierr);
ierr = DMCreateMatrix(da,&A); CHKERRQ(ierr);
ierr = MatSetFromOptions(A); CHKERRQ(ierr); // create right-hand-side (RHS) b, approx solution u, exact solution uexact
ierr = DMCreateGlobalVector(da,&b); CHKERRQ(ierr);
ierr = VecDuplicate(b,&u); CHKERRQ(ierr);
ierr = VecDuplicate(b,&uexact); CHKERRQ(ierr); // fill vectors and assemble linear system
ierr = formExact(da,uexact); CHKERRQ(ierr);
ierr = formRHS(da,b); CHKERRQ(ierr);
ierr = formMatrix(da,A); CHKERRQ(ierr); // create and solve the linear system
ierr = KSPCreate(PETSC_COMM_WORLD,&ksp); CHKERRQ(ierr);
ierr = KSPSetOperators(ksp,A,A); CHKERRQ(ierr);
ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr);
ierr = KSPSolve(ksp,b,u); CHKERRQ(ierr); // report on grid and numerical error
ierr = VecAXPY(u,-1.0,uexact); CHKERRQ(ierr); // u <- u + (-1.0) uxact
ierr = VecNorm(u,NORM_INFINITY,&errnorm); CHKERRQ(ierr);
ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,
"on %d x %d grid: error |u-uexact|_inf = %g\n",
info.mx,info.my,errnorm); CHKERRQ(ierr); VecDestroy(&u); VecDestroy(&uexact); VecDestroy(&b);
MatDestroy(&A); KSPDestroy(&ksp); DMDestroy(&da);
return PetscFinalize();
}
//ENDMAIN //STARTMATRIX
PetscErrorCode formMatrix(DM da, Mat A) {
PetscErrorCode ierr;
DMDALocalInfo info;
MatStencil row, col[];
double hx, hy, v[];
int i, j, ncols; ierr = DMDAGetLocalInfo(da,&info); CHKERRQ(ierr);
hx = 1.0/(info.mx-); hy = 1.0/(info.my-);
for (j = info.ys; j < info.ys+info.ym; j++) {
for (i = info.xs; i < info.xs+info.xm; i++) {
row.j = j; // row of A corresponding to (x_i,y_j)
row.i = i;
col[].j = j; // in this diagonal entry
col[].i = i;
ncols = ;
if (i== || i==info.mx- || j== || j==info.my-) {
v[] = 1.0; // if on boundary, just insert diagonal entry
} else {
v[] = *(hy/hx + hx/hy); // ... everywhere else we build a row
// if neighbor is NOT a known boundary value then we put an entry
if (i- > ) {
col[ncols].j = j; col[ncols].i = i-; v[ncols++] = -hy/hx; }
if (i+ < info.mx-) {
col[ncols].j = j; col[ncols].i = i+; v[ncols++] = -hy/hx; }
if (j- > ) {
col[ncols].j = j-; col[ncols].i = i; v[ncols++] = -hx/hy; }
if (j+ < info.my-) {
col[ncols].j = j+; col[ncols].i = i; v[ncols++] = -hx/hy; }
}
ierr = MatSetValuesStencil(A,,&row,ncols,col,v,INSERT_VALUES); CHKERRQ(ierr);
}
}
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
return ;
}
//ENDMATRIX //STARTEXACT
PetscErrorCode formExact(DM da, Vec uexact) {
PetscErrorCode ierr;
DMDALocalInfo info;
int i, j;
double hx, hy, x, y, **auexact; ierr = DMDAGetLocalInfo(da,&info); CHKERRQ(ierr);
hx = 1.0/(info.mx-); hy = 1.0/(info.my-);
ierr = DMDAVecGetArray(da, uexact, &auexact);CHKERRQ(ierr);
for (j = info.ys; j < info.ys+info.ym; j++) {
y = j * hy;
for (i = info.xs; i < info.xs+info.xm; i++) {
x = i * hx;
auexact[j][i] = x*x * (1.0 - x*x) * y*y * (y*y - 1.0);
}
}
ierr = DMDAVecRestoreArray(da, uexact, &auexact);CHKERRQ(ierr);
return ;
} PetscErrorCode formRHS(DM da, Vec b) {
PetscErrorCode ierr;
int i, j;
double hx, hy, x, y, f, **ab;
DMDALocalInfo info; ierr = DMDAGetLocalInfo(da,&info); CHKERRQ(ierr);
hx = 1.0/(info.mx-); hy = 1.0/(info.my-);
ierr = DMDAVecGetArray(da, b, &ab);CHKERRQ(ierr);
for (j=info.ys; j<info.ys+info.ym; j++) {
y = j * hy;
for (i=info.xs; i<info.xs+info.xm; i++) {
x = i * hx;
if (i== || i==info.mx- || j== || j==info.my-) {
ab[j][i] = 0.0; // on bdry the eqn is 1*u = 0
} else { // if not bdry; note f = - (u_xx + u_yy) where u is exact
f = 2.0 * ( (1.0 - 6.0*x*x) * y*y * (1.0 - y*y)
+ (1.0 - 6.0*y*y) * x*x * (1.0 - x*x) );
ab[j][i] = hx * hy * f;
}
}
}
ierr = DMDAVecRestoreArray(da, b, &ab); CHKERRQ(ierr);
return ;
}
//ENDEXACT
学习poisson.c的更多相关文章
- 各大公司广泛使用的在线学习算法FTRL详解
各大公司广泛使用的在线学习算法FTRL详解 现在做在线学习和CTR常常会用到逻辑回归( Logistic Regression),而传统的批量(batch)算法无法有效地处理超大规模的数据集和在线数据 ...
- 各大公司广泛使用的在线学习算法FTRL详解 - EE_NovRain
转载请注明本文链接:http://www.cnblogs.com/EE-NovRain/p/3810737.html 现在做在线学习和CTR常常会用到逻辑回归( Logistic Regression ...
- R语言学习路线和常用数据挖掘包(转)
对于初学R语言的人,最常见的方式是:遇到不会的地方,就跑到论坛上吼一嗓子,然后欣然or悲伤的离去,一直到遇到下一个问题再回来.当然,这不是最好的学习方式,最好的方式是——看书.目前,市面上介绍R语言的 ...
- FTRL(Follow The Regularized Leader)学习总结
摘要: 1.算法概述 2.算法要点与推导 3.算法特性及优缺点 4.注意事项 5.实现和具体例子 6.适用场合 内容: 1.算法概述 FTRL是一种适用于处理超大规模数据的,含大量稀疏特征的在线学习的 ...
- Keras学习笔记(完结)
使用Keras中文文档学习 基本概念 Keras的核心数据结构是模型,也就是一种组织网络层的方式,最主要的是序贯模型(Sequential).创建好一个模型后就可以用add()向里面添加层.模型搭建完 ...
- 学习笔记TF044:TF.Contrib组件、统计分布、Layer、性能分析器tfprof
TF.Contrib,开源社区贡献,新功能,内外部测试,根据反馈意见改进性能,改善API友好度,API稳定后,移到TensorFlow核心模块.生产代码,以最新官方教程和API指南参考. 统计分布.T ...
- [zz]如何学习Polygon Mesh Processing这本书?
图形学初学者,如何学习Polygon Mesh Processing这本书?修改修改 导师暑假让我看看这本书,目前看了一半觉得这本书比较偏重数学基础,对于具体的 implementation提及的并不 ...
- 图像边缘检测——几种图像边缘检测算子的学习及python 实现
本文学习利用python学习边缘检测的滤波器,首先读入的图片代码如下: import cv2 from pylab import * saber = cv2.imread("construc ...
- numpy之random学习
在机器学习中参数初始化需要进行随机生成,同时样本也需要随机生成,或者遵从一定规则随机生成,所以对随机生成的使用显得格外重要. 有的是生成随机数,有的是随机序列,有点是从随机序列中选择元素等等. 简单的 ...
随机推荐
- html5 浏览文件
var fileInput = document.getElementById('test-image-file'), info = document.getElementById('test-fil ...
- 复旦大学2018--2019学年第一学期(18级)高等代数I期末考试第七大题解答
七.(本题10分) 设 $V$ 为 $n$ 维线性空间, $\varphi,\psi$ 是 $V$ 上的线性变换, 满足 $\varphi\psi=\varphi$. 证明: $\mathrm{Ke ...
- 推荐一个SAM文件或者bam文件中flag含义解释工具
SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这样的文件.samtools网站上有专门的文档介绍SAM文件.具体地址:http://samtools. ...
- 从Scratch到Python——Python生成二维码
# Python利用pyqrcode模块生成二维码 import pyqrcode import sys number = pyqrcode.create('从Scratch到Python--Pyth ...
- 进程部分(IPC机制及生产者消费者模型)和线程部分
进程部分 一:进程间通信IPC机制:由于进程之间的内存空间是相互隔离的,所以为了进程间的通信需要一个共享的内存空间, 但是共享带来的问题是数据在写的时候就不安全了,所以需要一种机制既有能共享的内存 空 ...
- 《Java8实战》读书笔记
个人感悟: 1.lambda表达式,补充了JAVA在面向对象之外,面向过程的一面.在写面向过程代码的时候更方面了,甚至可以利用代码来做类似数学公式的运算(P64) 2.流,对集合的操作,就像用SQL对 ...
- [Linux] 关闭防火墙以及开放端口
一. service iptables stop 临时关闭, chkconfig iptables off完全关闭 service iptables status状态, service iptable ...
- Windows Server 2012 R2 英文版安装中文语言包教程
Windows Server 是云操作系统的主要组成部分. 有了 Windows Server,再加上云操作系统内的开发者技术,您就可以构建现代业务应用程序. 现代业务应用程序通常涵盖内部部署资源和公 ...
- ES5 & ES6 基础
一.什么是ES5 附上一览表 (5.1中文 (2011.6)): http://lzw.me/pages/ecmascript/ (5.1英文PDF):http://www.ecma-internat ...
- js里面对数据处理的方法
1,charAt()方法可返回指定位置的字符 JavaScript String 对象 例:stringObject.charAt(index) index:表示字符串中某个位置的数字,即字符在字 ...