Skip to content

Commit

Permalink
setup richards qfunction
Browse files Browse the repository at this point in the history
  • Loading branch information
rezgarshakeri committed Jul 11, 2022
1 parent 4b46c93 commit 5e0f655
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 79 deletions.
40 changes: 35 additions & 5 deletions examples/Hdiv-mixed/problems/richard2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@

#include "../include/register-problem.h"
#include "../qfunctions/richard-system2d.h"
#include "../qfunctions/richard-force2d.h"
#include "../qfunctions/pressure-boundary2d.h"

PetscErrorCode Hdiv_RICHARD2D(Ceed ceed, ProblemData problem_data, void *ctx) {
AppCtx app_ctx = *(AppCtx *)ctx;
//RICHARDContext richard_ctx;
//CeedQFunctionContext richard_context;
RICHARDContext richard_ctx;
CeedQFunctionContext richard_context;

PetscFunctionBeginUser;

//PetscCall( PetscCalloc1(1, &richard_ctx) );
PetscCall( PetscCalloc1(1, &richard_ctx) );

// ------------------------------------------------------
// SET UP POISSON_QUAD2D
Expand All @@ -37,8 +38,8 @@ PetscErrorCode Hdiv_RICHARD2D(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->force = RichardForce2D;
problem_data->force_loc = RichardForce2D_loc;
problem_data->residual = RichardSystem2D;
problem_data->residual_loc = RichardSystem2D_loc;
problem_data->jacobian = JacobianRichardSystem2D;
Expand All @@ -51,9 +52,38 @@ PetscErrorCode Hdiv_RICHARD2D(Ceed ceed, ProblemData problem_data, void *ctx) {
// ------------------------------------------------------
// Command line Options
// ------------------------------------------------------
CeedScalar kappa = 10., alpha_a = 1., b_a = 10., rho_0 = 998.2,
beta = 0., g = 9.8, p0 = 101325;

PetscOptionsBegin(app_ctx->comm, NULL, "Options for Hdiv-mixed problem", NULL);
PetscCall( PetscOptionsScalar("-kappa", "Hydraulic Conductivity", NULL,
kappa, &kappa, NULL));
PetscCall( PetscOptionsScalar("-alpha_a", "Parameter for relative permeability",
NULL,
alpha_a, &alpha_a, NULL));
PetscCall( PetscOptionsScalar("-b_a", "Parameter for relative permeability",
NULL,
b_a, &b_a, NULL));
PetscCall( PetscOptionsScalar("-rho_0", "Density at p0", NULL,
rho_0, &rho_0, NULL));
PetscCall( PetscOptionsScalar("-beta", "Water compressibility", NULL,
beta, &beta, NULL));
PetscOptionsEnd();

richard_ctx->kappa = kappa;
richard_ctx->alpha_a = alpha_a;
richard_ctx->b_a = b_a;
richard_ctx->rho_0 = rho_0;
richard_ctx->beta = beta;
richard_ctx->g = g;
richard_ctx->p0 = p0;
CeedQFunctionContextCreate(ceed, &richard_context);
CeedQFunctionContextSetData(richard_context, CEED_MEM_HOST, CEED_COPY_VALUES,
sizeof(*richard_ctx), richard_ctx);
problem_data->qfunction_context = richard_context;
CeedQFunctionContextSetDataDestroy(richard_context, CEED_MEM_HOST,
FreeContextPetsc);

//PetscCall( PetscFree(richard_ctx) );
PetscFunctionReturn(0);
}
2 changes: 1 addition & 1 deletion examples/Hdiv-mixed/qfunctions/darcy-system2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ CEED_QFUNCTION(JacobianDarcySystem2D)(void *ctx, CeedInt Q,

// -----------------------------------------------------------------------------

#endif //End of DARCY_MASS2D_H
#endif //End of DARCY_SYSTEM2D_H
2 changes: 1 addition & 1 deletion examples/Hdiv-mixed/qfunctions/darcy-system3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@ CEED_QFUNCTION(JacobianDarcySystem3D)(void *ctx, CeedInt Q,

// -----------------------------------------------------------------------------

#endif //End of DARCY_MASS3D_H
#endif //End of DARCY_SYSTEM3D_H
119 changes: 119 additions & 0 deletions examples/Hdiv-mixed/qfunctions/richard-force2d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
// the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
// reserved. See files LICENSE and NOTICE for details.
//
// This file is part of CEED, a collection of benchmarks, miniapps, software
// libraries and APIs for efficient high-order finite element and spectral
// element discretizations for exascale applications. For more information and
// source code availability see http://github.com/ceed.
//
// The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
// a collaborative effort of two U.S. Department of Energy organizations (Office
// of Science and the National Nuclear Security Administration) responsible for
// the planning and preparation of a capable exascale ecosystem, including
// software, applications, hardware, advanced system engineering and early
// testbed platforms, in support of the nation's exascale computing imperative.

/// @file
/// Force of Richard problem 2D (quad element) using PETSc

#ifndef RICHARD_FORCE2D_H
#define RICHARD_FORCE2D_H

#include <math.h>
#include "utils.h"

// See Matthew Farthing, Christopher Kees, Cass Miller (2003)
// https://www.sciencedirect.com/science/article/pii/S0309170802001872
// -----------------------------------------------------------------------------
// Strong form:
// k*K^{-1} * u = -\grad(p) + rho*g in \Omega x [0,T]
// -\div(u) = -f + d (rho/rho_0*theta)/dt in \Omega x [0,T]
// p = p_b on \Gamma_D x [0,T]
// u.n = u_b on \Gamma_N x [0,T]
// p = p_0 in \Omega, t = 0
//
// Where g is gravity vector, rho = rho_0*exp(beta * (p - p0)), p0 = 101325 Pa is atmospheric pressure
// f = fs/rho_0, where g is gravity, rho_0 is the density at p_0, K = kappa*I, and
// k_r = b_a + alpha_a * (\psi - x2), where \psi = p / (rho_0 * norm(g)) and x2 is vertical axis
//
// Weak form: Find (u, p) \in VxQ (V=H(div), Q=L^2) on \Omega
// (v, k*K^{-1} * u) -(v, rho*g) - (\div(v), p) = - <v, p_b*n>_{\Gamma_D}
// -(q, \div(u)) = -(q, f) + (v, d (rho/rho_0*theta)/dt )
//
// where k*K^{-1} = (rho_0^2*norm(g)/rho*k_r)*K^{-1}
// This QFunction sets up the force
// Inputs:
// x : interpolation of the physical coordinate
// w : weight of quadrature
// J : dx/dX. x physical coordinate, X reference coordinate [-1,1]^dim
//
// Output:
// force_u : which is 0.0 for this problem (-<v, p0 n> is in pressure-boundary qfunction)
// force_p : -(q, f) = -\int( q * f * w*detJ)dx
// -----------------------------------------------------------------------------
// 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 RICHARD_CTX
#define RICHARD_CTX
typedef struct RICHARDContext_ *RICHARDContext;
struct RICHARDContext_ {
CeedScalar kappa;
CeedScalar alpha_a;
CeedScalar b_a;
CeedScalar rho_0;
CeedScalar beta;
CeedScalar g;
CeedScalar p0;
};
#endif
// -----------------------------------------------------------------------------
// Force evaluation for Richard problem
// -----------------------------------------------------------------------------
CEED_QFUNCTION(RichardForce2D)(void *ctx, const CeedInt Q,
const CeedScalar *const *in,
CeedScalar *const *out) {
// *INDENT-OFF*
// Inputs
const CeedScalar (*coords) = in[0],
(*w) = in[1],
(*dxdX)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[2];
// Outputs
CeedScalar (*rhs_u) = out[0], (*rhs_p) = out[1],
(*true_soln) = out[2];
// Context
RICHARDContext context = (RICHARDContext)ctx;
const CeedScalar kappa = context->kappa;//10.;
// Quadrature Point Loop
CeedPragmaSIMD
for (CeedInt i=0; i<Q; i++) {
// Setup, (x,y) and J = dx/dX
CeedScalar x = coords[i+0*Q], y = coords[i+1*Q];
const CeedScalar J[2][2] = {{dxdX[0][0][i], dxdX[1][0][i]},
{dxdX[0][1][i], dxdX[1][1][i]}};
const CeedScalar det_J = MatDet2x2(J);
// *INDENT-ON*
CeedScalar pe = sin(PI_DOUBLE*x) * sin(PI_DOUBLE*y);
CeedScalar grad_pe[2] = {PI_DOUBLE*cos(PI_DOUBLE*x) *sin(PI_DOUBLE*y), PI_DOUBLE*sin(PI_DOUBLE*x) *cos(PI_DOUBLE*y)};
CeedScalar K[2][2] = {{kappa, 0.},{0., kappa}};
CeedScalar ue[2];
AlphaMatVecMult2x2(-1., K, grad_pe, ue);
CeedScalar f = 2*PI_DOUBLE*PI_DOUBLE*sin(PI_DOUBLE*x)*sin(PI_DOUBLE*y);

// 1st eq: component 1
rhs_u[i+0*Q] = 0.;
// 1st eq: component 2
rhs_u[i+1*Q] = 0.;
// 2nd eq
rhs_p[i] = -f*w[i]*det_J;
// True solution Ue=[p,u]
true_soln[i+0*Q] = pe;
true_soln[i+1*Q] = ue[0];
true_soln[i+2*Q] = ue[1];
} // End of Quadrature Point Loop
return 0;
}
// -----------------------------------------------------------------------------

#endif //End of RICHARD_FORCE2D_H
Loading

0 comments on commit 5e0f655

Please sign in to comment.