/[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 4852 - (show annotations)
Wed Apr 9 03:58:08 2014 UTC (6 years, 1 month ago) by caltinay
File size: 31136 byte(s)
paso util sweep

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

Properties

Name Value
svn:mergeinfo /branches/amg_from_3530/paso/src/FCT_Solver.cpp:3531-3826 /branches/lapack2681/paso/src/FCT_Solver.cpp:2682-2741 /branches/pasowrap/paso/src/FCT_Solver.cpp:3661-3674 /branches/py3_attempt2/paso/src/FCT_Solver.cpp:3871-3891 /branches/restext/paso/src/FCT_Solver.cpp:2610-2624 /branches/ripleygmg_from_3668/paso/src/FCT_Solver.cpp:3669-3791 /branches/stage3.0/paso/src/FCT_Solver.cpp:2569-2590 /branches/symbolic_from_3470/paso/src/FCT_Solver.cpp:3471-3974 /branches/symbolic_from_3470/ripley/test/python/paso/src/FCT_Solver.cpp:3517-3974 /release/3.0/paso/src/FCT_Solver.cpp:2591-2601 /trunk/paso/src/FCT_Solver.cpp:4257-4344 /trunk/ripley/test/python/paso/src/FCT_Solver.cpp:3480-3515

  ViewVC Help
Powered by ViewVC 1.1.26