/[escript]/trunk/paso/src/FCT_Solver.cpp
ViewVC logotype

Contents of /trunk/paso/src/FCT_Solver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3822 - (show annotations)
Mon Feb 13 06:22:06 2012 UTC (8 years, 3 months ago) by gross
Original Path: trunk/paso/src/FCT_Solver.c
File MIME type: text/plain
File size: 30294 byte(s)
Transport solver supports now constraints (except the linear CN)
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 /**************************************************************/
16
17 /* Paso: Transport solver with flux correction (L is row sum zero)
18 *
19 * - Mv_t=Lv v(0)=u
20 *
21 * to return v(dt)
22 *
23 */
24 /**************************************************************/
25
26 /* Author: l.gross@uq.edu.au */
27
28 /**************************************************************/
29
30 #include "FCT_Solver.h"
31 #include "Preconditioner.h"
32 #include "PasoUtil.h"
33
34
35 Paso_FCT_Solver* Paso_FCT_Solver_alloc(Paso_TransportProblem *fctp, Paso_Options* options)
36 {
37 Paso_FCT_Solver* out=NULL;
38 const dim_t blockSize=Paso_TransportProblem_getBlockSize(fctp);
39 const dim_t n = Paso_TransportProblem_getTotalNumRows(fctp);
40
41 out=MEMALLOC(1,Paso_FCT_Solver);
42 if (! Esys_checkPtr(out)) {
43 out->transportproblem = Paso_TransportProblem_getReference(fctp);
44 out->mpi_info = Esys_MPIInfo_getReference(fctp->mpi_info);
45 out->flux_limiter = Paso_FCT_FluxLimiter_alloc(fctp);
46 out->b = MEMALLOC(n, double);
47 if ( (options->ode_solver == PASO_CRANK_NICOLSON) || (options->ode_solver == PASO_BACKWARD_EULER) ) {
48 out->du = MEMALLOC(n, double);
49 out->z = MEMALLOC(n, double);
50 } else {
51 out->du = NULL;
52 out->z=NULL;
53 }
54 out->u_coupler = Paso_Coupler_alloc(Paso_TransportProblem_borrowConnector(fctp), blockSize);
55 out->u_old_coupler = Paso_Coupler_alloc(Paso_TransportProblem_borrowConnector(fctp), blockSize);
56 out->omega=0;
57
58 if ( options->ode_solver == PASO_LINEAR_CRANK_NICOLSON ) {
59 out->method = PASO_LINEAR_CRANK_NICOLSON;
60 } else if ( options->ode_solver == PASO_CRANK_NICOLSON ) {
61 out->method = PASO_CRANK_NICOLSON;
62 } else if ( options->ode_solver == PASO_BACKWARD_EULER ) {
63 out->method = PASO_BACKWARD_EULER;
64 } else {
65 Esys_setError(VALUE_ERROR, "Paso_FCT_Solver_alloc: unknown integration scheme.");
66 out->method = UNKNOWN;
67 }
68
69 }
70
71 if (Esys_noError()) {
72 return out;
73 } else {
74 Paso_FCT_Solver_free(out);
75 return NULL;
76 }
77
78 }
79
80 void Paso_FCT_Solver_free(Paso_FCT_Solver *in)
81 {
82 if (in != NULL) {
83 Paso_TransportProblem_free(in->transportproblem);
84 Paso_FCT_FluxLimiter_free(in->flux_limiter);
85 Esys_MPIInfo_free(in->mpi_info);
86 Paso_Coupler_free(in->u_old_coupler);
87 Paso_Coupler_free(in->u_coupler);
88
89 MEMFREE(in->b);
90 MEMFREE(in->z);
91 MEMFREE(in->du);
92 MEMFREE(in);
93
94 }
95 }
96
97 double Paso_FCT_Solver_getSafeTimeStepSize(Paso_TransportProblem* fctp)
98 {
99 dim_t i, n;
100 double dt_max=LARGE_POSITIVE_FLOAT;
101 n=Paso_SystemMatrix_getTotalNumRows(fctp->transport_matrix);
102 /* set low order transport operator */
103 Paso_FCT_setLowOrderOperator(fctp);
104
105 if (Esys_noError()) {
106 /*
107 * calculate time step size:
108 */
109 dt_max=LARGE_POSITIVE_FLOAT;
110 #pragma omp parallel private(i)
111 {
112 double dt_max_loc=LARGE_POSITIVE_FLOAT;
113 #pragma omp for schedule(static)
114 for (i=0;i<n;++i) {
115 const double l_ii=fctp->main_diagonal_low_order_transport_matrix[i];
116 const double m_i=fctp->lumped_mass_matrix[i];
117 if ( m_i > 0 ) {
118 if (l_ii<0) dt_max_loc=MIN(dt_max_loc,m_i/(-l_ii));
119 }
120 }
121 #pragma omp critical
122 {
123 dt_max=MIN(dt_max,dt_max_loc);
124 }
125 }
126 #ifdef ESYS_MPI
127 {
128 dt_max_loc=dt_max;
129 MPI_Allreduce(&dt_max_loc, &dt_max, 1, MPI_DOUBLE, MPI_MIN, fctp->mpi_info->comm);
130 }
131 #endif
132 if (dt_max<LARGE_POSITIVE_FLOAT) dt_max*=2.;
133 }
134 return dt_max;
135 }
136
137 /* modifies the main diagonal of the iteration matrix to introduce new dt */
138 void Paso_FCT_Solver_initialize(const double dt, Paso_FCT_Solver *fct_solver, Paso_Options* options, Paso_Performance* pp)
139 {
140 Paso_TransportProblem* fctp = fct_solver->transportproblem;
141 const index_t* main_iptr=Paso_TransportProblem_borrowMainDiagonalPointer(fctp);
142 const dim_t n=Paso_SystemMatrix_getTotalNumRows(fctp->transport_matrix);
143 const double theta = Paso_FCT_Solver_getTheta(fct_solver);
144 const double omega=1./(dt* theta);
145 dim_t i;
146 Paso_Options options2;
147
148
149
150 Paso_solve_free(fctp->iteration_matrix);
151 /*
152 * fctp->iteration_matrix[i,i]=m[i]/(dt theta) -l[i,i]
153 *
154 */
155 fct_solver->omega=omega;
156 fct_solver->dt = dt;
157 #pragma omp parallel for private(i)
158 for (i = 0; i < n; ++i) {
159 const double m_i=fctp->lumped_mass_matrix[i];
160 const double l_ii = fctp->main_diagonal_low_order_transport_matrix[i];
161 if ( m_i > 0 ) {
162 fctp->iteration_matrix->mainBlock->val[main_iptr[i]] = m_i * omega - l_ii;
163 } else {
164 fctp->iteration_matrix->mainBlock->val[main_iptr[i]] = ABS(m_i * omega - l_ii)/(EPSILON*EPSILON);
165 }
166 }
167
168 /* allocate preconditioner/solver */
169 Paso_Options_setDefaults(&options2);
170 options2.verbose = options->verbose;
171 if (fct_solver->method == PASO_LINEAR_CRANK_NICOLSON ) {
172 options2.preconditioner = PASO_GS;
173 } else {
174 options2.preconditioner = PASO_JACOBI;
175 /* options2.preconditioner = PASO_GS; */
176 }
177 options2.use_local_preconditioner = FALSE;
178 options2.sweeps=-1;
179
180 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
181 Paso_SystemMatrix_setPreconditioner(fctp->iteration_matrix, &options2);
182 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
183 }
184
185 /* entry point for update proceedures */
186 err_t Paso_FCT_Solver_update(Paso_FCT_Solver *fct_solver, double* u, double *u_old, Paso_Options* options, Paso_Performance *pp)
187 {
188 const index_t method=fct_solver->method;
189 err_t err_out = SOLVER_NO_ERROR;
190
191
192 if (method == PASO_LINEAR_CRANK_NICOLSON) {
193 err_out=Paso_FCT_Solver_update_LCN(fct_solver, u, u_old, options, pp);
194
195 } else if (method == PASO_CRANK_NICOLSON) {
196 err_out=Paso_FCT_Solver_updateNL(fct_solver, u, u_old, options, pp);
197
198 } else if (method == PASO_BACKWARD_EULER) {
199 err_out=Paso_FCT_Solver_updateNL(fct_solver, u, u_old, options, pp);
200 } else {
201 err_out = SOLVER_INPUT_ERROR;
202 }
203 return err_out;
204
205 }
206
207 /* linear crank-nicolson update */
208 err_t Paso_FCT_Solver_update_LCN(Paso_FCT_Solver *fct_solver, double * u, double *u_old, Paso_Options* options, Paso_Performance *pp)
209 {
210 double const dt = fct_solver->dt;
211 dim_t sweep_max;
212 double *b = fct_solver->b;
213 double const RTOL = options->tolerance;
214 const dim_t n=Paso_TransportProblem_getTotalNumRows(fct_solver->transportproblem);
215 Paso_SystemMatrix * iteration_matrix = fct_solver->transportproblem->iteration_matrix;
216 err_t errorCode = SOLVER_NO_ERROR;
217
218 Paso_Coupler_startCollect(fct_solver->u_old_coupler,u_old);
219 Paso_Coupler_finishCollect(fct_solver->u_old_coupler);
220
221 /* b[i]=m*u_tilde[i] = m u_old[i] + dt/2 sum_{j <> i} l_{ij}*(u_old[j]-u_old[i])
222 = u_tilde[i] = u_old[i] where constraint m<0.
223 * note that iteration_matrix stores the negative values of the
224 * low order transport matrix l. Therefore a=-dt*0.5 is used. */
225
226 Paso_FCT_Solver_setMuPaLu(b, fct_solver->transportproblem->lumped_mass_matrix,
227 fct_solver->u_old_coupler, -dt*0.5, iteration_matrix);
228 /* solve for u_tilde : u_tilda = m^{-1} * b */
229 Paso_FCT_FluxLimiter_setU_tilda(fct_solver->flux_limiter, b);
230 /* u_tilda_connector is completed */
231
232 /* calculate anti-diffusive fluxes for u_tilda */
233 Paso_FCT_setAntiDiffusionFlux_linearCN(fct_solver->flux_limiter->antidiffusive_fluxes,
234 fct_solver->transportproblem, dt,
235 fct_solver->flux_limiter->u_tilde_coupler,
236 fct_solver->u_old_coupler);
237
238
239 /* b_i += sum_{j} limitation factor_{ij} * antidiffusive_flux_{ij} */
240 Paso_FCT_FluxLimiter_addLimitedFluxes_Start(fct_solver->flux_limiter);
241 Paso_FCT_FluxLimiter_addLimitedFluxes_Complete(fct_solver->flux_limiter, b);
242
243
244 /* solve (m-dt/2*L) u = b in the form (omega*m-L) u = b * omega with omega*dt/2=1 */
245
246 /* initial guess is u<- -u + 2*u_tilde */
247 Paso_Update(n, -1., u, 2., fct_solver->flux_limiter->u_tilde);
248 Paso_Scale(n, b,fct_solver->omega );
249 sweep_max = MAX((int) (- 2 * log(RTOL)/log(2.)-0.5),1);
250
251 if (options->verbose) {
252 const double norm_u_tilde=Paso_lsup(n, fct_solver->flux_limiter->u_tilde, fct_solver->flux_limiter->mpi_info);
253 printf("Paso_FCT_Solver_update_LCN: u_tilda lsup = %e (rtol = %e, max. sweeps = %d)\n",norm_u_tilde,RTOL,sweep_max);
254 }
255 errorCode = Paso_Preconditioner_Smoother_solve_byTolerance( iteration_matrix, ((Paso_Preconditioner*) (iteration_matrix->solver_p))->gs,
256 u, b, RTOL, &sweep_max, TRUE);
257 if (errorCode == PRECONDITIONER_NO_ERROR) {
258 if (options->verbose) printf("Paso_FCT_Solver_update_LCN: convergence after %d Gauss-Seidel steps.\n",sweep_max);
259 errorCode=SOLVER_NO_ERROR;
260 } else {
261 if (options->verbose) printf("Paso_FCT_Solver_update_LCN: Gauss-Seidel failed within %d stesp (rel. tolerance %e).\n",sweep_max,RTOL);
262 errorCode= SOLVER_MAXITER_REACHED;
263 }
264 return errorCode;
265
266 }
267
268 err_t Paso_FCT_Solver_updateNL(Paso_FCT_Solver *fct_solver, double* u, double *u_old, Paso_Options* options, Paso_Performance *pp)
269 {
270 const dim_t num_critical_rates_max=3; /* number of rates >=critical_rate accepted before divergence is triggered */
271 const double critical_rate=0.95; /* expected value of convergence rate */
272
273 double *b = fct_solver->b;
274 double *z = fct_solver->z;
275 double *du = fct_solver->du;
276 double const dt = fct_solver->dt;
277 Paso_TransportProblem* fctp = fct_solver->transportproblem;
278 Paso_FCT_FluxLimiter* flux_limiter = fct_solver->flux_limiter;
279 dim_t i;
280 double norm_u_tilde, ATOL, norm_du=LARGE_POSITIVE_FLOAT, norm_du_old, rate=1.;
281 err_t errorCode=SOLVER_NO_ERROR;
282 const dim_t n=Paso_SystemMatrix_getTotalNumRows(fctp->transport_matrix);
283 const double atol=options->absolute_tolerance;
284 const double rtol=options->tolerance;
285 const dim_t max_m=options->iter_max;
286 dim_t m=0, num_critical_rates=0 ;
287 /* ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */
288
289 bool_t converged=FALSE, max_m_reached=FALSE,diverged=FALSE;
290 options->num_iter=0;
291
292 Paso_Coupler_startCollect(fct_solver->u_old_coupler,u_old);
293 Paso_Coupler_finishCollect(fct_solver->u_old_coupler);
294 /* prepare u_tilda and flux limiter */
295 if ( fct_solver->method == PASO_BACKWARD_EULER ) {
296 /* b[i]=m_i* u_old[i] */
297 #pragma omp for private(i) schedule(static)
298 for (i = 0; i < n; ++i) {
299 if (fctp->lumped_mass_matrix[i] > 0 ) {
300 b[i]=u_old[i]* fctp->lumped_mass_matrix[i];
301 } else {
302 b[i]=u_old[i];
303 }
304 }
305 } else {
306 /* b[i]=m_i* u_old[i] + dt/2 sum_{j <> i} l_{ij}*(u_old[j]-u_old[i]) = m_i * u_tilde_i where m_i>0
307 * = u_old[i] otherwise
308 * note that iteration_matrix stores the negative values of the
309 * low order transport matrix l. Therefore a=-dt*0.5 is used. */
310 Paso_FCT_Solver_setMuPaLu(b,fctp->lumped_mass_matrix,fct_solver->u_old_coupler,-dt*0.5,fctp->iteration_matrix);
311 }
312 Paso_FCT_FluxLimiter_setU_tilda(flux_limiter, b); /* u_tilda = m^{-1} b */
313 /* u_tilda_connector is completed */
314 for (i = 0; i < n; ++i) printf("%d : %e %e\n",i, fctp->lumped_mass_matrix[i], flux_limiter->u_tilde[i]);
315 /**********************************************************************************************************************/
316 /* calculate stopping criterium */
317 norm_u_tilde=Paso_lsup(n, flux_limiter->u_tilde, flux_limiter->mpi_info);
318 ATOL= rtol * norm_u_tilde + atol ;
319 if (options->verbose) printf("Paso_FCT_Solver_updateNL: iteration starts u_tilda lsup = %e (abs. tol = %e)\n",norm_u_tilde,ATOL);
320 /* ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */
321
322 /* u_old is an initial guess for u*/
323 Paso_Copy(n,u,u_old);
324
325 while ( (!converged) && (!diverged) && (! max_m_reached) && Esys_noError()) {
326 Paso_Coupler_startCollect(fct_solver->u_coupler,u);
327 Paso_Coupler_finishCollect(fct_solver->u_coupler);
328
329 /* set antidiffusive_flux_{ij} for u */
330 if (fct_solver->method == PASO_BACKWARD_EULER) {
331 Paso_FCT_setAntiDiffusionFlux_BE(fct_solver->flux_limiter->antidiffusive_fluxes, fctp, dt, fct_solver->u_coupler, fct_solver->u_old_coupler);
332 } else {
333 Paso_FCT_setAntiDiffusionFlux_CN(fct_solver->flux_limiter->antidiffusive_fluxes, fctp, dt, fct_solver->u_coupler, fct_solver->u_old_coupler);
334 }
335 /* start the calculation of the limitation factors_{fct_solver->ij} */
336 Paso_FCT_FluxLimiter_addLimitedFluxes_Start(flux_limiter); /* uses u_tilde */
337
338 /*
339 * z_m[i]=b[i] - (m_i*u[i] - omega*sum_{j<>i} l_{ij} (u[j]-u[i]) ) where m_i>0
340 * ==b[i] - u[i] = u_tilda[i]-u[i] =0 otherwise
341 *
342 * omega = dt/2 or dt .
343 *
344 * note that iteration_matrix stores the negative values of the
345 * low order transport matrix l. Therefore a=dt*theta is used.
346 */
347 if (fct_solver-> method == PASO_BACKWARD_EULER) {
348 Paso_FCT_Solver_setMuPaLu(z, fctp->lumped_mass_matrix, fct_solver->u_coupler, dt, fctp->iteration_matrix);
349 } else {
350 Paso_FCT_Solver_setMuPaLu(z, fctp->lumped_mass_matrix, fct_solver->u_coupler, dt/2, fctp->iteration_matrix);
351 }
352
353
354 Paso_Update(n,-1.,z,1.,b); /* z=b-z */
355 for (i = 0; i < n; ++i) printf("%d : %e\n",i,z[i]);
356
357 /* z_i += sum_{j} limitation factor_{ij} * antidiffusive_flux_{ij} */
358 Paso_FCT_FluxLimiter_addLimitedFluxes_Complete(flux_limiter, z);
359
360 /* we solve (m/omega - L ) * du = z */
361 if (fct_solver->method == PASO_BACKWARD_EULER) {
362 dim_t cntIter = options->iter_max;
363 double tol= Paso_l2(n, z, fctp->mpi_info) ;
364
365 if ( m ==0) {
366 tol *=0.5;
367 } else {
368 tol *= MIN(MAX(rate*rate, 1e-2), 0.5);
369 }
370 /* use BiCGSTab with jacobi preconditioner ( m - omega * L ) */
371 Paso_zeroes(n,du);
372 errorCode = Paso_Solver_BiCGStab(fctp->iteration_matrix, z, du, &cntIter, &tol, pp);
373
374 /* errorCode = Paso_Solver_GMRES(fctp->iteration_matrix, z, du, &cntIter, &tol, 10, 2000, pp); */
375 if (options->verbose) printf("Paso_FCT_Solver_updateNL: BiCGStab is completed after %d steps (residual =%e).\n",cntIter, tol);
376 options->num_iter+=cntIter;
377 if ( errorCode != SOLVER_NO_ERROR) break;
378 } else {
379 /* just use the main diagonal of (m/omega - L ) */
380
381 Paso_Preconditioner_Smoother_solve(fctp->iteration_matrix, ((Paso_Preconditioner*) (fctp->iteration_matrix->solver_p))->jacobi,
382 du, z, 1, FALSE);
383 options->num_iter++;
384 }
385 for (i = 0; i < n; ++i) printf("%d : %e\n",i,du[i]);
386
387 Paso_Update(n,1.,u,fct_solver->omega,du);
388 norm_du_old=norm_du;
389 norm_du=Paso_lsup(n,du, fctp->mpi_info);
390 if (m ==0) {
391 if (options->verbose) printf("Paso_FCT_Solver_updateNL: step %d: increment= %e\n",m+1, norm_du * fct_solver->omega);
392 } else {
393 if (norm_du_old > 0.) {
394 rate=norm_du/norm_du_old;
395 } else if (norm_du <= 0.) {
396 rate=0.;
397 } else {
398 rate=LARGE_POSITIVE_FLOAT;
399 }
400 if (options->verbose) printf("Paso_FCT_Solver_updateNL: step %d: increment= %e (rate = %e)\n",m+1, norm_du * fct_solver->omega, rate);
401 num_critical_rates+=( rate<critical_rate ? 0 : 1);
402 max_m_reached=(m>max_m);
403 diverged = (num_critical_rates >= num_critical_rates_max);
404 converged=(norm_du * fct_solver->omega <= ATOL) ;
405 }
406 m++;
407 } /* end of while loop */
408 if (errorCode == SOLVER_NO_ERROR) {
409 if (converged) {
410 if (options->verbose) printf("Paso_FCT_Solver_updateNL: iteration is completed.\n");
411 errorCode=SOLVER_NO_ERROR;
412 } else if (diverged) {
413 if (options->verbose) printf("Paso_FCT_Solver_updateNL: divergence.\n");
414 errorCode=SOLVER_DIVERGENCE;
415 } else if (max_m_reached) {
416 if (options->verbose) printf("Paso_FCT_Solver_updateNL: maximum number of iteration steps reached.\n");
417 errorCode=SOLVER_MAXITER_REACHED;
418 }
419
420 }
421 return errorCode;
422 }
423
424
425 /*
426 * AntiDiffusionFlux:
427 *
428 * f_{ij} = (m_{ij} - dt (1-theta) d_{ij}) (u_old[j]-u_old[i]) - (m_{ij} + dt theta d_{ij}) (u[j]-u[i])
429 *
430 * m=fc->mass matrix
431 * d=artifical diffusion matrix = L - K = - fc->iteration matrix - fc->transport matrix (away from main diagonal)
432 *
433 * for CN : theta =0.5
434 * for BE : theta = 1.
435 */
436
437 void Paso_FCT_setAntiDiffusionFlux_CN(Paso_SystemMatrix *flux_matrix,
438 const Paso_TransportProblem* fct,
439 const double dt,
440 const Paso_Coupler* u_coupler,
441 const Paso_Coupler* u_old_coupler)
442 {
443 dim_t i;
444 index_t iptr_ij;
445
446 const double *u = Paso_Coupler_borrowLocalData(u_coupler);
447 const double *u_old= Paso_Coupler_borrowLocalData(u_old_coupler);
448 const double *remote_u=Paso_Coupler_borrowRemoteData(u_coupler);
449 const double *remote_u_old=Paso_Coupler_borrowRemoteData(u_old_coupler);
450 const double dt_half= dt/2;
451 const Paso_SystemMatrixPattern *pattern=fct->iteration_matrix->pattern;
452 const dim_t n=Paso_SystemMatrix_getTotalNumRows(fct->iteration_matrix);
453
454 #pragma omp parallel for schedule(static) private(i, iptr_ij)
455 for (i = 0; i < n; ++i) {
456 const double u_i = u[i];
457 const double u_old_i = u_old[i];
458
459 #pragma ivdep
460 for (iptr_ij=(pattern->mainPattern->ptr[i]);iptr_ij<pattern->mainPattern->ptr[i+1]; ++iptr_ij) {
461 const index_t j = pattern->mainPattern->index[iptr_ij];
462 const double m_ij = fct->mass_matrix->mainBlock->val[iptr_ij];
463 const double d_ij = fct->transport_matrix->mainBlock->val[iptr_ij]+fct->iteration_matrix->mainBlock->val[iptr_ij]; /* this is in fact -d_ij */
464 const double u_old_j = u_old[j];
465 const double u_j = u[j];
466
467 /* (m_{ij} - dt (1-theta) d_{ij}) (u_old[j]-u_old[i]) - (m_{ij} + dt theta d_{ij}) (u[j]-u[i]) */
468 flux_matrix->mainBlock->val[iptr_ij]=(m_ij+dt_half*d_ij)*(u_old_j-u_old_i) - (m_ij-dt_half*d_ij)*(u_j-u_i);
469
470 }
471 #pragma ivdep
472 for (iptr_ij=(pattern->col_couplePattern->ptr[i]);iptr_ij<pattern->col_couplePattern->ptr[i+1]; ++iptr_ij) {
473 const index_t j = pattern->col_couplePattern->index[iptr_ij];
474 const double m_ij = fct->mass_matrix->col_coupleBlock->val[iptr_ij];
475 const double d_ij = fct->transport_matrix->col_coupleBlock->val[iptr_ij]+fct->iteration_matrix->col_coupleBlock->val[iptr_ij]; /* this is in fact -d_ij */
476 const double u_old_j = remote_u_old[j];
477 const double u_j = remote_u[j];
478 flux_matrix->col_coupleBlock->val[iptr_ij]=(m_ij+dt_half*d_ij)*(u_old_j-u_old_i)- (m_ij-dt_half*d_ij)*(u_j-u_i);
479 }
480 }
481 }
482
483 void Paso_FCT_setAntiDiffusionFlux_BE(Paso_SystemMatrix *flux_matrix,
484 const Paso_TransportProblem* fct,
485 const double dt,
486 const Paso_Coupler* u_coupler,
487 const Paso_Coupler* u_old_coupler)
488 {
489 dim_t i;
490 index_t iptr_ij;
491
492 const double *u=Paso_Coupler_borrowLocalData(u_coupler);
493 const double *u_old= Paso_Coupler_borrowLocalData(u_old_coupler);
494 const double *remote_u=Paso_Coupler_borrowRemoteData(u_coupler);
495 const double *remote_u_old=Paso_Coupler_borrowRemoteData(u_old_coupler);
496 const Paso_SystemMatrixPattern *pattern=fct->iteration_matrix->pattern;
497 const dim_t n=Paso_SystemMatrix_getTotalNumRows(fct->iteration_matrix);
498
499 #pragma omp parallel for schedule(static) private(i, iptr_ij)
500 for (i = 0; i < n; ++i) {
501 const double u_i = u[i];
502 const double u_old_i = u_old[i];
503 #pragma ivdep
504 for (iptr_ij=pattern->mainPattern->ptr[i]; iptr_ij<pattern->mainPattern->ptr[i+1]; ++iptr_ij) {
505
506 const index_t j = pattern->mainPattern->index[iptr_ij];
507 const double m_ij = fct->mass_matrix->mainBlock->val[iptr_ij];
508 const double d_ij = fct->transport_matrix->mainBlock->val[iptr_ij]+fct->iteration_matrix->mainBlock->val[iptr_ij]; /* this is in fact -d_ij */
509 const double u_old_j = u_old[j];
510 const double u_j = u[j];
511
512 flux_matrix->mainBlock->val[iptr_ij]=m_ij*(u_old_j-u_old_i)- (m_ij-dt*d_ij)*(u_j-u_i);
513 }
514 #pragma ivdep
515 for (iptr_ij=pattern->col_couplePattern->ptr[i]; iptr_ij<pattern->col_couplePattern->ptr[i+1]; ++iptr_ij) {
516 const index_t j = pattern->col_couplePattern->index[iptr_ij];
517 const double m_ij = fct->mass_matrix->col_coupleBlock->val[iptr_ij]; /* this is in fact -d_ij */
518 const double d_ij = fct->transport_matrix->col_coupleBlock->val[iptr_ij]+fct->iteration_matrix->col_coupleBlock->val[iptr_ij];
519 const double u_old_j = remote_u_old[j];
520 const double u_j = remote_u[j];
521
522 flux_matrix->col_coupleBlock->val[iptr_ij]=m_ij*(u_old_j-u_old_i)- (m_ij-dt*d_ij)*(u_j-u_i);
523 }
524 }
525 }
526
527 /* special version of the ant-diffusive fluxes for the linear Crank-Nicolson scheme
528 * in fact this is evaluated for u = 2*u_tilde - u_old which is the predictor
529 * of the solution of the the stabilized problem at time dt using the forward Euler scheme
530 *
531 * f_{ij} = (m_{ij} - dt/2 d_{ij}) (u_old[j]-u_old[i]) - (m_{ij} + dt/2 d_{ij}) (u[j]-u[i])
532 * = (m_{ij} - dt/2 d_{ij}) * (u_old[j]-u_old[i]) - (m_{ij} + dt/2 d_{ij}) * ( 2*(u_tilde[j]-u_tilde[i]) - (u_old[j] -u_old [i]) )
533 * = 2* m_{ij} * ( (u_old[j]-u_tilde[j] - (u_old[i]) - u_tilde[i]) ) - dt d_{ij} * (u_tilde[j]-u_tilde[i])
534 *
535 */
536
537 void Paso_FCT_setAntiDiffusionFlux_linearCN(Paso_SystemMatrix *flux_matrix,
538 const Paso_TransportProblem* fct,
539 const double dt,
540 const Paso_Coupler* u_tilde_coupler,
541 const Paso_Coupler* u_old_coupler)
542 {
543 dim_t i;
544 index_t iptr_ij;
545
546 const double *u_tilde=Paso_Coupler_borrowLocalData(u_tilde_coupler);
547 const double *u_old= Paso_Coupler_borrowLocalData(u_old_coupler);
548 const double *remote_u_tilde=Paso_Coupler_borrowRemoteData(u_tilde_coupler);
549 const double *remote_u_old=Paso_Coupler_borrowRemoteData(u_old_coupler);
550 const Paso_SystemMatrixPattern *pattern=fct->iteration_matrix->pattern;
551 const dim_t n=Paso_SystemMatrix_getTotalNumRows(fct->iteration_matrix);
552
553 #pragma omp parallel for schedule(static) private(i, iptr_ij)
554 for (i = 0; i < n; ++i) {
555 const double u_tilde_i = u_tilde[i];
556 const double u_old_i = u_old[i];
557 const double du_i = u_tilde_i - u_old_i;
558 #pragma ivdep
559 for (iptr_ij=(pattern->mainPattern->ptr[i]);iptr_ij<pattern->mainPattern->ptr[i+1]; ++iptr_ij) {
560
561 const index_t j = pattern->mainPattern->index[iptr_ij];
562 const double m_ij = fct->mass_matrix->mainBlock->val[iptr_ij];
563 const double d_ij = fct->transport_matrix->mainBlock->val[iptr_ij]+fct->iteration_matrix->mainBlock->val[iptr_ij]; /* this is in fact -d_ij */
564 const double u_tilde_j = u_tilde[j];
565 const double u_old_j = u_old[j];
566 const double du_j = u_tilde_j - u_old_j;
567
568 flux_matrix->mainBlock->val[iptr_ij]= 2 * m_ij * ( du_i - du_j ) - dt * d_ij * ( u_tilde_i - u_tilde_j);
569 }
570 #pragma ivdep
571 for (iptr_ij=(pattern->col_couplePattern->ptr[i]);iptr_ij<pattern->col_couplePattern->ptr[i+1]; ++iptr_ij) {
572
573 const index_t j = pattern->col_couplePattern->index[iptr_ij];
574 const double m_ij = fct->mass_matrix->col_coupleBlock->val[iptr_ij];
575 const double d_ij = fct->transport_matrix->col_coupleBlock->val[iptr_ij]+fct->iteration_matrix->col_coupleBlock->val[iptr_ij];/* this is in fact -d_ij */
576 const double u_tilde_j = remote_u_tilde[j];
577 const double u_old_j = remote_u_old[j];
578 const double du_j = u_tilde_j - u_old_j;
579
580 flux_matrix->col_coupleBlock->val[iptr_ij]= 2 * m_ij * ( du_i - du_j ) - dt * d_ij * ( u_tilde_i - u_tilde_j);
581
582 }
583 }
584
585 }
586
587 /**************************************************************/
588
589 /* Creates the low order transport matrix and stores its negative values
590 * into the iteration_matrix except for the main diagonal which is stored
591 * separately.
592 * If fc->iteration_matrix==NULL, fc->iteration_matrix is allocated
593 *
594 * a=transport_matrix
595 * b= low_order_transport_matrix = - iteration_matrix
596 * c=main diagonal low_order_transport_matrix
597 * initialize c[i] mit a[i,i]
598 *
599 * d_ij=max(0,-a[i,j],-a[j,i])
600 * b[i,j]=-(a[i,j]+d_ij)
601 * c[i]-=d_ij
602 */
603
604 void Paso_FCT_setLowOrderOperator(Paso_TransportProblem * fc) {
605
606 dim_t i;
607 index_t iptr_ij, iptr_ji;
608 const index_t* main_iptr=Paso_TransportProblem_borrowMainDiagonalPointer(fc);
609
610 if (fc->iteration_matrix==NULL) {
611 fc->iteration_matrix=Paso_SystemMatrix_alloc(fc->transport_matrix->type,
612 fc->transport_matrix->pattern,
613 fc->transport_matrix->row_block_size,
614 fc->transport_matrix->col_block_size, TRUE);
615 }
616
617 if (Esys_noError()) {
618 const Paso_SystemMatrixPattern *pattern=fc->iteration_matrix->pattern;
619 const dim_t n=Paso_SystemMatrix_getTotalNumRows(fc->iteration_matrix);
620 #pragma omp parallel for private(i, iptr_ij, iptr_ji) schedule(static)
621 for (i = 0; i < n; ++i) {
622 double sum=fc->transport_matrix->mainBlock->val[main_iptr[i]];
623
624 /* printf("sum[%d] = %e -> ", i, sum); */
625 /* look at a[i,j] */
626 for (iptr_ij=pattern->mainPattern->ptr[i];iptr_ij<pattern->mainPattern->ptr[i+1]; ++iptr_ij) {
627 const index_t j = pattern->mainPattern->index[iptr_ij];
628 const double rtmp1 = fc->transport_matrix->mainBlock->val[iptr_ij];
629 if (j!=i) {
630 /* find entry a[j,i] */
631 #pragma ivdep
632 for (iptr_ji=pattern->mainPattern->ptr[j]; iptr_ji<pattern->mainPattern->ptr[j+1]; ++iptr_ji) {
633
634 if ( pattern->mainPattern->index[iptr_ji] == i) {
635 const double rtmp2=fc->transport_matrix->mainBlock->val[iptr_ji];
636 /*
637 printf("a[%d,%d]=%e\n",i,j,rtmp1);
638 printf("a[%d,%d]=%e\n",j,i,rtmp2);
639 */
640
641 const double d_ij=-MIN3(0.,rtmp1,rtmp2);
642 fc->iteration_matrix->mainBlock->val[iptr_ij]=-(rtmp1+d_ij);
643 /* printf("l[%d,%d]=%e\n",i,j,fc->iteration_matrix->mainBlock->val[iptr_ij]); */
644 sum-=d_ij;
645 break;
646 }
647 }
648 }
649 }
650 for (iptr_ij=pattern->col_couplePattern->ptr[i];iptr_ij<pattern->col_couplePattern->ptr[i+1]; ++iptr_ij) {
651 const index_t j = pattern->col_couplePattern->index[iptr_ij];
652 const double rtmp1 = fc->transport_matrix->col_coupleBlock->val[iptr_ij];
653 /* find entry a[j,i] */
654 #pragma ivdep
655 for (iptr_ji=pattern->row_couplePattern->ptr[j]; iptr_ji<pattern->row_couplePattern->ptr[j+1]; ++iptr_ji) {
656 if (pattern->row_couplePattern->index[iptr_ji]==i) {
657 const double rtmp2=fc->transport_matrix->row_coupleBlock->val[iptr_ji];
658 const double d_ij=-MIN3(0.,rtmp1,rtmp2);
659 fc->iteration_matrix->col_coupleBlock->val[iptr_ij]=-(rtmp1+d_ij);
660 fc->iteration_matrix->row_coupleBlock->val[iptr_ji]=-(rtmp2+d_ij);
661 sum-=d_ij;
662 break;
663 }
664 }
665 }
666 /* set main diagonal entry */
667 fc->main_diagonal_low_order_transport_matrix[i]=sum;
668 /* printf("%e \n", sum); */
669 }
670
671 }
672 }
673
674 /*
675 * out_i=m_i u_i + a * \sum_{j <> i} l_{ij} (u_j-u_i) where m_i>0
676 * = u_i where m_i<=0
677 *
678 */
679 void Paso_FCT_Solver_setMuPaLu(double* out,
680 const double* M,
681 const Paso_Coupler* u_coupler,
682 const double a,
683 const Paso_SystemMatrix *L)
684 {
685 dim_t i;
686 const Paso_SystemMatrixPattern *pattern = L->pattern;
687 const double *u=Paso_Coupler_borrowLocalData(u_coupler);
688 const double *remote_u=Paso_Coupler_borrowRemoteData(u_coupler);
689 index_t iptr_ij;
690 const dim_t n=Paso_SystemMatrix_getTotalNumRows(L);
691
692 #pragma omp parallel for private(i) schedule(static)
693 for (i = 0; i < n; ++i) {
694 if ( M[i] > 0.) {
695 out[i]=M[i]*u[i];
696 } else {
697 out[i]=u[i];
698 }
699 }
700 if (ABS(a)>0) {
701 #pragma omp parallel for schedule(static) private(i, iptr_ij)
702 for (i = 0; i < n; ++i) {
703 double sum=0;
704 const double u_i=u[i];
705 #pragma ivdep
706 for (iptr_ij=(pattern->mainPattern->ptr[i]);iptr_ij<pattern->mainPattern->ptr[i+1]; ++iptr_ij) {
707 const index_t j=pattern->mainPattern->index[iptr_ij];
708 const double l_ij=L->mainBlock->val[iptr_ij];
709 sum+=l_ij*(u[j]-u_i);
710
711 }
712 #pragma ivdep
713 for (iptr_ij=(pattern->col_couplePattern->ptr[i]);iptr_ij<pattern->col_couplePattern->ptr[i+1]; ++iptr_ij) {
714 const index_t j=pattern->col_couplePattern->index[iptr_ij];
715 const double l_ij=L->col_coupleBlock->val[iptr_ij];
716 sum+=l_ij*(remote_u[j]-u_i);
717 }
718 out[i]+=a*sum;
719 }
720 }
721 }
722
723 /* *************************************************************************************************************************** */
724
725
726
727
728
729
730

  ViewVC Help
Powered by ViewVC 1.1.26