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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4836 - (hide annotations)
Mon Apr 7 05:51:55 2014 UTC (6 years, 2 months ago) by caltinay
File size: 30563 byte(s)
"Some" SystemMatrix clean up.....

1 gross 3793
2 jfenwick 3981 /*****************************************************************************
3 gross 3793 *
4 jfenwick 4657 * Copyright (c) 2003-2014 by University of Queensland
5 jfenwick 3981 * http://www.uq.edu.au
6 gross 3793 *
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 jfenwick 3981 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 jfenwick 4657 * Development 2012-2013 by School of Earth Sciences
13     * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 jfenwick 3981 *
15     *****************************************************************************/
16 gross 3793
17    
18 jfenwick 3981 /************************************************************************************/
19 gross 3793
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 jfenwick 3981 /************************************************************************************/
28 gross 3793
29     /* Author: l.gross@uq.edu.au */
30    
31 jfenwick 3981 /************************************************************************************/
32 gross 3793
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 jfenwick 4325 out=new Paso_FCT_Solver;
45 gross 3793 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 jfenwick 4325 out->b = new double[n];
49 gross 3793 if ( (options->ode_solver == PASO_CRANK_NICOLSON) || (options->ode_solver == PASO_BACKWARD_EULER) ) {
50 jfenwick 4325 out->du = new double[n];
51     out->z = new double[n];
52 gross 3793 } else {
53     out->du = NULL;
54     out->z=NULL;
55     }
56 caltinay 4817 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 gross 3793
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    
80     void Paso_FCT_Solver_free(Paso_FCT_Solver *in)
81     {
82     if (in != NULL) {
83 caltinay 4817 Paso_TransportProblem_free(in->transportproblem);
84     Paso_FCT_FluxLimiter_free(in->flux_limiter);
85     Esys_MPIInfo_free(in->mpi_info);
86     delete[] in->b;
87     delete[] in->z;
88     delete[] in->du;
89     delete in;
90 gross 3793 }
91     }
92    
93     double Paso_FCT_Solver_getSafeTimeStepSize(Paso_TransportProblem* fctp)
94     {
95 caltinay 4836 dim_t i;
96 gross 3823 double dt_max_loc=LARGE_POSITIVE_FLOAT;
97 gross 3793 double dt_max=LARGE_POSITIVE_FLOAT;
98 caltinay 4836 const dim_t n = fctp->transport_matrix->getTotalNumRows();
99 gross 3793 /* set low order transport operator */
100     Paso_FCT_setLowOrderOperator(fctp);
101    
102     if (Esys_noError()) {
103     /*
104     * calculate time step size:
105     */
106     dt_max=LARGE_POSITIVE_FLOAT;
107 gross 3823 #pragma omp parallel private(i, dt_max_loc)
108 gross 3793 {
109 gross 3824 dt_max_loc=LARGE_POSITIVE_FLOAT;
110 gross 3793 #pragma omp for schedule(static)
111     for (i=0;i<n;++i) {
112 caltinay 3795 const double l_ii=fctp->main_diagonal_low_order_transport_matrix[i];
113     const double m_i=fctp->lumped_mass_matrix[i];
114 gross 3822 if ( m_i > 0 ) {
115 gross 3793 if (l_ii<0) dt_max_loc=MIN(dt_max_loc,m_i/(-l_ii));
116 gross 3822 }
117     }
118 gross 3793 #pragma omp critical
119     {
120     dt_max=MIN(dt_max,dt_max_loc);
121     }
122     }
123     #ifdef ESYS_MPI
124     {
125 gross 3822 dt_max_loc=dt_max;
126     MPI_Allreduce(&dt_max_loc, &dt_max, 1, MPI_DOUBLE, MPI_MIN, fctp->mpi_info->comm);
127 gross 3793 }
128     #endif
129 gross 3822 if (dt_max<LARGE_POSITIVE_FLOAT) dt_max*=2.;
130 gross 3793 }
131     return dt_max;
132     }
133    
134     /* modifies the main diagonal of the iteration matrix to introduce new dt */
135     void Paso_FCT_Solver_initialize(const double dt, Paso_FCT_Solver *fct_solver, Paso_Options* options, Paso_Performance* pp)
136     {
137     Paso_TransportProblem* fctp = fct_solver->transportproblem;
138     const index_t* main_iptr=Paso_TransportProblem_borrowMainDiagonalPointer(fctp);
139 caltinay 4836 const dim_t n = fctp->transport_matrix->getTotalNumRows();
140 gross 3793 const double theta = Paso_FCT_Solver_getTheta(fct_solver);
141     const double omega=1./(dt* theta);
142     dim_t i;
143     Paso_Options options2;
144    
145    
146    
147 caltinay 4836 Paso_solve_free(fctp->iteration_matrix.get());
148 gross 3793 /*
149     * fctp->iteration_matrix[i,i]=m[i]/(dt theta) -l[i,i]
150     *
151     */
152     fct_solver->omega=omega;
153     fct_solver->dt = dt;
154     #pragma omp parallel for private(i)
155     for (i = 0; i < n; ++i) {
156 gross 3822 const double m_i=fctp->lumped_mass_matrix[i];
157 caltinay 3795 const double l_ii = fctp->main_diagonal_low_order_transport_matrix[i];
158 gross 3822 if ( m_i > 0 ) {
159     fctp->iteration_matrix->mainBlock->val[main_iptr[i]] = m_i * omega - l_ii;
160     } else {
161     fctp->iteration_matrix->mainBlock->val[main_iptr[i]] = ABS(m_i * omega - l_ii)/(EPSILON*EPSILON);
162     }
163 gross 3793 }
164    
165     /* allocate preconditioner/solver */
166     Paso_Options_setDefaults(&options2);
167     options2.verbose = options->verbose;
168     if (fct_solver->method == PASO_LINEAR_CRANK_NICOLSON ) {
169     options2.preconditioner = PASO_GS;
170     } else {
171     options2.preconditioner = PASO_JACOBI;
172     /* options2.preconditioner = PASO_GS; */
173     }
174     options2.use_local_preconditioner = FALSE;
175 gross 3807 options2.sweeps=-1;
176 gross 3793
177     Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
178 caltinay 4836 fctp->iteration_matrix->setPreconditioner(&options2);
179 gross 3793 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
180     }
181    
182 jfenwick 4345 /* entry point for update procedures */
183 gross 3793 err_t Paso_FCT_Solver_update(Paso_FCT_Solver *fct_solver, double* u, double *u_old, Paso_Options* options, Paso_Performance *pp)
184     {
185     const index_t method=fct_solver->method;
186     err_t err_out = SOLVER_NO_ERROR;
187    
188    
189     if (method == PASO_LINEAR_CRANK_NICOLSON) {
190     err_out=Paso_FCT_Solver_update_LCN(fct_solver, u, u_old, options, pp);
191    
192     } else if (method == PASO_CRANK_NICOLSON) {
193     err_out=Paso_FCT_Solver_updateNL(fct_solver, u, u_old, options, pp);
194    
195     } else if (method == PASO_BACKWARD_EULER) {
196     err_out=Paso_FCT_Solver_updateNL(fct_solver, u, u_old, options, pp);
197     } else {
198     err_out = SOLVER_INPUT_ERROR;
199     }
200     return err_out;
201     }
202    
203     /* linear crank-nicolson update */
204     err_t Paso_FCT_Solver_update_LCN(Paso_FCT_Solver *fct_solver, double * u, double *u_old, Paso_Options* options, Paso_Performance *pp)
205     {
206     double const dt = fct_solver->dt;
207 gross 3823 dim_t sweep_max, i;
208 gross 3793 double *b = fct_solver->b;
209     double const RTOL = options->tolerance;
210     const dim_t n=Paso_TransportProblem_getTotalNumRows(fct_solver->transportproblem);
211 caltinay 4836 paso::SystemMatrix_ptr iteration_matrix(fct_solver->transportproblem->iteration_matrix);
212 gross 3823 const index_t* main_iptr=Paso_TransportProblem_borrowMainDiagonalPointer(fct_solver->transportproblem);
213 gross 3793 err_t errorCode = SOLVER_NO_ERROR;
214 gross 3823 double norm_u_tilde;
215 gross 3793
216 caltinay 4817 fct_solver->u_old_coupler->startCollect(u_old);
217     fct_solver->u_old_coupler->finishCollect();
218 gross 3793
219 gross 3822 /* b[i]=m*u_tilde[i] = m u_old[i] + dt/2 sum_{j <> i} l_{ij}*(u_old[j]-u_old[i])
220     = u_tilde[i] = u_old[i] where constraint m<0.
221 gross 3793 * note that iteration_matrix stores the negative values of the
222     * low order transport matrix l. Therefore a=-dt*0.5 is used. */
223    
224     Paso_FCT_Solver_setMuPaLu(b, fct_solver->transportproblem->lumped_mass_matrix,
225 gross 3807 fct_solver->u_old_coupler, -dt*0.5, iteration_matrix);
226 gross 3793 /* solve for u_tilde : u_tilda = m^{-1} * b */
227     Paso_FCT_FluxLimiter_setU_tilda(fct_solver->flux_limiter, b);
228     /* u_tilda_connector is completed */
229 gross 3807
230 gross 3793 /* calculate anti-diffusive fluxes for u_tilda */
231     Paso_FCT_setAntiDiffusionFlux_linearCN(fct_solver->flux_limiter->antidiffusive_fluxes,
232     fct_solver->transportproblem, dt,
233     fct_solver->flux_limiter->u_tilde_coupler,
234     fct_solver->u_old_coupler);
235 gross 3807
236    
237 gross 3793 /* b_i += sum_{j} limitation factor_{ij} * antidiffusive_flux_{ij} */
238     Paso_FCT_FluxLimiter_addLimitedFluxes_Start(fct_solver->flux_limiter);
239     Paso_FCT_FluxLimiter_addLimitedFluxes_Complete(fct_solver->flux_limiter, b);
240    
241 gross 3823 Paso_Scale(n, b,fct_solver->omega );
242 gross 3807 /* solve (m-dt/2*L) u = b in the form (omega*m-L) u = b * omega with omega*dt/2=1 */
243 gross 3823 #pragma omp for private(i) schedule(static)
244     for (i = 0; i < n; ++i) {
245     if (! (fct_solver->transportproblem->lumped_mass_matrix[i] > 0 ) ) {
246     b[i] = fct_solver->flux_limiter->u_tilde[i]
247     * fct_solver->transportproblem->iteration_matrix->mainBlock->val[main_iptr[i]];
248     }
249     }
250 gross 3793 /* initial guess is u<- -u + 2*u_tilde */
251     Paso_Update(n, -1., u, 2., fct_solver->flux_limiter->u_tilde);
252 gross 3823
253 gross 3793 sweep_max = MAX((int) (- 2 * log(RTOL)/log(2.)-0.5),1);
254 gross 3823 norm_u_tilde=Paso_lsup(n, fct_solver->flux_limiter->u_tilde, fct_solver->flux_limiter->mpi_info);
255 gross 3807 if (options->verbose) {
256 gross 3823 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);
257 gross 3807 }
258 gross 3793 errorCode = Paso_Preconditioner_Smoother_solve_byTolerance( iteration_matrix, ((Paso_Preconditioner*) (iteration_matrix->solver_p))->gs,
259     u, b, RTOL, &sweep_max, TRUE);
260     if (errorCode == PRECONDITIONER_NO_ERROR) {
261     if (options->verbose) printf("Paso_FCT_Solver_update_LCN: convergence after %d Gauss-Seidel steps.\n",sweep_max);
262     errorCode=SOLVER_NO_ERROR;
263     } else {
264     if (options->verbose) printf("Paso_FCT_Solver_update_LCN: Gauss-Seidel failed within %d stesp (rel. tolerance %e).\n",sweep_max,RTOL);
265     errorCode= SOLVER_MAXITER_REACHED;
266     }
267     return errorCode;
268    
269     }
270    
271     err_t Paso_FCT_Solver_updateNL(Paso_FCT_Solver *fct_solver, double* u, double *u_old, Paso_Options* options, Paso_Performance *pp)
272     {
273 gross 3794 const dim_t num_critical_rates_max=3; /* number of rates >=critical_rate accepted before divergence is triggered */
274     const double critical_rate=0.95; /* expected value of convergence rate */
275    
276 gross 3793 double *b = fct_solver->b;
277     double *z = fct_solver->z;
278     double *du = fct_solver->du;
279     double const dt = fct_solver->dt;
280     Paso_TransportProblem* fctp = fct_solver->transportproblem;
281     Paso_FCT_FluxLimiter* flux_limiter = fct_solver->flux_limiter;
282     dim_t i;
283 gross 3794 double norm_u_tilde, ATOL, norm_du=LARGE_POSITIVE_FLOAT, norm_du_old, rate=1.;
284 gross 3793 err_t errorCode=SOLVER_NO_ERROR;
285 caltinay 4836 const dim_t n = fctp->transport_matrix->getTotalNumRows();
286 gross 3793 const double atol=options->absolute_tolerance;
287     const double rtol=options->tolerance;
288     const dim_t max_m=options->iter_max;
289     dim_t m=0, num_critical_rates=0 ;
290     /* ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */
291    
292 jfenwick 4521 bool converged=FALSE, max_m_reached=FALSE,diverged=FALSE;
293 gross 3793 options->num_iter=0;
294    
295 caltinay 4817 fct_solver->u_old_coupler->startCollect(u_old);
296     fct_solver->u_old_coupler->finishCollect();
297 gross 3793 /* prepare u_tilda and flux limiter */
298     if ( fct_solver->method == PASO_BACKWARD_EULER ) {
299     /* b[i]=m_i* u_old[i] */
300     #pragma omp for private(i) schedule(static)
301     for (i = 0; i < n; ++i) {
302 gross 3822 if (fctp->lumped_mass_matrix[i] > 0 ) {
303     b[i]=u_old[i]* fctp->lumped_mass_matrix[i];
304     } else {
305     b[i]=u_old[i];
306     }
307 gross 3793 }
308     } else {
309 gross 3822 /* 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
310     * = u_old[i] otherwise
311 gross 3793 * note that iteration_matrix stores the negative values of the
312     * low order transport matrix l. Therefore a=-dt*0.5 is used. */
313     Paso_FCT_Solver_setMuPaLu(b,fctp->lumped_mass_matrix,fct_solver->u_old_coupler,-dt*0.5,fctp->iteration_matrix);
314     }
315     Paso_FCT_FluxLimiter_setU_tilda(flux_limiter, b); /* u_tilda = m^{-1} b */
316     /* u_tilda_connector is completed */
317 jfenwick 3981 /********************************************************************************************************************************************/
318 jfenwick 4345 /* calculate stopping criterion */
319 gross 3793 norm_u_tilde=Paso_lsup(n, flux_limiter->u_tilde, flux_limiter->mpi_info);
320     ATOL= rtol * norm_u_tilde + atol ;
321     if (options->verbose) printf("Paso_FCT_Solver_updateNL: iteration starts u_tilda lsup = %e (abs. tol = %e)\n",norm_u_tilde,ATOL);
322     /* ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */
323    
324     /* u_old is an initial guess for u*/
325     Paso_Copy(n,u,u_old);
326    
327     while ( (!converged) && (!diverged) && (! max_m_reached) && Esys_noError()) {
328 caltinay 4817 fct_solver->u_coupler->startCollect(u);
329     fct_solver->u_coupler->finishCollect();
330 gross 3793
331     /* set antidiffusive_flux_{ij} for u */
332     if (fct_solver->method == PASO_BACKWARD_EULER) {
333     Paso_FCT_setAntiDiffusionFlux_BE(fct_solver->flux_limiter->antidiffusive_fluxes, fctp, dt, fct_solver->u_coupler, fct_solver->u_old_coupler);
334     } else {
335     Paso_FCT_setAntiDiffusionFlux_CN(fct_solver->flux_limiter->antidiffusive_fluxes, fctp, dt, fct_solver->u_coupler, fct_solver->u_old_coupler);
336     }
337     /* start the calculation of the limitation factors_{fct_solver->ij} */
338     Paso_FCT_FluxLimiter_addLimitedFluxes_Start(flux_limiter); /* uses u_tilde */
339    
340     /*
341 gross 3822 * z_m[i]=b[i] - (m_i*u[i] - omega*sum_{j<>i} l_{ij} (u[j]-u[i]) ) where m_i>0
342     * ==b[i] - u[i] = u_tilda[i]-u[i] =0 otherwise
343     *
344     * omega = dt/2 or dt .
345 gross 3793 *
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     /* z_i += sum_{j} limitation factor_{ij} * antidiffusive_flux_{ij} */
359     Paso_FCT_FluxLimiter_addLimitedFluxes_Complete(flux_limiter, z);
360    
361     /* we solve (m/omega - L ) * du = z */
362     if (fct_solver->method == PASO_BACKWARD_EULER) {
363     dim_t cntIter = options->iter_max;
364     double tol= Paso_l2(n, z, fctp->mpi_info) ;
365    
366     if ( m ==0) {
367     tol *=0.5;
368     } else {
369     tol *= MIN(MAX(rate*rate, 1e-2), 0.5);
370     }
371     /* use BiCGSTab with jacobi preconditioner ( m - omega * L ) */
372     Paso_zeroes(n,du);
373     errorCode = Paso_Solver_BiCGStab(fctp->iteration_matrix, z, du, &cntIter, &tol, pp);
374    
375     /* errorCode = Paso_Solver_GMRES(fctp->iteration_matrix, z, du, &cntIter, &tol, 10, 2000, pp); */
376     if (options->verbose) printf("Paso_FCT_Solver_updateNL: BiCGStab is completed after %d steps (residual =%e).\n",cntIter, tol);
377     options->num_iter+=cntIter;
378     if ( errorCode != SOLVER_NO_ERROR) break;
379     } else {
380     /* just use the main diagonal of (m/omega - L ) */
381    
382     Paso_Preconditioner_Smoother_solve(fctp->iteration_matrix, ((Paso_Preconditioner*) (fctp->iteration_matrix->solver_p))->jacobi,
383     du, z, 1, FALSE);
384 gross 3825
385 gross 3793 options->num_iter++;
386     }
387 gross 3823
388 gross 3793
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 jfenwick 4345 * d=artificial diffusion matrix = L - K = - fc->iteration matrix - fc->transport matrix (away from main diagonal)
434 gross 3793 *
435     * for CN : theta =0.5
436     * for BE : theta = 1.
437     */
438    
439 caltinay 4836 void Paso_FCT_setAntiDiffusionFlux_CN(paso::SystemMatrix_ptr flux_matrix,
440 gross 3793 const Paso_TransportProblem* fct,
441 caltinay 4817 const double dt,
442     paso::const_Coupler_ptr u_coupler,
443     paso::const_Coupler_ptr u_old_coupler)
444 gross 3793 {
445     dim_t i;
446     index_t iptr_ij;
447    
448 caltinay 4817 const double *u = u_coupler->borrowLocalData();
449     const double *u_old= u_old_coupler->borrowLocalData();
450     const double *remote_u=u_coupler->borrowRemoteData();
451     const double *remote_u_old=u_old_coupler->borrowRemoteData();
452 gross 3793 const double dt_half= dt/2;
453 caltinay 4818 paso::const_SystemMatrixPattern_ptr pattern(fct->iteration_matrix->pattern);
454 caltinay 4836 const dim_t n = fct->iteration_matrix->getTotalNumRows();
455 gross 3793
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 caltinay 4836 void Paso_FCT_setAntiDiffusionFlux_BE(paso::SystemMatrix_ptr flux_matrix,
486 gross 3793 const Paso_TransportProblem* fct,
487 caltinay 4817 const double dt,
488     paso::const_Coupler_ptr u_coupler,
489     paso::const_Coupler_ptr u_old_coupler)
490 gross 3793 {
491     dim_t i;
492     index_t iptr_ij;
493    
494 caltinay 4817 const double *u = u_coupler->borrowLocalData();
495     const double *u_old = u_old_coupler->borrowLocalData();
496     const double *remote_u = u_coupler->borrowRemoteData();
497     const double *remote_u_old = u_old_coupler->borrowRemoteData();
498 caltinay 4818 paso::const_SystemMatrixPattern_ptr pattern(fct->iteration_matrix->pattern);
499 caltinay 4836 const dim_t n = fct->iteration_matrix->getTotalNumRows();
500 gross 3793
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 jfenwick 4345 * of the solution of the the stabilised problem at time dt using the forward Euler scheme
532 gross 3793 *
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 caltinay 4836 void Paso_FCT_setAntiDiffusionFlux_linearCN(paso::SystemMatrix_ptr flux_matrix,
540 caltinay 4817 const Paso_TransportProblem* fct, const double dt,
541     paso::const_Coupler_ptr u_tilde_coupler,
542     paso::const_Coupler_ptr u_old_coupler)
543 gross 3793 {
544     dim_t i;
545     index_t iptr_ij;
546    
547 caltinay 4817 const double *u_tilde = u_tilde_coupler->borrowLocalData();
548     const double *u_old = u_old_coupler->borrowLocalData();
549     const double *remote_u_tilde = u_tilde_coupler->borrowRemoteData();
550     const double *remote_u_old = u_old_coupler->borrowRemoteData();
551 caltinay 4818 paso::const_SystemMatrixPattern_ptr pattern(fct->iteration_matrix->pattern);
552 caltinay 4836 const dim_t n = fct->iteration_matrix->getTotalNumRows();
553 gross 3793
554     #pragma omp parallel for schedule(static) private(i, iptr_ij)
555     for (i = 0; i < n; ++i) {
556 caltinay 3795 const double u_tilde_i = u_tilde[i];
557     const double u_old_i = u_old[i];
558 gross 3807 const double du_i = u_tilde_i - u_old_i;
559 gross 3793 #pragma ivdep
560     for (iptr_ij=(pattern->mainPattern->ptr[i]);iptr_ij<pattern->mainPattern->ptr[i+1]; ++iptr_ij) {
561    
562 caltinay 3795 const index_t j = pattern->mainPattern->index[iptr_ij];
563     const double m_ij = fct->mass_matrix->mainBlock->val[iptr_ij];
564     const double d_ij = fct->transport_matrix->mainBlock->val[iptr_ij]+fct->iteration_matrix->mainBlock->val[iptr_ij]; /* this is in fact -d_ij */
565     const double u_tilde_j = u_tilde[j];
566     const double u_old_j = u_old[j];
567 gross 3807 const double du_j = u_tilde_j - u_old_j;
568 gross 3793
569 gross 3807 flux_matrix->mainBlock->val[iptr_ij]= 2 * m_ij * ( du_i - du_j ) - dt * d_ij * ( u_tilde_i - u_tilde_j);
570 gross 3793 }
571     #pragma ivdep
572     for (iptr_ij=(pattern->col_couplePattern->ptr[i]);iptr_ij<pattern->col_couplePattern->ptr[i+1]; ++iptr_ij) {
573    
574 caltinay 3795 const index_t j = pattern->col_couplePattern->index[iptr_ij];
575     const double m_ij = fct->mass_matrix->col_coupleBlock->val[iptr_ij];
576     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 */
577     const double u_tilde_j = remote_u_tilde[j];
578     const double u_old_j = remote_u_old[j];
579 gross 3807 const double du_j = u_tilde_j - u_old_j;
580 gross 3793
581 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);
582 gross 3793
583     }
584     }
585    
586     }
587    
588 jfenwick 3981 /************************************************************************************/
589 gross 3793
590     /* Creates the low order transport matrix and stores its negative values
591     * into the iteration_matrix except for the main diagonal which is stored
592     * separately.
593     * If fc->iteration_matrix==NULL, fc->iteration_matrix is allocated
594     *
595     * a=transport_matrix
596     * b= low_order_transport_matrix = - iteration_matrix
597     * c=main diagonal low_order_transport_matrix
598 jfenwick 4345 * initialise c[i] mit a[i,i]
599 gross 3793 *
600     * d_ij=max(0,-a[i,j],-a[j,i])
601     * b[i,j]=-(a[i,j]+d_ij)
602     * c[i]-=d_ij
603     */
604    
605     void Paso_FCT_setLowOrderOperator(Paso_TransportProblem * fc) {
606    
607     dim_t i;
608     index_t iptr_ij, iptr_ji;
609     const index_t* main_iptr=Paso_TransportProblem_borrowMainDiagonalPointer(fc);
610    
611 caltinay 4836 if (!fc->iteration_matrix.get()) {
612     fc->iteration_matrix.reset(new paso::SystemMatrix(
613     fc->transport_matrix->type, fc->transport_matrix->pattern,
614     fc->transport_matrix->row_block_size,
615     fc->transport_matrix->col_block_size, true));
616 gross 3793 }
617    
618     if (Esys_noError()) {
619 caltinay 4818 paso::const_SystemMatrixPattern_ptr pattern(fc->iteration_matrix->pattern);
620 caltinay 4836 const dim_t n = fc->iteration_matrix->getTotalNumRows();
621 gross 3793 #pragma omp parallel for private(i, iptr_ij, iptr_ji) schedule(static)
622     for (i = 0; i < n; ++i) {
623     double sum=fc->transport_matrix->mainBlock->val[main_iptr[i]];
624    
625     /* printf("sum[%d] = %e -> ", i, sum); */
626     /* look at a[i,j] */
627     for (iptr_ij=pattern->mainPattern->ptr[i];iptr_ij<pattern->mainPattern->ptr[i+1]; ++iptr_ij) {
628 caltinay 3795 const index_t j = pattern->mainPattern->index[iptr_ij];
629     const double rtmp1 = fc->transport_matrix->mainBlock->val[iptr_ij];
630 gross 3793 if (j!=i) {
631     /* find entry a[j,i] */
632     #pragma ivdep
633     for (iptr_ji=pattern->mainPattern->ptr[j]; iptr_ji<pattern->mainPattern->ptr[j+1]; ++iptr_ji) {
634    
635     if ( pattern->mainPattern->index[iptr_ji] == i) {
636 caltinay 3795 const double rtmp2=fc->transport_matrix->mainBlock->val[iptr_ji];
637 gross 3793 /*
638     printf("a[%d,%d]=%e\n",i,j,rtmp1);
639     printf("a[%d,%d]=%e\n",j,i,rtmp2);
640     */
641    
642 caltinay 3795 const double d_ij=-MIN3(0.,rtmp1,rtmp2);
643 gross 3793 fc->iteration_matrix->mainBlock->val[iptr_ij]=-(rtmp1+d_ij);
644     /* printf("l[%d,%d]=%e\n",i,j,fc->iteration_matrix->mainBlock->val[iptr_ij]); */
645     sum-=d_ij;
646     break;
647     }
648     }
649     }
650     }
651     for (iptr_ij=pattern->col_couplePattern->ptr[i];iptr_ij<pattern->col_couplePattern->ptr[i+1]; ++iptr_ij) {
652 caltinay 3795 const index_t j = pattern->col_couplePattern->index[iptr_ij];
653     const double rtmp1 = fc->transport_matrix->col_coupleBlock->val[iptr_ij];
654 gross 3793 /* find entry a[j,i] */
655     #pragma ivdep
656     for (iptr_ji=pattern->row_couplePattern->ptr[j]; iptr_ji<pattern->row_couplePattern->ptr[j+1]; ++iptr_ji) {
657     if (pattern->row_couplePattern->index[iptr_ji]==i) {
658 caltinay 3795 const double rtmp2=fc->transport_matrix->row_coupleBlock->val[iptr_ji];
659     const double d_ij=-MIN3(0.,rtmp1,rtmp2);
660 gross 3793 fc->iteration_matrix->col_coupleBlock->val[iptr_ij]=-(rtmp1+d_ij);
661     fc->iteration_matrix->row_coupleBlock->val[iptr_ji]=-(rtmp2+d_ij);
662     sum-=d_ij;
663     break;
664     }
665     }
666     }
667     /* set main diagonal entry */
668     fc->main_diagonal_low_order_transport_matrix[i]=sum;
669     /* printf("%e \n", sum); */
670     }
671    
672     }
673     }
674    
675     /*
676 gross 3822 * out_i=m_i u_i + a * \sum_{j <> i} l_{ij} (u_j-u_i) where m_i>0
677     * = u_i where m_i<=0
678 gross 3793 *
679     */
680     void Paso_FCT_Solver_setMuPaLu(double* out,
681 caltinay 4817 const double* M,
682     paso::const_Coupler_ptr u_coupler,
683     const double a,
684 caltinay 4836 paso::const_SystemMatrix_ptr L)
685 gross 3793 {
686     dim_t i;
687 caltinay 4818 paso::const_SystemMatrixPattern_ptr pattern(L->pattern);
688 caltinay 4817 const double *u = u_coupler->borrowLocalData();
689     const double *remote_u = u_coupler->borrowRemoteData();
690 caltinay 3795 index_t iptr_ij;
691 caltinay 4836 const dim_t n = L->getTotalNumRows();
692 gross 3793
693     #pragma omp parallel for private(i) schedule(static)
694     for (i = 0; i < n; ++i) {
695 gross 3822 if ( M[i] > 0.) {
696     out[i]=M[i]*u[i];
697     } else {
698     out[i]=u[i];
699     }
700 gross 3793 }
701     if (ABS(a)>0) {
702     #pragma omp parallel for schedule(static) private(i, iptr_ij)
703     for (i = 0; i < n; ++i) {
704 gross 3823 if ( M[i] > 0.) {
705 caltinay 3795 double sum=0;
706     const double u_i=u[i];
707 gross 3793 #pragma ivdep
708     for (iptr_ij=(pattern->mainPattern->ptr[i]);iptr_ij<pattern->mainPattern->ptr[i+1]; ++iptr_ij) {
709     const index_t j=pattern->mainPattern->index[iptr_ij];
710 caltinay 3795 const double l_ij=L->mainBlock->val[iptr_ij];
711 gross 3793 sum+=l_ij*(u[j]-u_i);
712    
713     }
714     #pragma ivdep
715     for (iptr_ij=(pattern->col_couplePattern->ptr[i]);iptr_ij<pattern->col_couplePattern->ptr[i+1]; ++iptr_ij) {
716     const index_t j=pattern->col_couplePattern->index[iptr_ij];
717 caltinay 3795 const double l_ij=L->col_coupleBlock->val[iptr_ij];
718 gross 3793 sum+=l_ij*(remote_u[j]-u_i);
719     }
720     out[i]+=a*sum;
721 gross 3823 }
722 gross 3793 }
723     }
724     }
725    
726 jfenwick 3981 /* ************************************************************************************************************************************************* */
727 gross 3793
728    
729    
730    
731    
732    
733    

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