Skip to content

Commit

Permalink
Add test with TESTTYPE_POST_PROCESS
Browse files Browse the repository at this point in the history
This is passing locally but we might need to change the *.bin file as
there is still a bug in the computed error depending on the number of
ranks.
  • Loading branch information
LeilaGhaffari committed Jul 13, 2023
1 parent 63875d8 commit 09cea8b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
1 change: 1 addition & 0 deletions examples/fluids/navierstokes.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
//TESTARGS(name="euler_implicit") -ceed {ceed_resource} -test_type solver -problem euler_vortex -degree 3 -dm_plex_box_faces 1,1,2 -dm_plex_box_lower 0,0,0 -dm_plex_box_upper 125,125,250 -dm_plex_dim 3 -units_meter 1e-4 -units_second 1e-4 -mean_velocity 1.4,-2.,0 -bc_inflow 4,6 -bc_outflow 3,5 -bc_slip_z 1,2 -vortex_strength 2 -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -dm_mat_preallocate_skip 0 -ts_type alpha -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-euler-implicit.bin
//TESTARGS(name="euler_explicit") -ceed {ceed_resource} -test_type solver -problem euler_vortex -degree 3 -q_extra 2 -dm_plex_box_faces 2,2,1 -dm_plex_box_lower 0,0,0 -dm_plex_box_upper 125,125,250 -dm_plex_dim 3 -units_meter 1e-4 -units_second 1e-4 -mean_velocity 1.4,-2.,0 -bc_inflow 4,6 -bc_outflow 3,5 -bc_slip_z 1,2 -vortex_strength 2 -ts_dt 1e-7 -ts_rk_type 5bs -ts_rtol 1e-10 -ts_atol 1e-10 -compare_final_state_atol 1E-7 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-euler-explicit.bin
//TESTARGS(name="shocktube_explicit_su_yzb") -ceed {ceed_resource} -test_type solver -problem shocktube -degree 1 -q_extra 2 -dm_plex_box_faces 50,1,1 -units_meter 1e-2 units_second 1e-2 -dm_plex_box_lower 0,0,0 -dm_plex_box_upper 1000,20,20 -dm_plex_dim 3 -bc_slip_x 5,6 -bc_slip_y 3,4 -bc_Slip_z 1,2 -yzb -stab su -compare_final_state_atol 1E-11 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-shocktube-explicit-su-yzb.bin
//TESTARGS(name="exact_post_process") -ceed {ceed_resource} -test_type post_process -options_file examples/fluids/tests-output/blasius_test.yaml -ts_dt 1e-9 -compare_final_state_atol 1e-5 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-exact-postprocess.bin

/// @file
/// Navier-Stokes example using PETSc
Expand Down
5 changes: 3 additions & 2 deletions examples/fluids/navierstokes.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ typedef enum {
TESTTYPE_SOLVER = 1,
TESTTYPE_TURB_SPANSTATS = 2,
TESTTYPE_DIFF_FILTER = 3,
TESTTYPE_POST_PROCESS = 4,
} TestType;
static const char *const TestTypes[] = {"none", "solver", "turb_spanstats", "diff_filter", "TestType", "TESTTYPE_", NULL};
static const char *const TestTypes[] = {"none", "solver", "turb_spanstats", "diff_filter", "post_process", "TestType", "TESTTYPE_", NULL};

// Test mode type
typedef enum {
Expand Down Expand Up @@ -391,7 +392,7 @@ PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q);

// Get error for problems with exact solutions
PetscErrorCode ComputeL2Error(DM dm, CeedData ceed_data, User user, Vec Q, PetscReal l2_error[5], OperatorApplyContext op_error_ctx,
CeedContextFieldLabel time_label, CeedScalar time);
CeedContextFieldLabel time_label, CeedScalar time, Vec *E);
PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, ProblemData *problem, Vec Qsource_soln, PetscScalar final_time);

// Post-processing
Expand Down
44 changes: 27 additions & 17 deletions examples/fluids/src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q) {
}

PetscErrorCode ComputeL2Error(DM dm, CeedData ceed_data, User user, Vec Q, PetscReal l2_error[5], OperatorApplyContext op_error_ctx,
CeedContextFieldLabel time_label, CeedScalar time) {
Vec Q_loc, E_loc, E;
CeedContextFieldLabel time_label, CeedScalar time, Vec *E) {
Vec Q_loc, E_loc;
PetscReal l2_norm[5];
PetscFunctionBeginUser;

Expand All @@ -152,55 +152,65 @@ PetscErrorCode ComputeL2Error(DM dm, CeedData ceed_data, User user, Vec Q, Petsc
PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc));
PetscCall(UpdateBoundaryValues(user, Q_loc, time));

PetscCall(VecDuplicate(Q, &E));
PetscCall(VecDuplicate(Q_loc, &E_loc));
PetscCall(VecZeroEntries(E_loc));
PetscCall(FixMultiplicity_NS(dm, ceed_data, user, op_error_ctx, Q_loc, E_loc, E, time_label, time));
PetscCall(FixMultiplicity_NS(dm, ceed_data, user, op_error_ctx, Q_loc, E_loc, *E, time_label, time));

PetscCall(VecStrideNormAll(E_loc, NORM_1, l2_norm));
PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, l2_norm, 5, MPIU_REAL, MPI_SUM, PETSC_COMM_WORLD));
for (int i = 0; i < 5; i++) l2_error[i] = sqrt(l2_norm[i]);

PetscCall(VecDestroy(&E_loc));
PetscCall(VecDestroy(&E));

PetscFunctionReturn(PETSC_SUCCESS);
};

// Get error for problems with true solutions
PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, ProblemData *problem, Vec Q, PetscScalar final_time) {
PetscReal l2_error[5];
Vec E_source;
const char *state_var_source = "Conservative";
PetscFunctionBeginUser;

// Compute the L2 error in the source state variables
PetscCall(VecDuplicate(Q, &E_source));
if (user->phys->ics_time_label) PetscCall(UpdateContextLabel(user->comm, final_time, ceed_data->op_ics_ctx->op, user->phys->ics_time_label));
CeedOperatorApply(ceed_data->op_ics_ctx->op, ceed_data->x_coord, ceed_data->q_true, CEED_REQUEST_IMMEDIATE);
PetscCall(ComputeL2Error(dm, ceed_data, user, Q, l2_error, ceed_data->op_error_ctx, user->phys->ics_time_label, final_time));
PetscCall(ComputeL2Error(dm, ceed_data, user, Q, l2_error, ceed_data->op_error_ctx, user->phys->ics_time_label, final_time, &E_source));

// Print the error
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\nL2 Error:\n"));
if (user->phys->state_var == STATEVAR_PRIMITIVE) state_var_source = "Primitive";
for (int i = 0; i < 5; i++) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %s variables-Component %d: %g\n", state_var_source, i, (double)l2_error[i]));

if (user->app_ctx->test_type != TESTTYPE_POST_PROCESS) {
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\nL2 Error:\n"));
if (user->phys->state_var == STATEVAR_PRIMITIVE) state_var_source = "Primitive";
for (int i = 0; i < 5; i++) {
PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %s variables-Component %d: %g\n", state_var_source, i, (double)l2_error[i]));
}
}
if (problem->convert_error.qfunction) {
PetscReal l2_error_converted[5];
Vec E_target;
const char *state_var_target = "Primitive";
if (user->phys->state_var == STATEVAR_PRIMITIVE) state_var_target = "Conservative";

// Convert the L2 error to the target state variable
PetscCall(ComputeL2Error(dm, ceed_data, user, Q, l2_error_converted, ceed_data->op_convert_error_ctx, user->phys->ics_time_label, final_time));

// Print the error
if (user->phys->state_var == STATEVAR_PRIMITIVE) state_var_target = "Conservative";
for (int i = 0; i < 5; i++) {
PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %s variables-Component %d: %g\n", state_var_target, i, (double)l2_error_converted[i]));
PetscCall(VecDuplicate(Q, &E_target));
PetscCall(ComputeL2Error(dm, ceed_data, user, Q, l2_error_converted, ceed_data->op_convert_error_ctx, user->phys->ics_time_label, final_time,
&E_target));
if (user->app_ctx->test_type == TESTTYPE_POST_PROCESS) {
PetscCall(RegressionTests_NS(user->app_ctx, E_target));
} else {
for (int i = 0; i < 5; i++) {
PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %s variables-Component %d: %g\n", state_var_target, i, (double)l2_error_converted[i]));
}
}
// Cleanup
CeedQFunctionDestroy(&ceed_data->qf_convert_error);
PetscCall(VecDestroy(&E_target));
}

// Cleanup
CeedVectorDestroy(&ceed_data->q_true);
PetscCall(VecDestroy(&E_source));

PetscFunctionReturn(PETSC_SUCCESS);
}
Expand All @@ -212,7 +222,7 @@ PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, ProblemData *pro
PetscFunctionBeginUser;

// Print relative error
if (problem->has_true_soln && user->app_ctx->test_type == TESTTYPE_NONE) {
if (problem->has_true_soln && (user->app_ctx->test_type == TESTTYPE_NONE || user->app_ctx->test_type == TESTTYPE_POST_PROCESS)) {
PetscCall(GetError_NS(ceed_data, dm, user, problem, Q, final_time));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/fluids/src/setuplibceed.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user, App
CeedQFunctionAddOutput(ceed_data->qf_ics, "q0", num_comp_q, CEED_EVAL_NONE);

// -- Create QFunction for computing the L2 error
if (problem->has_true_soln && user->app_ctx->test_type == TESTTYPE_NONE) {
if (problem->has_true_soln && (user->app_ctx->test_type == TESTTYPE_NONE || user->app_ctx->test_type == TESTTYPE_POST_PROCESS)) {
CeedQFunctionCreateInterior(ceed, 1, problem->error.qfunction, problem->error.qfunction_loc, &ceed_data->qf_error);
CeedQFunctionSetContext(ceed_data->qf_error, problem->error.qfunction_context);
CeedQFunctionContextDestroy(&problem->error.qfunction_context);
Expand Down
Binary file not shown.

0 comments on commit 09cea8b

Please sign in to comment.