@@ -73,18 +73,32 @@ int main(int argc, char **argv) {
73
73
CeedData ceed_data ;
74
74
PetscCall ( PetscCalloc1 (1 , & ceed_data ) );
75
75
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 ) );
78
81
PetscCall ( PetscCalloc1 (1 , & ctx_residual_ut ) );
79
82
PetscCall ( PetscCalloc1 (1 , & ctx_initial_u0 ) );
80
83
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
+
88
102
// ---------------------------------------------------------------------------
89
103
// Process command line options
90
104
// ---------------------------------------------------------------------------
@@ -112,23 +126,28 @@ int main(int argc, char **argv) {
112
126
// Create DM
113
127
// ---------------------------------------------------------------------------
114
128
DM dm , dm_u0 , dm_p0 , dm_H1 ;
129
+ // DM for mixed problem
115
130
PetscCall ( CreateDM (comm , vec_type , & dm ) );
131
+ // DM for projecting initial velocity to Hdiv space
116
132
PetscCall ( CreateDM (comm , vec_type , & dm_u0 ) );
133
+ // DM for projecting initial pressure in L2
117
134
PetscCall ( CreateDM (comm , vec_type , & dm_p0 ) );
135
+ // DM for projecting solution U into H1 space for PetscViewer
118
136
PetscCall ( CreateDM (comm , vec_type , & dm_H1 ) );
119
137
// TODO: add mesh option
120
138
// perturb to have smooth random mesh
121
139
//PetscCall( PerturbVerticesSmooth(dm) );
122
140
123
141
// ---------------------------------------------------------------------------
124
- // Setup FE
142
+ // Setup FE for H(div) mixed-problem and H1 projection in post-processing.c
125
143
// ---------------------------------------------------------------------------
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
+
128
147
// ---------------------------------------------------------------------------
129
- // Create local Force vector
148
+ // Create global unkown solution
130
149
// ---------------------------------------------------------------------------
131
- Vec U ; // U=[p,u], U0=u0
150
+ Vec U ; // U=[p,u]
132
151
PetscCall ( DMCreateGlobalVector (dm , & U ) );
133
152
134
153
// ---------------------------------------------------------------------------
@@ -141,86 +160,89 @@ int main(int argc, char **argv) {
141
160
//PetscCall( DMAddBoundariesPressure(ceed, ceed_data, app_ctx, problem_data, dm,
142
161
// bc_pressure) );
143
162
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
+
144
171
// ---------------------------------------------------------------------------
145
172
// Setup TSSolve for Richard problem
146
173
// ---------------------------------------------------------------------------
147
174
TS ts ;
148
175
if (problem_data -> has_ts ) {
149
176
// ---------------------------------------------------------------------------
150
- // Create global initial conditions
177
+ // Setup context for initial conditions
151
178
// ---------------------------------------------------------------------------
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 ) );
162
186
//VecView(U, PETSC_VIEWER_STDOUT_WORLD);
163
187
// 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 ) );
168
192
//VecView(U, PETSC_VIEWER_STDOUT_WORLD);
169
193
}
170
194
195
+ // ---------------------------------------------------------------------------
196
+ // Setup SNES for Darcy problem
197
+ // ---------------------------------------------------------------------------
171
198
SNES snes ;
172
199
KSP ksp ;
173
200
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 ) );
177
205
// Create SNES
178
206
PetscCall ( SNESCreate (comm , & snes ) );
179
207
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 ) );
181
209
//VecView(U, PETSC_VIEWER_STDOUT_WORLD);
182
210
}
183
211
184
212
// ---------------------------------------------------------------------------
185
213
// Compute L2 error of mms problem
186
214
// ---------------------------------------------------------------------------
215
+ PetscCall ( SetupErrorOperatorCtx (dm , ceed , ceed_data , app_ctx -> ctx_error ) );
187
216
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 ) );
190
219
191
220
// ---------------------------------------------------------------------------
192
- // Print output results
221
+ // Print solver iterations and final norms
193
222
// ---------------------------------------------------------------------------
194
223
PetscCall ( PrintOutput (ceed , app_ctx , problem_data -> has_ts , mem_type_backend ,
195
224
ts , snes , ksp , U , l2_error_u , l2_error_p ) );
196
225
197
226
// ---------------------------------------------------------------------------
198
227
// Save solution (paraview)
199
228
// ---------------------------------------------------------------------------
200
- Vec U_H1 ;
201
- PetscCall ( DMCreateGlobalVector (dm_H1 , & U_H1 ) );
202
- PetscCall ( VecZeroEntries (U_H1 ) );
203
229
if (app_ctx -> view_solution ) {
204
230
PetscViewer viewer_p ;
205
- PetscCall ( PetscViewerVTKOpen (comm ,"solution_p .vtu" ,FILE_MODE_WRITE ,
231
+ PetscCall ( PetscViewerVTKOpen (comm ,"darcy_pressure .vtu" ,FILE_MODE_WRITE ,
206
232
& viewer_p ) );
207
233
PetscCall ( VecView (U , viewer_p ) );
208
234
PetscCall ( PetscViewerDestroy (& viewer_p ) );
209
235
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 ) );
218
239
219
240
PetscViewer viewer_u ;
220
- PetscCall ( PetscViewerVTKOpen (comm ,"solution_u .vtu" ,FILE_MODE_WRITE ,
241
+ PetscCall ( PetscViewerVTKOpen (comm ,"darcy_velocity .vtu" ,FILE_MODE_WRITE ,
221
242
& viewer_u ) );
222
243
PetscCall ( VecView (U_H1 , viewer_u ) );
223
244
PetscCall ( PetscViewerDestroy (& viewer_u ) );
245
+ PetscCall ( VecDestroy (& U_H1 ) );
224
246
}
225
247
// ---------------------------------------------------------------------------
226
248
// Free objects
@@ -232,10 +254,7 @@ int main(int argc, char **argv) {
232
254
PetscCall ( DMDestroy (& dm_p0 ) );
233
255
PetscCall ( DMDestroy (& dm_H1 ) );
234
256
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 ) );
239
258
if (problem_data -> has_ts ) {
240
259
PetscCall ( TSDestroy (& ts ) );
241
260
} else {
@@ -252,8 +271,11 @@ int main(int argc, char **argv) {
252
271
PetscCall ( PetscFree (ctx_initial_u0 ) );
253
272
PetscCall ( PetscFree (ctx_initial_p0 ) );
254
273
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 ) );
257
279
258
280
// Free libCEED objects
259
281
//CeedVectorDestroy(&bc_pressure);
0 commit comments