第7章 有限要素法のプログラミング

Similar documents
2 [1] 7 5 C 2.1 (kikuchi-fem-mac ) input.dat (cat input.dat type input.dat ))

OHP.dvi


mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

1 4 2 EP) (EP) (EP)

sim98-8.dvi

[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

XMPによる並列化実装2

untitled

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology

ex14.dvi

ohp08.dvi

1.ppt

r08.dvi

C 2 / 21 1 y = x 1.1 lagrange.c 1 / Laglange / 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 float x[10], y[10]; 7 float xx, pn, p; 8 in


卒 業 研 究 報 告.PDF

Taro-ファイル処理(公開版).jtd

Microsoft Word - no15.docx

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

G1. tateyama~$ gcc -c xxxxx.c ( ) xxxxx.o tateyama~$ gcc -o xxxxx.o yyyyy.o..... zzzzz.o Makefile make Makefile : xxxxx.o yyyyy.o... zzzzz.o ; gcc -o

実際の株価データを用いたオプション料の計算

9 8 7 (x-1.0)*(x-1.0) *(x-1.0) (a) f(a) (b) f(a) Figure 1: f(a) a =1.0 (1) a 1.0 f(1.0)

:30 12:00 I. I VI II. III. IV. a d V. VI

:30 12:00 I. I VI II. III. IV. a d V. VI

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

untitled

C言語によるアルゴリズムとデータ構造

gengo1-12

Krylov (b) x k+1 := x k + α k p k (c) r k+1 := r k α k Ap k ( := b Ax k+1 ) (d) β k := r k r k 2 2 (e) : r k 2 / r 0 2 < ε R (f) p k+1 :=

gengo1-12

1. ファイルにアクセスするには ファイルにアクセスするには 1. ファイルを開く 2. アクセスする 3. ファイルを閉じるという手順を踏まなければなりません 1.1. ファイルを読み込む まずはファイルの内容を画面に表示させるプログラムを作りましょう 開始 FILE *fp char fname

gengo1-12

r07.dvi

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

ohp07.dvi

C言語入門

2017 p vs. TDGL 4 Metropolis Monte Carlo equation of continuity s( r, t) t + J( r, t) = 0 (79) J s flux (67) J (79) J( r, t) = k δf δs s( r,

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

(K&R 2.9) ~, &,, >>, << 2. (K&R 5.7) 3. (K&R 5.9) 4. (K&R 5.10) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, std

‚æ4›ñ

新・明解C言語 ポインタ完全攻略

joho09.ppt

新版明解C言語 実践編

超初心者用


計算機プログラミング

USB ID TA DUET 24:00 DUET XXX -YY.c ( ) XXX -YY.txt() XXX ID 3 YY ID 5 () #define StudentID 231

file"a" file"b" fp = fopen("a", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); fp = fopen("b", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose

PowerPoint プレゼンテーション

6 6.1 sound_wav_files flu00.wav.wav 44.1 khz 1/44100 spwave Text with Time spwave t T = N t N 44.1 khz t = 1 sec j t f j {f 0, f 1, f 2,, f N 1

スライド 1

PowerPoint プレゼンテーション

p = 1, 2, cos 2n + p)πj = cos 2nπj 2n + p)πj, sin = sin 2nπj 7.1) f j = a ) 0 + a p + a n+p cos 2nπj p=1 p=0 1 + ) b n+p p=0 sin 2nπj 1 2 a 0 +

QR

115 9 MPIBNCpack 9.1 BNCpack 1CPU X = , B =

Taro-リストⅠ(公開版).jtd

ex01.dvi

Microsoft PowerPoint - kougi9.ppt

Microsoft PowerPoint - guidance.ppt

8 / 0 1 i++ i 1 i-- i C !!! C 2

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

#define N1 N+1 double x[n1] =.5, 1., 2.; double hokan[n1] = 1.65, 2.72, 7.39 ; double xx[]=.2,.4,.6,.8,1.2,1.4,1.6,1.8; double lagrng(double xx); main

PowerPoint Presentation

Microsoft Word - no204.docx

02: 変数と標準入出力

‚æ2›ñ C„¾„ê‡Ìš|

[ 1] 1 Hello World!! 1 #include <s t d i o. h> 2 3 int main ( ) { 4 5 p r i n t f ( H e l l o World!! \ n ) ; 6 7 return 0 ; 8 } 1:

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

<4D F736F F D B B83578B6594BB2D834A836F815B82D082C88C60202E646F63>

j x j j j + 1 l j l j = x j+1 x j, n x n x 1 = n 1 l j j=1 H j j + 1 l j l j E

C言語による数値計算プログラミング演習

02: 変数と標準入出力

2009 T060050

untitled

comment.dvi

program.dvi

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

AutoTuned-RB

プログラミング基礎

Microsoft PowerPoint - kougi2.ppt

memo

ファイル入出力

ex01.dvi

Taro-リストⅢ(公開版).jtd

DOPRI5.dvi

スライド 1

スライド 1

double float

PowerPoint Presentation

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

1 return main() { main main C 1 戻り値の型 関数名 引数 関数ブロックをあらわす中括弧 main() 関数の定義 int main(void){ printf("hello World!!\n"); return 0; 戻り値 1: main() 2.2 C main

数値計算法

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

Transcription:

April 3, 2019 1 / 34

7.1 ( ) 2 Poisson 2 / 34

7.2 femfp.c [1] main( ) input( ) assem( ) ecm( ) f( ) solve( ) gs { solve( ) output( ) 3 / 34

7.3 fopen() #include <stdio.h> FILE *fopen(char *fname, char *mode); fname mode ( ) NULL mode mode r rb r+ rb+ w wb w+ wb+ a ab a ab+ 4 / 34

fclose() #include <stdio.h> int fclose(file *fp); fopen fp 0 EOF fprintf() include <stdio.h> int fprintf(file *fp, char *fmt,...); fp fmt 5 / 34

fputs() #include <stdio.h> int fputs(char *string, FILE *fp); fp string 0 EOF fgets() #include <stdio.h> char *fgets(char *string, int n, FILE *fp); fp string 0 EOF 6 / 34

7.4 main() /* The Finite Element Method for the Poisson Equation */ /* Full Matrix Version */ /* The Gauss Elimination Method */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define NDIM1 200 #define NDIM2 400 7 / 34

void input(int *nnode, int *nelmt, int *nbc, int ielmt[][3], double x[], double y[], int ibc[]); void assem(int nnode, int nelmt, int nbc, int ielmt[][3], double x[], double y[], int ibc[], double am[][ndim1], double fm[]); void ecm(int ielmt[][3], double x[], double y[], int ie, double ae[][3], double fe[]); void solve(int m, double a[][ndim1], double f[]); void gs_solve(int m, double a[][ndim1], double f[]); void output(double fm[], int nnode, double x[], double y[]); double f(double x, double y); 8 / 34

/* Main Program */ int main() { int nnode, nelmt, nbc, ielmt[ndim2][3], ibc[ndim1]; double am[ndim1][ndim1], fm[ndim1], x[ndim1], y[ndim1]; input(&nnode, &nelmt, &nbc, ielmt, x, y, ibc); assem(nnode, nelmt, nbc, ielmt, x, y, ibc, am, fm); solve(nnode, am, fm); //gs_solve(nnode, am, fm); output(fm, nnode, x, y); return(exit_success); [1] : void output(double fm[], int nnode, double x[], double y[]); output(fm, nnode, x, y); 9 / 34

input() /* Input */ void input(int *nnode, int *nelmt, int *nbc, int ielmt[][3], double x[], double y[], int ibc[]) { int j; FILE *fpin, *fpout; if((fpin=fopen("indata.txt","r")) == NULL){ printf(" \n"); exit(exit_failure); if((fpout=fopen("outdata.txt","w")) == NULL){ fclose(fpin); printf(" \n"); exit(exit_failure); 10 / 34

/* Input of Data */ printf("input (nnode, nelmt, nbc): "); fscanf(fpin, "%d%d%d", nnode, nelmt, nbc); printf("\ninput(x,y) of node i: \n"); for(j=0; j<*nnode; j++){ printf(" for i=%d: \n", j+1); fscanf(fpin, "%lg%lg", &(x[j]), &(y[j])); printf("input(1st, 2nd, 3rd) nodes of element i: \n"); for(j=0; j<*nelmt; j++){ printf(" for i=%d: \n", j+1); fscanf(fpin, "%d%d%d", &(ielmt[j][0]), &(ielmt[j][1]), &(ielmt[j][2])); 11 / 34

if(*nbc>0){ printf("input nodes with zero Dirichlet data"); printf(" for i=1 to %d: ", *nbc); for(j=0; j<*nbc; j++) fscanf(fpin, "%d", &(ibc[j])); printf("\n"); printf("nodes of elements \n"); for(j=0; j<2; j++) printf(" i i1 i2 i3 "); printf("\n"); for(j=0; j<*nelmt; j+=2){ printf("*%4d*%4d%4d%4d ", j+1, ielmt[j][0], ielmt[j][1], ielmt[j][2]); if(j<*nelmt-1) printf("*%4d*%4d%4d%4d", j+2, ielmt[j+1][0], ielmt[j+1][1], ielmt[j+1][2]); printf("\n"); 12 / 34

if(*nbc>0){ printf("nodes with zero Dirichlet data \n"); for(j=0; j<*nbc; j++) printf("%4d", ibc[j]); printf("\n"); [1] : FILE *fpin, *fpout;... : indata.txt, : outdata.txt fscanf(fpin,... 13 / 34

assem() /* The Direct Stiffness Method */ void assem(int nnode, int nelmt, int nbc, int ielmt[][3], double x[], double y[], int ibc[], double am[ndim1][ndim1], double fm[]) { int i, j, ie, ii, jj; double ae[3][3], fe[3]; /* Initial Clear */ for(i=0; i<nnode; i++){ fm[i]=0.0; for(j=0; j<nnode; j++) am[i][j]=0.0; 14 / 34

/* Assemblage of Total Matrix and Vector */ for(ie=0; ie<nelmt; ie++){ ecm(ielmt, x, y, ie, ae, fe); for(i=0; i<3; i++){ ii=ielmt[ie][i]-1; fm[ii]+=fe[i]; for(j=0; j<3; j++){ jj=ielmt[ie][j]-1; am[ii][jj]+=ae[i][j]; 15 / 34

/* The Homogeneous Dirichlet Condition */ if (nbc!=0){ for(i=0;i<nbc; i++){ ii=ibc[i]-1; fm[ii]=0.0; for(j=0; j<nnode;j++){ am[ii][j]=0.0; am[j][ii]=0.0; am[ii][ii]=1.0; 16 / 34

ecm() /* Element Coeffcient Matrix and Free Vector */ void ecm(int ielmt[][3], double x[], double y[], int ie, double ae[][3], double fe[]) { int i, j, k; double d, s, xe[3], ye[3], b[3], c[3]; for(i=0; i<3;i++){ j=ielmt[ie][i]-1; xe[i]=x[j]; ye[i]=y[j]; d=xe[0]*(ye[1]-ye[2])+xe[1]*(ye[2]-ye[0]) +xe[2]*(ye[0]-ye[1]); s=fabs(d)/2.0; 17 / 34

/* Calculation of Element Coefficient Matrix */ for(i=0; i<3; i++){ j=i+1; if(i==2) j=0; k=i-1; if(i==0) k=2; b[i]=(ye[j]-ye[k])/d; c[i]=(xe[k]-xe[j])/d; for(i=0; i<3; i++){ for(j=0; j<3; j++) ae[i][j]=s*(b[j]*b[i]+c[j]*c[i]); /* Calculation of Element Free Vector */ for(i=0; i<3; i++) fe[i]=s*f(xe[i], ye[i])/3.0; 18 / 34

solve() /* The Gauss Elimination Method */ void solve(int m, double a[][ndim1], double f[]) /* m=number of unknowns */ { int i, j, k; double aa; /* Forward Elimination */ for(i=0; i<m-1; i++){ for(j=i+1; j<m; j++){ aa=a[j][i]/a[i][i]; f[j]-=aa*f[i]; for(k=i+1; k<m; k++) a[j][k]-=aa*a[i][k]; 19 / 34

/* Backward Substitution */ f[m-1]/=a[m-1][m-1]; for(i=m-2; i>=0; i--){ for(j=i+1; j<m; j++) f[i]-=a[i][j]*f[j]; f[i]/=a[i][i]; 20 / 34

[1] : gs solve() gs solve() /* The Gauss Seidel Method */ void gs_solve(int m, double a[][ndim1], double f[]) { int i, j, k; double aa = 0.0; double u[ndim1],u_old[ndim1]; double max_delta=0.0, max_delta_old; double max_u=0.0; int count = 0; for(i=0; i<m; i++){ u[i] = 0.0; 21 / 34

do{ max_delta_old = max_delta; for(i=0; i<m; i++){ u_old[i] = u[i]; for(i=0; i<m; i++){ aa = 0.0; for(j=0; j<m; j++){ if(i!= j){ aa += a[i][j]*u[j]; u[i]=(f[i] - aa)/a[i][i]; 22 / 34

max_delta = fabs(u[0] - u_old[0]); for(i=1; i<m; i++){ if(max_delta < fabs(u[i] - u_old[i])){ max_delta = fabs(u[i] - u_old[i]); max_u = fabs(u[0]); for(i=1; i<m; i++){ if(max_u < fabs(u[i])){ max_u = fabs(u[i]); 23 / 34

count++; while(fabs((max_delta - max_delta_old)/max_u) >= 1.0E-5); printf("count = %d\n",count); for(i=0; i<m; i++){ f[i] = u[i]; 24 / 34

output() /* 0utput of Approximate Nodal Values of u */ void output(double fm[], int nnode, double x[], double y[]) { int j,m; FILE *fpout; if((fpout=fopen("outdata.txt","a")) == NULL){ printf(" \n"); exit(exit_failure); 25 / 34

fprintf(fpout, "\nnodal values of u\n"); fprintf(fpout, " i u \n"); for(j=0; j<nnode; j++) fprintf(fpout, "%4d%12.3e\n", j+1, fm[j]); fprintf(fpout, "\nmathematica 6 Data\n"); fprintf(fpout, "ListPlot3D[{\n"); for(j=0; j<nnode-1; j++){ fprintf(fpout, "{%f, %f, %f,\n", x[j], y[j], fm[j]); fprintf(fpout, "{%f, %f, %f\n, Mesh -> All]\n", x[j], y[j], fm[j]); fclose(fpout); [1] : FILE *fpout;... 26 / 34

f() /* Function for Free Term */ double f(double x, double y) { return(1.0); 27 / 34

7.5 indata.txt 9 8 5 0 0 0 0.5 0 1 0.5 0 0.5 0.5 0.5 1 1 0 1 0.5 1 1 1 4 5 1 5 2 2 5 6 2 6 3 4 7 8 4 8 5 5 8 9 5 9 6 1 2 3 4 7 outdata.txt Basic data nnode = 9 nelmt = 8 nbc = 5 x,y-coordinates of nodes i x(i) y(i) 1 0.0000 0.0000 2 0.0000 0.5000 3 0.0000 1.0000 4 0.5000 0.0000 5 0.5000 0.5000 6 0.5000 1.0000 7 1.0000 0.0000 8 1.0000 0.5000 9 1.0000 1.0000 Nodes of elements i i1 i2 i3 1 1 4 5 2 1 5 2 3 2 5 6 4 2 6 3 5 4 7 8 6 4 8 5 7 5 8 9 8 5 9 6 28 / 34

Nodes with zero Dirichlet data 1 2 3 4 7 Nodal values of u i u 1 0.000e+000 2 0.000e+000 3 0.000e+000 4 0.000e+000 5 1.771e-001 6 2.292e-001 7 0.000e+000 8 2.292e-001 9 3.125e-001 Mathematica 6 Data ListPlot3D[{ {0.000000, 0.000000, 0.000000, {0.000000, 0.500000, 0.000000, {0.000000, 1.000000, 0.000000, {0.500000, 0.000000, 0.000000, {0.500000, 0.500000, 0.177083, {0.500000, 1.000000, 0.229167, {1.000000, 0.000000, 0.000000, {1.000000, 0.500000, 0.229167, {1.000000, 1.000000, 0.312500, Mesh -> All] 29 / 34

Mathematica outdata.txt ListPlot3D[...] Shift + Enter 30 / 34

7.6 1 2 Poisson Mathematica 31 / 34

2 indata 105.txt femfp.c femfp.c 32 / 34

7.7 2 Poisson 1 C 2 C 3 1 4 33 / 34

[1]. :.,, 1980. 34 / 34