TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
util_math.c
Go to the documentation of this file.
1 
2 /* MATH2 CONTAINS ALGEBRAIC ROUTINES FOR GEOtop AND OTHER MODELS
3 MATH2 Version 0.9375 KMackenzie
4 
5 file util_math.c
6 
7 Copyright, 2009 Stefano Endrizzi, Emanuele Cordano, Matteo Dall'Amico and Riccardo Rigon
8 
9 This file is part of MATH2.
10  MATH2 is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MATH2 is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 
25 /*----------------------------------------------------------------------------------------------------------*/
26 /*Used to calculate shadow matrix*/
27 /*----------------------------------------------------------------------------------------------------------*/
28 
29 #include "turtle.h"
30 #include "util_math.h"
31 
32 
33 /*----------------------------------------------------------------------------------------------------------*/
34 
35 void tridiag(short a, long r, long c, long nx, DOUBLEVECTOR *diag_inf, DOUBLEVECTOR *diag, DOUBLEVECTOR *diag_sup, DOUBLEVECTOR *b, DOUBLEVECTOR *e)
36 
37 {
38 
39 long j;
40 double bet;
41 DOUBLEVECTOR *gam;
42 
43 gam=new_doublevector(nx);
44 
45 if(diag->co[1]==0.0){
46  printf("type=%d r=%ld c=%ld\n",a,r,c);
47  t_error("Error 1 in tridiag");
48 }
49 
50 bet=diag->co[1];
51 e->co[1]=b->co[1]/bet;
52 
53 //Decomposition and forward substitution
54 for(j=2;j<=nx;j++){
55  gam->co[j]=diag_sup->co[j-1]/bet;
56  bet=diag->co[j]-diag_inf->co[j-1]*gam->co[j];
57  if(bet==0.0){
58  printf("type=%d r=%ld c=%ld\n",a,r,c);
59  printf("l=%ld diag(l)=%f diag_inf(l-1)=%f diag_sup(l-1)=%f\n",j,diag->co[j],diag_inf->co[j-1],diag_sup->co[j-1]);
60  t_error("Error 2 in tridiag");
61  }
62  e->co[j]=(b->co[j]-diag_inf->co[j-1]*e->co[j-1])/bet;
63 }
64 
65 //Backsubstitution
66 for(j=(nx-1);j>=1;j--){
67  e->co[j]-=gam->co[j+1]*e->co[j+1];
68 }
69 
71 
72 }
73 
74 /*----------------------------------------------------------------------------------------------------------*/
75 
76 double norm(DOUBLEVECTOR *V){
77 
78  long l;
79  double N=0.0;
80 
81  for(l=1;l<=V->nh;l++){
82  N+=pow(V->co[l],2.0);
83  }
84 
85  N=pow(N,0.5);
86  return(N);
87 
88 }
89 
90 /*----------------------------------------------------------------------------------------------------------*/
91