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

Annotation of /trunk/paso/src/FCT_Solver.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26