Anda di halaman 1dari 15

Advanced Finite Element Methods

Study Work 3
Task 3.1

a)
Java Codes:
Main Class
public class MainNRM {
public static void main(String[] args){
Vector r0 = new Vector(0, 0.1);
Vector u0 = new Vector(0, 0);
double lamda=0;
double deltaLamda = 0.1;
double s=0.1;
double epsilon = 0.0001;
int controlStep = 2;
int maxIteration =500;
NewtonRaphson nrm= new NewtonRaphson(u0, r0, controlStep, maxIteration,
epsilon, lamda, deltaLamda);
nrm.operate();
//ArcControl acm= new ArcControl(u0, r0, controlStep, maxIteration,
epsilon, lamda, s);
//acm.operate();
}
}

Class For Load Controlled NR Method


public class NewtonRaphson {
private double error;
private double lamda;
private int controlStep;
private double deltaLamda;
private double epsilon;
private int maxIteration;
private Vector u0;
private Vector u;
private Vector r0;

public NewtonRaphson(Vector u0, Vector r0, int controlStep, int


maxIteration, double epsilon, double lamda, double deltaLamda){
this.u0=u0;
this.u=new Vector(0,0);
this.u.set(0, u0.get(0));
this.u.set(1, u0.get(1));
this.r0=r0;
this.controlStep=controlStep+1;
this.maxIteration=maxIteration;
this.epsilon=epsilon;
this.lamda=lamda;
this.deltaLamda=deltaLamda;
}
public void operate(){
for(int i=1;i<controlStep;i++){
System.out.println("______________________LOAD CONTROL
METHOD__________________________");
System.out.println("**********************************************************
*********");
//System.out.println("Start");
System.out.println("n = "+i);
System.out.println("______________________________________________________");
int n=-1;// n used for k
error = Double.MAX_VALUE;
lamda+= deltaLamda;
while(error>epsilon && n<maxIteration){
//K step
n++;
System.out.println("
k = "+n);
System.out.println("

u vector = "+u.get(0)+",

"+u.get(1));
InternalForceVector r = new
InternalForceVector(u.get(0),u.get(1));
Vector ri = new Vector(r.r1(),r.r2());
System.out.println("
internal force vector =
"+ri.get(0)+", "+ri.get(1));
Vector temp = (r0.multiply(lamda)).add(-1,ri);
System.out.println("
temp"+temp.get(0)+","+temp.get(1));
KMatrix k = new KMatrix(u.get(0), u.get(1));
Matrix tmat = k.getKmatrix();
tmat.print("K matrix");
Matrix kInverse = tmat.inverse2X2();
Vector deltaU = kInverse.multiply(1,temp);
deltaU.print("deltaU");
Vector newU = u.add(1,deltaU);
newU.print("newU");

Vector differenceU=newU.add(-1,this.u0);
error=deltaU.norm()/differenceU.norm();
System.out.println("
Error = " + error);
u.set(0, newU.get(0));
u.set(1, newU.get(1));
System.out.println("___________________________________________________");
}
if(error>epsilon){
System.out.println("Max. no. of iteration completed");
}else{
System.out.println("Converged");
}
System.out.println("Final u for n = "+ i + " and k = "+ n +" is
"+u.get(0)+", "+u.get(1));
this.u0.set(0,u.get(0));
this.u0.set(1,u.get(1));
}
System.out.println("***************************************************");
}
}

Vector Class
import java.util.Arrays;
public class Vector {
private double[] elements;
public Vector(double... x) {
this.elements = x;
}
public Vector(int n) {
this.elements = new double[n];
}
public int getN() {
return this.elements.length;
}
public void fill(double v) {
Arrays.fill(this.elements, v);
}
public void set(int idx, double v) {
if (idx >= 0 && idx < getN()) {
this.elements[idx] = v;
}
}

public double get(int idx) {


if (idx >= 0 && idx < getN()) {
return this.elements[idx];
} else {
return Double.NaN;
}
}
public void print(String l) {
System.out.print(l + " = (");
System.out.printf("%12.5e", get(0));
for (int i = 1; i < this.elements.length; i++) {
System.out.printf(", %.6f", get(i));
}
System.out.println(")^T");
}
public Vector multiply(double alpha) {
int n = this.getN();
Vector r = new Vector(n);
for (int i = 0; i < n; i++) {
r.set(i, alpha * this.get(i));
}
return r;
}
public Vector add(double alpha, Vector y) {
Vector r = new Vector(this.getN());
for (int i = 0; i < this.getN(); i++) {
r.set(i, this.get(i) + alpha * y.get(i));
}
return r;
}
public double scalarProduct(Vector y) {
double s = 0;
for (int i = 0; i < this.getN(); i++) {
s += this.get(i) * y.get(i);
}
return s;
}

public double norm() {


double s = 0;
for (int i = 0; i < this.getN(); i++) {
s += Math.pow(Math.abs(this.get(i)),2);
}
return Math.sqrt(s);
}
}

Matrix Class
public class Matrix {
private double[][] elements;
public Matrix(int m, int n) {
this.elements = new double[m][n];
}
public Matrix(int m, int n, double... values) {
this(m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
this.set(i, j, values[i * n + j]);
}
}
}
public int getM() {
return this.elements.length;
}
public int getN() {
return this.elements[0].length;
}
public void set(int i, int j, double v) {
this.elements[i][j] = v;
}
public void setRow(int idx, double... values) {
for (int i = 0; i < this.getN(); i++) {
this.set(idx, i, values[i]);
}
}
public double get(int i, int j) {
return this.elements[i][j];
}
public void print(String l) {
int m = getM();
System.out.println(l + " = ");
for (int i = 0; i < m; i++) {
System.out.print("|");
for (int j = 0; j < getN(); j++) {
System.out.printf(" %.6f", get(i, j));
}
System.out.println(" |");
}
}
public Matrix transpose() {
int m = this.getM();
int n = this.getN();

Matrix r = new Matrix(n, m);


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
r.set(j, i, this.get(i, j));
}
}
return r;
}
public Matrix add(double alpha, Matrix b) {
int m = this.getM();
int n = this.getN();
Matrix r = new Matrix(m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
r.set(i, j, this.get(i, j) + alpha
* b.get(i, j));
}
}
return r;
}
public Vector multiply(double alpha, Vector x) {
Vector r = new Vector(this.getM());
for (int i = 0; i < this.getM(); i++) {
double s = 0;
for (int j = 0; j < this.getN(); j++) {
s += this.get(i, j) * x.get(j);
}
r.set(i, alpha * s);
}
return r;
}
public Matrix multiply(double alpha, Matrix b) {
Matrix r = new Matrix(this.getM(), b.getN());
for (int i = 0; i < this.getM(); i++) {
for (int j = 0; j < b.getN(); j++) {
double s = 0;
for (int k = 0; k < this.getN(); k++) {
s += this.get(i, k) * b.get(k, j);
}
r.set(i, j, alpha * s);
}
}
return r;
}
public Matrix inverse2X2() {
Matrix r = new Matrix(2, 2);

double det=this.elements[0][0]*this.elements[1][1]this.elements[0][1]*this.elements[1][0];
r.set(0,0,this.elements[1][1]/det);
r.set(0,1,this.elements[1][0]/(-det));
r.set(1,0,this.elements[0][1]/(-det));
r.set(1,1,this.elements[0][0]/det);
return r;
}
}

Class For Internal Load Vector


public class InternalForceVector {
private double r1;
private double r2;
public InternalForceVector(double a, double b){
this.r1=1.5*a+Math.pow(a,3)-a*b+a*Math.pow(b,2);
this.r2=-0.5*Math.pow(a,2)+0.5*b+Math.pow(a,2)*b1.5*Math.pow(b,2)+Math.pow(b,3);
}
public double r1(){
return this.r1;
}
public double r2(){
return this.r2;
}
}

Class For Stiffness Matrix


public class KMatrix {
private Matrix k;
public KMatrix(double a,double b){
this.k=new Matrix(2,2);
this.k.set(0,0,
this.k.set(0,1,
this.k.set(1,0,
this.k.set(1,1,

1.5-b+b*b+3*a*a);
-a+2*a*b);
-a+2*a*b);
0.5+a*a-3*b+3*b*b);

}
public Matrix getKmatrix(){
return this.k;
}
}

Results
______________________LOAD CONTROL METHOD__________________________
*******************************************************************
n = 1
___________________________________________________________________
k = 0
u vector = 0.0, 0.0
internal force vector = 0.0, 0.0
temp0.0,0.010000000000000002
K matrix =
| 1.500000 0.000000 |
| 0.000000 0.500000 |
deltaU = ( 0.00000e+00, 0.020000)^T
newU = ( 0.00000e+00, 0.020000)^T
Error = 1.0
_______________________________________________________________
k = 1
u vector = 0.0, 0.020000000000000004
internal force vector = 0.0, 0.009408000000000001
temp0.0,5.920000000000005E-4
K matrix =
| 1.480400 0.000000 |
| 0.000000 0.441200 |
deltaU = ( 0.00000e+00, 0.001342)^T
newU = ( 0.00000e+00, 0.021342)^T
Error = 0.06287170773152086
_______________________________________________________________
k = 2
u vector = 0.0, 0.02134179510426111
internal force vector = 0.0, 0.00999740981948021
temp0.0,2.590180519792054E-6
K matrix =
| 1.479114 0.000000 |
| 0.000000 0.437341 |
deltaU = ( 0.00000e+00, 0.000006)^T
newU = ( 0.00000e+00, 0.021348)^T
Error = 2.7743315296950577E-4
_______________________________________________________________
k = 3
u vector = 0.0, 0.02134771766888269
internal force vector = 0.0, 0.009999999949630856
temp0.0,5.036914635381251E-11
K matrix =
| 1.479108 0.000000 |
| 0.000000 0.437324 |
deltaU = ( 0.00000e+00, 0.000000)^T
newU = ( 0.00000e+00, 0.021348)^T
Error = 5.395228019081785E-9
_______________________________________________________________

Converged
Final u for n = 1 and k = 3 is 0.0, 0.021347717784058497
______________________LOAD CONTROL METHOD__________________________
*******************************************************************
n = 2

___________________________________________________________________
k = 0
u vector = 0.0, 0.021347717784058497
internal force vector = 0.0, 0.010000000000000004
temp0.0,0.01
K matrix =
| 1.479108 0.000000 |
| 0.000000 0.437324 |
deltaU = ( 0.00000e+00, 0.022866)^T
newU = ( 0.00000e+00, 0.044214)^T
Error = 1.0
_______________________________________________________________
k = 1
u vector = 0.0, 0.04421405830332751
internal force vector = 0.0, 0.019261138032989126
temp0.0,7.38861967010878E-4
K matrix =
| 1.457741 0.000000 |
| 0.000000 0.373222 |
deltaU = ( 0.00000e+00, 0.001980)^T
newU = ( 0.00000e+00, 0.046194)^T
Error = 0.0796780400806182
_______________________________________________________________
k = 2
u vector = 0.0, 0.046193740714199225
internal force vector = 0.0, 0.019994648888563574
temp0.0,5.351111436430189E-6
K matrix =
| 1.455940 0.000000 |
| 0.000000 0.367820 |
deltaU = ( 0.00000e+00, 0.000015)^T
newU = ( 0.00000e+00, 0.046208)^T
Error = 5.851903093431454E-4
_______________________________________________________________
k = 3
u vector = 0.0, 0.04620828887948901
internal force vector = 0.0, 0.01999999971186001
temp0.0,2.8813999450316707E-10
K matrix =
| 1.455927 0.000000 |
| 0.000000 0.367781 |
deltaU = ( 0.00000e+00, 0.000000)^T
newU = ( 0.00000e+00, 0.046208)^T
Error = 3.151399293281078E-8
_______________________________________________________________

Converged
Final u for n = 2 and k = 3 is 0.0, 0.04620828966294489
******************************************************************
*****************************************

b)
Java Codes:
Vector Class, Matrix Class, Class for internal force vector and
stiffness matrix are same as for load controlled NRM
Main Class
public class MainNRM {
public static void main(String[] args){
Vector r0 = new Vector(0, 0.1);
Vector u0 = new Vector(0, 0);
double lamda=0;
double deltaLamda = 0.1;
double s=0.1;
double epsilon = 0.0001;
int controlStep = 2;
int maxIteration =500;
//NewtonRaphson nrm= new NewtonRaphson(u0, r0, controlStep,
maxIteration, epsilon, lamda, deltaLamda);
//nrm.operate();
ArcControl acm= new ArcControl(u0, r0, controlStep, maxIteration,
epsilon, lamda, s);
acm.operate();
}
}

Class for Arc length controlled NRM


public class ArcControl {
private double error;
private double lamda;
private int controlStep;
private double s;
private double epsilon;
private int maxIteration;
private Vector u0;
private Vector u;
private Vector r0;
public ArcControl(Vector u0, Vector r0, int controlStep, int maxIteration,
double epsilon, double lamda, double s){
this.u0=u0;
this.u=new Vector(0,0);

this.u.set(0, u0.get(0));
this.u.set(1, u0.get(1));
this.r0=r0;
this.controlStep=controlStep+1;
this.maxIteration=maxIteration;
this.epsilon=epsilon;
this.lamda=lamda;
this.s=s;
}
public void operate(){
Vector newU;
//double lamda0=lamda;
//Vector A=new Vector(0,0);
//double B=0;
Vector C=new Vector(0,0);
for(int n=1;n<controlStep;n++){
System.out.println("______________________ARC CONTROL
METHOD__________________________");
System.out.println("**********************************************************
********");
System.out.println("

n = "+n);

System.out.println("__________________________________________________________
____");
int k = -1;
error = Double.MAX_VALUE;
while(error>epsilon && k<maxIteration){
//K step
k++;
System.out.println("
k = "+k);
System.out.println("

u vector = "+u.get(0)+",

"+u.get(1));
InternalForceVector r = new
InternalForceVector(u.get(0),u.get(1));
Vector ri = new Vector(r.r1(),r.r2());
System.out.println("
internal force vector =
"+ri.get(0)+", "+ri.get(1));
KMatrix kmat = new KMatrix(u.get(0), u.get(1));
Matrix tmat = kmat.getKmatrix();
tmat.print("K matrix");
Matrix kInverse = tmat.inverse2X2();
Vector delULamda =kInverse.multiply(1, r0);
delULamda.print("DelULamda vector");
double deltaLamda;
Vector delU;

if(k==0){

//System.out.println("Predictor Step ");


double
s0=Math.sqrt(delULamda.norm()*delULamda.norm()+1);
System.out.println("s0 = "+ s0);
delU=delULamda.multiply(s/s0);
delU.print("delU Vector");
deltaLamda=s/s0;
}else{
//System.out.println("Corrector Step ");
Vector deltaUr=kInverse.multiply(1,
(r0.multiply(lamda)).add(-1,ri));
deltaUr.print("deltaUr");
//f=0; ie. constraint condition
if(k==1){
C.set(0, delULamda.get(0));
C.set(1, delULamda.get(1));
}
//for general sphere
//deltaLamda = -(u.add(1,u0)).scalarProduct(deltaUr)/((u.add(-1,u0)).scalarProduct(delULamda)+(lamdalamda0));
//initial plane from question
deltaLamda = (C.scalarProduct(deltaUr))/(C.scalarProduct(delULamda)+1);
//initial plane from exercise
//deltaLamda = (A.scalarProduct(deltaUr))/(A.scalarProduct(delULamda)+B);
delU = deltaUr.add(1,
delULamda.multiply(deltaLamda));
}
System.out.println("delta lamda = "+deltaLamda);
newU=u.add(1,delU);
newU.print("newU");
lamda += deltaLamda;
System.out.println("new lamda = "+lamda);
u.set(0, newU.get(0));
u.set(1, newU.get(1));
Vector differenceU=newU.add(-1,this.u0);

error=delU.norm()/differenceU.norm();
System.out.println("

Error = " + error);

System.out.println("_________________________________________________________"
);
}

if(error>epsilon){
System.out.println("Max. no. of iteration completed");
}else{
System.out.println("Converged");
}

this.u0.set(0,u.get(0));
this.u0.set(1,u.get(1));
//lamda0=lamda;
System.out.println("Final u for n = "+ n + " and k = "+ k +" is
"+u.get(0)+", "+u.get(1));
System.out.println("*********************************************************"
);
}
}
}

Results
______________________ARC CONTROL METHOD__________________________
******************************************************************
n = 1
______________________________________________________________
k = 0
u vector = 0.0, 0.0
internal force vector = 0.0, 0.0
K matrix =
| 1.500000 0.000000 |
| 0.000000 0.500000 |
DelULamda vector = ( 0.00000e+00, 0.200000)^T
s0 = 1.019803902718557
delU Vector = ( 0.00000e+00, 0.019612)^T
delta lamda = 0.09805806756909201
newU = ( 0.00000e+00, 0.019612)^T
new lamda = 0.09805806756909201
Error = 1.0
_________________________________________________________
k = 1
u vector = 0.0, 0.019611613513818404
internal force vector = 0.0, 0.00923642660826067
K matrix =
| 1.480773 0.000000 |
| 0.000000 0.442319 |

DelULamda vector = ( 0.00000e+00, 0.226081)^T


deltaUr = ( 0.00000e+00, 0.001287)^T
delta lamda = -2.768737851586462E-4
newU = ( 0.00000e+00, 0.020836)^T
new lamda = 0.09778119378393337
Error = 0.05877562783397699
_________________________________________________________
k = 2
u vector = 0.0, 0.020836278887133516
internal force vector = 0.0, 0.009775959748042323
K matrix =
| 1.479598 0.000000 |
| 0.000000 0.438794 |
DelULamda vector = ( 0.00000e+00, 0.227898)^T
deltaUr = ( 0.00000e+00, 0.000005)^T
delta lamda = -1.058192309782656E-6
newU = ( 0.00000e+00, 0.020841)^T
new lamda = 0.09778013559162359
Error = 2.2458590297621248E-4
_________________________________________________________
k = 3
u vector = 0.0, 0.020840959472835614
internal force vector = 0.0, 0.009778013527670075
K matrix =
| 1.479593 0.000000 |
| 0.000000 0.438780 |
DelULamda vector = ( 0.00000e+00, 0.227905)^T
deltaUr = ( 0.00000e+00, 0.000000)^T
delta lamda = -1.543128210065057E-11
newU = ( 0.00000e+00, 0.020841)^T
new lamda = 0.0977801355761923
Error = 3.2750648263706105E-9
_________________________________________________________

Converged
Final u for n = 1 and k = 3 is 0.0, 0.02084095954109111
*********************************************************
______________________ARC CONTROL METHOD__________________________
******************************************************************
n = 2
______________________________________________________________
k = 0
u vector = 0.0, 0.02084095954109111
internal force vector = 0.0, 0.00977801355761923
K matrix =
| 1.479593 0.000000 |
| 0.000000 0.438780 |
DelULamda vector = ( 0.00000e+00, 0.227905)^T
s0 = 1.0256415015149642
delU Vector = ( 0.00000e+00, 0.022221)^T
delta lamda = 0.0974999547622547
newU = ( 0.00000e+00, 0.043062)^T
new lamda = 0.195280090338447
Error = 1.0
_________________________________________________________
k = 1
u vector = 0.0, 0.0430616440795631

internal force vector = 0.0, 0.018829213683671714


K matrix =
| 1.458793 0.000000 |
| 0.000000 0.376378 |
DelULamda vector = ( 0.00000e+00, 0.265690)^T
deltaUr = ( 0.00000e+00, 0.001857)^T
delta lamda = -4.6076326656166423E-4
newU = ( 0.00000e+00, 0.044796)^T
new lamda = 0.19481932707188535
Error = 0.07239486610675033
_________________________________________________________
k = 2
u vector = 0.0, 0.044795855570190904
internal force vector = 0.0, 0.01947781521089656
K matrix =
| 1.457211 0.000000 |
| 0.000000 0.371632 |
DelULamda vector = ( 0.00000e+00, 0.269083)^T
deltaUr = ( 0.00000e+00, 0.000011)^T
delta lamda = -2.74730035081708E-6
newU = ( 0.00000e+00, 0.044806)^T
new lamda = 0.19481657977153452
Error = 4.3146804576250393E-4
_________________________________________________________
k = 3
u vector = 0.0, 0.04480619580384743
internal force vector = 0.0, 0.01948165783114269
K matrix =
| 1.457201 0.000000 |
| 0.000000 0.371604 |
DelULamda vector = ( 0.00000e+00, 0.269104)^T
deltaUr = ( 0.00000e+00, 0.000000)^T
delta lamda = -9.742908103097177E-11
newU = ( 0.00000e+00, 0.044806)^T
new lamda = 0.19481657967410543
Error = 1.5301397438083002E-8
_________________________________________________________

Converged
Final u for n = 2 and k = 3 is 0.0, 0.04480619617054905
*********************************************************

Anda mungkin juga menyukai