@@ -73,18 +73,32 @@ int main(int argc, char **argv) {
7373 CeedData ceed_data ;
7474 PetscCall ( PetscCalloc1 (1 , & ceed_data ) );
7575
76- OperatorApplyContext ctx_residual_ut , ctx_initial_u0 , ctx_initial_p0 ,
77- ctx_post_Hdiv , ctx_post_H1 ;
76+ OperatorApplyContext ctx_jacobian , ctx_residual , ctx_residual_ut ,
77+ ctx_initial_u0 , ctx_initial_p0 ,
78+ ctx_error , ctx_Hdiv , ctx_H1 ;
79+ PetscCall ( PetscCalloc1 (1 , & ctx_jacobian ) );
80+ PetscCall ( PetscCalloc1 (1 , & ctx_residual ) );
7881 PetscCall ( PetscCalloc1 (1 , & ctx_residual_ut ) );
7982 PetscCall ( PetscCalloc1 (1 , & ctx_initial_u0 ) );
8083 PetscCall ( PetscCalloc1 (1 , & ctx_initial_p0 ) );
81- PetscCall ( PetscCalloc1 (1 , & ctx_post_Hdiv ) );
82- PetscCall ( PetscCalloc1 (1 , & ctx_post_H1 ) );
83- ceed_data -> ctx_residual_ut = ctx_residual_ut ;
84- ceed_data -> ctx_initial_u0 = ctx_initial_u0 ;
85- ceed_data -> ctx_initial_p0 = ctx_initial_p0 ;
86- ceed_data -> ctx_post_Hdiv = ctx_post_Hdiv ;
87- ceed_data -> ctx_post_H1 = ctx_post_H1 ;
84+ PetscCall ( PetscCalloc1 (1 , & ctx_error ) );
85+ PetscCall ( PetscCalloc1 (1 , & ctx_Hdiv ) );
86+ PetscCall ( PetscCalloc1 (1 , & ctx_H1 ) );
87+ // Context for Richards problem
88+ app_ctx -> ctx_residual = ctx_residual ;
89+ app_ctx -> ctx_jacobian = ctx_jacobian ;
90+ // Context for Richards problem
91+ app_ctx -> ctx_residual_ut = ctx_residual_ut ;
92+ // Context for initial velocity
93+ app_ctx -> ctx_initial_u0 = ctx_initial_u0 ;
94+ // Context for initial pressure
95+ app_ctx -> ctx_initial_p0 = ctx_initial_p0 ;
96+ // Context for MMS
97+ app_ctx -> ctx_error = ctx_error ;
98+ // Context for post-processing
99+ app_ctx -> ctx_Hdiv = ctx_Hdiv ;
100+ app_ctx -> ctx_H1 = ctx_H1 ;
101+
88102 // ---------------------------------------------------------------------------
89103 // Process command line options
90104 // ---------------------------------------------------------------------------
@@ -112,23 +126,28 @@ int main(int argc, char **argv) {
112126 // Create DM
113127 // ---------------------------------------------------------------------------
114128 DM dm , dm_u0 , dm_p0 , dm_H1 ;
129+ // DM for mixed problem
115130 PetscCall ( CreateDM (comm , vec_type , & dm ) );
131+ // DM for projecting initial velocity to Hdiv space
116132 PetscCall ( CreateDM (comm , vec_type , & dm_u0 ) );
133+ // DM for projecting initial pressure in L2
117134 PetscCall ( CreateDM (comm , vec_type , & dm_p0 ) );
135+ // DM for projecting solution U into H1 space for PetscViewer
118136 PetscCall ( CreateDM (comm , vec_type , & dm_H1 ) );
119137 // TODO: add mesh option
120138 // perturb to have smooth random mesh
121139 //PetscCall( PerturbVerticesSmooth(dm) );
122140
123141 // ---------------------------------------------------------------------------
124- // Setup FE
142+ // Setup FE for H(div) mixed-problem and H1 projection in post-processing.c
125143 // ---------------------------------------------------------------------------
126- SetupFEHdiv (comm , dm , dm_u0 , dm_p0 );
127- SetupFEH1 (problem_data , app_ctx , dm_H1 );
144+ PetscCall ( SetupFEHdiv (comm , dm , dm_u0 , dm_p0 ) );
145+ PetscCall ( SetupFEH1 (problem_data , app_ctx , dm_H1 ) );
146+
128147 // ---------------------------------------------------------------------------
129- // Create local Force vector
148+ // Create global unkown solution
130149 // ---------------------------------------------------------------------------
131- Vec U ; // U=[p,u], U0=u0
150+ Vec U ; // U=[p,u]
132151 PetscCall ( DMCreateGlobalVector (dm , & U ) );
133152
134153 // ---------------------------------------------------------------------------
@@ -141,86 +160,89 @@ int main(int argc, char **argv) {
141160 //PetscCall( DMAddBoundariesPressure(ceed, ceed_data, app_ctx, problem_data, dm,
142161 // bc_pressure) );
143162
163+ // ---------------------------------------------------------------------------
164+ // Setup context for projection problem; post-processing.c
165+ // ---------------------------------------------------------------------------
166+ PetscCall ( SetupProjectVelocityCtx_Hdiv (comm , dm , ceed , ceed_data ,
167+ app_ctx -> ctx_Hdiv ) );
168+ PetscCall ( SetupProjectVelocityCtx_H1 (comm , dm_H1 , ceed , ceed_data ,
169+ vec_type , app_ctx -> ctx_H1 ) );
170+
144171 // ---------------------------------------------------------------------------
145172 // Setup TSSolve for Richard problem
146173 // ---------------------------------------------------------------------------
147174 TS ts ;
148175 if (problem_data -> has_ts ) {
149176 // ---------------------------------------------------------------------------
150- // Create global initial conditions
177+ // Setup context for initial conditions
151178 // ---------------------------------------------------------------------------
152- SetupResidualOperatorCtx_U0 (comm , dm_u0 , ceed , ceed_data ,
153- ceed_data -> ctx_initial_u0 );
154- SetupResidualOperatorCtx_P0 (comm , dm_p0 , ceed , ceed_data ,
155- ceed_data -> ctx_initial_p0 );
156- SetupResidualOperatorCtx_Ut (comm , dm , ceed , ceed_data ,
157- ceed_data -> ctx_residual_ut );
158- CreateInitialConditions (ceed_data , U , vec_type ,
159- ceed_data -> ctx_initial_u0 ,
160- ceed_data -> ctx_initial_p0 ,
161- ceed_data -> ctx_residual_ut );
179+ PetscCall ( SetupResidualOperatorCtx_U0 (comm , dm_u0 , ceed , ceed_data ,
180+ app_ctx -> ctx_initial_u0 ) );
181+ PetscCall ( SetupResidualOperatorCtx_P0 (comm , dm_p0 , ceed , ceed_data ,
182+ app_ctx -> ctx_initial_p0 ) );
183+ PetscCall ( SetupResidualOperatorCtx_Ut (comm , dm , ceed , ceed_data ,
184+ app_ctx -> ctx_residual_ut ) );
185+ PetscCall ( CreateInitialConditions (ceed_data , app_ctx , vec_type , U ) );
162186 //VecView(U, PETSC_VIEWER_STDOUT_WORLD);
163187 // Solve Richards problem
164- PetscCall ( VecZeroEntries ( ceed_data -> ctx_residual_ut -> X_loc ) );
165- PetscCall ( VecZeroEntries (ceed_data -> ctx_residual_ut -> X_t_loc ) );
166- PetscCall ( TSSolveRichard ( dm , ceed_data , app_ctx ,
167- & U , & ts ) );
188+ PetscCall ( TSCreate ( comm , & ts ) );
189+ PetscCall ( VecZeroEntries (app_ctx -> ctx_residual_ut -> X_loc ) );
190+ PetscCall ( VecZeroEntries ( app_ctx -> ctx_residual_ut -> X_t_loc ) );
191+ PetscCall ( TSSolveRichard ( ceed_data , app_ctx , ts , & U ) );
168192 //VecView(U, PETSC_VIEWER_STDOUT_WORLD);
169193 }
170194
195+ // ---------------------------------------------------------------------------
196+ // Setup SNES for Darcy problem
197+ // ---------------------------------------------------------------------------
171198 SNES snes ;
172199 KSP ksp ;
173200 if (!problem_data -> has_ts ) {
174- // ---------------------------------------------------------------------------
175- // Setup SNES for Darcy problem
176- // ---------------------------------------------------------------------------
201+ PetscCall ( SetupJacobianOperatorCtx (dm , ceed , ceed_data , vec_type ,
202+ app_ctx -> ctx_jacobian ) );
203+ PetscCall ( SetupResidualOperatorCtx (dm , ceed , ceed_data ,
204+ app_ctx -> ctx_residual ) );
177205 // Create SNES
178206 PetscCall ( SNESCreate (comm , & snes ) );
179207 PetscCall ( SNESGetKSP (snes , & ksp ) );
180- PetscCall ( PDESolver (comm , dm , ceed , ceed_data , vec_type , snes , ksp , & U ) );
208+ PetscCall ( PDESolver (ceed_data , app_ctx , snes , ksp , & U ) );
181209 //VecView(U, PETSC_VIEWER_STDOUT_WORLD);
182210 }
183211
184212 // ---------------------------------------------------------------------------
185213 // Compute L2 error of mms problem
186214 // ---------------------------------------------------------------------------
215+ PetscCall ( SetupErrorOperatorCtx (dm , ceed , ceed_data , app_ctx -> ctx_error ) );
187216 CeedScalar l2_error_u , l2_error_p ;
188- PetscCall ( ComputeL2Error (dm , ceed , ceed_data , U , & l2_error_u ,
189- & l2_error_p ) );
217+ PetscCall ( ComputeL2Error (ceed_data , app_ctx , U ,
218+ & l2_error_u , & l2_error_p ) );
190219
191220 // ---------------------------------------------------------------------------
192- // Print output results
221+ // Print solver iterations and final norms
193222 // ---------------------------------------------------------------------------
194223 PetscCall ( PrintOutput (ceed , app_ctx , problem_data -> has_ts , mem_type_backend ,
195224 ts , snes , ksp , U , l2_error_u , l2_error_p ) );
196225
197226 // ---------------------------------------------------------------------------
198227 // Save solution (paraview)
199228 // ---------------------------------------------------------------------------
200- Vec U_H1 ;
201- PetscCall ( DMCreateGlobalVector (dm_H1 , & U_H1 ) );
202- PetscCall ( VecZeroEntries (U_H1 ) );
203229 if (app_ctx -> view_solution ) {
204230 PetscViewer viewer_p ;
205- PetscCall ( PetscViewerVTKOpen (comm ,"solution_p .vtu" ,FILE_MODE_WRITE ,
231+ PetscCall ( PetscViewerVTKOpen (comm ,"darcy_pressure .vtu" ,FILE_MODE_WRITE ,
206232 & viewer_p ) );
207233 PetscCall ( VecView (U , viewer_p ) );
208234 PetscCall ( PetscViewerDestroy (& viewer_p ) );
209235
210- SetupProjectVelocityCtx_Hdiv (comm , dm , ceed , ceed_data ,
211- ceed_data -> ctx_post_Hdiv );
212- SetupProjectVelocityCtx_H1 (comm , dm_H1 , ceed , ceed_data ,
213- ceed_data -> ctx_post_H1 );
214-
215- ProjectVelocity (ceed_data , U , vec_type , & U_H1 ,
216- ceed_data -> ctx_post_Hdiv ,
217- ceed_data -> ctx_post_H1 );
236+ Vec U_H1 ; // velocity in H1 space for post-processing
237+ PetscCall ( DMCreateGlobalVector (dm_H1 , & U_H1 ) );
238+ PetscCall ( ProjectVelocity (app_ctx , U , & U_H1 ) );
218239
219240 PetscViewer viewer_u ;
220- PetscCall ( PetscViewerVTKOpen (comm ,"solution_u .vtu" ,FILE_MODE_WRITE ,
241+ PetscCall ( PetscViewerVTKOpen (comm ,"darcy_velocity .vtu" ,FILE_MODE_WRITE ,
221242 & viewer_u ) );
222243 PetscCall ( VecView (U_H1 , viewer_u ) );
223244 PetscCall ( PetscViewerDestroy (& viewer_u ) );
245+ PetscCall ( VecDestroy (& U_H1 ) );
224246 }
225247 // ---------------------------------------------------------------------------
226248 // Free objects
@@ -232,10 +254,7 @@ int main(int argc, char **argv) {
232254 PetscCall ( DMDestroy (& dm_p0 ) );
233255 PetscCall ( DMDestroy (& dm_H1 ) );
234256 PetscCall ( VecDestroy (& U ) );
235- PetscCall ( VecDestroy (& U_H1 ) );
236- PetscCall ( VecDestroy (& ceed_data -> ctx_residual_ut -> X_loc ) );
237- PetscCall ( VecDestroy (& ceed_data -> ctx_residual_ut -> X_t_loc ) );
238- PetscCall ( VecDestroy (& ceed_data -> ctx_residual_ut -> Y_loc ) );
257+ PetscCall ( CtxVecDestroy (app_ctx ) );
239258 if (problem_data -> has_ts ) {
240259 PetscCall ( TSDestroy (& ts ) );
241260 } else {
@@ -252,8 +271,11 @@ int main(int argc, char **argv) {
252271 PetscCall ( PetscFree (ctx_initial_u0 ) );
253272 PetscCall ( PetscFree (ctx_initial_p0 ) );
254273 PetscCall ( PetscFree (ctx_residual_ut ) );
255- PetscCall ( PetscFree (ctx_post_H1 ) );
256- PetscCall ( PetscFree (ctx_post_Hdiv ) );
274+ PetscCall ( PetscFree (ctx_residual ) );
275+ PetscCall ( PetscFree (ctx_jacobian ) );
276+ PetscCall ( PetscFree (ctx_error ) );
277+ PetscCall ( PetscFree (ctx_H1 ) );
278+ PetscCall ( PetscFree (ctx_Hdiv ) );
257279
258280 // Free libCEED objects
259281 //CeedVectorDestroy(&bc_pressure);
0 commit comments