Skip to content

Commit

Permalink
Create Initial conditions for Richards problem
Browse files Browse the repository at this point in the history
  • Loading branch information
rezgarshakeri committed Aug 10, 2022
1 parent 833e410 commit 3a8fc58
Show file tree
Hide file tree
Showing 25 changed files with 1,029 additions and 405 deletions.
2 changes: 1 addition & 1 deletion examples/Hdiv-mixed/include/setup-fe.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
// ---------------------------------------------------------------------------
// Setup FE
// ---------------------------------------------------------------------------
PetscErrorCode SetupFE(MPI_Comm comm, DM dm);
PetscErrorCode SetupFE(MPI_Comm comm, DM dm, DM dm_u0, DM dm_p0);

#endif // setupfe_h
13 changes: 8 additions & 5 deletions examples/Hdiv-mixed/include/setup-libceed.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
// Convert PETSc MemType to libCEED MemType
CeedMemType MemTypeP2C(PetscMemType mtype);
// Destroy libCEED objects
PetscErrorCode CeedDataDestroy(CeedData ceed_data);
PetscErrorCode CeedDataDestroy(CeedData ceed_data, ProblemData problem_data);
// Utility function - essential BC dofs are encoded in closure indices as -(i+1)
PetscInt Involute(PetscInt i);
// Utility function to create local CEED restriction from DMPlex
PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm,
CeedInt height, DMLabel domain_label, CeedInt value,
CeedElemRestriction *elem_restr);
// Utility function to create local CEED Oriented restriction from DMPlex
PetscErrorCode CreateRestrictionFromPlexOriented(Ceed ceed, DM dm, CeedInt P,
CeedElemRestriction *elem_restr_oriented, CeedElemRestriction *elem_restr);
PetscErrorCode CreateRestrictionFromPlexOriented(Ceed ceed, DM dm, DM dm_u0,
DM dm_p0, CeedInt P,
CeedElemRestriction *elem_restr_u, CeedElemRestriction *elem_restr_p,
CeedElemRestriction *elem_restr_u0, CeedElemRestriction *elem_restr_p0);
// Set up libCEED for a given degree
PetscErrorCode SetupLibceed(DM dm, Ceed ceed, AppCtx app_ctx,
PetscErrorCode SetupLibceed(DM dm, DM dm_u0, DM dm_p0, Ceed ceed,
AppCtx app_ctx,
ProblemData problem_data,
PetscInt U_loc_size, CeedData ceed_data);
CeedData ceed_data);
#endif // setuplibceed_h
2 changes: 1 addition & 1 deletion examples/Hdiv-mixed/include/setup-solvers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PetscErrorCode SetupResidualOperatorCtx(DM dm, Ceed ceed, CeedData ceed_data,
OperatorApplyContext ctx_residual);
PetscErrorCode SetupErrorOperatorCtx(DM dm, Ceed ceed, CeedData ceed_data,
OperatorApplyContext ctx_error);
PetscErrorCode ApplyJacobian(Mat A, Vec X, Vec Y);
PetscErrorCode ApplyMatOp(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,
Expand Down
12 changes: 11 additions & 1 deletion examples/Hdiv-mixed/include/setup-ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@

#include "structs.h"

PetscErrorCode CreateInitialConditions(DM dm, CeedData ceed_data, Vec *U0);
PetscErrorCode CreateInitialConditions(DM dm, DM dm_u0, DM dm_p0,
CeedData ceed_data,
Vec U, VecType vec_type,
OperatorApplyContext ctx_initial_u0,
OperatorApplyContext ctx_initial_p0);
PetscErrorCode SetupResidualOperatorCtx_Ut(DM dm, Ceed ceed, CeedData ceed_data,
OperatorApplyContext ctx_residual_ut);
PetscErrorCode SetupResidualOperatorCtx_U0(MPI_Comm comm, DM dm, Ceed ceed,
CeedData ceed_data,
OperatorApplyContext ctx_initial_u0);
PetscErrorCode SetupResidualOperatorCtx_P0(MPI_Comm comm, DM dm, Ceed ceed,
CeedData ceed_data,
OperatorApplyContext ctx_initial_p0);
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,
Expand Down
69 changes: 18 additions & 51 deletions examples/Hdiv-mixed/include/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,7 @@ struct AppCtx_ {
PetscFunctionList problems;
char problem_name[PETSC_MAX_PATH_LEN];
CeedContextFieldLabel solution_time_label;
};

// 2) richard
// We have 3 experiment parameters as described in Table 1:P1, P2, P3
// Matthew Farthing, Christopher Kees, Cass Miller (2003)
// https://www.sciencedirect.com/science/article/pii/S0309170802001872

#ifndef PHYSICS_RICHARDP2_STRUCT
#define PHYSICS_RICHARDP2_STRUCT
typedef struct RICHARDP2Context_ *RICHARDP2Context;
struct RICHARDP2Context_ {
CeedScalar K_star;
CeedScalar theta_s;
CeedScalar theta_r;
CeedScalar alpha_v;
CeedScalar n_v;
CeedScalar m_v;
CeedScalar m_r;
CeedScalar rho_0;
CeedScalar beta;
};
#endif

#ifndef PHYSICS_RICHARDP3_STRUCT
#define PHYSICS_RICHARDP3_STRUCT
typedef struct RICHARDP3Context_ *RICHARDP3Context;
struct RICHARDP3Context_ {
CeedScalar K_star;
CeedScalar theta_s;
CeedScalar theta_r;
CeedScalar alpha_star_v;
CeedScalar n_v;
CeedScalar m_v;
CeedScalar rho_0;
CeedScalar beta;
};
#endif

// Struct that contains all enums and structs used for the physics of all problems
typedef struct Physics_ *Physics;
struct Physics_ {
RICHARDP2Context richard_p2_ctx;
RICHARDP3Context richard_p3_ctx;
CeedScalar t_final;
};

// PETSc operator contexts
Expand All @@ -73,27 +31,36 @@ struct OperatorApplyContext_ {
CeedOperator op_apply;
DM dm;
Ceed ceed;
CeedScalar t;
CeedContextFieldLabel solution_time_label, final_time_label;
};

// libCEED data struct
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, 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;
elem_restr_p, elem_restr_p_i, elem_restr_u0,
elem_restr_p0;
CeedQFunction qf_residual, qf_jacobian, qf_error, qf_ics_u, qf_ics_p,
qf_rhs_u0, qf_rhs_p0;
CeedOperator op_residual, op_jacobian, op_error, op_ics_u, op_ics_p,
op_rhs_u0, op_rhs_p0;
CeedVector x_ceed, y_ceed, x_coord, x_t_ceed, rhs_u0_ceed,
u0_ceed, v0_ceed, rhs_p0_ceed, p0_ceed, q0_ceed;
OperatorApplyContext ctx_residual, ctx_jacobian, ctx_error, ctx_residual_ut,
ctx_initial_u0, ctx_initial_p0;
CeedInt num_elem;
};

// Problem specific data
typedef struct ProblemData_ *ProblemData;
struct ProblemData_ {
CeedQFunctionUser true_solution, residual, jacobian, error, ics,
bc_pressure;
CeedQFunctionUser true_solution, residual, jacobian, error, ics_u, ics_p,
bc_pressure, rhs_u0, rhs_p0;
const char *true_solution_loc, *residual_loc, *jacobian_loc,
*error_loc, *bc_pressure_loc, *ics_loc;
*error_loc, *bc_pressure_loc, *ics_u_loc, *ics_p_loc, *rhs_u0_loc,
*rhs_p0_loc;
CeedQuadMode quadrature_mode;
CeedInt elem_node, dim, q_data_size_face;
CeedQFunctionContext qfunction_context;
Expand Down
114 changes: 63 additions & 51 deletions examples/Hdiv-mixed/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ int main(int argc, char **argv) {
CeedData ceed_data;
PetscCall( PetscCalloc1(1, &ceed_data) );

Physics phys_ctx;
PetscCall( PetscCalloc1(1, &phys_ctx) );

OperatorApplyContext ctx_residual_ut, ctx_initial_u0, ctx_initial_p0;
PetscCall( PetscCalloc1(1, &ctx_residual_ut) );
PetscCall( PetscCalloc1(1, &ctx_initial_u0) );
PetscCall( PetscCalloc1(1, &ctx_initial_p0) );
ceed_data->ctx_residual_ut = ctx_residual_ut;
ceed_data->ctx_initial_u0 = ctx_initial_u0;
ceed_data->ctx_initial_p0 = ctx_initial_p0;
// ---------------------------------------------------------------------------
// Process command line options
// ---------------------------------------------------------------------------
Expand All @@ -102,33 +106,32 @@ int main(int argc, char **argv) {
// ---------------------------------------------------------------------------
// Create DM
// ---------------------------------------------------------------------------
DM dm;
DM dm, dm_u0, dm_p0;
PetscCall( CreateDM(comm, vec_type, &dm) );
PetscCall( CreateDM(comm, vec_type, &dm_u0) );
PetscCall( CreateDM(comm, vec_type, &dm_p0) );
// TODO: add mesh option
// perturb to have smooth random mesh
// PetscCall( PerturbVerticesSmooth(dm) );
//PetscCall( PerturbVerticesSmooth(dm) );

// ---------------------------------------------------------------------------
// Setup FE
// ---------------------------------------------------------------------------
SetupFE(comm, dm);
SetupFE(comm, dm, dm_u0, dm_p0);

// ---------------------------------------------------------------------------
// Create local Force vector
// ---------------------------------------------------------------------------
Vec U_loc;
PetscInt U_loc_size;
//CeedVector bc_pressure;
PetscCall( DMCreateLocalVector(dm, &U_loc) );
// Local size for libCEED
PetscCall( VecGetSize(U_loc, &U_loc_size) );
Vec U; // U=[p,u], U0=u0
PetscCall( DMCreateGlobalVector(dm, &U) );
PetscCall( VecZeroEntries(U) );

// ---------------------------------------------------------------------------
// Setup libCEED
// ---------------------------------------------------------------------------
// -- Set up libCEED objects
PetscCall( SetupLibceed(dm, ceed, app_ctx, problem_data,
U_loc_size, ceed_data) );
PetscCall( SetupLibceed(dm, dm_u0, dm_p0, ceed, app_ctx, problem_data,
ceed_data) );
//CeedVectorView(force_ceed, "%12.8f", stdout);
//PetscCall( DMAddBoundariesPressure(ceed, ceed_data, app_ctx, problem_data, dm,
// bc_pressure) );
Expand All @@ -141,67 +144,76 @@ int main(int argc, char **argv) {
// ---------------------------------------------------------------------------
// Create global initial conditions
// ---------------------------------------------------------------------------
Vec U0;
CreateInitialConditions(dm, ceed_data, &U0);
VecView(U0, PETSC_VIEWER_STDOUT_WORLD);
PetscCall( VecDestroy(&U0) );
SetupResidualOperatorCtx_U0(comm, dm_u0, ceed, ceed_data,
ceed_data->ctx_initial_u0);
SetupResidualOperatorCtx_P0(comm, dm_u0, ceed, ceed_data,
ceed_data->ctx_initial_p0);
CreateInitialConditions(dm, dm_u0, dm_p0, ceed_data, U, vec_type,
ceed_data->ctx_initial_u0,
ceed_data->ctx_initial_p0);
VecView(U, PETSC_VIEWER_STDOUT_WORLD);
}

// ---------------------------------------------------------------------------
// Solve PDE
// ---------------------------------------------------------------------------
// Create SNES
SNES snes;
KSP ksp;
Vec U;
PetscCall( SNESCreate(comm, &snes) );
PetscCall( SNESGetKSP(snes, &ksp) );
PetscCall( PDESolver(comm, dm, ceed, ceed_data, vec_type, snes, ksp, &U) );
//VecView(U, PETSC_VIEWER_STDOUT_WORLD);
if (!problem_data->has_ts) {
// ---------------------------------------------------------------------------
// Solve PDE
// ---------------------------------------------------------------------------
// Create SNES
SNES snes;
KSP ksp;
PetscCall( SNESCreate(comm, &snes) );
PetscCall( SNESGetKSP(snes, &ksp) );
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, &l2_error_u,
&l2_error_p) );
// ---------------------------------------------------------------------------
// Compute L2 error of mms problem
// ---------------------------------------------------------------------------
CeedScalar l2_error_u, l2_error_p;
PetscCall( ComputeL2Error(dm, ceed,ceed_data, U, &l2_error_u,
&l2_error_p) );

// ---------------------------------------------------------------------------
// Print output results
// ---------------------------------------------------------------------------
PetscCall( PrintOutput(ceed, mem_type_backend,
snes, ksp, U, l2_error_u, l2_error_p, app_ctx) );
// ---------------------------------------------------------------------------
// Print output results
// ---------------------------------------------------------------------------
PetscCall( PrintOutput(ceed, mem_type_backend,
snes, ksp, U, l2_error_u, l2_error_p, app_ctx) );

// ---------------------------------------------------------------------------
// Save solution (paraview)
// ---------------------------------------------------------------------------
PetscViewer viewer;
// ---------------------------------------------------------------------------
// Save solution (paraview)
// ---------------------------------------------------------------------------
PetscViewer viewer;

PetscCall( PetscViewerVTKOpen(comm,"solution.vtu",FILE_MODE_WRITE,&viewer) );
PetscCall( VecView(U, viewer) );
PetscCall( PetscViewerDestroy(&viewer) );
PetscCall( PetscViewerVTKOpen(comm,"solution.vtu",FILE_MODE_WRITE,&viewer) );
PetscCall( VecView(U, viewer) );
PetscCall( PetscViewerDestroy(&viewer) );

PetscCall( SNESDestroy(&snes) );

}
// ---------------------------------------------------------------------------
// Free objects
// ---------------------------------------------------------------------------

// Free PETSc objects
PetscCall( DMDestroy(&dm) );
PetscCall( DMDestroy(&dm_u0) );
PetscCall( DMDestroy(&dm_p0) );
PetscCall( VecDestroy(&U) );
PetscCall( VecDestroy(&U_loc) );
PetscCall( SNESDestroy(&snes) );
PetscCall( CeedDataDestroy(ceed_data, problem_data) );

// -- Function list
PetscCall( PetscFunctionListDestroy(&app_ctx->problems) );

// -- Structs
PetscCall( PetscFree(app_ctx) );
PetscCall( PetscFree(problem_data) );
PetscCall( PetscFree(phys_ctx) );
PetscCall( PetscFree(ctx_initial_u0) );
PetscCall( PetscFree(ctx_initial_p0) );
PetscCall( PetscFree(ctx_residual_ut) );

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

return PetscFinalize();
Expand Down
10 changes: 5 additions & 5 deletions examples/Hdiv-mixed/problems/darcy2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "../qfunctions/darcy-true2d.h"
#include "../qfunctions/darcy-system2d.h"
#include "../qfunctions/darcy-error2d.h"
#include "../qfunctions/pressure-boundary2d.h"
//#include "../qfunctions/pressure-boundary2d.h"

PetscErrorCode Hdiv_DARCY2D(Ceed ceed, ProblemData problem_data, void *ctx) {
AppCtx app_ctx = *(AppCtx *)ctx;
Expand All @@ -47,8 +47,8 @@ PetscErrorCode Hdiv_DARCY2D(Ceed ceed, ProblemData problem_data, void *ctx) {
problem_data->jacobian_loc = JacobianDarcySystem2D_loc;
problem_data->error = DarcyError2D;
problem_data->error_loc = DarcyError2D_loc;
problem_data->bc_pressure = BCPressure2D;
problem_data->bc_pressure_loc = BCPressure2D_loc;
//problem_data->bc_pressure = BCPressure2D;
//problem_data->bc_pressure_loc = BCPressure2D_loc;
problem_data->has_ts = PETSC_FALSE;

// ------------------------------------------------------
Expand Down Expand Up @@ -77,9 +77,9 @@ PetscErrorCode Hdiv_DARCY2D(Ceed ceed, ProblemData problem_data, void *ctx) {
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) );
problem_data->qfunction_context = darcy_context;

PetscFunctionReturn(0);
}
10 changes: 5 additions & 5 deletions examples/Hdiv-mixed/problems/darcy3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "../qfunctions/darcy-true3d.h"
#include "../qfunctions/darcy-system3d.h"
#include "../qfunctions/darcy-error3d.h"
#include "../qfunctions/pressure-boundary3d.h"
//#include "../qfunctions/pressure-boundary3d.h"

PetscErrorCode Hdiv_DARCY3D(Ceed ceed, ProblemData problem_data, void *ctx) {
AppCtx app_ctx = *(AppCtx *)ctx;
Expand All @@ -47,8 +47,8 @@ PetscErrorCode Hdiv_DARCY3D(Ceed ceed, ProblemData problem_data, void *ctx) {
problem_data->jacobian_loc = JacobianDarcySystem3D_loc;
problem_data->error = DarcyError3D;
problem_data->error_loc = DarcyError3D_loc;
problem_data->bc_pressure = BCPressure3D;
problem_data->bc_pressure_loc = BCPressure3D_loc;
//problem_data->bc_pressure = BCPressure3D;
//problem_data->bc_pressure_loc = BCPressure3D_loc;
problem_data->has_ts = PETSC_FALSE;

// ------------------------------------------------------
Expand Down Expand Up @@ -77,9 +77,9 @@ PetscErrorCode Hdiv_DARCY3D(Ceed ceed, ProblemData problem_data, void *ctx) {
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) );
problem_data->qfunction_context = darcy_context;

PetscFunctionReturn(0);
}
Loading

0 comments on commit 3a8fc58

Please sign in to comment.