File: sol_multi_tau.C

package info (click to toggle)
lorene 0.0.0~cvs20161116%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 26,444 kB
  • ctags: 13,953
  • sloc: cpp: 212,946; fortran: 21,645; makefile: 1,750; sh: 4
file content (248 lines) | stat: -rw-r--r-- 5,827 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "headcpp.h"
#include "math.h"

#include "tbl.h"
#include "matrice.h"
#include "cheby.h"

double f_left (double) {
	return 1. ;
}

double f_right (double) {
	return 0. ;
}

double f_source (double x) {
	if (x<0)
	    return 1. ;
	else 
	   if (x>0) return 0. ;
	   else
	      return 0.5 ;
}

double f_solution (double x) {
	double ee = exp(1.) ;
	static double bmoins = -1./8./(1+ee*ee) - ee*ee/8./(1+ee*ee*ee*ee) ;
	static double bplus = ee*ee*ee*ee/8.*(ee*ee/(1+ee*ee*ee*ee)-1./(1+ee*ee)) ;
	
	if (x<0)
	    return 0.25 -(ee*ee/4.+bmoins*ee*ee*ee*ee)*exp(2*x)
	 		+ bmoins*exp(-2*x) ;
	else
	    return bplus*(exp(-2*x)-exp(2*x)/ee/ee/ee/ee) ;
}

		//*******************
		// First derivative
		//*******************
Tbl ope_der (const Tbl& so) {
	// Verification of size :
	assert (so.get_ndim() == 1) ;
	int size = so.get_dim(0) ;
	
	// The result is set to zero
	Tbl res (size) ;
	res.annule_hard() ;
	
	// the computation
	for (int n=0 ; n<size ; n++)
	     for (int p=n+1 ; p<size ; p++)
		if ((p+n)%2 == 1)
			res.set(n) += p*so(p)*2 ;
	
	// Normalisation of the first coef :
	res.set(0) /= 2. ;
	
	return res ;
}
		//*******************
		// Second derivative
		//*******************
Tbl ope_der_sec (const Tbl& so) {
	// Verification of size :
	assert (so.get_ndim() == 1) ;
	int size = so.get_dim(0) ;
	
	// The result is set to zero
	Tbl res (size) ;
	res.annule_hard() ;
	
	// the computation
	for (int n=0 ; n<size ; n++)
	     for (int p=n+2 ; p<size ; p++)
		if ((p+n)%2 == 0)
			res.set(n) += p*(p*p-n*n)*so(p) ;
	
	// Normalisation of the first coef :
	res.set(0) /= 2. ;
	
	return res ;
}

		//**************************************
		//    The operator (multi domain case)
		//**************************************

Matrice multi_ope (int n) {

	// The result :
	Matrice res(n,n) ;
	res.set_etat_qcq() ;
	
	// Work arrays :
	Tbl so (n) ;
	Tbl dd_so (n) ;
	
	// Column by column :
	for (int col=0 ; col<n ; col++) {
		so.annule_hard() ;
		so.set(col) = 1 ;
		
		// The derivative
		dd_so = ope_der_sec(so) ;
		
		// Put in the matrix :
		for (int line=0 ; line<n ; line++)
		    res.set(line, col) = -4*dd_so(line) + 4*so(line) ;
	}

	return res ;
}

int main () {

	int nr ;
	cout << "Please enter the number of coefficients :" << endl ;
	cin >> nr ;
	
	cout << "The operator matrix is " << endl ;
	Matrice operator_mat (multi_ope(nr)) ;
	cout << operator_mat << endl ;
	
	// Resolution with a tau method :
	Matrice systeme(nr*2, nr*2) ;
	systeme.annule_hard() ;
	
	// Boundary conditions are inforced by additional equations:	
	// Left boundary condition :
	for (int i=0 ; i<nr ; i++)
	    systeme.set(0, i) = (i%2==0) ? 1 : -1 ;
	// Equation in the first domain :
	for (int i=0 ; i<nr-2 ; i++)
	    for (int j=0 ; j<nr ; j++)
	        systeme.set(i+1,j) = operator_mat(i,j) ;
	// Continuity of the solution :
	for (int i=0 ; i<nr ; i++)
	    systeme.set(nr-1, i) = 1 ;
	for (int i=0 ; i<nr ; i++)
	    systeme.set(nr-1, i+nr) = (i%2==0) ? -1 : 1 ;
	    
	// Continuity of the first derivative :
	for (int i=0 ; i<nr ; i++)
	    systeme.set(nr, i) = i*i ;
	for (int i=0 ; i<nr ; i++)
	    systeme.set(nr, i+nr) = (i%2==0) ? i*i : -i*i ;
	
	// Equation in the second domain :
	for (int i=0 ; i<nr-2 ; i++)
	    for (int j=0 ; j<nr ; j++)
	        systeme.set(i+1+nr,j+nr) = operator_mat(i,j) ;
	    
	// Right boundary condition :
	for (int i=0 ; i<nr ; i++)
	    systeme.set(2*nr-1, i+nr) = 1 ;
	    
	systeme.set_lu() ;
	
	cout << "Multi-domain systeme of equations :" << endl ;
	cout << systeme << endl ;
	    
	// Coeficients of the source :
	// left hand side :
	Tbl conf (nr) ;
	conf.set_etat_qcq() ;
	Tbl colocation(coloc_cheb(nr)) ;
	for (int i=0 ; i<nr ; i++)
	    conf.set(i) = f_left(0.5*(colocation(i)-1)) ;
	Tbl coefs_left (coef_cheb(conf)) ;
	for (int i=0 ; i<nr ; i++)
	    conf.set(i) = f_right(0.5*(colocation(i)+1)) ;
	Tbl coefs_right (coef_cheb(conf)) ;
	
	
	cout << "Coefficients of the source, left and right" << endl ;
	cout << coefs_left << endl ;
	cout << coefs_right << endl ;
	
	// Second member of the system :
	Tbl sec_member (2*nr) ;
	sec_member.set_etat_qcq() ;
	// Leftoundary conditions :
	sec_member.set(0) = 0 ;
	// Source on the left:
	for (int i=1 ; i<nr-1 ; i++)
	    sec_member.set(i) = coefs_left(i-1) ;
	// continuity of the function :
	sec_member.set(nr-1) = 0 ;
	// continuity of the first derivative :
	sec_member.set(nr) = 0 ;
	// Source on the right:
	for (int i=1 ; i<nr-1 ; i++)
	    sec_member.set(i+nr) = coefs_right(i-1) ;
	// Right boundary condition :
	sec_member.set(2*nr-1) = 0 ;
	
	cout << "Second member for the multi-domain system" << endl ;
	cout << sec_member << endl ;
	
	// The system is inverted :
	Tbl coef_sol (systeme.inverse(sec_member)) ;
	    
	cout << "Coefficients of the solution : " << endl ;
	cout << coef_sol << endl ;
	
	// Output in a file for plotting purposes :
	char name_out[30] ;
	sprintf(name_out, "plot_multi_tau_%i.dat", nr) ;
	ofstream fiche (name_out) ;
	
	int resolution = 200 ;
	double x=-1 ;
	double step = 2./resolution ;
	
	// We will also compute the maximum difference between the solution and its numerical value :
	double error_max = 0 ;
	
	double xi ;
	int offset ;
	for (int i=0 ;  i<resolution+1 ; i++)  {
	
	    // in which domain ?
	    if (x<0) {
	        offset=0 ;
		xi = 2*x+1 ;
		}
	    else {
	        offset=nr ;
		xi = 2*x-1 ;
	    }
	    
	    // One computes the solution at the current point
	    double val_func = 0 ;
	    for (int j=0 ; j<nr ; j++)
	         val_func += coef_sol(j+offset)*cheby(j, xi) ;
	   fiche << x << " " << val_func << " " << f_solution(x) << endl ;
	   double error = fabs(val_func-f_solution(x)) ;
	   if (error > error_max)
	        error_max = error ;
	   
	   x += step ;
	}
	
	cout << "Error max : " << endl ;
	cout << error_max << endl ;
	
	return 0 ;
}