162b7942eSJames Wright // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors. 262b7942eSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 362b7942eSJames Wright // 462b7942eSJames Wright // SPDX-License-Identifier: BSD-2-Clause 562b7942eSJames Wright // 662b7942eSJames Wright // This file is part of CEED: http://github.com/ceed 762b7942eSJames Wright 862b7942eSJames Wright #include "../qfunctions/sgs_dd_model.h" 962b7942eSJames Wright 1062b7942eSJames Wright #include <petscdmplex.h> 1162b7942eSJames Wright 1262b7942eSJames Wright #include "../navierstokes.h" 1362b7942eSJames Wright 14c38c977aSJames Wright typedef struct { 15c38c977aSJames Wright CeedElemRestriction elem_restr_grid_aniso, elem_restr_sgs; 16c38c977aSJames Wright CeedVector grid_aniso_ceed; 17ee1455b7SJames Wright CeedQFunctionContext sgsdd_qfctx; 18c38c977aSJames Wright } *SGS_DD_ModelSetupData; 19c38c977aSJames Wright 20c38c977aSJames Wright PetscErrorCode SGS_DD_ModelSetupDataDestroy(SGS_DD_ModelSetupData sgs_dd_setup_data) { 21b4c37c5cSJames Wright Ceed ceed; 22b4c37c5cSJames Wright 23c38c977aSJames Wright PetscFunctionBeginUser; 24b4c37c5cSJames Wright PetscCall(CeedElemRestrictionGetCeed(sgs_dd_setup_data->elem_restr_sgs, &ceed)); 25b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&sgs_dd_setup_data->elem_restr_grid_aniso)); 26b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&sgs_dd_setup_data->elem_restr_sgs)); 27b4c37c5cSJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&sgs_dd_setup_data->grid_aniso_ceed)); 28b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionContextDestroy(&sgs_dd_setup_data->sgsdd_qfctx)); 29c38c977aSJames Wright 30c38c977aSJames Wright PetscCall(PetscFree(sgs_dd_setup_data)); 31d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 32c38c977aSJames Wright } 33c38c977aSJames Wright 34ee1455b7SJames Wright // @brief Create DM for storing subgrid stress at nodes 3567263decSKenneth E. Jansen PetscErrorCode SGS_DD_ModelCreateDM(DM dm_source, DM *dm_sgs, PetscInt degree, PetscInt q_extra, PetscInt *num_components) { 36ee1455b7SJames Wright PetscSection section; 37ee1455b7SJames Wright 38ee1455b7SJames Wright PetscFunctionBeginUser; 39ee1455b7SJames Wright *num_components = 6; 40ee1455b7SJames Wright 41ee1455b7SJames Wright PetscCall(DMClone(dm_source, dm_sgs)); 42ee1455b7SJames Wright PetscCall(PetscObjectSetName((PetscObject)*dm_sgs, "Subgrid Stress Projection")); 43ee1455b7SJames Wright 44*da4ca0cfSJames Wright PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, degree, 1, q_extra, 1, num_components, *dm_sgs)); 45ee1455b7SJames Wright 46ee1455b7SJames Wright PetscCall(DMGetLocalSection(*dm_sgs, §ion)); 47ee1455b7SJames Wright PetscCall(PetscSectionSetFieldName(section, 0, "")); 48ee1455b7SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 0, "KMSubgridStressXX")); 49ee1455b7SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 1, "KMSubgridStressYY")); 50ee1455b7SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 2, "KMSubgridStressZZ")); 51ee1455b7SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 3, "KMSubgridStressYZ")); 52ee1455b7SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 4, "KMSubgridStressXZ")); 53ee1455b7SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 5, "KMSubgridStressXY")); 54ee1455b7SJames Wright 55d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 56ee1455b7SJames Wright }; 57ee1455b7SJames Wright 58ee1455b7SJames Wright // @brief Create CeedOperator to calculate data-drive SGS at nodes 59ee1455b7SJames Wright PetscErrorCode SGS_DD_ModelSetupNodalEvaluation(Ceed ceed, User user, CeedData ceed_data, SGS_DD_ModelSetupData sgs_dd_setup_data) { 60ee1455b7SJames Wright SGS_DD_Data sgs_dd_data = user->sgs_dd_data; 61ee1455b7SJames Wright CeedQFunction qf_multiplicity, qf_sgs_dd_nodal; 62ee1455b7SJames Wright CeedOperator op_multiplicity, op_sgs_dd_nodal; 6367263decSKenneth E. Jansen CeedInt num_elem, elem_size, num_comp_q, num_comp_grad_velo, num_comp_x, num_comp_grid_aniso; 64defe8520SJames Wright PetscInt dim; 654b0f6111SJames Wright CeedVector multiplicity, inv_multiplicity; 66ee1455b7SJames Wright CeedElemRestriction elem_restr_inv_multiplicity, elem_restr_grad_velo, elem_restr_sgs; 67ee1455b7SJames Wright 68ee1455b7SJames Wright PetscFunctionBeginUser; 69ee1455b7SJames Wright PetscCall(DMGetDimension(user->dm, &dim)); 70b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_x, &num_comp_x)); 71b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q)); 72b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(sgs_dd_setup_data->elem_restr_grid_aniso, &num_comp_grid_aniso)); 73b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumElements(ceed_data->elem_restr_q, &num_elem)); 74b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetElementSize(ceed_data->elem_restr_q, &elem_size)); 75ee1455b7SJames Wright 76ee1455b7SJames Wright { // Get velocity gradient information 77ee1455b7SJames Wright CeedOperatorField op_field; 78b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorGetFieldByName(user->grad_velo_proj->l2_rhs_ctx->op, "velocity gradient", &op_field)); 79b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(op_field, &elem_restr_grad_velo)); 80b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_grad_velo, &num_comp_grad_velo)); 81ee1455b7SJames Wright } 8267263decSKenneth E. Jansen PetscCall(GetRestrictionForDomain(ceed, sgs_dd_data->dm_sgs, 0, 0, 0, 0, -1, 0, &elem_restr_sgs, NULL, NULL)); 83b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_sgs, &sgs_dd_data->sgs_nodal_ceed, NULL)); 84ee1455b7SJames Wright 85ee1455b7SJames Wright // -- Create inverse multiplicity for correcting nodal assembly 86b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &multiplicity, NULL)); 87b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, multiplicity)); 88b4c37c5cSJames Wright PetscCallCeed( 89b4c37c5cSJames Wright ceed, CeedElemRestrictionCreateStrided(ceed, num_elem, elem_size, 1, num_elem * elem_size, CEED_STRIDES_BACKEND, &elem_restr_inv_multiplicity)); 90b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_inv_multiplicity, &inv_multiplicity, NULL)); 91ee1455b7SJames Wright 92b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, InverseMultiplicity, InverseMultiplicity_loc, &qf_multiplicity)); 93b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_multiplicity, "multiplicity", num_comp_q, CEED_EVAL_NONE)); 94b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_multiplicity, "inverse multiplicity", 1, CEED_EVAL_NONE)); 95ee1455b7SJames Wright 96b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_multiplicity, NULL, NULL, &op_multiplicity)); 97b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetName(op_multiplicity, "SGS DD Model - Create Multiplicity Scaling")); 98b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_multiplicity, "multiplicity", ceed_data->elem_restr_q, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE)); 99b4c37c5cSJames Wright PetscCallCeed( 100b4c37c5cSJames Wright ceed, CeedOperatorSetField(op_multiplicity, "inverse multiplicity", elem_restr_inv_multiplicity, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE)); 101ee1455b7SJames Wright 102b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorApply(op_multiplicity, multiplicity, inv_multiplicity, CEED_REQUEST_IMMEDIATE)); 103ee1455b7SJames Wright 104ee1455b7SJames Wright // -- Create operator for SGS DD model nodal evaluation 105ee1455b7SJames Wright switch (user->phys->state_var) { 106ee1455b7SJames Wright case STATEVAR_PRIMITIVE: 107b4c37c5cSJames Wright PetscCallCeed( 108b4c37c5cSJames Wright ceed, CeedQFunctionCreateInterior(ceed, 1, ComputeSGS_DDAnisotropicNodal_Prim, ComputeSGS_DDAnisotropicNodal_Prim_loc, &qf_sgs_dd_nodal)); 109ee1455b7SJames Wright break; 110ee1455b7SJames Wright case STATEVAR_CONSERVATIVE: 111b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, ComputeSGS_DDAnisotropicNodal_Conserv, ComputeSGS_DDAnisotropicNodal_Conserv_loc, 112b4c37c5cSJames Wright &qf_sgs_dd_nodal)); 113ee1455b7SJames Wright break; 114ee1455b7SJames Wright default: 115ee1455b7SJames Wright SETERRQ(PetscObjectComm((PetscObject)user->dm), PETSC_ERR_SUP, 116ee1455b7SJames Wright "Anisotropic data-driven SGS nodal evaluation not available for chosen state variable"); 117ee1455b7SJames Wright } 118ee1455b7SJames Wright 119ee1455b7SJames Wright // Mesh/geometry order and solution basis order may differ, therefore must interpolate 120ee1455b7SJames Wright CeedBasis basis_x_to_q; 121b4c37c5cSJames Wright PetscCallCeed(ceed, CeedBasisCreateProjection(ceed_data->basis_x, ceed_data->basis_q, &basis_x_to_q)); 122ee1455b7SJames Wright 123b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionSetContext(qf_sgs_dd_nodal, sgs_dd_setup_data->sgsdd_qfctx)); 124b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_nodal, "q", num_comp_q, CEED_EVAL_NONE)); 125b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_nodal, "x", num_comp_x, CEED_EVAL_INTERP)); 126b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_nodal, "gradient velocity", num_comp_grad_velo, CEED_EVAL_NONE)); 127b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_nodal, "anisotropy tensor", num_comp_grid_aniso, CEED_EVAL_NONE)); 128b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_nodal, "inverse multiplicity", 1, CEED_EVAL_NONE)); 129b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_sgs_dd_nodal, "km_sgs", sgs_dd_data->num_comp_sgs, CEED_EVAL_NONE)); 130ee1455b7SJames Wright 131b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_sgs_dd_nodal, NULL, NULL, &op_sgs_dd_nodal)); 132b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_nodal, "q", ceed_data->elem_restr_q, CEED_BASIS_COLLOCATED, user->q_ceed)); 133b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_nodal, "x", ceed_data->elem_restr_x, basis_x_to_q, ceed_data->x_coord)); 134b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_nodal, "gradient velocity", elem_restr_grad_velo, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE)); 135b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_nodal, "anisotropy tensor", sgs_dd_setup_data->elem_restr_grid_aniso, CEED_BASIS_COLLOCATED, 136b4c37c5cSJames Wright sgs_dd_setup_data->grid_aniso_ceed)); 137b4c37c5cSJames Wright PetscCallCeed(ceed, 138b4c37c5cSJames Wright CeedOperatorSetField(op_sgs_dd_nodal, "inverse multiplicity", elem_restr_inv_multiplicity, CEED_BASIS_COLLOCATED, inv_multiplicity)); 139b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_nodal, "km_sgs", elem_restr_sgs, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE)); 140ee1455b7SJames Wright 1414b0f6111SJames Wright PetscCall(OperatorApplyContextCreate(user->grad_velo_proj->dm, sgs_dd_data->dm_sgs, ceed, op_sgs_dd_nodal, NULL, sgs_dd_data->sgs_nodal_ceed, NULL, 1424b0f6111SJames Wright NULL, &sgs_dd_data->op_nodal_evaluation_ctx)); 143ee1455b7SJames Wright 144ee1455b7SJames Wright sgs_dd_setup_data->elem_restr_sgs = elem_restr_sgs; 145ee1455b7SJames Wright 146b4c37c5cSJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&multiplicity)); 147b4c37c5cSJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&inv_multiplicity)); 148b4c37c5cSJames Wright PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_to_q)); 149b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_inv_multiplicity)); 150b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_multiplicity)); 151b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_sgs_dd_nodal)); 152b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_multiplicity)); 153b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_sgs_dd_nodal)); 154d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 155ee1455b7SJames Wright } 156ee1455b7SJames Wright 1579c678832SJames Wright // @brief Create CeedOperator to compute SGS contribution to the residual 1589c678832SJames Wright PetscErrorCode SGS_ModelSetupNodalIFunction(Ceed ceed, User user, CeedData ceed_data, SGS_DD_ModelSetupData sgs_dd_setup_data) { 1599c678832SJames Wright SGS_DD_Data sgs_dd_data = user->sgs_dd_data; 16067263decSKenneth E. Jansen CeedInt num_comp_q, num_comp_qd, num_comp_x; 161defe8520SJames Wright PetscInt dim; 1629c678832SJames Wright CeedQFunction qf_sgs_apply; 1639c678832SJames Wright CeedOperator op_sgs_apply; 1649c678832SJames Wright CeedBasis basis_sgs; 1659c678832SJames Wright 1669c678832SJames Wright PetscFunctionBeginUser; 1679c678832SJames Wright PetscCall(DMGetDimension(user->dm, &dim)); 168b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q)); 169b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &num_comp_qd)); 170b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_x, &num_comp_x)); 1719c678832SJames Wright 17267263decSKenneth E. Jansen PetscCall(CreateBasisFromPlex(ceed, sgs_dd_data->dm_sgs, 0, 0, 0, 0, &basis_sgs)); 1739c678832SJames Wright 1749c678832SJames Wright switch (user->phys->state_var) { 1759c678832SJames Wright case STATEVAR_PRIMITIVE: 176b4c37c5cSJames Wright PetscCallCeed(ceed, 177b4c37c5cSJames Wright CeedQFunctionCreateInterior(ceed, 1, IFunction_NodalSubgridStress_Prim, IFunction_NodalSubgridStress_Prim_loc, &qf_sgs_apply)); 1789c678832SJames Wright break; 1799c678832SJames Wright case STATEVAR_CONSERVATIVE: 180b4c37c5cSJames Wright PetscCallCeed( 181b4c37c5cSJames Wright ceed, CeedQFunctionCreateInterior(ceed, 1, IFunction_NodalSubgridStress_Conserv, IFunction_NodalSubgridStress_Conserv_loc, &qf_sgs_apply)); 1829c678832SJames Wright break; 1839c678832SJames Wright default: 1849c678832SJames Wright SETERRQ(PetscObjectComm((PetscObject)user->dm), PETSC_ERR_SUP, "Nodal SGS evaluation not available for chosen state variable"); 1859c678832SJames Wright } 1869c678832SJames Wright 187b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionSetContext(qf_sgs_apply, sgs_dd_setup_data->sgsdd_qfctx)); 188b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_apply, "q", num_comp_q, CEED_EVAL_INTERP)); 189b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_apply, "qdata", num_comp_qd, CEED_EVAL_NONE)); 190b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_apply, "x", num_comp_x, CEED_EVAL_INTERP)); 191b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_apply, "km_sgs", sgs_dd_data->num_comp_sgs, CEED_EVAL_INTERP)); 192b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_sgs_apply, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD)); 1939c678832SJames Wright 194b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_sgs_apply, NULL, NULL, &op_sgs_apply)); 195b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_apply, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE)); 196b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_apply, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data)); 197b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_apply, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord)); 198b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_apply, "km_sgs", sgs_dd_setup_data->elem_restr_sgs, basis_sgs, sgs_dd_data->sgs_nodal_ceed)); 199b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_apply, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE)); 2009c678832SJames Wright 2019c678832SJames Wright PetscCall( 2029c678832SJames Wright OperatorApplyContextCreate(user->dm, user->dm, ceed, op_sgs_apply, user->q_ceed, user->g_ceed, NULL, NULL, &sgs_dd_data->op_sgs_apply_ctx)); 2039c678832SJames Wright 204b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_sgs_apply)); 205b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_sgs_apply)); 206d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 2079c678832SJames Wright } 2089c678832SJames Wright 2099c678832SJames Wright // @brief Calculate and add data-driven SGS residual to the global residual 2109c678832SJames Wright PetscErrorCode SGS_DD_ModelApplyIFunction(User user, const Vec Q_loc, Vec G_loc) { 2119c678832SJames Wright SGS_DD_Data sgs_dd_data = user->sgs_dd_data; 2129c678832SJames Wright Vec VelocityGradient, SGSNodal_loc; 2139c678832SJames Wright PetscMemType sgs_nodal_mem_type, q_mem_type; 2149c678832SJames Wright 2159c678832SJames Wright PetscFunctionBeginUser; 2169c678832SJames Wright PetscCall(DMGetGlobalVector(user->grad_velo_proj->dm, &VelocityGradient)); 2179c678832SJames Wright PetscCall(VelocityGradientProjectionApply(user, Q_loc, VelocityGradient)); 2189c678832SJames Wright 2199c678832SJames Wright // -- Compute Nodal SGS tensor 2209c678832SJames Wright PetscCall(DMGetLocalVector(sgs_dd_data->dm_sgs, &SGSNodal_loc)); 2219c678832SJames Wright PetscCall(VecP2C(Q_loc, &q_mem_type, user->q_ceed)); // q_ceed is an implicit input 2229c678832SJames Wright 2239c678832SJames Wright PetscCall(ApplyCeedOperatorGlobalToLocal(VelocityGradient, SGSNodal_loc, sgs_dd_data->op_nodal_evaluation_ctx)); 2249c678832SJames Wright 2259c678832SJames Wright PetscCall(VecC2P(user->q_ceed, q_mem_type, Q_loc)); 2269c678832SJames Wright PetscCall(VecP2C(SGSNodal_loc, &sgs_nodal_mem_type, sgs_dd_data->sgs_nodal_ceed)); // sgs_nodal_ceed is an implicit input 2279c678832SJames Wright 2289c678832SJames Wright // -- Compute contribution of the SGS stress 2299c678832SJames Wright PetscCall(ApplyAddCeedOperatorLocalToLocal(Q_loc, G_loc, sgs_dd_data->op_sgs_apply_ctx)); 2309c678832SJames Wright 2319c678832SJames Wright // -- Return local SGS vector 2329c678832SJames Wright PetscCall(VecC2P(sgs_dd_data->sgs_nodal_ceed, sgs_nodal_mem_type, SGSNodal_loc)); 2339c678832SJames Wright PetscCall(DMRestoreLocalVector(sgs_dd_data->dm_sgs, &SGSNodal_loc)); 2349c678832SJames Wright PetscCall(DMRestoreGlobalVector(user->grad_velo_proj->dm, &VelocityGradient)); 2359c678832SJames Wright 236d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 2379c678832SJames Wright } 2389c678832SJames Wright 23962b7942eSJames Wright // @brief B = A^T, A is NxM, B is MxN 24062b7942eSJames Wright PetscErrorCode TransposeMatrix(const PetscScalar *A, PetscScalar *B, const PetscInt N, const PetscInt M) { 24162b7942eSJames Wright PetscFunctionBeginUser; 24262b7942eSJames Wright for (PetscInt i = 0; i < N; i++) { 24362b7942eSJames Wright for (PetscInt j = 0; j < M; j++) { 24462b7942eSJames Wright B[j * N + i] = A[i * M + j]; 24562b7942eSJames Wright } 24662b7942eSJames Wright } 247d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 24862b7942eSJames Wright } 24962b7942eSJames Wright 25062b7942eSJames Wright // @brief Read neural network coefficients from file and put into context struct 25162b7942eSJames Wright PetscErrorCode SGS_DD_ModelContextFill(MPI_Comm comm, char data_dir[PETSC_MAX_PATH_LEN], SGS_DDModelContext *psgsdd_ctx) { 25262b7942eSJames Wright SGS_DDModelContext sgsdd_ctx; 25362b7942eSJames Wright PetscInt num_inputs = (*psgsdd_ctx)->num_inputs, num_outputs = (*psgsdd_ctx)->num_outputs, num_neurons = (*psgsdd_ctx)->num_neurons; 25462b7942eSJames Wright char file_path[PETSC_MAX_PATH_LEN]; 25562b7942eSJames Wright PetscScalar *temp; 25662b7942eSJames Wright 25762b7942eSJames Wright PetscFunctionBeginUser; 25862b7942eSJames Wright { 25962b7942eSJames Wright SGS_DDModelContext sgsdd_temp; 26062b7942eSJames Wright PetscCall(PetscNew(&sgsdd_temp)); 26162b7942eSJames Wright *sgsdd_temp = **psgsdd_ctx; 26262b7942eSJames Wright sgsdd_temp->offsets.bias1 = 0; 26362b7942eSJames Wright sgsdd_temp->offsets.bias2 = sgsdd_temp->offsets.bias1 + num_neurons; 26462b7942eSJames Wright sgsdd_temp->offsets.weight1 = sgsdd_temp->offsets.bias2 + num_neurons; 26562b7942eSJames Wright sgsdd_temp->offsets.weight2 = sgsdd_temp->offsets.weight1 + num_neurons * num_inputs; 26662b7942eSJames Wright sgsdd_temp->offsets.out_scaling = sgsdd_temp->offsets.weight2 + num_inputs * num_neurons; 26762b7942eSJames Wright PetscInt total_num_scalars = sgsdd_temp->offsets.out_scaling + 2 * num_outputs; 26862b7942eSJames Wright sgsdd_temp->total_bytes = sizeof(*sgsdd_ctx) + total_num_scalars * sizeof(sgsdd_ctx->data[0]); 26962b7942eSJames Wright PetscCall(PetscMalloc(sgsdd_temp->total_bytes, &sgsdd_ctx)); 27062b7942eSJames Wright *sgsdd_ctx = *sgsdd_temp; 27162b7942eSJames Wright PetscCall(PetscFree(sgsdd_temp)); 27262b7942eSJames Wright } 27362b7942eSJames Wright 27462b7942eSJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/%s", data_dir, "b1.dat")); 27562b7942eSJames Wright PetscCall(PHASTADatFileReadToArrayReal(comm, file_path, &sgsdd_ctx->data[sgsdd_ctx->offsets.bias1])); 27662b7942eSJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/%s", data_dir, "b2.dat")); 27762b7942eSJames Wright PetscCall(PHASTADatFileReadToArrayReal(comm, file_path, &sgsdd_ctx->data[sgsdd_ctx->offsets.bias2])); 27862b7942eSJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/%s", data_dir, "OutScaling.dat")); 27962b7942eSJames Wright PetscCall(PHASTADatFileReadToArrayReal(comm, file_path, &sgsdd_ctx->data[sgsdd_ctx->offsets.out_scaling])); 28062b7942eSJames Wright 28162b7942eSJames Wright { 28262b7942eSJames Wright PetscCall(PetscMalloc1(num_inputs * num_neurons, &temp)); 28362b7942eSJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/%s", data_dir, "w1.dat")); 28462b7942eSJames Wright PetscCall(PHASTADatFileReadToArrayReal(comm, file_path, temp)); 28562b7942eSJames Wright PetscCall(TransposeMatrix(temp, &sgsdd_ctx->data[sgsdd_ctx->offsets.weight1], num_inputs, num_neurons)); 28662b7942eSJames Wright PetscCall(PetscFree(temp)); 28762b7942eSJames Wright } 28862b7942eSJames Wright { 28962b7942eSJames Wright PetscCall(PetscMalloc1(num_outputs * num_neurons, &temp)); 29062b7942eSJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/%s", data_dir, "w2.dat")); 29162b7942eSJames Wright PetscCall(PHASTADatFileReadToArrayReal(comm, file_path, temp)); 29262b7942eSJames Wright PetscCall(TransposeMatrix(temp, &sgsdd_ctx->data[sgsdd_ctx->offsets.weight2], num_neurons, num_outputs)); 29362b7942eSJames Wright PetscCall(PetscFree(temp)); 29462b7942eSJames Wright } 29562b7942eSJames Wright 29662b7942eSJames Wright PetscCall(PetscFree(*psgsdd_ctx)); 29762b7942eSJames Wright *psgsdd_ctx = sgsdd_ctx; 298d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 29962b7942eSJames Wright } 30062b7942eSJames Wright 30162b7942eSJames Wright PetscErrorCode SGS_DD_ModelSetup(Ceed ceed, User user, CeedData ceed_data, ProblemData *problem) { 302ee1455b7SJames Wright PetscReal alpha = 0; 30362b7942eSJames Wright SGS_DDModelContext sgsdd_ctx; 30462b7942eSJames Wright MPI_Comm comm = user->comm; 305ee1455b7SJames Wright char sgs_dd_dir[PETSC_MAX_PATH_LEN] = "./dd_sgs_parameters"; 306c38c977aSJames Wright SGS_DD_ModelSetupData sgs_dd_setup_data; 307ee1455b7SJames Wright NewtonianIdealGasContext gas; 30862b7942eSJames Wright PetscFunctionBeginUser; 30962b7942eSJames Wright 3109ab09d51SJames Wright PetscCall(VelocityGradientProjectionSetup(ceed, user, ceed_data, problem)); 3119ab09d51SJames Wright 31262b7942eSJames Wright PetscCall(PetscNew(&sgsdd_ctx)); 31362b7942eSJames Wright 3144b0f6111SJames Wright PetscOptionsBegin(comm, NULL, "SGS Data-Driven Model Options", NULL); 31562b7942eSJames Wright PetscCall(PetscOptionsReal("-sgs_model_dd_leakyrelu_alpha", "Slope parameter for Leaky ReLU activation function", NULL, alpha, &alpha, NULL)); 31662b7942eSJames Wright PetscCall(PetscOptionsString("-sgs_model_dd_parameter_dir", "Path to directory with model parameters (weights, biases, etc.)", NULL, sgs_dd_dir, 31762b7942eSJames Wright sgs_dd_dir, sizeof(sgs_dd_dir), NULL)); 31862b7942eSJames Wright PetscOptionsEnd(); 31962b7942eSJames Wright 3204b0f6111SJames Wright sgsdd_ctx->num_layers = 1; 32162b7942eSJames Wright sgsdd_ctx->num_inputs = 6; 32262b7942eSJames Wright sgsdd_ctx->num_outputs = 6; 32362b7942eSJames Wright sgsdd_ctx->num_neurons = 20; 32462b7942eSJames Wright sgsdd_ctx->alpha = alpha; 32562b7942eSJames Wright 32601ab89c1SJames Wright PetscCall(SGS_DD_ModelContextFill(comm, sgs_dd_dir, &sgsdd_ctx)); 32762b7942eSJames Wright 328ee1455b7SJames Wright // -- Create DM for storing SGS tensor at nodes 329ee1455b7SJames Wright PetscCall(PetscNew(&user->sgs_dd_data)); 33067263decSKenneth E. Jansen PetscCall( 33167263decSKenneth E. Jansen SGS_DD_ModelCreateDM(user->dm, &user->sgs_dd_data->dm_sgs, user->app_ctx->degree, user->app_ctx->q_extra, &user->sgs_dd_data->num_comp_sgs)); 332ee1455b7SJames Wright 333c38c977aSJames Wright PetscCall(PetscNew(&sgs_dd_setup_data)); 334c38c977aSJames Wright 335b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionContextGetDataRead(problem->apply_vol_ifunction.qfunction_context, CEED_MEM_HOST, &gas)); 336ee1455b7SJames Wright sgsdd_ctx->gas = *gas; 337b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionContextRestoreDataRead(problem->apply_vol_ifunction.qfunction_context, &gas)); 338b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &sgs_dd_setup_data->sgsdd_qfctx)); 339b4c37c5cSJames Wright PetscCallCeed(ceed, 340b4c37c5cSJames Wright CeedQFunctionContextSetData(sgs_dd_setup_data->sgsdd_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sgsdd_ctx->total_bytes, sgsdd_ctx)); 341b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(sgs_dd_setup_data->sgsdd_qfctx, CEED_MEM_HOST, FreeContextPetsc)); 342ee1455b7SJames Wright 343c38c977aSJames Wright // -- Compute and store anisotropy tensor 344c38c977aSJames Wright PetscCall(GridAnisotropyTensorProjectionSetupApply(ceed, user, ceed_data, &sgs_dd_setup_data->elem_restr_grid_aniso, 345c38c977aSJames Wright &sgs_dd_setup_data->grid_aniso_ceed)); 346c38c977aSJames Wright 347ee1455b7SJames Wright // -- Create Nodal Evaluation Operator 348ee1455b7SJames Wright PetscCall(SGS_DD_ModelSetupNodalEvaluation(ceed, user, ceed_data, sgs_dd_setup_data)); 349ee1455b7SJames Wright 3509c678832SJames Wright // -- Create Operator to evalutate residual of SGS stress 3519c678832SJames Wright PetscCall(SGS_ModelSetupNodalIFunction(ceed, user, ceed_data, sgs_dd_setup_data)); 3529c678832SJames Wright 353c38c977aSJames Wright PetscCall(SGS_DD_ModelSetupDataDestroy(sgs_dd_setup_data)); 354d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 35562b7942eSJames Wright } 356ee1455b7SJames Wright 357ee1455b7SJames Wright PetscErrorCode SGS_DD_DataDestroy(SGS_DD_Data sgs_dd_data) { 358ee1455b7SJames Wright PetscFunctionBeginUser; 359d949ddfcSJames Wright if (!sgs_dd_data) PetscFunctionReturn(PETSC_SUCCESS); 360b4c37c5cSJames Wright Ceed ceed = sgs_dd_data->op_sgs_apply_ctx->ceed; 361ee1455b7SJames Wright 362b4c37c5cSJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&sgs_dd_data->sgs_nodal_ceed)); 363ee1455b7SJames Wright PetscCall(OperatorApplyContextDestroy(sgs_dd_data->op_nodal_evaluation_ctx)); 364ee1455b7SJames Wright PetscCall(DMDestroy(&sgs_dd_data->dm_sgs)); 365ee1455b7SJames Wright PetscCall(PetscFree(sgs_dd_data)); 366ee1455b7SJames Wright 367d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 368ee1455b7SJames Wright } 369