@@ -110,104 +110,47 @@ int main(int argc, char **argv) {
110110 }
111111 PetscCall ( DMSetVecType (dm , vec_type ) );
112112 }
113- // ---------------------------------------------------------------------------
114- // Create global, local solution, local rhs vector
115- // ---------------------------------------------------------------------------
116- Vec U_g , U_loc ;
117- PetscInt U_l_size , U_g_size , U_loc_size ;
118- // Create global and local solution vectors
119- PetscCall ( DMCreateGlobalVector (dm , & U_g ) );
120- PetscCall ( VecGetSize (U_g , & U_g_size ) );
121- // Local size for matShell
122- PetscCall ( VecGetLocalSize (U_g , & U_l_size ) );
123- // Create local unknown vector U_loc
124- PetscCall ( DMCreateLocalVector (dm , & U_loc ) );
125- // Local size for libCEED
126- PetscCall ( VecGetSize (U_loc , & U_loc_size ) );
127113
128- // Get RHS vector
114+ // ---------------------------------------------------------------------------
115+ // Create local RHS vector
116+ // ---------------------------------------------------------------------------
129117 Vec rhs_loc ;
118+ PetscInt rhs_loc_size ;
130119 PetscScalar * r ;
131120 CeedVector rhs_ceed , target ;
132121 PetscMemType mem_type ;
133- PetscCall ( VecDuplicate (U_loc , & rhs_loc ) );
122+ PetscCall ( DMCreateLocalVector (dm , & rhs_loc ) );
123+ // Local size for libCEED
124+ PetscCall ( VecGetSize (rhs_loc , & rhs_loc_size ) );
134125 PetscCall ( VecZeroEntries (rhs_loc ) );
135126 PetscCall ( VecGetArrayAndMemType (rhs_loc , & r , & mem_type ) );
136- CeedVectorCreate (ceed , U_l_size , & rhs_ceed );
127+ CeedVectorCreate (ceed , rhs_loc_size , & rhs_ceed );
137128 CeedVectorSetArray (rhs_ceed , MemTypeP2C (mem_type ), CEED_USE_POINTER , r );
138129
139130 // ---------------------------------------------------------------------------
140- // Setup libCEED
131+ // Setup libCEED - Compute local rhs and true solution (target)
141132 // ---------------------------------------------------------------------------
142133 // -- Set up libCEED objects
143- PetscCall ( SetupLibceed (dm , ceed , app_ctx , problem_data , U_g_size ,
144- U_loc_size , ceed_data , rhs_ceed , & target ) );
134+ PetscCall ( SetupLibceed (dm , ceed , app_ctx , problem_data ,
135+ rhs_loc_size , ceed_data , rhs_ceed , & target ) );
145136 //CeedVectorView(rhs_ceed, "%12.8f", stdout);
137+
146138 // ---------------------------------------------------------------------------
147- // Gather RHS
139+ // Create global rhs
148140 // ---------------------------------------------------------------------------
149141 Vec rhs ;
150142 CeedVectorTakeArray (rhs_ceed , MemTypeP2C (mem_type ), NULL );
151143 PetscCall ( VecRestoreArrayAndMemType (rhs_loc , & r ) );
152- PetscCall ( VecDuplicate ( U_g , & rhs ) );
144+ PetscCall ( DMCreateGlobalVector ( dm , & rhs ) );
153145 PetscCall ( VecZeroEntries (rhs ) );
154146 PetscCall ( DMLocalToGlobal (dm , rhs_loc , ADD_VALUES , rhs ) );
155- // ---------------------------------------------------------------------------
156- // Setup Mat, KSP
157- // ---------------------------------------------------------------------------
158- user -> comm = comm ;
159- user -> dm = dm ;
160- user -> X_loc = U_loc ;
161- PetscCall ( VecDuplicate (U_loc , & user -> Y_loc ) );
162- user -> x_ceed = ceed_data -> x_ceed ;
163- user -> y_ceed = ceed_data -> y_ceed ;
164- user -> op_apply = ceed_data -> op_residual ;
165- user -> op_error = ceed_data -> op_error ;
166- user -> elem_restr_u = ceed_data -> elem_restr_u ;
167- user -> ceed = ceed ;
168- // Operator
169- Mat mat ;
170- PetscCall ( MatCreateShell (comm , U_l_size , U_l_size , U_g_size , U_g_size ,
171- user , & mat ) );
172- PetscCall ( MatShellSetOperation (mat , MATOP_MULT ,
173- (void (* )(void ))MatMult_Ceed ) );
174- PetscCall ( MatShellSetVecType (mat , vec_type ) );
175-
176- KSP ksp ;
177- PetscCall ( KSPCreate (comm , & ksp ) );
178- PetscCall ( KSPSetOperators (ksp , mat , mat ) );
179- PetscCall ( KSPSetFromOptions (ksp ) );
180- PetscCall ( KSPSetUp (ksp ) );
181- PetscCall ( KSPSolve (ksp , rhs , U_g ) );
182- //VecView(U_g, PETSC_VIEWER_STDOUT_WORLD);
183- // ---------------------------------------------------------------------------
184- // Compute pointwise L2 error
185- // ---------------------------------------------------------------------------
186- CeedScalar l2_error_u , l2_error_p ;
187- PetscCall ( ComputeError (user , U_g , target ,
188- & l2_error_u , & l2_error_p ) );
189147
190148 // ---------------------------------------------------------------------------
191- // Output results
149+ // Solve PDE
192150 // ---------------------------------------------------------------------------
193- KSPType ksp_type ;
194- KSPConvergedReason reason ;
195- PetscReal rnorm ;
196- PetscInt its ;
197- PetscCall ( KSPGetType (ksp , & ksp_type ) );
198- PetscCall ( KSPGetConvergedReason (ksp , & reason ) );
199- PetscCall ( KSPGetIterationNumber (ksp , & its ) );
200- PetscCall ( KSPGetResidualNorm (ksp , & rnorm ) );
201- PetscCall ( PetscPrintf (comm ,
202- " KSP:\n"
203- " KSP Type : %s\n"
204- " KSP Convergence : %s\n"
205- " Total KSP Iterations : %" PetscInt_FMT "\n"
206- " Final rnorm : %e\n"
207- " L2 Error of u and p : %e, %e\n" ,
208- ksp_type , KSPConvergedReasons [reason ], its ,
209- (double )rnorm , (double )l2_error_u ,
210- (double )l2_error_p ) );
151+ Vec U_g ;
152+ PDESolver (comm , dm , ceed , ceed_data , user , vec_type , target , rhs , & U_g );
153+ //VecView(U_g, PETSC_VIEWER_STDOUT_WORLD);
211154
212155 // ---------------------------------------------------------------------------
213156 // Save solution (paraview)
@@ -225,12 +168,8 @@ int main(int argc, char **argv) {
225168 // Free PETSc objects
226169 PetscCall ( DMDestroy (& dm ) );
227170 PetscCall ( VecDestroy (& U_g ) );
228- PetscCall ( VecDestroy (& U_loc ) );
229171 PetscCall ( VecDestroy (& rhs ) );
230172 PetscCall ( VecDestroy (& rhs_loc ) );
231- PetscCall ( VecDestroy (& user -> Y_loc ) );
232- PetscCall ( MatDestroy (& mat ) );
233- PetscCall ( KSPDestroy (& ksp ) );
234173
235174 // -- Function list
236175 PetscCall ( PetscFunctionListDestroy (& app_ctx -> problems ) );
0 commit comments