Skip to content

Commit

Permalink
Moved everything to left and deleted qfunction for rhs
Browse files Browse the repository at this point in the history
  • Loading branch information
rezgarshakeri committed Jul 19, 2022
1 parent 5a5d3c0 commit abcf86e
Show file tree
Hide file tree
Showing 26 changed files with 1,101 additions and 550 deletions.
1 change: 1 addition & 0 deletions examples/Hdiv-mixed/include/register-problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ PetscErrorCode Hdiv_DARCY3D(Ceed ceed, ProblemData problem_data, void *ctx);
// 3) darcy3dprism

// 4) richard
PetscErrorCode Hdiv_RICHARD2D(Ceed ceed, ProblemData problem_data, void *ctx);

extern int FreeContextPetsc(void *);

Expand Down
3 changes: 1 addition & 2 deletions examples/Hdiv-mixed/include/setup-libceed.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ PetscErrorCode CreateRestrictionFromPlexOriented(Ceed ceed, DM dm, CeedInt P,
// Set up libCEED for a given degree
PetscErrorCode SetupLibceed(DM dm, Ceed ceed, AppCtx app_ctx,
ProblemData problem_data,
PetscInt rhs_loc_size, CeedData ceed_data,
CeedVector rhs_ceed, CeedVector *target);
PetscInt U_loc_size, CeedData ceed_data);
#endif // setuplibceed_h
4 changes: 1 addition & 3 deletions examples/Hdiv-mixed/include/setup-solvers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ PetscErrorCode ApplyJacobian(Mat A, Vec X, Vec Y);
PetscErrorCode SNESFormResidual(SNES snes, Vec X, Vec Y, void *ctx);
PetscErrorCode SNESFormJacobian(SNES snes, Vec U, Mat J, Mat J_pre, void *ctx);
PetscErrorCode PDESolver(MPI_Comm comm, DM dm, Ceed ceed, CeedData ceed_data,
VecType vec_type, SNES snes, KSP ksp,
Vec F, Vec *U_g);
VecType vec_type, SNES snes, KSP ksp, Vec *U);
PetscErrorCode ComputeL2Error(DM dm, Ceed ceed, CeedData ceed_data, Vec U,
CeedVector target,
CeedScalar *l2_error_u, CeedScalar *l2_error_p);
PetscErrorCode PrintOutput(Ceed ceed,
CeedMemType mem_type_backend,
Expand Down
17 changes: 17 additions & 0 deletions examples/Hdiv-mixed/include/setup-ts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef setup_ts_h
#define setup_ts_h

#include <ceed.h>
#include <petsc.h>

#include "structs.h"

PetscErrorCode CreateInitialConditions(DM dm, CeedData ceed_data, Vec *U0);
PetscErrorCode SetupResidualOperatorCtx_Ut(DM dm, Ceed ceed, CeedData ceed_data,
OperatorApplyContext ctx_residual_ut);
PetscErrorCode TSFormIResidual(TS ts, PetscReal time, Vec X, Vec X_t, Vec Y,
void *ctx_residual_ut);
PetscErrorCode TSSolveRichard(DM dm, CeedData ceed_data, AppCtx app_ctx,
Vec *U, PetscScalar *f_time, TS *ts);

#endif // setup_ts_h
23 changes: 12 additions & 11 deletions examples/Hdiv-mixed/include/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ struct Physics_ {
typedef struct OperatorApplyContext_ *OperatorApplyContext;
struct OperatorApplyContext_ {
MPI_Comm comm;
Vec X_loc, Y_loc;
CeedVector x_ceed, y_ceed;
Vec X_loc, Y_loc, X_t_loc;
CeedVector x_ceed, y_ceed, x_t_ceed;
CeedOperator op_apply;
DM dm;
Ceed ceed;
Expand All @@ -79,23 +79,24 @@ typedef struct CeedData_ *CeedData;
struct CeedData_ {
CeedBasis basis_x, basis_u, basis_p, basis_u_face;
CeedElemRestriction elem_restr_x, elem_restr_u, elem_restr_U_i,
elem_restr_p;
CeedQFunction qf_residual, qf_jacobian, qf_error;
CeedOperator op_residual, op_jacobian, op_error;
CeedVector x_ceed, y_ceed, x_coord;
OperatorApplyContext ctx_residual, ctx_jacobian, ctx_error;
elem_restr_p, elem_restr_p_i;
CeedQFunction qf_residual, qf_jacobian, qf_error, qf_ics;
CeedOperator op_residual, op_jacobian, op_error, op_ics;
CeedVector x_ceed, y_ceed, x_coord, U0_ceed, x_t_ceed;
OperatorApplyContext ctx_residual, ctx_jacobian, ctx_error, ctx_residual_ut;
};

// Problem specific data
typedef struct ProblemData_ *ProblemData;
struct ProblemData_ {
CeedQFunctionUser force, residual, jacobian, error,
setup_true, bc_pressure;
const char *force_loc, *residual_loc, *jacobian_loc,
*error_loc, *setup_true_loc, *bc_pressure_loc;
CeedQFunctionUser true_solution, residual, jacobian, error, ics,
bc_pressure;
const char *true_solution_loc, *residual_loc, *jacobian_loc,
*error_loc, *bc_pressure_loc, *ics_loc;
CeedQuadMode quadrature_mode;
CeedInt elem_node, dim, q_data_size_face;
CeedQFunctionContext qfunction_context;
PetscBool has_ts;
};

#endif // structs_h
63 changes: 25 additions & 38 deletions examples/Hdiv-mixed/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ int main(int argc, char **argv) {
Physics phys_ctx;
PetscCall( PetscCalloc1(1, &phys_ctx) );

OperatorApplyContext op_apply_ctx;
PetscCall( PetscCalloc1(1, &op_apply_ctx) );

// ---------------------------------------------------------------------------
// Process command line options
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -119,39 +116,35 @@ int main(int argc, char **argv) {
// ---------------------------------------------------------------------------
// Create local Force vector
// ---------------------------------------------------------------------------
Vec F_loc;
PetscInt F_loc_size;
CeedScalar *f;
CeedVector force_ceed, target, bc_pressure;
PetscMemType force_mem_type;
PetscCall( DMCreateLocalVector(dm, &F_loc) );
Vec U_loc;
PetscInt U_loc_size;
//CeedVector bc_pressure;
PetscCall( DMCreateLocalVector(dm, &U_loc) );
// Local size for libCEED
PetscCall( VecGetSize(F_loc, &F_loc_size) );
PetscCall( VecZeroEntries(F_loc) );
PetscCall( VecGetArrayAndMemType(F_loc, &f, &force_mem_type) );
CeedVectorCreate(ceed, F_loc_size, &force_ceed);
CeedVectorSetArray(force_ceed, MemTypeP2C(force_mem_type), CEED_USE_POINTER, f);
CeedVectorCreate(ceed, F_loc_size, &bc_pressure);
CeedVectorSetArray(bc_pressure, MemTypeP2C(force_mem_type), CEED_USE_POINTER,
f);
PetscCall( VecGetSize(U_loc, &U_loc_size) );

// ---------------------------------------------------------------------------
// Setup libCEED - Compute local F and true solution (target)
// Setup libCEED
// ---------------------------------------------------------------------------
// -- Set up libCEED objects
PetscCall( SetupLibceed(dm, ceed, app_ctx, problem_data,
F_loc_size, ceed_data, force_ceed, &target) );
U_loc_size, ceed_data) );
//CeedVectorView(force_ceed, "%12.8f", stdout);
PetscCall( DMAddBoundariesPressure(ceed, ceed_data, app_ctx, problem_data, dm,
bc_pressure) );
//PetscCall( DMAddBoundariesPressure(ceed, ceed_data, app_ctx, problem_data, dm,
// bc_pressure) );


// ---------------------------------------------------------------------------
// Create global F
// Setup TSSolve for Richard problem
// ---------------------------------------------------------------------------
Vec F;
CeedVectorTakeArray(force_ceed, MemTypeP2C(force_mem_type), NULL);
PetscCall( VecRestoreArrayAndMemType(F_loc, &f) );
PetscCall( DMCreateGlobalVector(dm, &F) );
PetscCall( VecZeroEntries(F) );
PetscCall( DMLocalToGlobal(dm, F_loc, ADD_VALUES, F) );
if (problem_data->has_ts) {
// ---------------------------------------------------------------------------
// Create global initial conditions
// ---------------------------------------------------------------------------
Vec U0;
CreateInitialConditions(dm, ceed_data, &U0);
PetscCall( VecDestroy(&U0) );
}

// ---------------------------------------------------------------------------
// Solve PDE
Expand All @@ -162,14 +155,14 @@ int main(int argc, char **argv) {
Vec U;
PetscCall( SNESCreate(comm, &snes) );
PetscCall( SNESGetKSP(snes, &ksp) );
PetscCall( PDESolver(comm, dm, ceed, ceed_data, vec_type, snes, ksp, F, &U) );
PetscCall( PDESolver(comm, dm, ceed, ceed_data, vec_type, snes, ksp, &U) );
//VecView(U, PETSC_VIEWER_STDOUT_WORLD);

// ---------------------------------------------------------------------------
// Compute L2 error of mms problem
// ---------------------------------------------------------------------------
CeedScalar l2_error_u, l2_error_p;
PetscCall( ComputeL2Error(dm, ceed,ceed_data, U, target, &l2_error_u,
PetscCall( ComputeL2Error(dm, ceed,ceed_data, U, &l2_error_u,
&l2_error_p) );

// ---------------------------------------------------------------------------
Expand All @@ -194,11 +187,8 @@ int main(int argc, char **argv) {
// Free PETSc objects
PetscCall( DMDestroy(&dm) );
PetscCall( VecDestroy(&U) );
PetscCall( VecDestroy(&F) );
PetscCall( VecDestroy(&F_loc) );
PetscCall( VecDestroy(&U_loc) );
PetscCall( SNESDestroy(&snes) );
PetscCall( VecDestroy(&op_apply_ctx->Y_loc) );
PetscCall( VecDestroy(&op_apply_ctx->X_loc) );

// -- Function list
PetscCall( PetscFunctionListDestroy(&app_ctx->problems) );
Expand All @@ -207,12 +197,9 @@ int main(int argc, char **argv) {
PetscCall( PetscFree(app_ctx) );
PetscCall( PetscFree(problem_data) );
PetscCall( PetscFree(phys_ctx) );
PetscCall( PetscFree(op_apply_ctx) );

// Free libCEED objects
CeedVectorDestroy(&force_ceed);
CeedVectorDestroy(&bc_pressure);
CeedVectorDestroy(&target);
//CeedVectorDestroy(&bc_pressure);
PetscCall( CeedDataDestroy(ceed_data) );
CeedDestroy(&ceed);

Expand Down
1 change: 1 addition & 0 deletions examples/Hdiv-mixed/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
#include "include/register-problem.h"
#include "include/setup-matops.h"
#include "include/setup-solvers.h"
#include "include/setup-ts.h"

#endif // MAIN_H
16 changes: 11 additions & 5 deletions examples/Hdiv-mixed/problems/darcy2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/// Utility functions for setting up Darcy problem in 2D

#include "../include/register-problem.h"
#include "../qfunctions/darcy-force2d.h"
#include "../qfunctions/darcy-true2d.h"
#include "../qfunctions/darcy-system2d.h"
#include "../qfunctions/darcy-error2d.h"
#include "../qfunctions/pressure-boundary2d.h"
Expand All @@ -39,8 +39,8 @@ PetscErrorCode Hdiv_DARCY2D(Ceed ceed, ProblemData problem_data, void *ctx) {
problem_data->elem_node = 4;
problem_data->q_data_size_face = 3;
problem_data->quadrature_mode = CEED_GAUSS;
problem_data->force = DarcyForce2D;
problem_data->force_loc = DarcyForce2D_loc;
problem_data->true_solution = DarcyTrue2D;
problem_data->true_solution_loc = DarcyTrue2D_loc;
problem_data->residual = DarcySystem2D;
problem_data->residual_loc = DarcySystem2D_loc;
problem_data->jacobian = JacobianDarcySystem2D;
Expand All @@ -49,23 +49,29 @@ PetscErrorCode Hdiv_DARCY2D(Ceed ceed, ProblemData problem_data, void *ctx) {
problem_data->error_loc = DarcyError2D_loc;
problem_data->bc_pressure = BCPressure2D;
problem_data->bc_pressure_loc = BCPressure2D_loc;
problem_data->has_ts = PETSC_FALSE;

// ------------------------------------------------------
// Command line Options
// ------------------------------------------------------
CeedScalar kappa = 1.;
CeedScalar kappa = 1., rho_a0 = 998.2, g = 9.8;
PetscOptionsBegin(app_ctx->comm, NULL, "Options for Hdiv-mixed problem", NULL);
PetscCall( PetscOptionsScalar("-kappa", "Hydraulic Conductivity", NULL,
kappa, &kappa, NULL));
PetscCall( PetscOptionsScalar("-rho_a0", "Density at p0", NULL,
rho_a0, &rho_a0, NULL));
PetscOptionsEnd();

darcy_ctx->kappa = kappa;
darcy_ctx->rho_a0 = rho_a0;
darcy_ctx->g = g;

CeedQFunctionContextCreate(ceed, &darcy_context);
CeedQFunctionContextSetData(darcy_context, CEED_MEM_HOST, CEED_COPY_VALUES,
sizeof(*darcy_ctx), darcy_ctx);
problem_data->qfunction_context = darcy_context;
CeedQFunctionContextSetDataDestroy(darcy_context, CEED_MEM_HOST,
FreeContextPetsc);
//PetscCall( PetscFree(darcy_ctx) );
PetscCall( PetscFree(darcy_ctx) );
PetscFunctionReturn(0);
}
15 changes: 10 additions & 5 deletions examples/Hdiv-mixed/problems/darcy3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/// Utility functions for setting up Darcy problem in 3D

#include "../include/register-problem.h"
#include "../qfunctions/darcy-force3d.h"
#include "../qfunctions/darcy-true3d.h"
#include "../qfunctions/darcy-system3d.h"
#include "../qfunctions/darcy-error3d.h"
#include "../qfunctions/pressure-boundary3d.h"
Expand All @@ -39,8 +39,8 @@ PetscErrorCode Hdiv_DARCY3D(Ceed ceed, ProblemData problem_data, void *ctx) {
problem_data->elem_node = 8;
problem_data->q_data_size_face = 4;
problem_data->quadrature_mode = CEED_GAUSS;
problem_data->force = DarcyForce3D;
problem_data->force_loc = DarcyForce3D_loc;
problem_data->true_solution = DarcyTrue3D;
problem_data->true_solution_loc = DarcyTrue3D_loc;
problem_data->residual = DarcySystem3D;
problem_data->residual_loc = DarcySystem3D_loc;
problem_data->jacobian = JacobianDarcySystem3D;
Expand All @@ -49,23 +49,28 @@ PetscErrorCode Hdiv_DARCY3D(Ceed ceed, ProblemData problem_data, void *ctx) {
problem_data->error_loc = DarcyError3D_loc;
problem_data->bc_pressure = BCPressure3D;
problem_data->bc_pressure_loc = BCPressure3D_loc;
problem_data->has_ts = PETSC_FALSE;

// ------------------------------------------------------
// Command line Options
// ------------------------------------------------------
CeedScalar kappa = 1.;
CeedScalar kappa = 1., rho_a0 = 998.2, g = 9.8;
PetscOptionsBegin(app_ctx->comm, NULL, "Options for Hdiv-mixed problem", NULL);
PetscCall( PetscOptionsScalar("-kappa", "Hydraulic Conductivity", NULL,
kappa, &kappa, NULL));
PetscCall( PetscOptionsScalar("-rho_a0", "Density at p0", NULL,
rho_a0, &rho_a0, NULL));
PetscOptionsEnd();

darcy_ctx->kappa = kappa;
darcy_ctx->rho_a0 = rho_a0;
darcy_ctx->g = g;
CeedQFunctionContextCreate(ceed, &darcy_context);
CeedQFunctionContextSetData(darcy_context, CEED_MEM_HOST, CEED_COPY_VALUES,
sizeof(*darcy_ctx), darcy_ctx);
problem_data->qfunction_context = darcy_context;
CeedQFunctionContextSetDataDestroy(darcy_context, CEED_MEM_HOST,
FreeContextPetsc);

PetscCall( PetscFree(darcy_ctx) );
PetscFunctionReturn(0);
}
3 changes: 2 additions & 1 deletion examples/Hdiv-mixed/problems/register-problem.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ PetscErrorCode RegisterProblems_Hdiv(AppCtx app_ctx) {
// 3) darcy3d-prism

// 4) richard

PetscCall( PetscFunctionListAdd(&app_ctx->problems, "richard2d",
Hdiv_RICHARD2D) );
PetscFunctionReturn(0);
}

Expand Down
Loading

0 comments on commit abcf86e

Please sign in to comment.