1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf ·...

124
1D-FEM in C: Steady State Heat Conduction Kengo Nakajima Information Technology Center Programming for Parallel Computing (616-2057) Seminar on Advanced Computing (616-4009)

Transcript of 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf ·...

Page 1: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

1D-F

EM in

C: S

tead

y St

ate

Hea

t C

ondu

ctio

n

Ken

go N

akaj

ima

Info

rmat

ion

Tech

nolo

gy C

ente

r

Pro

gram

min

g fo

r Par

alle

l Com

putin

g (6

16-2

057)

S

emin

ar o

n A

dvan

ced

Com

putin

g (6

16-4

009)

Page 2: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D2

•1D

-cod

e fo

r Sta

tic L

inea

r-E

last

ic P

robl

ems

by

Gal

erki

n FE

M•

Spa

rse

Line

ar S

olve

r–

Con

juga

te G

radi

ent M

etho

d–

Pre

cond

ition

ing

•S

tora

ge o

f Spa

rse

Mat

rices

•P

rogr

am

Page 3: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D3

Key

wor

ds

•1D

Ste

ady

Sta

te H

eat C

ondu

ctio

n P

robl

ems

•G

aler

kin

Met

hod

•Li

near

Ele

men

t•

Pre

cond

ition

ed C

onju

gate

Gra

dien

t Met

hod

Page 4: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D4

1D S

tead

y S

tate

Hea

t Con

duct

ion

x=0

(xm

in)

x= x

max

Qhe

at g

ener

atio

n/vo

lum

e0

Q

xTx

•U

nifo

rm: S

ectio

nal A

rea:

A, T

herm

al C

ondu

ctiv

ity:

•H

eat G

ener

atio

n R

ate/

Volu

me/

Tim

e [Q

L-3 T

-1]

•B

ound

ary

Con

ditio

ns–

x=0

T= 0

(F

ixed

Tem

pera

ture

)

–x=

x max:

(Ins

ulat

ed)

0

xT

Q

Page 5: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D5

1D S

tead

y S

tate

Hea

t Con

duct

ion

x=0

(xm

in)

x= x

max

Q

0

xTheat

gen

erat

ion/

volu

me

0

QxT

x

Q

•U

nifo

rm: S

ectio

nal A

rea:

A, T

herm

al C

ondu

ctiv

ity:

•H

eat G

ener

atio

n R

ate/

Volu

me/

Tim

e [Q

L-3 T

-1]

•B

ound

ary

Con

ditio

ns–

x=0

T= 0

(F

ixed

Tem

pera

ture

)

–x=

x max:

(Ins

ulat

ed)

Page 6: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D6

Ana

lytic

al S

olut

ion

x=0

(xm

in)

x= x

max

0@0

xT

max

@0x

xxT

xx

Qx

QT

xT

CC

xC

xQ

T

xx

Tx

QC

Cx

QT

QT

max

2

22

12

max

max

11

21

0@0

,021

@0,

0

QxT

x

he

at g

ener

atio

n/vo

lum

eQ

Page 7: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D7

1D L

inea

r Ele

men

t (1/

4)一次元線形要素

•1D

Lin

ear E

lem

ent

–Le

ngth

=L

•N

ode

(Ver

tex)(

節点

•El

emen

t(要

素)

–T i

Tem

pera

ture

at

i–

T jTe

mpe

ratu

re a

t j

–Te

mpe

ratu

re T

on e

ach

elem

ent i

s lin

ear

func

tion

of x

(Pie

cew

ise

Line

ar):

xT

21

iX

jX

x

L

TiT

jT

Page 8: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D8

1D L

inea

r Ele

men

t (1/

4)一次元線形要素

xT

21

iX

jX

x

L

TiT

jT

•1D

Lin

ear E

lem

ent

–Le

ngth

=L

•N

ode

(Ver

tex)

•El

emen

t

–T i

Tem

pera

ture

at

i–

T jTe

mpe

ratu

re a

t j

–Te

mpe

ratu

re T

on e

ach

elem

ent i

s lin

ear

func

tion

of x

(Pie

cew

ise

Line

ar):

Page 9: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D9

Pie

cew

ise

Line

ar

12

34

51

23

4G

loba

l Nod

e ID

Ele

men

t ID

12

11

22

12

31

24

Loca

l Nod

e ID

for e

ach

elem

.G

radi

ent o

f tem

pera

ture

is c

onst

ant i

n ea

ch

elem

ent (

mig

ht b

e di

scon

tinuo

us a

t eac

h “n

ode”

)

Page 10: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D10

1D L

inea

r Ele

m.:

Sha

pe F

unct

ion

(2/4

)•

Coe

f’s a

re c

alcu

late

d ba

sed

on in

fo. a

t eac

h no

dej

ji

iX

xT

TX

xT

T

@

,@

jj

ii

XT

XT

21

21

,

•C

oeffi

cien

ts:

LT

TL

XT

XT

ij

ij

ji

21

,

•T

can

be w

ritte

n as

follo

ws,

acc

ordi

ng to

Tia

nd T

j :

ji

ij

TLX

xT

Lx

XT

Ni

Nj

iX

jX

x

TiT

jT

L

Ni

Ni,

Nj

Sha

pe F

unct

ion(形状関数)or

In

terp

olat

ion

Func

tion(

内挿関

数), f

unct

ion

of x

(onl

y)N

Page 11: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

1D L

inea

r Ele

m.:

Sha

pe F

unct

ion

(3/4

)

LXx

NL

xX

Ni

jj

i,

•N

umbe

r of S

hape

Fun

ctio

ns

= N

umbe

r of V

ertic

es o

f E

ach

Ele

men

t–

Ni:

Func

tion

of P

ositi

on–

A ki

nd o

f Tes

t/Tria

l Fun

ctio

ns

•Li

near

com

bina

tion

of s

hape

func

tions

pro

vide

s di

spla

cem

ent “

in” e

ach

elem

ent

–C

oef’s

(unk

now

s): T

empe

ratu

re a

t eac

h no

de

iX

jX

x

TiT

jT

L

jj

ii

TN

TN

T

i

ia

Tria

l/Tes

t Fun

ctio

n (k

now

n fu

nctio

n of

pos

ition

, def

ined

in

dom

ain

and

at b

ound

ary.

“B

asis

” in

line

ar a

lgeb

ra.

Coe

ffici

ents

(unk

now

n)

i

M ii

Ma

u

1

MT

Page 12: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D12

1D L

inea

r Ele

m.:

Sha

pe F

unct

ion

(4/4

)

LXx

NL

xX

Ni

jj

i,

•Va

lue

of N

i–

=1 a

t one

of t

he n

odes

in

elem

ent

–=0

on

othe

r nod

es

iX

jX

x

N

iN

jN

0.1

iX

jX

x

TiT

jT

L

Page 13: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D13

Gal

erki

n M

etho

d (1

/4)

•G

over

ning

Equ

atio

n fo

r 1D

S

tead

y S

tate

Hea

t C

ondu

ctio

n P

robl

ems

(Uni

form

):

02

2

Q

dxT

d

}{

NT

Dis

tribu

tion

of te

mpe

ratu

re in

eac

h el

emen

t (m

atrix

form

), :

Tem

pera

ture

at e

ach

node

•Fo

llow

ing

inte

gral

equ

atio

n is

obt

aine

d at

eac

h el

emen

t by

Gal

erki

n m

etho

d, w

here

[N]’s

are

als

o w

eigh

ting

func

tions

:

02

2

dVQ

dxT

dN

T

V

Page 14: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

x=0

(xm

in)

x= x

max

Q体積当たり一様発熱

FEM

1D14

Gal

erki

n M

etho

d (2

/4)

•G

reen

’s T

heor

em (1

D)

•A

pply

this

to th

e 1s

t par

t of e

qn w

ith 2

nd-o

rder

diff

.:

dSdxdT

NdV

dxdTdxN

ddV

dxT

dN

S

TT

V

T

V

2

2

VV

S

dVdxdB

dxdAdS

dxdBA

dVdx

Bd

A2

2

•C

onsi

der t

he fo

llow

ing

term

s: dxdTq

: Hea

t flu

x at

ele

men

t sur

face

[QL-

2 T-1

]

}{

},{

dxNd

dxdTN

T

V

SS

Page 15: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

x=0

(xm

in)

x= x

max

Q体積当たり一様発熱

FEM

1D15

Gal

erki

n M

etho

d (3

/4)

•Fi

nally

, fol

low

ing

eqn

is

obta

ined

by

cons

ider

ing

heat

gen

erat

ion

term

0

dVN

QdS

Nq

dVdxN

ddxN

d

V

T

S

T

T

V

•Th

is is

cal

led

“wea

k fo

rm(

弱形

式)

”. O

rigin

al P

DE

co

nsis

ts o

f ter

ms

with

2nd

-ord

er d

iff.,

but t

his

“wea

k fo

rm” o

nly

incl

udes

1st-o

rder

diff

by

Gre

en’s

theo

rem

.–

Req

uire

men

ts fo

r sha

pe fu

nctio

ns a

re “w

eake

r” in

“wea

k fo

rm”.

Line

ar fu

nctio

ns c

an d

escr

ibe

effe

cts

of 2

nd-o

rder

di

ffere

ntia

tion.

V

SS

Q

Page 16: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D16

Gal

erki

n M

etho

d (4

/4)

•Th

ese

term

s co

inci

de a

t ele

men

t bou

ndar

ies

and

disa

ppea

r. Fi

nally

, onl

y te

rms

on th

e do

mai

n bo

unda

ries

rem

ain.

0

dVN

QdS

Nq

dVdxN

ddxN

d

V

T

S

T

T

V

x=0

(xm

in)

x= x

max

Q体積当たり一様発熱 V

SS

Page 17: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D17

Wea

k Fo

rm a

nd B

ound

ary

Con

ditio

ns•

Valu

e of

dep

ende

nt v

aria

ble

is

defin

ed (D

irich

let)

–W

eigh

ting

Func

tion

= 0

–Pr

inci

pal B

.C. (

Bou

ndar

y C

ondi

tion)(第一種境界条件)

–Es

sent

ial B

.C.(

基本境界条件)

•D

eriv

ativ

es o

f Unk

now

ns

(Neu

man

n)–

Nat

ural

ly sa

tisfie

d in

wea

k fo

rm–

Seco

ndar

y B

.C.(

第二種境界

条件)

–N

atur

al B

.C(自然境界条件)

dxdTq

whe

re

x=0

(xm

in)

x= x

max

Q体積当たり一様発熱 V

SS

0

dVN

QdS

Nq

dVdxN

ddxN

d

V

T

S

T

T

V

Page 18: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D18

Wea

k Fo

rm w

ith B

.C.:

on e

ach

elem

.

dVdxN

ddxN

dk

T

V

e

)(

)(

)(

)(

ee

ef

k

d

SN

qdV

NQ

fS

T

V

Te

)

(

Page 19: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D19

Inte

grat

ion

over

Eac

h E

lem

ent:

[k]

LXx

NL

xX

Ni

jj

i,

LdxdN

LdxdN

ji

1,

1

0

iX

LX

j

x

uiu

ju

L

Lx

NLx

Nj

i,

1

11

11

11

11

/1,/1

/1/1

020

LAdx

LA

dxA

LL

LL

dVdxN

ddxN

d L

L

T

V

2x1

mat

rix1x

2 m

atrix

A: S

ectio

nal A

rea

L: L

engt

h

Page 20: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D20

Inte

grat

ion

over

Eac

h E

lem

ent:

{f}

(1/2

)

LXx

NL

xX

Ni

jj

i,

LdxdN

LdxdN

ji

1,

1

11

2//

1

0

ALQ

dxL

xL

xA

QdV

NQ

L

V

T

1

:

1

Hea

t Gen

erat

ion

(Vol

ume)

Lx

NLx

Nj

i,

1

A: S

ectio

nal A

rea

L: L

engt

h

Page 21: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D21

Inte

grat

ion

over

Eac

h E

lem

ent:

{f}

(2/2

)

LXx

NL

xX

Ni

jj

i,

LdxdN

LdxdN

ji

1,

1

dxdTq

AqAq

dSN

qL

xS

T

,

10

11

2//

1

0

ALQ

dxL

xL

xA

QdV

NQ

L

V

T

Hea

t Gen

erat

ion

(Vol

ume)

Surf

ace

Hea

t Flu

x

whe

n su

rface

hea

t flu

x ac

ts o

n on

ly th

is s

urfa

ce.

Page 22: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D22

Glo

bal E

quat

ions

•A

ccum

ulat

e E

lem

ent E

quat

ions

:

)(

)(

)(

ee

ef

k

fF

kK

,

FK

ofve

ctor

glob

al:

This

is th

e fin

al li

near

equ

atio

ns

(glo

bal e

quat

ions

) to

be s

olve

d.

Ele

men

t Mat

rix, E

lem

ent E

quat

ions

Glo

bal M

atrix

, Glo

bal E

quat

ions

Page 23: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D23

EC

CS

2012

Sys

tem

Cre

atin

g D

irect

ory

>$ cd Documents

>$ mkdir2014summer

your

favo

rite

nam

e>$ cd 2014summer

This

is y

our “

top”

dire

ctor

y, a

nd is

cal

led <$E-TOP>

in th

is c

lass

.

1D C

ode

for S

tead

y-St

ate

Hea

t Con

duct

ion

Prob

lem

s

>$ cd <$E-TOP>

>$ cp /home03/skengon/Documents/class_eps/F/1d.tar .

>$ cp /home03/skengon/Documents/class_eps/C/1d.tar .

>$ tarxvf 1d.tar

>$ cd 1d

Page 24: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D24

Com

pile

& G

O !

>$ cd <$E-TOP>/1d

>$ cc –O 1d.c (or g95 –O 1d.f)

>$ ./a.out

Con

trol

Dat

ainput.dat

4NE

(Num

ber o

f Ele

men

ts)

1.0 1.0 1.0 1.0

x

(Len

gth

of E

ach

Ele

m.: L

) ,Q, A,

100

Num

ber o

f MA

X. I

tera

tions

for C

G S

olve

r1.e-8

Con

verg

ence

Crit

eria

for C

G S

olve

r

12

34

51

23

4

x=1

x=0

x=1

x=2

x=3

x=4

Ele

men

t ID

Nod

e ID

(Glo

bal)

Page 25: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D25

Res

ults

>$ ./a.out

4 iters, RESID= 4.154074e-17

### TEMPERATURE

1 0.000000E+00 0.000000E+00

2 3.500000E+00 3.500000E+00

3 6.000000E+00 6.000000E+00

4 7.500000E+00 7.500000E+00

5 8.000000E+00 8.000000E+00

Computational

Analytical

12

34

51

23

4

Ele

men

t ID

Nod

e ID

(Glo

bal)

x=1

x=0

x=1

x=2

x=3

x=4

Page 26: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D26

Ele

men

t Eqn

’s/A

ccum

ulat

ion

(1/3

)•

4 el

emen

ts, 5

nod

es

•[k

] and

{f}

of E

lem

ent-1

:

11

11

)1(

LAk

12

34

51

23

4

Ele

men

t ID

Nod

e ID

(Glo

bal)

x=0

x=1

x=2

x=3

x=4

112

)1(AL

Qf

•A

s for

Ele

men

t-4:

11

11

)4(

LAk

112

)4(AL

Qf

Page 27: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D27

Ele

men

t Eqn

’s/A

ccum

ulat

ion

(2/3

)•

Elem

ent-b

y-El

emen

t Acc

umul

atio

n:

4 1

)(

e

ek

K

4 1

)(

e

ef

F

++

+

++

+

Page 28: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D28

Ele

men

t Eqn

’s/A

ccum

ulat

ion

(3/3

)•

Rel

atio

ns to

FD

M

+1

4 1

)(

e

ek

K-1

-1 +1+1 -1

-1 +1+1 -1

-1 +1+1 -1

-1 +1

++

+

11

11

)(

LAk

e

[]

+1 -1

-1 +2 -1-1 +2 -1

-1 +2 -1-1 +1

LA

LA

LAT

TT

ALL

TT

T

dVL

TT

TdV

dxT

d

ii

ii

ii

V

ii

i

V

11

21

1

21

12

2

22

2

Som

ethi

ng fa

mili

ar …

FEM

: Coe

ffici

ent M

atric

es a

re g

ener

ally

spa

rse

(man

y ZE

RO

’s)

Page 29: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D29

2nd

–Ord

er D

iffer

entia

tion

in F

DM

•A

ppro

xim

ate

Der

ivat

ive

at×

(cen

ter o

f ian

d i+

1)

xx

i-1

i i

+1

×x

dxdi

i

i

1

2/1

x→0

: Rea

l Der

ivat

ive

•2n

d-O

rder

Diff

. at i

21

1

11

2/12/1

222 x

xx

xx

dxddxd

dxdi

ii

ii

ii

ii

i

Page 30: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D30

Ele

men

t-by-

Ele

men

t Ope

ratio

nve

ry fl

exib

le if

eac

h el

emen

t has

diff

eren

t mat

eria

l pr

oper

ty, s

ize,

etc

.

4 1

)(

e

ek

K+1 -1

-1 +1+1 -1

-1 +1

+1 -1-1 +1

+1 -1-1 +1

+

11

11

)(

)(

)(

)(

e

ee

e

LAk

)1(

)1()1( LA

)2(

)2()2( LA

+)3(

)3()3( LA

)4(

)4()4( LA

Page 31: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D31

Ele

men

t/Glo

bal O

pera

tions

12

34

51

23

4

12

e1

2e

12

e1

2e

0

dVN

XdS

N

dVdxN

ddxN

dE

V

T

S

T

T

V

)1( 2

)1( 1)1( 2

)1( 1)1( 22

)1( 21

)1( 12)1( 11

)1()1(

)1(}

{}

{]

[

ffk

kk

k

fk

)2( 2

)2( 1)2( 2

)2( 1)2( 22

)2( 21

)2( 12)2( 11

)2()2(

)2(}

{}

{]

[

ffk

kk

k

fk

)3( 2

)3( 1)3( 2

)3( 1)3( 22

)3( 21

)3( 12)3( 11

)3()3(

)3(}

{}

{]

[

ffk

kk

k

fk

)4( 2

)4( 1)4( 2

)4( 1)4( 22

)4( 21

)4( 12)4( 11

)4()4(

)4(}

{}

{]

[

ffk

kk

k

fk

12

34

51

23

4

54321

54321

551

414

41

313

31

212

21

111

}{

}]{

[

BBBBB

DAL

AUD

ALAU

DAL

AUD

ALAU

DF

KM

appi

ng In

form

atio

n ne

eded

, fro

m e

lem

ent-m

atrix

to g

loba

l-mat

rix.

Glo

bal N

ode

ID

Loca

l Nod

e ID

0

dVN

XdS

N

dVdxN

ddxN

dE

V

T

S

T

T

V

0

dVN

XdS

N

dVdxN

ddxN

dE

V

T

S

T

T

V

0

dVN

XdS

N

dVdxN

ddxN

dE

V

T

S

T

T

V

Page 32: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D32

Acc

umul

atio

n to

G

loba

l/ove

rall

Mat

rix

1

1

23

45

6

78

9

23

4

56

78

910

1112

1314

1516

16151413121110987654321

16151413121110987654321

}{

}]{

[

FFFFFFFFFFFFFFFF

DX

XX

XD

XX

XX

XD

XX

XX

XD

XX

XX

DX

XX

XX

XX

DX

XX

XX

XX

XD

XX

XX

XX

XD

XX

XX

DX

XX

XX

XX

DX

XX

XX

XX

XD

XX

XX

XX

XD

XX

XX

DX

XX

XX

DX

XX

XX

DX

XX

XD

FK

Aro

und

the

FEM

in 3

min

utes

Page 33: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D33

•1D

-cod

e fo

r Sta

tic L

inea

r-E

last

ic P

robl

ems

by

Gal

erki

n FE

M•

Spa

rse

Line

ar S

olve

r–

Con

juga

te G

radi

ent M

etho

d–

Pre

cond

ition

ing

•S

tora

ge o

f Spa

rse

Mat

rices

•P

rogr

am

Page 34: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D34

Larg

e-Sc

ale

Line

ar E

quat

ions

in

Scie

ntifi

c A

pplic

atio

ns•

Sol

ving

larg

e-sc

ale

linea

r equ

atio

ns A

x=b

is th

e m

ost

impo

rtant

and

exp

ensi

vepa

rt of

var

ious

type

s of

sc

ient

ific

com

putin

g.–

for b

oth

linea

r and

non

linea

r app

licat

ions

•V

ario

us ty

pes

of m

etho

ds p

ropo

sed

& d

evel

oped

.–

for d

ense

and

spa

rse

mat

rices

–cl

assi

fied

into

direct

and iterative

met

hods

•D

ense

Mat

rices

:密行

列: G

loba

lly C

oupl

ed P

robl

ems

–B

EM

, Spe

ctra

l Met

hods

, MO

/MD

(gas

, liq

uid)

•S

pars

e M

atric

es:疎

行列

: Loc

ally

Def

ined

Pro

blem

s–

FEM

, FD

M, D

EM

, MD

(sol

id),

BE

M w

/FM

M

Page 35: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

35

Dire

ct M

etho

d直

接法

G

auss

ian

Elim

inat

ion/

LU F

acto

rizat

ion.

co

mpu

te A

-1di

rect

ly.G

ood

Rob

ust f

or w

ide

rang

e of

app

licat

ions

.

Goo

d fo

r bot

h de

nse

and

spar

se m

atric

esB

ad

Mor

e ex

pens

ive

than

iter

ativ

e m

etho

ds (m

emor

y, C

PU

)

not s

cala

ble

FEM

1D

Page 36: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

36

Wha

t is

Itera

tive

Met

hod

?反

復法

Sol

ver-I

tera

tive

Sta

rting

from

a in

itial

vec

torx

(0) ,

itera

tive

met

hod

obta

ins

the

final

con

verg

ed s

olut

ions

by

itera

tions

,,

)2()1(

xx

Ab

Initi

al S

olut

ion

初期

解Li

near

Equ

atio

ns連

立一

次方

程式

)0(

)0( 2

)0( 1

)0(

nxxx x

x

nn

nnn

n

nn

bbb

xxx

aa

a

aa

aa

aa

21

21

21

222

21

112

11

Page 37: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

1D-P

art1

37

Itera

tive

Met

hod

反復

•S

tatio

nary

Met

hod

–O

nly

x(s

olut

ion

vect

or) c

hang

es d

urin

g ite

ratio

ns.

–S

OR

, Gau

ss-S

eide

l, Ja

cobi

–G

ener

ally

slo

w, i

mpr

actic

al

•N

on-S

tatio

nary

Met

hod

–W

ith re

stric

tion/

optim

izat

ion

cond

ition

s–

Kry

lov-

Sub

spac

e–

CG

: Con

juga

te G

radi

ent

–B

iCG

STA

B: B

i-Con

juga

te G

radi

ent S

tabi

lized

–G

MR

ES

: Gen

eral

ized

Min

imal

Res

idua

l

Nb

Mx

xb

Ax

)

()1

(k

k

Page 38: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

38

Itera

tive

Met

hod

(con

t.)G

ood

Le

ss e

xpen

sive

than

dire

ct m

etho

ds, e

spec

ially

in m

emor

y.

Sui

tabl

e fo

r par

alle

l and

vec

tor c

ompu

ting.

Bad

Con

verg

ence

stro

ngly

dep

ends

on

prob

lem

s, b

ound

ary

cond

ition

s (c

ondi

tion

num

ber e

tc.)

Pr

econ

ditio

ning

is re

quire

d : K

ey T

echn

olog

y fo

r Pa

ralle

l FEM

FEM

1D

Page 39: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

Sol

ver-I

tera

tive

39

Non

-Sta

tiona

ry/K

rylo

v Su

bspa

ce

Met

hod

(1/2

)非

定常

法・

クリ

ロフ

部分

空間

x

AI

bx

bA

x

Com

pute

x 0,x

1,x 2

, ...,

xk

by th

e fo

llow

ing

itera

tive

proc

edur

es:

11

11

1

kk

kk

kk

xr

xA

xb

xA

Ib

x

kk

whe

reA

xb

r

: r

esid

ual

1 00

k ii

kr

xx

11

11

1

11

kk

kk

k

kk

kk

rA

IA

rr

Ar

Ax

bx

rA

bA

xb

r

Page 40: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

Sol

ver-I

tera

tive

40

Non

-Sta

tiona

ry/K

rylo

v Su

bspa

ce

Met

hod

(2/2

)非

定常

法・

クリ

ロフ

部分

空間

z kis

a v

ecto

r whi

ch b

elon

gs to

kth

Kry

lov

Sub

spac

e(クリ

ロフ部分空間)

, app

roxi

mat

e so

lutio

n ve

ctor

xk

is d

eriv

ed

by th

e K

rylo

v S

ubsp

ace:

0

1 1

1 10

0

1 10

00

2 00

0

1 00

rA

II

rA

Ir

z

rA

Ir

xr

AI

rx

rx

x

k i

ik i

ik

k i

ik i

i

k ii

k

01

02

00

,,

,,

rA

rA

Ar

rk

Page 41: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

41

Con

juga

te G

radi

ent M

etho

d共

役勾

配法

•C

onju

gate

Gra

dien

t: C

G–

Mos

t pop

ular

“non

-sta

tiona

ry” i

tera

tive

met

hod

•fo

r Sym

met

ric P

ositi

ve D

efin

ite(S

PD

) Mat

rices

–対

称正定

–{x

}T [A]

{x}>

0 fo

r arb

itrar

y {x

}–

All

of d

iago

nal c

ompo

nent

s, e

igen

vaul

es a

nd le

adin

g pr

inci

pal m

inor

s >

0(主小行列式・首座行列式)

–M

atric

es o

f Gal

erki

n-ba

sed

FEM

: hea

t con

duct

ion,

Poi

sson

, st

atic

line

ar e

last

ic p

robl

ems

•A

lgor

ithm

–“S

teep

est D

esce

nt M

etho

d”–

x(i)=

x(i-1)

+

i p(i)

•x(i)

:so

lutio

n,p(i)

:se

arch

dire

ctio

n, i

: coe

ffici

ent

–S

olut

ion

{x}

min

imiz

es {

x-y}

T[A

]{x-

y}, w

here

{y}

is e

xact

so

lutio

n.

nnn

nn

n

nnnn

aa

aa

a

aa

aa

aa

aa

aa

aa

aa

aa

aa

aa

43

21

444

4342

41

334

3332

31

224

2322

21

114

1312

11

det

nnn

nn

n

nnnn

aa

aa

a

aa

aa

aa

aa

aa

aa

aa

aa

aa

aa

43

21

444

4342

41

334

3332

31

224

2322

21

114

1312

11

det

FEM

1D

Page 42: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

42

Proc

edur

es o

f Con

juga

te G

radi

ent

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

•M

at-V

ec. M

ultip

licat

ion

•D

ot P

rodu

cts

•D

AX

PY

(Dou

ble

Pre

cisi

on: a

{X} +

{Y})

x(i) : Vector

i

: Scalar

FEM

1D

Page 43: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

43

Proc

edur

es o

f Con

juga

te G

radi

ent

•M

at-V

ec. M

ultip

licat

ion

•D

ot P

rodu

cts

•D

AX

PY

x(i) : Vector

i

: Scalar

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

FEM

1D

Page 44: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

44

Proc

edur

es o

f Con

juga

te G

radi

ent

•M

at-V

ec. M

ultip

licat

ion

•D

ot P

rodu

cts

•D

AX

PY

x(i) : Vector

i

: Scalar

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

FEM

1D

Page 45: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

45

Proc

edur

es o

f Con

juga

te G

radi

ent

•M

at-V

ec. M

ultip

licat

ion

•D

ot P

rodu

cts

•D

AX

PY

•D

oubl

e•

{y}=

a{x

} + {y

}

x(i) : Vector

i

: Scalar

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

FEM

1D

Page 46: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

46

Proc

edur

es o

f Con

juga

te G

radi

ent

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

x(i) : Vector

i

: Scalar

FEM

1D

Page 47: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

47

Der

ivat

ion

of C

G A

lgor

ithm

(1/5

)

b

yb

xAx

xAy

yAy

xAx

xAy

yAy

xAx

yAx

xy

xA

yx

T

,,

2,

,,

2,

,,

,,

y

xA

yx

T

Sol

utio

n x

min

imiz

es th

e fo

llow

ing

equa

tion

if y

is th

e ex

act

solu

tion

(Ay=

b)

Con

st.

b

xAx

xx

f,

,21

Ther

efor

e, th

e so

lutio

n x

min

imiz

es th

e fo

llow

ing

f(x):

Ahh

bAx

hx

fh

xf

,21

,

Arb

itrar

y ve

ctor

h

FEM

1D

Page 48: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

48

b

xAx

xx

f,

,21

Ahh

bAx

hx

fh

xf

,21

,

Arb

itrar

y ve

ctor

h

Ahh

bAx

hx

f

Ahh

bh

Axh

bx

Axx

bh

bx

Ahh

Ahx

Axh

Axx

bh

bx

Ahh

xAx

hx

bh

xh

xA

hx

hx

f

,21

,

,21

,,

,,

21

,,

,21

,21

,21

,21

,,

,21

,21

,)

(,

21

FEM

1D

Page 49: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

49

Der

ivat

ion

of C

G A

lgor

ithm

(2/5

)

)(

)(

)1(

kk

kk

px

x

CG

met

hod

min

imiz

es f(

x)at

eac

h ite

ratio

n.

Ass

ume

that

app

roxi

mat

e so

lutio

n: x

(0) ,

and

sear

ch d

irect

ion

vect

or p

(k) is

def

ined

at k

-thite

ratio

n.

)

()

()

()

()

(2

)(

)(

,,

21k

kk

kk

kk

kk

kx

fAx

bp

App

px

f

Min

imiz

atio

n of

f(x(k

+1)

) is

done

as

follo

ws:

)

()

(

)(

)(

)(

)(

)(

)(

)(

)(

,,,,

0k

k

kk

kk

kk

kk

kk

k

App

rp

App

Axb

pp

xf

)(

)(

kk

Axb

r

re

sidu

al v

ecto

r

FEM

1D

(1)

Page 50: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

50

Der

ivat

ion

of C

G A

lgor

ithm

(3/5

)

)(

)(

)1(

kk

kk

Apr

r

Res

idua

l vec

tor a

t (k+

1)-th

itera

tion:

It’s

luck

y if

we

can

get e

xact

sol

utio

n y

at (k

+1)

-thite

ratio

n:)1

(1

)1(

k

kk

px

y

)(

)(

)1(

)(

)1(

)(

)(

)1(

)1(

,k

kk

kk

k

kk

kk

ApAx

Axr

rAx

br

Axb

r

FEM

1D

(2)

)0()0(

)(

)1(

)1(

,p

rp

rp

kk

kk

Sea

rch

dire

ctio

n ve

ctor

pis

def

ined

by

the

follo

win

g re

curre

nce

form

ula:

(3)

Page 51: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

51

Der

ivat

ion

of C

G A

lgor

ithm

(4/5

)B

TW, w

e ha

ve th

e fo

llow

ing

(con

veni

ent)

orth

ogon

ality

rela

tion:

Thus

, fol

low

ing

rela

tion

is o

btai

ned:

0,

0,

,)

()1

()1

(1

)(

)1(

)(

k

kk

kk

kk

App

pAp

xy

Ap

0,

,,

,,

,,

,

)(

)(

)(

)(

)(

)(

)(

)(

)(

)(

)(

)(

)(

)1(

)(

)1(

)(

)1(

)(

kk

kk

kk

kk

k

kk

kk

kk

kk

kk

kk

kk

App

rp

Apr

p

ApAx

bp

px

Ab

pAx

bp

AxAy

px

yAp

)

()

(

)(

)(

,,k

k

kk

kAp

pr

p

0,

)1(

)(

kk

xy

Ap

FEM

1D

Page 52: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

52

Der

ivat

ion

of C

G A

lgor

ithm

(5/5

)

)(

)(

)(

)1(

)(

)(

)(

)1(

)(

)(

)1(

)(

)1(

,,

0,

,,

,

kk

kk

k

kk

kk

kk

kk

kk

k

App

Apr

App

Apr

App

rAp

p

0,

)(

)1(

kk

App

p(k)an

dp(k

+1)

are

“con

juga

te(共役)” f

or m

atrix

A

FEM

1D

(4)

)1

()1

(

)1(

)(

1,,

i

i

ii

iAp

pAp

r

Compute p(0)=r(0)= b-[A]x(0)

fori= 1, 2, …

calc. i-1

x(i)= x(i-1) + i-1p(i-1)

r(i)= r(i-1) –

i-1[A]p(i-1)

check convergence |r|

(if not converged)

calc. i

-1

p(i)= r(i) + i

-1p(i-1)

end

)1

()1

(

)1(

)1(

1,,

ii

ii

iAp

pr

p

Page 53: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

53

Pro

perti

es o

f CG

Alg

orith

mFE

M1D Fo

llow

ing

“con

juga

te(共役)” r

elat

ions

hip

is o

btai

ned

for

arbi

trary

(i,j)

:

In N

-dim

ensi

onal

spa

ce, o

nly

N s

ets

of o

rthog

onal

and

line

arly

in

depe

nden

t res

idua

l vec

tor r

(k) .

This

mea

ns C

G m

etho

d co

nver

ges

afte

r N it

erat

ions

if n

umbe

r of u

nkno

wns

is N

. A

ctua

lly, r

ound

-off

erro

r som

etim

es a

ffect

s co

nver

genc

e.

j

iAp

pj

i

0

,)

()

(

)

()

()

()

()

()

(,

,,

0,

kk

kk

ji

rr

rp

ji

rr

Follo

win

g re

latio

nshi

ps a

re a

lso

obta

ined

for p

(k)an

dr(k

) :

Page 54: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

54

Pro

of (1

/3)

Mat

hem

atic

al In

duct

ion

数学的帰納法

FEM

1D

j

iAp

pj

ir

rj

i

ji

0,

0,

)(

)(

)(

)(

)

()

(

)(

)(

,,k

k

kk

kAp

pr

p

(1

)

(2)

(3)

(4)

)(

)(

)1(

kk

kk

Apr

r

)0()0(

)(

)1(

)1(

,p

rp

rp

kk

kk

)

()

(

)(

)1(

,,k

k

kk

kAp

pAp

r

Page 55: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

55

Pro

of (2

/3)

Mat

hem

atic

al In

duct

ion

数学的帰納法

FEM

1D (*) i

s sat

isfie

dfo

rj

ik

jk

i

whe

re,

0,

,

,,

,,

,

)(

)1(

1)

()

(

)(

)1(

1)

()

()

(

)(

)(

)(

)1(

)(

)(

)1(

ki

ik

ki

k

ki

ii

kk

ik

kk

ki

ki

ik

App

App

App

pAp

r

Apr

rr

rr

r

j

iAp

pj

ir

rj

i

ji

0,

0,

)(

)(

)(

)(

if i <

k

if i =

k

0

,,

,,

,,

,,

,,

,,

,,

,

)1(

)1(

1)1

()1

(1

)1(

1)1

()1

(1

)(

)1(

1

)(

)(

)1(

1)

()

(

)(

)(

)(

)(

)(

)(

)(

)(

)(

)1(

1)

()

()

(

)(

)(

)(

)(

)(

)1(

kk

kk

kk

kk

kk

kk

kk

kk

kk

kk

kk

kk

kk

kk

k

kk

kk

kk

k

kk

kk

kk

k

App

rp

Apr

pr

p

rr

pr

r

rp

rr

App

rr

App

pr

r

Apr

rr

rr

(2)

(4)

(2)

(3)

(3)

(2)

(1)

(*)

(*)

(*)

(*)

(1)

)

()

(

)(

)(

,,k

k

kk

kAp

pr

p

(1

)

(2)

(3)

(4)

)(

)(

)1(

kk

kk

Apr

r

)(

)1(

)1(

kk

kk

pr

p

)

()

(

)(

)1(

,,k

k

kk

kAp

pAp

r

Page 56: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

56

Pro

of (3

/3)

Mat

hem

atic

al In

duct

ion

数学的帰納法

FEM

1D

j

iAp

pj

ir

rj

i

ji

0,

0,

)(

)(

)(

)(

(*)

0,

,,

)(

)(

)(

)1(

)(

)1(

k

kk

kk

kk

App

Apr

App

(*) i

s sat

isfie

dfo

rj

ik

jk

i

whe

re,

0,

1,

,,

)1(

)(

)1(

)(

)1(

)(

)(

)1(

)(

)1(

ii

k

k

ik

ik

kk

ik

rr

r

Apr

App

rAp

p

if

i < k

if i =

k

(3)

(2)

(3)

(4)

(*)

)

()

(

)(

)(

,,k

k

kk

kAp

pr

p

(1

)

(2)

(3)

(4)

)(

)(

)1(

kk

kk

Apr

r

)(

)1(

)1(

kk

kk

pr

p

)

()

(

)(

)1(

,,k

k

kk

kAp

pAp

r

Page 57: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

57FE

M1D

0,

,,

,

,,

,,

,0

,

)(

)(

)(

)(

)(

)(

)(

)(

)(

)1(

1)

()

()

(

)(

)(

)(

)(

)(

)1(

)(

)1(

kk

kk

kk

kk

k

kk

kk

kk

k

kk

kk

kk

k

kk

rp

rr

App

rr

App

pr

r

Apr

rr

rr

rr

(2

)

(3)

(*)

(1)

)

()

(

)(

)(

,,k

k

kk

kAp

pr

p

(1

)

(2)

(3)

(4)

)(

)(

)1(

kk

kk

Apr

r

)(

)1(

)1(

kk

kk

pr

p

)

()

(

)(

)1(

,,k

k

kk

kAp

pAp

r

)

()

()

()

(,

,k

kk

kr

pr

r

Page 58: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

58

k, k

k

kk

k

kk

kk

k

kk

kk

kk

kk

k

rr

rr

rAp

r

rr

rr

App

Apr

)1(

)1(

)1(

)(

)1(

)(

)1(

)(

)(

)1(

)1(

)(

)(

)(

)1(

,,

,

,,,,

Usu

ally,

we

use

sim

pler

def

initi

ons

of

k, k

as fo

llow

s:

)

()

()

()

(

)(

)(

)(

)(

)(

)(

)(

)(

)(

)(

)(

)(

,,

,,,,

,,

kk

kk

kk

kk

kk

kk

kk

kk

k

rr

rp

App

rr

App

rp

App

Axb

p

FEM

1D

Page 59: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

59

Proc

edur

es o

f Con

juga

te G

radi

ent

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

x(i) : Vector

i

: Scalar

FEM

1D

)

()

(

)1(

)1(

,,i

i

ii

iAp

pr

r

)2

()2

(

)1(

)1(

1,,

ii

ii

ir

rr

r

2

i

1

i

1

i

Page 60: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

60

Prec

ondi

tioni

ng fo

r Ite

rativ

e So

lver

s

Con

verg

ence

rate

of i

tera

tive

solv

ers

stro

ngly

dep

ends

on

the

spec

tral p

rope

rties

(eig

enva

lue

dist

ribut

ion)

of

the

coef

ficie

nt m

atrix

A.

E

igen

valu

e di

strib

utio

n is

sm

all,

eige

nval

ues

are

clos

e to

1

In “i

ll-co

nditi

oned

” pro

blem

s, “c

ondi

tion

num

ber”

(ratio

of

max

/min

eig

enva

lue

if A

is s

ymm

etric

) is

larg

e(条件数)

.

A pr

econ

ditio

ner M

(who

se p

rope

rties

are

sim

ilar t

o th

ose

of A

)tra

nsfo

rms

the

linea

r sys

tem

into

one

with

m

ore

favo

rabl

e sp

ectra

l pro

perti

es (

前処

理)

M

trans

form

s A

x=b

into

A'x

=b'w

here

A'=

M-1

A, b

'=M

-1b

If

M~A

, M-1

Ais

clo

se to

iden

tity

mat

rix.

If

M-1

=A-1

, thi

s is

the

best

pre

cond

ition

er(G

auss

ian

Elim

.)

Gen

eral

ly, A

'x’=

b’w

here

A'=

ML-1

AM

R-1

, b'=

ML-1

b, x

’=M

Rx

M

L/MR: L

eft/R

ight

Pre

cond

ition

ing(

左/右前処理)

FEM

1D

Page 61: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

61

Prec

ondi

tione

d C

G S

olve

r

x

FEM

1D Compute r(0)= b-[A]x(0)

fori= 1, 2, …

solve [M]z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

[M]= [M1][M2]

[A’]x’=b’

[A’]=[M1]-1[A][M2]-1

x’=[M2]x, b’=[M1]-1b

p’=>[M2]p, r’=>[M1]-1r

p’(i)= r’(i-1) + ’

i-1p’(i-1)

[M2]p(i)= [M1]-1r(i-1) + ’

i-1[M2]p(i-1)

p(i)= [M2]-1[M1]-1r(i-1) + ’

i-1 p(i-1)

p(i)= [M]-1r(i-1) + ’

i-1 p(i-1)

’i-1= ([M]-1r(i-1),r(i-1))/

([M]-1r(i-2),r(i-2))

’i-1= ([M]-1r(i-1),r(i-1))/

( p(i-1),[A]p(i-1))

Page 62: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

62

ILU

(0),

IC(0

)•

Wid

ely

used

Pre

cond

ition

ers

for S

pars

e M

atric

es–

Inco

mpl

ete

LU F

acto

rizat

ion(

不完全

LU分解)

–In

com

plet

e C

hole

sky

Fact

oriz

atio

n (fo

r Sym

met

ric

Mat

rices

)(不完全コレスキー分解)

•In

com

plet

e D

irect

Met

hod

–E

ven

if or

igin

al m

atrix

is s

pars

e, in

vers

e m

atrix

is n

ot

nece

ssar

ily s

pars

e.–

fill-i

n–

ILU

(0)/I

C(0

) with

out f

ill-in

hav

e sa

me

non-

zero

pat

tern

w

ith th

e or

igin

al (s

pars

e) m

atric

es

FEM

1D

Page 63: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

63

Dia

gona

l Sca

ling,

Poi

nt-J

acob

i

N

N

DD

DD

M

0...

00

00

0...

......

00

00

0...

0

1

2

1

•solve [M]z(i-1)= r(i-1)

is v

ery

easy

.•

Pro

vide

s fa

st c

onve

rgen

ce fo

r sim

ple

prob

lem

s.•

1d.f,

1d.

c

FEM

1D

Page 64: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D64

•1D

-cod

e fo

r Sta

tic L

inea

r-E

last

ic P

robl

ems

by

Gal

erki

n FE

M•

Spa

rse

Line

ar S

olve

r–

Con

juga

te G

radi

ent M

etho

d–

Pre

cond

ition

ing

•S

tora

ge o

f Spa

rse

Mat

rices

•P

rogr

am

Page 65: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

65

Coe

f. M

atrix

der

ived

from

FEM

•S

pars

e M

atrix

–M

any

“0”’s

•S

torin

g al

l com

pone

nts

(e.g

. A(i,

j)) is

not

effi

cien

t fo

r spa

rse

mat

rices

–A

(i,j)

is s

uita

ble

for d

ense

m

atric

es

16151413121110987654321

16151413121110987654321

FFFFFFFFFFFFFFFF

DX

XX

XD

XX

XX

XD

XX

XX

XD

XX

XX

DX

XX

XX

XX

DX

XX

XX

XX

XD

XX

XX

XX

XD

XX

XX

DX

XX

XX

XX

DX

XX

XX

XX

XD

XX

XX

XX

XD

XX

XX

DX

XX

XX

DX

XX

XX

DX

XX

XD

•N

umbe

r of n

on-z

ero

off-d

iago

nal c

ompo

nent

s is

O

(100

) in

FEM

If nu

mbe

r of u

nkno

wns

is 1

08 :

•A

(i,j):

O(1

016) w

ords

•A

ctua

l Non

-zer

o C

ompo

nent

s:O

(1010

) wor

ds

•O

nly

(rea

lly) n

on-z

ero

off-d

iag.

com

pone

nts

shou

ld

be s

tore

d on

mem

ory

FEM

1D

Page 66: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

66

Var

iabl

es/A

rray

s in

1d.

f, 1d

.cre

late

d to

coe

ffici

ent m

atrix

nam

ety

pesi

zede

scrip

tion

N NPLU

I I

- -

# U

nkno

wns

# N

on-Z

ero

Off-

Dia

gona

l Com

pone

nts

Diag(:)

RN

Dia

gona

l Com

pone

nts

U(:)

RN

Unk

now

n Ve

ctor

Rhs(:)

RN

RH

S V

ecto

r

Index(:)

I0:N

N+1

Off-

Dia

gona

l Com

pone

nts

(Num

ber o

f Non

-Zer

o O

ff-D

iago

nals

at E

ach

RO

W)

Item(:)

INPLU

Off-

Dia

gona

l Com

pone

nts

(Cor

resp

ondi

ng C

olum

n ID

)

AMat(:)

RNPLU

Off-

Dia

gona

l Com

pone

nts

(Val

ue)

Onl

y no

n-ze

ro c

ompo

nent

s ar

e st

ored

acc

ordi

ngto

“Com

pres

sed

Row

Sto

rage

”.

FEM

1D

Page 67: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

67

Mat

-Vec

. Mul

tiplic

atio

n fo

r Spa

rse

Mat

rixC

ompr

esse

d R

ow S

tora

ge (C

RS

)

Diag (i)

Dia

gona

l Com

pone

nts (

REA

L, i=

1~N

)Index(i)

Num

ber o

f Non

-Zer

o O

ff-D

iago

nals

at E

ach

RO

W (I

NT,

i=0~

N)

Item(k)

Off-

Dia

gona

l Com

pone

nts (

Cor

resp

ondi

ng C

olum

n ID

)(IN

T, k

=1, i

ndex

(N))

AMat(k)

Off-

Dia

gona

l Com

pone

nts (

Valu

e)(

REA

L, k

=1, i

ndex

(N) )

{Y}= [A]{X}

do i= 1, N

Y(i)= Diag(i)*X(i)

do k= Index(i-1)+1, Index(i)

Y(i)= Y(i) + Amat(k)*X(Item(k))

enddo

enddo

16151413121110987654321

16151413121110987654321

FFFFFFFFFFFFFFFF

DX

XX

XD

XX

XX

XD

XX

XX

XD

XX

XX

DX

XX

XX

XX

DX

XX

XX

XX

XD

XX

XX

XX

XD

XX

XX

DX

XX

XX

XX

DX

XX

XX

XX

XD

XX

XX

XX

XD

XX

XX

DX

XX

XX

DX

XX

XX

DX

XX

XD

FEM

1D

Page 68: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

68

CR

S or

CSR

?

for C

ompr

esse

d R

ow S

tora

ge

•“C

RS

” in

Fran

ce–

Com

pagn

ie R

épub

licai

ne d

e S

écur

ité

•R

epub

lic S

ecur

ity C

ompa

ny o

f Fra

nce

•Fr

ench

sci

entis

ts m

ay fe

el

unco

mfo

rtabl

e w

hen

we

use

“CR

S” i

n te

chni

cal p

aper

s an

d/or

pre

sent

atio

ns.

•In

Jap

an a

nd U

SA

, “C

RS

” is

very

gen

eral

for

abbr

evia

tion

of “C

ompr

esse

d R

ow S

tora

ge”,

but t

hey

usua

lly u

se “C

SR

” in

Eur

ope

(esp

ecia

lly in

Fra

nce)

.

FEM

1D

Page 69: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

69

Mat

-Vec

. Mul

tiplic

atio

n fo

r Spa

rse

Mat

rixC

ompr

esse

d R

ow S

tora

ge (C

RS

)

{Q}=[A]{P}

for(i=0;i<N;i++){

W[Q][i] = Diag[i] * W[P][i];

for(k=Index[i];k<Index[i+1];k++){

W[Q][i] += AMat[k]*W[P][Item[k]];

}}

FEM

1D

Page 70: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

70

Mat

-Vec

. Mul

tiplic

atio

n fo

r Den

se M

atrix

Ver

y E

asy,

Stra

ight

forw

ard

NN

NN

NN

NN

NN

NN

NN

NN

aa

aa

aa

aa

aa

aa

aa

aa

,1

,2,

1,

,11

,12,1

1,1

,21

,222

21

,11

,112

11

...

......

...

...

NN

NN

yyyy

xxxx

1

21

1

21

{Y}= [A]{X}

do j= 1, N

Y(j)= 0.d0

do i= 1, N

Y(j)= Y(j) + A(i,j)*X(i)

enddo

enddo

FEM

1D

Page 71: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

71

Com

pres

sed

Row

Sto

rage

(CR

S)

3.51

01.3

06.9

3.15.9

01.

131.

234.1

00

5.24.6

00

5.94.

120

05.6

00

03.4

05.

110

4.10

5.91.3

00

7.25. 2

8.90

1.40

01.3

05.1

07.5

00

1.90

7.30

5.20

6.33.4

00

02.3

00

4.21.1

1 2 3 4 5 6 7 8

12

34

56

78

FEM

1D

Page 72: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

72

Com

pres

sed

Row

Sto

rage

(CR

S): C

Num

berin

g st

arts

from

0 in

pro

gram

2.4

①3.

2④

4.3

◎2.

5③

3.7

⑤9.

1⑦

1.5

④3.

1⑥

4.1

①2.

5④

2.7

3.1

◎9.

5①

10.4

②4.

3⑥

6.5

②9.

5⑥

6.4

①2.

5②

1.4

⑤13

.1⑦

9.5

①1.

3②

9.6

③3.

1⑤

N= 8

Dia

gona

l Com

pone

nts

Diag[0]= 1.1

Diag[1]= 3.6

Diag[2]= 5.7

Diag[3]= 9.8

Diag[4]= 11.5

Diag[5]= 12.4

Diag[6]= 23.1

Diag[7]= 51.3

1.1

3.6

5.7

9.8

11.5

12.4

23.1

51.3

01

23

45

67

0 1 2 3 4 5 6 7

FEM

1D

Page 73: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

73

Com

pres

sed

Row

Sto

rage

(CR

S):

C0

2.4

①3.

2④

4.3

◎2.

5③

3.7

⑤9.

1⑦

1.5

④3.

1⑥

4.1

①2.

5④

2.7

3.1

◎9.

5①

10.4

②4.

3⑥

6.5

②9.

5⑥

6.4

①2.

5②

1.4

⑤13

.1⑦

9.5

①1.

3②

9.6

③3.

1⑤

12

34

56

71.

1◎ 3.

6① 5.

7② 9.

8③ 11

.5④ 12

.4⑤ 23

.1⑥ 51

.3⑦

0 1 2 3 4 5 6 7

FEM

1D

Page 74: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

74

Com

pres

sed

Row

Sto

rage

(CR

S):

C

0 1 2 3 4 5 6 7

2.4

①3.

2④

4.3

◎2.

5③

3.7

⑤9.

1⑦

1.5

④3.

1⑥

4.1

①2.

5④

2.7

3.1

◎9.

5①

10.4

②4.

3⑥

6.5

②9.

5⑥

6.4

①2.

5②

1.4

⑤13

.1⑦

9.5

①1.

3②

9.6

③3.

1⑤

1.1

◎ 3.6

① 5.7

② 9.8

③ 11.5

④ 12.4

⑤ 23.1

⑥ 51.3

# N

on-Z

ero

Off-

Dia

g.2 Index[1]= 2

4 Index[2]= 6

2 Index[3]= 8

3 Index[4]= 11

4 Index[5]= 15

2 Index[6]= 17

4 Index[7]= 21

4 Index[8]= 25

Index[0]= 0

NPLU= 25

(=Index[N])

(Index[i])th~(Index[i+1])

th:

Non

-Zer

o O

ff-D

iag.

Com

pone

nts

corre

spon

ding

to i-

thro

w.

FEM

1D

Page 75: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

75

Com

pres

sed

Row

Sto

rage

(CR

S):

C

0 1 2 3 4 5 6 7

2.4

①3.

2④

4.3

◎2.

5③

3.7

⑤9.

1⑦

1.5

④3.

1⑥

3.1

◎9.

5①

10.4

②4.

3⑥

6.5

②9.

5⑥

6.4

①2.

5②

1.4

⑤13

.1⑦

9.5

①1.

3②

9.6

③3.

1⑤

1.1

◎ 3.6

① 5.7

② 9.8

③ 11.5

④ 12.4

⑤ 23.1

⑥ 51.3

4.1

①2.

5④

2.7

NPLU= 25

(=Index[N])

2 Index[1]= 2

4 Index[2]= 6

2 Index[3]= 8

3 Index[4]= 11

4 Index[5]= 15

2 Index[6]= 17

4 Index[7]= 21

4 Index[8]= 25

Index[0]= 0

# N

on-Z

ero

Off-

Dia

g.

(Index[i])th~(Index[i+1])

th:

Non

-Zer

o O

ff-D

iag.

Com

pone

nts

corre

spon

ding

to i-

thro

w.

FEM

1D

Page 76: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

76

Com

pres

sed

Row

Sto

rage

(CR

S):

C

Item[ 6]= 4, AMat[ 6]= 1.5

Item[18]= 2, AMat[18]= 2.5

0 1 2 3 4 5 6 7

2.4

①3.

2④

4.3

◎2.

5③

3.7

⑤9.

1⑦

3.1

4.1

①2.

5④

2.7

3.1

◎9.

5①

10.4

②4.

3⑥

6.5

②9.

5⑥

6.4

①1.

4⑤

13.1

9.5

①1.

3②

9.6

③3.

1⑤

1.1

◎ 3.6

① 5.7

② 9.8

③ 11.5

④ 12.4

⑤ 23.1

⑥ 51.3

1.5

2.5

FEM

1D

Page 77: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

77

Com

pres

sed

Row

Sto

rage

(CR

S):

C

0 1 2 3 4 5 6 7

2.4

①,0

3.2

④,1

4.3

◎,2

2.5

③,3

3.7

⑤,4

9.1

⑦,5

1.5

④,6

3.1

⑥,7

4.1

①,8

2.5

④,9

2.7

⑤,1

03.

1◎

,11

9.5

①,1

210

.4②

,13

4.3

⑥,1

46.

5②

,15

9.5

⑥,1

66.

4①

,17

2.5

②,1

81.

4⑤

,19

13.1

⑦,2

09.

5①

,21

1.3

②,2

29.

6③

,23

3.1

⑤,2

4

1.1

◎ 3.6

① 5.7

② 9.8

③ 11.5

④ 12.4

⑤ 23.1

⑥ 51.3

{Y}=[A]{X}

for(i=0;i<N;i++){

Y[i] = Diag[i] * X[i];

for(k=Index[i];k<Index[i+1];k++){

Y[i] += AMat[k]*X[Item[k]];

}}

Diag [i]

Dia

gona

l Com

pone

nts (

REA

L, i=

0~N

-1)

Index[i[

Num

ber o

f Non

-Zer

o O

ff-D

iago

nals

at

Each

RO

W (I

NT,

i=0~

N)

Item[k]

Off-

Dia

gona

l Com

pone

nts

(Cor

resp

ondi

ng C

olum

n ID

)(IN

T, k

=0, i

ndex

[N])

Amat[k]

Off-

Dia

gona

l Com

pone

nts (

Valu

e)(

REA

L, k

=0, i

ndex

[N] )

FEM

1D

Page 78: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D78

•1D

-cod

e fo

r Sta

tic L

inea

r-E

last

ic P

robl

ems

by

Gal

erki

n FE

M•

Spa

rse

Line

ar S

olve

r–

Con

juga

te G

radi

ent M

etho

d–

Pre

cond

ition

ing

•S

tora

ge o

f Spa

rse

Mat

rices

•P

rogr

am

Page 79: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D79

Fini

te E

lem

ent P

roce

dure

s•

Initi

aliz

atio

n–

Con

trol D

ata

–N

ode,

Con

nect

ivity

of E

lem

ents

(N: N

ode#

, NE

: Ele

m#)

–In

itial

izat

ion

of A

rrays

(Glo

bal/E

lem

ent M

atric

es)

–E

lem

ent-G

loba

l Mat

rix M

appi

ng (I

ndex

, Ite

m)

•G

ener

atio

n of

Mat

rix–

Ele

men

t-by-

Ele

men

t Ope

ratio

ns (d

o ic

el=

1, N

E)

•E

lem

ent m

atric

es•

Acc

umul

atio

n to

glo

bal m

atrix

–B

ound

ary

Con

ditio

ns•

Line

ar S

olve

r–

Con

juga

te G

radi

ent M

etho

d

Page 80: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D80

Pro

gram

: 1d.

c (1

/6)

varia

bles

and

arr

ays

/*

// 1D Steady-State Heat Transfer

// FEM with Piece-wise Linear Elements

// CG (Conjugate Gradient) Method

//

// d/dx(CdT/dx) + Q = 0

// T=0@x=0

*/

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <assert.h>

int main(){

int NE, N, NPLU, IterMax;

int R, Z, Q, P, DD;

double dX, Resid, Eps, Area, QV, COND;

double X1, X2, U1, U2, DL, Strain, Sigma, Ck;

double QN, XL, C2, Xi, PHIa;

double *PHI, *Rhs, *X;

double *Diag, *AMat;

double **W;

int *Index, *Item, *Icelnod;

double Kmat[2][2], Emat[2][2];

int i, j, in1, in2, k, icel, k1, k2, jS;

int iter;

FILE *fp;

double BNorm2, Rho, Rho1=0.0, C1, Alpha, DNorm2;

int ierr = 1;

int errno = 0;

Page 81: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D81

Var

iabl

e/A

rray

s (1

/2)

Q

Nam

eTy

peS

ize

I/OD

efin

ition

NE

II

# El

emen

t

NI

O#

Nod

e

NPLU

IO

# N

on-Z

ero

Off-

Dia

g. C

ompo

nent

s

IterMax

II

MA

X It

erat

ion

Num

ber f

or C

G

errno

IO

ERR

OR

flag

R, Z, Q, P,

DD

IO

Nam

e of

Vec

tors

in C

G

dX

RI

Leng

th o

f Eac

h El

emen

t

Resid

RO

Res

idua

l for

CG

Eps

RI

Con

verg

ence

Crit

eria

for C

G

Area

RI

Sect

iona

l Are

a of

Ele

men

t

QV

RI

Hea

t Gen

erat

ion

Rat

e/Vo

lum

e/Ti

me

COND

RI

Ther

mal

Con

duct

ivity

Page 82: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D82

Var

iabl

e/A

rray

s (2

/2)

Nam

eTy

peS

ize

I/OD

efin

ition

XR

NO

Loca

tion

of E

ach

Nod

ePHI

RN

OTe

mpe

ratu

re o

f Eac

h N

ode

Rhs

RN

OR

HS

Vect

orDiag

RN

OD

iago

nal C

ompo

nent

sW

R[4][N]

OW

ork

Arr

ay fo

r CG

Amat

RNPLU

OO

ff-D

iago

nal C

ompo

nent

s (Va

lue)

Index

IN+1

ON

umbe

r of N

on-Z

ero

Off-

Dia

gona

ls a

t Ea

ch R

OW

Item

INPLU

OO

ff-D

iago

nal C

ompo

nent

s (C

orre

spon

ding

Col

umn

ID)

Icelnod

I2*NE

ON

ode

ID fo

r Eac

h El

emen

tKmat

R[2][2]

OEl

emen

t Mat

rix [k

]Emat

R[2][2]

OEl

emen

t Mat

rix

Page 83: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D83

Pro

gram

: 1d.

c (2

/6)

Initi

aliz

atio

n, A

lloca

tion

of A

rray

s/*

// +-------+

// | INIT. |

// +-------+

*/

fp = fopen("input.dat", "r");

assert(fp != NULL);

fscanf(fp, "%d", &NE);

fscanf(fp, "%lf %lf %lf %lf", &dX, &QV, &Area, &COND);

fscanf(fp, "%d", &IterMax);

fscanf(fp, "%lf", &Eps);

fclose(fp);

N= NE + 1;

PHI = calloc(N, sizeof(double));

X = calloc(N, sizeof(double));

Diag = calloc(N, sizeof(double));

AMat = calloc(2*N-2, sizeof(double));

Rhs = calloc(N, sizeof(double));

Index= calloc(N+1, sizeof(int));

Item = calloc(2*N-2, sizeof(int));

Icelnod= calloc(2*NE, sizeof(int));Con

trol

Dat

ainput.dat

4NE (

Num

ber o

f Ele

men

ts)

1.0 1.01.01.0

x

(Len

gth

of E

ach

Ele

m.: L

) ,Q, A,

100

Num

ber o

f MA

X. I

tera

tions

for C

G S

olve

r1.e-8

Con

verg

ence

Crit

eria

for C

G S

olve

r

12

34

51

23

4

NE

: # E

lem

ent

N

: # N

ode

(NE

+1)

Page 84: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

/*

// +-------+

// | INIT. |

// +-------+

*/

fp = fopen("input.dat", "r");

assert(fp != NULL);

fscanf(fp, "%d", &NE);

fscanf(fp, "%lf %lf %lf %lf", &dX, &QV, &Area, &COND);

fscanf(fp, "%d", &IterMax);

fscanf(fp, "%lf", &Eps);

fclose(fp);

N= NE + 1;

PHI = calloc(N, sizeof(double));

X = calloc(N, sizeof(double));

Diag = calloc(N, sizeof(double));

AMat = calloc(2*N-2, sizeof(double));

Rhs = calloc(N, sizeof(double));

Index= calloc(N+1, sizeof(int));

Item = calloc(2*N-2, sizeof(int));

Icelnod= calloc(2*NE, sizeof(int));

FEM

1D84

Pro

gram

: 1d.

c (2

/6)

Initi

aliz

atio

n, A

lloca

tion

of A

rray

s

Amat

: N

on-Z

ero

Off-

Dia

g. C

omp.

Item

: C

orre

spon

ding

Col

umn

ID

Page 85: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D85

Ele

men

t/Glo

bal O

pera

tions

12

34

51

23

4

12

e1

2e

12

e1

2e

0

dVN

XdS

N

dVdxN

ddxN

dE

V

T

S

T

T

V

)1( 2

)1( 1)1( 2

)1( 1)1( 22

)1( 21

)1( 12)1( 11

)1()1(

)1(}

{}

{]

[

ffk

kk

k

fk

)2( 2

)2( 1)2( 2

)2( 1)2( 22

)2( 21

)2( 12)2( 11

)2()2(

)2(}

{}

{]

[

ffk

kk

k

fk

)3( 2

)3( 1)3( 2

)3( 1)3( 22

)3( 21

)3( 12)3( 11

)3()3(

)3(}

{}

{]

[

ffk

kk

k

fk

)4( 2

)4( 1)4( 2

)4( 1)4( 22

)4( 21

)4( 12)4( 11

)4()4(

)4(}

{}

{]

[

ffk

kk

k

fk

12

34

51

23

4

54321

54321

551

414

41

313

31

212

21

111

}{

}]{

[

BBBBB

DAL

AUD

ALAU

DAL

AUD

ALAU

DF

KN

umbe

r of n

on-z

ero

off-

diag

. com

pone

nts

is 2

for

each

nod

e. T

his

num

ber

is 1

at b

ound

ary

node

s).

0

dVN

XdS

N

dVdxN

ddxN

dE

V

T

S

T

T

V

0

dVN

XdS

N

dVdxN

ddxN

dE

V

T

S

T

T

V

0

dVN

XdS

N

dVdxN

ddxN

dE

V

T

S

T

T

V

Glo

bal N

ode

ID

Loca

l Nod

e ID

Page 86: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D86

Atte

ntio

n: In

C p

rogr

am, n

ode

and

elem

ent I

D’s

sta

rt fro

m 0

.

12

34

51

23

4

01

23

40

12

3

01

e0

1e

01

e0

1e

Page 87: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D87

Pro

gram

: 1d.

c (2

/6)

Initi

aliz

atio

n, A

lloca

tion

of A

rray

s/*

// +-------+

// | INIT. |

// +-------+

*/

fp = fopen("input.dat", "r");

assert(fp != NULL);

fscanf(fp, "%d", &NE);

fscanf(fp, "%lf %lf %lf %lf", &dX, &QV, &Area, &COND);

fscanf(fp, "%d", &IterMax);

fscanf(fp, "%lf", &Eps);

fclose(fp);

N= NE + 1;

PHI = calloc(N, sizeof(double));

X = calloc(N, sizeof(double));

Diag = calloc(N, sizeof(double));

AMat = calloc(2*N-2, sizeof(double));

Rhs = calloc(N, sizeof(double));

Index= calloc(N+1, sizeof(int));

Item = calloc(2*N-2, sizeof(int));

Icelnod= calloc(2*NE, sizeof(int));

icel

Icel

nod[

2*ic

el]

=ic

elIc

elno

d[2*

icel

+1]

=ic

el+

1

Amat

: N

on-Z

ero

Off-

Dia

g. C

omp.

Item

: C

orre

spon

ding

Col

umn

ID

Num

ber o

f non

-zer

o of

f-dia

g.

com

pone

nts

is 2

for e

ach

node

. Thi

s nu

mbe

r is

1 at

bou

ndar

y no

des)

.

Tota

l Num

ber o

f Non

-Zer

o O

ff-D

iag.

C

ompo

nent

s:2*(N-2)+1+1= 2*N-2

Page 88: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D88

Pro

gram

: 1d.

c (3

/6)

Initi

aliz

atio

n, A

lloca

tion

of A

rray

s (c

ont.)

W = (double **)malloc(sizeof(double *)*4);

if(W == NULL) {

fprintf(stderr, "Error: %s¥n", strerror(errno));

return -1;

} for(i=0; i<4; i++) {

W[i] = (double *)malloc(sizeof(double)*N);

if(W[i] == NULL) {

fprintf(stderr, "Error: %s¥n", strerror(errno));

return -1;

}} for(i=0;i<N;i++) PHI[i] = 0.0;

for(i=0;i<N;i++) Diag[i] = 0.0;

for(i=0;i<N;i++) Rhs[i] = 0.0;

for(k=0;k<2*N-2;k++) AMat[k] = 0.0;

for(i=0;i<N;i++) X[i]= i*dX;

for(icel=0;icel<NE;icel++){

Icelnod[2*icel ]= icel;

Icelnod[2*icel+1]= icel+1;

} Kmat[0][0]= +1.0;

Kmat[0][1]= -1.0;

Kmat[1][0]= -1.0;

Kmat[1][1]= +1.0;

X:

X-c

oord

inat

e co

mpo

nent

of e

ach

node

Page 89: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D89

Pro

gram

: 1d.

c (3

/6)

Initi

aliz

atio

n, A

lloca

tion

of A

rray

s (c

ont.)

W = (double **)malloc(sizeof(double *)*4);

if(W == NULL) {

fprintf(stderr, "Error: %s¥n", strerror(errno));

return -1;

} for(i=0; i<4; i++) {

W[i] = (double *)malloc(sizeof(double)*N);

if(W[i] == NULL) {

fprintf(stderr, "Error: %s¥n", strerror(errno));

return -1;

}} for(i=0;i<N;i++) PHI[i] = 0.0;

for(i=0;i<N;i++) Diag[i] = 0.0;

for(i=0;i<N;i++) Rhs[i] = 0.0;

for(k=0;k<2*N-2;k++) AMat[k] = 0.0;

for(i=0;i<N;i++) X[i]= i*dX;

for(icel=0;icel<NE;icel++){

Icelnod[2*icel ]= icel;

Icelnod[2*icel+1]= icel+1;

} Kmat[0][0]= +1.0;

Kmat[0][1]= -1.0;

Kmat[1][0]= -1.0;

Kmat[1][1]= +1.0;

icel

Icel

nod[

2*ic

el]

=ic

elIc

elno

d[2*

icel

+1]

=ic

el+

1

Page 90: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D90

Pro

gram

: 1d.

c (3

/6)

Initi

aliz

atio

n, A

lloca

tion

of A

rray

s (c

ont.)

W = (double **)malloc(sizeof(double *)*4);

if(W == NULL) {

fprintf(stderr, "Error: %s¥n", strerror(errno));

return -1;

} for(i=0; i<4; i++) {

W[i] = (double *)malloc(sizeof(double)*N);

if(W[i] == NULL) {

fprintf(stderr, "Error: %s¥n", strerror(errno));

return -1;

}} for(i=0;i<N;i++) PHI[i] = 0.0;

for(i=0;i<N;i++) Diag[i] = 0.0;

for(i=0;i<N;i++) Rhs[i] = 0.0;

for(k=0;k<2*N-2;k++) AMat[k] = 0.0;

for(i=0;i<N;i++) X[i]= i*dX;

for(icel=0;icel<NE;icel++){

Icelnod[2*icel ]= icel;

Icelnod[2*icel+1]= icel+1;

} Kmat[0][0]= +1.0;

Kmat[0][1]= -1.0;

Kmat[1][0]= -1.0;

Kmat[1][1]= +1.0;

11

11

)(

LAdV

dxNd

dxNd

kT

V

e

[Kmat]

Page 91: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D91

Pro

gram

: 1d.

c (4

/6)

Glo

bal M

atrix

: Col

umn

ID fo

r Non

-Zer

o O

ff-D

iag’

s/*

// +--------------+

// | CONNECTIVITY |

// +--------------+

*/for(i=0;i<N+1;i++) Index[i] = 2;

Index[0]= 0;

Index[1]= 1;

Index[N]= 1;

for(i=0;i<N;i++){

Index[i+1]= Index[i+1] + Index[i];

}

NPLU= Index[N];

for(i=0;i<N;i++){

jS = Index[i];

if(i == 0){ Item[jS] = i+1;

}else if(i == N-1){

Item[jS] = i-1;

}else{

Item[jS] = i-1;

Item[jS+1] = i+1;

}}

Num

ber o

f non

-zer

o of

f-di

ag. c

ompo

nent

s is 2

for e

ach

node

. Thi

s num

ber i

s 1 a

t bou

ndar

y no

des)

.

Tota

l Num

ber o

f Non

-Zer

o O

ff-D

iag.

Com

pone

nts:

2*(N

-2)+

1+1=

2*N

-2=

NPL

U=

Inde

x[N

]

0 1 2 3 4 5 6 7

2.4

①3.

2④

4.3

◎2.

5③

3.7

⑤9.

1⑦

1.5

④3.

1⑥

4.1

①2.

5④

2.7

3.1

◎9.

5①

10.4

②4.

3⑥

6.5

②9.

5⑥

6.4

①2.

5②

1.4

⑤13

.1⑦

9.5

①1.

3②

9.6

③3.

1⑤

1.1

◎ 3.6

① 5.7

② 9.8

③ 11.5

④ 12.4

⑤ 23.1

⑥ 51.3

# N

on-Z

ero

Off-

Dia

g.2 Index[1]= 2

4 Index[2]= 6

2 Index[3]= 8

3 Index[4]= 11

4 Index[5]= 15

2 Index[6]= 17

4 Index[7]= 21

4 Index[8]= 25

Index[0]= 0

(Index[i])

th~(Index[

i+1])

th:

Non

-Zer

o O

ff-D

iag.

Com

pone

nts

corr

espo

ndin

g to

i-th

row.

Page 92: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D92

Pro

gram

: 1d.

c (4

/6)

Glo

bal M

atrix

: Col

umn

ID fo

r Non

-Zer

o O

ff-D

iag’

s/*

// +--------------+

// | CONNECTIVITY |

// +--------------+

*/for(i=0;i<N+1;i++) Index[i] = 2;

Index[0]= 0;

Index[1]= 1;

Index[N]= 1;

for(i=0;i<N;i++){

Index[i+1]= Index[i+1] + Index[i];

}

NPLU= Index[N];

for(i=0;i<N;i++){

jS = Index[i];

if(i == 0){ Item[jS] = i+1;

}else if(i == N-1){

Item[jS] = i-1;

}else{

Item[jS] = i-1;

Item[jS+1] = i+1;

}}

ii-1

i+1

i-1i

0 1 2 3 4 5 6 7

2.4

①3.

2④

4.3

◎2.

5③

3.7

⑤9.

1⑦

1.5

④3.

1⑥

4.1

①2.

5④

2.7

3.1

◎9.

5①

10.4

②4.

3⑥

6.5

②9.

5⑥

6.4

①2.

5②

1.4

⑤13

.1⑦

9.5

①1.

3②

9.6

③3.

1⑤

1.1

◎ 3.6

① 5.7

② 9.8

③ 11.5

④ 12.4

⑤ 23.1

⑥ 51.3

# N

on-Z

ero

Off-

Dia

g.2 Index[1]= 2

4 Index[2]= 6

2 Index[3]= 8

3 Index[4]= 11

4 Index[5]= 15

2 Index[6]= 17

4 Index[7]= 21

4 Index[8]= 25

Index[0]= 0

(Index[i])

th~(Index[

i+1])

th:

Non

-Zer

o O

ff-D

iag.

Com

pone

nts

corr

espo

ndin

g to

i-th

row.

Page 93: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D93

Pro

gram

: 1d.

c (5

/6)

Ele

men

t Mat

rix ~

Glo

bal M

atrix

/*

// +-----------------+

// | MATRIX assemble |

// +-----------------+

*/

for(icel=0;icel<NE;icel++){

in1= Icelnod[2*icel];

in2= Icelnod[2*icel+1];

X1 = X[in1];

X2 = X[in2];

DL = fabs(X2-X1);

Ck= Area*COND/DL;

Emat[0][0]= Ck*Kmat[0][0];

Emat[0][1]= Ck*Kmat[0][1];

Emat[1][0]= Ck*Kmat[1][0];

Emat[1][1]= Ck*Kmat[1][1];

Diag[in1]= Diag[in1] + Emat[0][0];

Diag[in2]= Diag[in2] + Emat[1][1];

if (icel==0){k1=Index[in1];

}else {k1=Index[in1]+1;}

k2=Index[in2];

AMat[k1]= AMat[k1] + Emat[0][1];

AMat[k2]= AMat[k2] + Emat[1][0];

QN= 0.5*QV*Area*dX;

Rhs[in1]= Rhs[in1] + QN;

Rhs[in2]= Rhs[in2] + QN;

}

in2

in1

icel

Page 94: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D94

Pro

gram

: 1d.

c (5

/6)

Ele

men

t Mat

rix ~

Glo

bal M

atrix

/*

// +-----------------+

// | MATRIX assemble |

// +-----------------+

*/

for(icel=0;icel<NE;icel++){

in1= Icelnod[2*icel];

in2= Icelnod[2*icel+1];

X1 = X[in1];

X2 = X[in2];

DL = fabs(X2-X1);

Ck= Area*COND/DL;

Emat[0][0]= Ck*Kmat[0][0];

Emat[0][1]= Ck*Kmat[0][1];

Emat[1][0]= Ck*Kmat[1][0];

Emat[1][1]= Ck*Kmat[1][1];

Diag[in1]= Diag[in1] + Emat[0][0];

Diag[in2]= Diag[in2] + Emat[1][1];

if (icel==0){k1=Index[in1];

}else {k1=Index[in1]+1;}

k2=Index[in2];

AMat[k1]= AMat[k1] + Emat[0][1];

AMat[k2]= AMat[k2] + Emat[1][0];

QN= 0.5*QV*Area*dX;

Rhs[in1]= Rhs[in1] + QN;

Rhs[in2]= Rhs[in2] + QN;

}

in2

in1

icel

K

mat

LALA

kEm

ate

1

11

1)

(

Page 95: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D95

Pro

gram

: 1d.

c (5

/6)

Ele

men

t Mat

rix ~

Glo

bal M

atrix

/*

// +-----------------+

// | MATRIX assemble |

// +-----------------+

*/

for(icel=0;icel<NE;icel++){

in1= Icelnod[2*icel];

in2= Icelnod[2*icel+1];

X1 = X[in1];

X2 = X[in2];

DL = fabs(X2-X1);

Ck= Area*COND/DL;

Emat[0][0]= Ck*Kmat[0][0];

Emat[0][1]= Ck*Kmat[0][1];

Emat[1][0]= Ck*Kmat[1][0];

Emat[1][1]= Ck*Kmat[1][1];

Diag[in1]= Diag[in1] + Emat[0][0];

Diag[in2]= Diag[in2] + Emat[1][1];

if (icel==0){k1=Index[in1];

}else {k1=Index[in1]+1;}

k2=Index[in2];

AMat[k1]= AMat[k1] + Emat[0][1];

AMat[k2]= AMat[k2] + Emat[1][0];

QN= 0.5*QV*Area*dX;

Rhs[in1]= Rhs[in1] + QN;

Rhs[in2]= Rhs[in2] + QN;

}

in2

in1

icel

1

11

1)

(

LEAk

Emat

e

Page 96: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D96

Pro

gram

: 1d.

c (5

/6)

Ele

men

t Mat

rix ~

Glo

bal M

atrix

/*

// +-----------------+

// | MATRIX assemble |

// +-----------------+

*/

for(icel=0;icel<NE;icel++){

in1= Icelnod[2*icel];

in2= Icelnod[2*icel+1];

X1 = X[in1];

X2 = X[in2];

DL = fabs(X2-X1);

Ck= Area*COND/DL;

Emat[0][0]= Ck*Kmat[0][0];

Emat[0][1]= Ck*Kmat[0][1];

Emat[1][0]= Ck*Kmat[1][0];

Emat[1][1]= Ck*Kmat[1][1];

Diag[in1]= Diag[in1] + Emat[0][0];

Diag[in2]= Diag[in2] + Emat[1][1];

if (icel==0){k1=Index[in1];

}else {k1=Index[in1]+1;}

k2=Index[in2];

AMat[k1]= AMat[k1] + Emat[0][1];

AMat[k2]= AMat[k2] + Emat[1][0];

QN= 0.5*QV*Area*dX;

Rhs[in1]= Rhs[in1] + QN;

Rhs[in2]= Rhs[in2] + QN;

}

in2

in1

icel

1

11

1)

(

LAk

Emat

e

k1

k2

ii-1

i+1

Inde

x[i]

Inde

x[i]+

1

Non

-zer

o O

ff-D

iag.

at i

-thro

w:

Index[i], Index[i]+1

Page 97: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

/*

// +-----------------+

// | MATRIX assemble |

// +-----------------+

*/

for(icel=0;icel<NE;icel++){

in1= Icelnod[2*icel];

in2= Icelnod[2*icel+1];

X1 = X[in1];

X2 = X[in2];

DL = fabs(X2-X1);

Ck= Area*COND/DL;

Emat[0][0]= Ck*Kmat[0][0];

Emat[0][1]= Ck*Kmat[0][1];

Emat[1][0]= Ck*Kmat[1][0];

Emat[1][1]= Ck*Kmat[1][1];

Diag[in1]= Diag[in1] + Emat[0][0];

Diag[in2]= Diag[in2] + Emat[1][1];

if (icel==0){k1=Index[in1];

}else {k1=Index[in1]+1;}

k2=Index[in2];

AMat[k1]= AMat[k1] + Emat[0][1];

AMat[k2]= AMat[k2] + Emat[1][0];

QN= 0.5*QV*Area*dX;

Rhs[in1]= Rhs[in1] + QN;

Rhs[in2]= Rhs[in2] + QN;

}

Gen

eral

Ele

men

ts: k

1 “in

2” a

s a

off-d

iag.

com

pone

nt o

f “in

1”

in2

in1

icel

1

11

1)

(

LAk

Emat

e

k1

FEM

1D97

in1

i-1in

2In

dex[

i]In

dex[

in1]

+1

Non

-zer

o O

ff-D

iag.

at i

-thro

w:

Index[i], Index[i]+1

Page 98: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

/*

// +-----------------+

// | MATRIX assemble |

// +-----------------+

*/

for(icel=0;icel<NE;icel++){

in1= Icelnod[2*icel];

in2= Icelnod[2*icel+1];

X1 = X[in1];

X2 = X[in2];

DL = fabs(X2-X1);

Ck= Area*COND/DL;

Emat[0][0]= Ck*Kmat[0][0];

Emat[0][1]= Ck*Kmat[0][1];

Emat[1][0]= Ck*Kmat[1][0];

Emat[1][1]= Ck*Kmat[1][1];

Diag[in1]= Diag[in1] + Emat[0][0];

Diag[in2]= Diag[in2] + Emat[1][1];

if (icel==0){k1=Index[in1];

}else {k1=Index[in1]+1;}

k2=Index[in2];

AMat[k1]= AMat[k1] + Emat[0][1];

AMat[k2]= AMat[k2] + Emat[1][0];

QN= 0.5*QV*Area*dX;

Rhs[in1]= Rhs[in1] + QN;

Rhs[in2]= Rhs[in2] + QN;

}

Gen

eral

Ele

men

ts: k

2 “in

1” a

s a

off-d

iag.

com

pone

nt o

f “in

2”

in2

in1

icel

1

11

1)

(

LAk

Emat

e

k2

FEM

1D98

in2

in1

i+1

Inde

x[in

2]In

dex[

i]+1

Non

-zer

o O

ff-D

iag.

at i

-thro

w:

Index[i], Index[i]+1

Page 99: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

/*

// +-----------------+

// | MATRIX assemble |

// +-----------------+

*/

for(icel=0;icel<NE;icel++){

in1= Icelnod[2*icel];

in2= Icelnod[2*icel+1];

X1 = X[in1];

X2 = X[in2];

DL = fabs(X2-X1);

Ck= Area*COND/DL;

Emat[0][0]= Ck*Kmat[0][0];

Emat[0][1]= Ck*Kmat[0][1];

Emat[1][0]= Ck*Kmat[1][0];

Emat[1][1]= Ck*Kmat[1][1];

Diag[in1]= Diag[in1] + Emat[0][0];

Diag[in2]= Diag[in2] + Emat[1][1];

if (icel==0){k1=Index[in1];

}else {k1=Index[in1]+1;}

k2=Index[in2];

AMat[k1]= AMat[k1] + Emat[0][1];

AMat[k2]= AMat[k2] + Emat[1][0];

QN= 0.5*QV*Area*dX;

Rhs[in1]= Rhs[in1] + QN;

Rhs[in2]= Rhs[in2] + QN;

}

0-th

Ele

men

t: k1

“in

2” a

s a

off-d

iag.

com

pone

nt o

f “in

1”

in2

in1

icel=

0

1

11

1)

(

LAk

Emat

e

k1

FEM

1D99

in1

in2

Inde

x[in

1]

Non

-zer

o O

ff-D

iag.

at i

-thro

w:Index[i]

Page 100: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D10

0

Pro

gram

: 1d.

c (5

/6)

RH

S: H

eat G

ener

atio

n Te

rm/*

// +-----------------+

// | MATRIX assemble |

// +-----------------+

*/

for(icel=0;icel<NE;icel++){

in1= Icelnod[2*icel];

in2= Icelnod[2*icel+1];

X1 = X[in1];

X2 = X[in2];

DL = fabs(X2-X1);

Ck= Area*COND/DL;

Emat[0][0]= Ck*Kmat[0][0];

Emat[0][1]= Ck*Kmat[0][1];

Emat[1][0]= Ck*Kmat[1][0];

Emat[1][1]= Ck*Kmat[1][1];

Diag[in1]= Diag[in1] + Emat[0][0];

Diag[in2]= Diag[in2] + Emat[1][1];

if (icel==0){k1=Index[in1];

}else {k1=Index[in1]+1;}

k2=Index[in2];

AMat[k1]= AMat[k1] + Emat[0][1];

AMat[k2]= AMat[k2] + Emat[1][0];

QN= 0.5*QV*Area*dX;

Rhs[in1]= Rhs[in1] + QN;

Rhs[in2]= Rhs[in2] + QN;

}

in2

in1

icel

11

2//

1

0

ALQ

dxL

xL

xA

QdV

NQ

L

V

T

Page 101: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D10

1

Pro

gram

: 1d.

c (6

/6)

Diri

chle

t B.C

. @ X

=0/*

// +---------------------+

// | BOUNDARY conditions |

// +---------------------+

*/

/* X=Xmin */

i=0;

jS= Index[i];

AMat[jS]= 0.0;

Diag[i ]= 1.0;

Rhs [i ]= 0.0;

for(k=0;k<NPLU;k++){

if(Item[k]==0){AMat[k]=0.0;

}}

Page 102: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

•U

nifo

rm: S

ectio

nal A

rea:

A, T

herm

al C

ondu

ctiv

ity:

•H

eat G

ener

atio

n R

ate/

Volu

me/

Tim

e [Q

L-3 T

-1]

•B

ound

ary

Con

ditio

ns–

x=0

T= 0

(F

ixed

Tem

pera

ture

)

–x=

x max:

(Ins

ulat

ed)

FEM

1D10

2

1D S

tead

y S

tate

Hea

t Con

duct

ion

x=0

(xm

in)

x= x

max

Q

0

xTheat

gen

erat

ion/

volu

me

0

QxT

x

Q

Page 103: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

•U

nifo

rm: S

ectio

nal A

rea:

A, T

herm

al C

ondu

ctiv

ity:

•H

eat G

ener

atio

n R

ate/

Volu

me/

Tim

e [Q

L-3 T

-1]

•B

ound

ary

Con

ditio

ns–

x=0

T= 0

(F

ixed

Tem

pera

ture

)

–x=

x max:

(Ins

ulat

ed)

FEM

1D10

3

(Lin

ear)

Equ

atio

n at

x=0

T 1=

0 (o

r T0=

0)

x=0

(xm

in)

x= x

max

Q

0

xTheat

gen

erat

ion/

volu

me

0

QxT

x

Q

Page 104: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D10

4

/*

// +---------------------+

// | BOUNDARY conditions |

// +---------------------+

*/

/* X=Xmin */

i=0;

jS= Index[i];

AMat[jS]= 0.0;

Diag[i ]= 1.0;

Rhs [i ]= 0.0;

for(k=0;k<NPLU;k++){

if(Item[k]==0){AMat[k]=0.0;

}}

Pro

gram

: 1d.

c (6

/6)

Diri

chle

t B.C

. @ X

=0

T 1=0

Dia

gona

l Com

pone

nt=1

RH

S=0

Off-

Dia

gona

l Com

pone

nts=

0.

Page 105: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D10

5

/*

// +---------------------+

// | BOUNDARY conditions |

// +---------------------+

*/

/* X=Xmin */

i=0;

jS= Index[i];

AMat[jS]= 0.0;

Diag[i ]= 1.0;

Rhs [i ]= 0.0;

for(k=0;k<NPLU;k++){

if(Item[k]==0){AMat[k]=0.0;

}}

Pro

gram

: 1d.

c (6

/6)

Diri

chle

t B.C

. @ X

=0

Eras

e !

T 1=0

Dia

gona

l Com

pone

nt=1

RH

S=0

Off-

Dia

gona

l Com

pone

nts=

0.

Page 106: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

/*

// +---------------------+

// | BOUNDARY conditions |

// +---------------------+

*/

/* X=Xmin */

i=0;

jS= Index[i];

AMat[jS]= 0.0;

Diag[i ]= 1.0;

Rhs [i ]= 0.0;

for(k=0;k<NPLU;k++){

if(Item[k]==0){AMat[k]=0.0;

}}

FEM

1D10

6

Pro

gram

: 1d.

c (6

/6)

Diri

chle

t B.C

. @ X

=0

Elim

inat

ion

and

Eras

e

Col

umn

com

pone

nts

of b

ound

ary

node

s (D

irich

let B

.C.)

are

mov

ed to

RH

S a

nd e

limin

ated

for k

eepi

ng s

ymm

etric

al fe

atur

e of

th

e m

atrix

(in

this

cas

e ju

st e

rase

off-

diag

onal

com

pone

nts)

T 1=0

Dia

gona

l Com

pone

nt=1

RH

S=0

Off-

Dia

gona

l Com

pone

nts=

0.

Page 107: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D10

7

if T 1≠

0/*

// +---------------------+

// | BOUNDARY conditions |

// +---------------------+

*/

/* X=Xmin */

i=0;

jS= Index[i];

AMat[jS]= 0.0;

Diag[i ]= 1.0;

Rhs [i ]= PHImin;

for(j=1;i<N;i++){

for (k=Index[j];k<Index[j+1];k++){

if(Item[k]==0){

Rhs [j]= Rhs[j] –

Amat[k]*PHImin;

AMat[k]= 0.0;

}}

jk

Item

jIn

dex

jIn

dex

kk

jj

Rhs

Amat

Dia

g

]

[

1]1

[

][

Col

umn

com

pone

nts

of b

ound

ary

node

s (D

irich

let B

.C.)

are

mov

ed to

RH

S a

nd

elim

inat

ed fo

r kee

ping

sym

met

rical

feat

ure

of th

e m

atrix

.

Page 108: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

/*

// +---------------------+

// | BOUNDARY conditions |

// +---------------------+

*/

/* X=Xmin */

i=0;

jS= Index[i];

AMat[jS]= 0.0;

Diag[i ]= 1.0;

Rhs [i ]= PHImin;

for(j=1;i<N;i++){

for (k=Index[j];k<Index[j+1];k++){

if(Item[k]==0){

Rhs [j]= Rhs[j] –

Amat[k]*PHImin;

AMat[k]= 0.0;

}}

FEM

1D10

8

if T 1≠

0

0]

[m

in

][

][

1]1

[

],[

sk

j

kIte

mk

j

kIte

m

jIn

dex

kk

jIn

dex

kk

jj

kIte

mw

here

Amat

Rhs

Amat

Rhs

Amat

Dia

g

s

ss

s

Col

umn

com

pone

nts

of b

ound

ary

node

s (D

irich

let B

.C.)

are

mov

ed to

RH

S a

nd

elim

inat

ed fo

r kee

ping

sym

met

rical

feat

ure

of th

e m

atrix

.

Page 109: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D10

9

Sec

onda

ry B

.C. (

Insu

late

d)

0@0

xT

max

@0x

xxT

0

QxT

x

x=0

(xm

in)

x= x

max

QH

eat G

en. R

ate

dxdTq

AqAq

dSN

qL

xS

T

,

10Su

rfac

e Fl

ux

max

@0x

xxT

Acc

ordi

ng to

insu

late

d B

.C.,

is s

atis

fied.

No

cont

ribut

ion

by th

is te

rm.

Insu

late

d B

.C. i

s au

tom

atic

ally

sat

isfie

d w

ithou

t exp

licit

oper

atio

ns->

Nat

ural

B.C

.

0

q

Page 110: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

0

Prec

ondi

tione

d C

G S

olve

r

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

solve [M]z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

N

N

DD

DD

M

0...

00

00

0...

......

00

00

0...

0

1

2

1

Page 111: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

1

Dia

gona

l Sca

ling,

Poi

nt-J

acob

i

N

N

DD

DD

M

0...

00

00

0...

......

00

00

0...

0

1

2

1

•solve [M]z(i-1)= r(i-1)

is v

ery

easy

.•

Pro

vide

s fa

st c

onve

rgen

ce fo

r sim

ple

prob

lem

s.•

1d.f,

1d.

c

Page 112: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

2

CG

Sol

ver (

1/6)

/*

// +---------------+

// | CG iterations |

// +---------------+

*/

R = 0;

Z = 1;

Q = 1;

P = 2;

DD= 3;

for(i=0;i<N;i++){

W[DD][i]= 1.0 / Diag[i];

}

Rec

ipro

cal n

umbe

rs (逆数)of

dia

gona

l co

mpo

nent

s ar

e st

ored

in W[DD][i].

Com

puta

tiona

l cos

t for

div

isio

n is

us

ually

exp

ensi

ve.

Page 113: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

3

CG

Sol

ver (

1/6)

/*

// +---------------+

// | CG iterations |

// +---------------+

*/

R = 0;

Z = 1;

Q = 1;

P = 2;

DD= 3;

for(i=0;i<N;i++){

W[DD][i]= 1.0 / Diag[i];

}

W[0][i]= W[R][i] ⇒ {r}

W[1][i]= W[Z][i] ⇒ {z}

W[1][i]= W[Q][i] ⇒ {q}

W[2][i]= W[P][i] ⇒ {p}

W[3][i]= W[DD][i] ⇒ 1/{D}

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

solve [M]z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

Page 114: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

4

CG

Sol

ver (

2/6)

/*

//--{r0}= {b} -[A]{xini} |

*/

for(i=0;i<N;i++){

W[R][i] = Diag[i]*U[i];

for(j=Index[i];j<Index[i+1];j++){

W[R][i] += AMat[j]*U[Item[j]];

}} BNorm2 = 0.0;

for(i=0;i<N;i++){

BNorm2 += Rhs[i] * Rhs[i];

W[R][i] = Rhs[i] -

W[R][i];

}

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

solve [M]z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

BNRM2=|b|2

for convergence criteria

of CG solvers

Page 115: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

5

CG

Sol

ver (

3/6)

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

solve [M]z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

for(iter=1;iter<=IterMax;iter++){

/*

//--{z}= [Minv]{r}

*/

for(i=0;i<N;i++){

W[Z][i] = W[DD][i] * W[R][i];

}

/*

//--RHO= {r}{z}

*/

Rho= 0.0;

for(i=0;i<N;i++){

Rho += W[R][i] * W[Z][i];

}

Page 116: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

6

CG

Sol

ver (

4/6)

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

solve [M]z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

/*

//--{p} = {z} if ITER=1

// BETA= RHO / RHO1 otherwise

*/if(iter == 1){

for(i=0;i<N;i++){

W[P][i] = W[Z][i];

}}else{

Beta = Rho / Rho1;

for(i=0;i<N;i++){

W[P][i] = W[Z][i] + Beta*W[P][i];

}}

/*

//--{q}= [A]{p}

*/for(i=0;i<N;i++){

W[Q][i] = Diag[i] * W[P][i];

for(j=Index[i];j<Index[i+1];j++){

W[Q][i] += AMat[j]*W[P][Item[j]];

}}

Page 117: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

7

CG

Sol

ver (

5/6)

/*

//--ALPHA= RHO / {p}{q}

*/C1 = 0.0;

for(i=0;i<N;i++){

C1 += W[P][i] * W[Q][i];

} Alpha = Rho / C1;

/*

//--

{x}= {x} + ALPHA*{p}

// {r}= {r} -ALPHA*{q}

*/for(i=0;i<N;i++){

U[i] += Alpha * W[P][i];

W[R][i] -= Alpha * W[Q][i];

}

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

solve [M]z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

Page 118: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

8

CG

Sol

ver (

6/6)

Compute r(0)= b-[A]x(0)

fori= 1, 2, …

solve [M]z(i-1)= r(i-1)

i-1= r(i-1) z(i-1)

ifi=1

p(1)= z(0)

else

i-1= i

-1/ i

-2

p(i)= z(i-1) + i

-1p(i-1)

endif

q(i)= [A]p(i)

i = i

-1/p(i)q(i)

x(i)= x(i-1) + ip(i)

r(i)= r(i-1) -

iq(i)

check convergence |r|

end

DNorm2 = 0.0;

for(i=0;i<N;i++){

DNorm2 += W[R][i] * W[R][i];

} Resid = sqrt(DNorm2/BNorm2);

if((iter)%1000 == 0){

printf("%8d%s%16.6e¥n",iter, "

iters, RESID=", Resid);

} if(Resid <= Eps){ierr = 0;

break;}

Rho1 = R

ho;

i-2

}

Eps

BN

orm

2D

Nor

m2

Res

id

bb

Axbr

Con

trol

Dat

ainput.dat

4NE (

Num

ber o

f Ele

men

ts)

1.0 1.01.01.0

x

(Len

gth

of E

ach

Ele

m.: L

) ,Q, A,

100

Num

ber o

f MA

X. It

erat

ions

for C

G S

olve

r1.e-8

Con

verg

ence

Crit

eria

for C

G S

olve

r

Page 119: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D11

9

Fini

te E

lem

ent P

roce

dure

s•

Initi

aliz

atio

n–

Con

trol D

ata

–N

ode,

Con

nect

ivity

of E

lem

ents

(N: N

ode#

, NE

: Ele

m#)

–In

itial

izat

ion

of A

rrays

(Glo

bal/E

lem

ent M

atric

es)

–E

lem

ent-G

loba

l Mat

rix M

appi

ng (I

ndex

, Ite

m)

•G

ener

atio

n of

Mat

rix–

Ele

men

t-by-

Ele

men

t Ope

ratio

ns (d

o ic

el=

1, N

E)

•E

lem

ent m

atric

es•

Acc

umul

atio

n to

glo

bal m

atrix

–B

ound

ary

Con

ditio

ns•

Line

ar S

olve

r–

Con

juga

te G

radi

ent M

etho

d

Page 120: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D12

0

Rem

edie

s fo

r Hig

her A

ccur

acy

•Fi

ner M

eshe

sNE=8, dX=12.5

8 iters, RESID= 2.822910E-16 U(N)= 1.953586E-01

### DISPLACEMENT

1 0.000000E+00 -0.000000E+00

2 1.101928E-02 1.103160E-02

3 2.348034E-02 2.351048E-02

4 3.781726E-02 3.787457E-02

5 5.469490E-02 5.479659E-02

6 7.520772E-02 7.538926E-02

7 1.013515E-01 1.016991E-01

8 1.373875E-01 1.381746E-01

9 1.953586E-01 1.980421E-01

NE=20, dX=5

20 iters, RESID= 5.707508E-15 U(N)= 1.975734E-01

### DISPLACEMENT

1 0.000000E+00 -0.000000E+00

2 4.259851E-03 4.260561E-03

3 8.719160E-03 8.720685E-03

4 1.339752E-02 1.339999E-02

……

17 1.145876E-01 1.146641E-01

18 1.295689E-01 1.296764E-01

19 1.473466E-01 1.475060E-01

20 1.692046E-01 1.694607E-01

21 1.975734E-01 1.980421E-01

22

11

log

log

AA

xA

EAFu

12

34

51

23

4

Page 121: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D12

1

Rem

edie

s fo

r Hig

her A

ccur

acy

•Fi

ner M

eshe

s•

Hig

her O

rder

Sha

pe/In

terp

olat

ion

Func

tion(

高次

補間

関数

・形

状関

数)

–H

ighe

r-Ord

er E

lem

ent(高次要素)

–Li

near

-Ele

men

t, 1s

t -Ord

er E

lem

ent:

Low

er O

rder(低次要

素)

•Fo

rmul

atio

n w

hich

ass

ures

con

tinui

ty o

f n-th

ord

er

deriv

ativ

es–

Cn

Con

tinui

ty(C

n 連続性)

Page 122: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

FEM

1D12

2

Rem

edie

s fo

r Hig

her A

ccur

acy

•Fi

ner M

eshe

s•

Hig

her O

rder

Sha

pe/In

terp

olat

ion

Func

tion(

高次

補間

関数

・形

状関

数)

–H

ighe

r-Ord

er E

lem

ent(高次要素)

–Li

near

-Ele

men

t, 1s

t -Ord

er E

lem

ent:

Low

er O

rder(低次要

素)

•Fo

rmul

atio

n w

hich

ass

ures

con

tinui

ty o

f n-th

orde

r de

rivat

ives

–C

nC

ontin

uity(C

n 連続性)

•Li

near

Ele

men

ts–

Pie

cew

ise

Line

ar–

C0

Con

tinui

ty•

Onl

y de

pend

ent v

aria

bles

are

con

tinuo

us a

t ele

men

t bou

ndar

y

Page 123: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

123

Exa

mpl

e: 1

D H

eat T

rans

fer (

1/2)

•Te

mp.

The

rmal

Fin

s•

Circ

ular

Sec

tiona

l Are

a,

r=1c

m

•B

ound

ary

Con

ditio

n–

x=0

: F

ixed

Tem

pera

ture

–x=

7.5

: Ins

ulat

ed

•C

onve

ctiv

e H

eat T

rans

fer

on C

ylin

dric

al S

urfa

ce–

q= h

(T-T

0)–

q:H

eat F

lux

•H

eat F

low

/Uni

t Sur

face

A

rea/

sec.

Con

vect

ive

Hea

t Tra

nsfe

r on

Cyl

indr

ical

Sur

face

CT S

150

Kcm

Wh

CT

20

/10.0

,0

Kcm

W

/

00.4

cm50.7

CT S

150

Kcm

Wh

CT

20

/10.0

,0

Kcm

W

/

00.4

cm50.7

銅Cup

per

FEM

1D

Page 124: 1D-FEM in C: Steady State Heat Conductionnkl.cc.u-tokyo.ac.jp/14e/02-FEM/FEM1D-C.pdf · 2014-08-18 · Galerkin Method (1/4) • Governing Equation for 1D Steady State Heat Conduction

124

Exa

mpl

e: 1

D H

eat T

rans

fer (

2/2)

050100

150 0.

002.

004.

006.

008.

00

X

deg-C

### RESULTS (linear interpolation)

ID X FEM. ANALYTICAL

1 0.00000 150.00000 150.00000 ERR(%): 0.00000

2 1.87500 102.62226 103.00165 ERR(%): 0.25292

3 3.75000 73.82803 74.37583 ERR(%): 0.36520

4 5.62500 58.40306 59.01653 ERR(%): 0.40898

5 7.50000 53.55410 54.18409 ERR(%): 0.41999

### RESULTS (linear interpolation)

ID X FEM. ANALYTICAL

1 0.00000 150.00000 150.00000 ERR(%): 0.00000

2 0.93750 123.71561 123.77127 ERR(%): 0.03711

3 1.87500 102.90805 103.00165 ERR(%): 0.06240

4 2.81250 86.65618 86.77507 ERR(%): 0.07926

5 3.75000 74.24055 74.37583 ERR(%): 0.09019

6 4.68750 65.11151 65.25705 ERR(%): 0.09703

7 5.62500 58.86492 59.01653 ERR(%): 0.10107

8 6.56250 55.22426 55.37903 ERR(%): 0.10317

9 7.50000 54.02836 54.18409 ERR(%): 0.10382

### RESULTS (quadratic interpolation)

ID X FEM. ANALYTICAL

1 0.00000 150.00000 150.00000 ERR(%): 0.00000

2 1.87500 102.98743 103.00165 ERR(%): 0.00948

3 3.75000 74.40203 74.37583 ERR(%): 0.01747

4 5.62500 59.02737 59.01653 ERR(%): 0.00722

5 7.50000 54.21426 54.18409 ERR(%): 0.02011

Qua

drat

ic in

terp

olat

ion

prov

ides

m

ore

accu

rate

sol

utio

n,

espe

cial

ly if

X is

clo

se to

7.5

0cm

.

0

max

0

cosh

cosh

)(

T

AhPX

xAhP

TT

xT

S

Exac

t Sol

utio

n

FEM

1D