1aa0b7f76SJames Wright // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors. 2aa0b7f76SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3aa0b7f76SJames Wright // 4aa0b7f76SJames Wright // SPDX-License-Identifier: BSD-2-Clause 5aa0b7f76SJames Wright // 6aa0b7f76SJames Wright // This file is part of CEED: http://github.com/ceed 7aa0b7f76SJames Wright 8aa0b7f76SJames Wright #include "../../qfunctions/sgs_dd_training.h" 9aa0b7f76SJames Wright 10aa0b7f76SJames Wright #include <petscdmplex.h> 11aa0b7f76SJames Wright 12aa0b7f76SJames Wright #include "../../include/smartsim.h" 13aa0b7f76SJames Wright #include "../../navierstokes.h" 14aa0b7f76SJames Wright 15aa0b7f76SJames Wright typedef struct { 16aa0b7f76SJames Wright CeedElemRestriction elem_restr_grid_aniso; 17aa0b7f76SJames Wright CeedVector grid_aniso_ceed; 18aa0b7f76SJames Wright CeedQFunctionContext sgs_dd_train_qfctx; 19aa0b7f76SJames Wright } *SGS_DD_TrainingSetupData; 20aa0b7f76SJames Wright 21aa0b7f76SJames Wright static PetscErrorCode SGS_DD_TrainingSetupDataDestroy(SGS_DD_TrainingSetupData sgs_dd_train_setup_data) { 22aa0b7f76SJames Wright Ceed ceed; 23aa0b7f76SJames Wright 24aa0b7f76SJames Wright PetscFunctionBeginUser; 25aa0b7f76SJames Wright PetscCall(CeedElemRestrictionGetCeed(sgs_dd_train_setup_data->elem_restr_grid_aniso, &ceed)); 26aa0b7f76SJames Wright 27aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&sgs_dd_train_setup_data->elem_restr_grid_aniso)); 28aa0b7f76SJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&sgs_dd_train_setup_data->grid_aniso_ceed)); 29aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionContextDestroy(&sgs_dd_train_setup_data->sgs_dd_train_qfctx)); 30aa0b7f76SJames Wright PetscCall(PetscFree(sgs_dd_train_setup_data)); 31aa0b7f76SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 32aa0b7f76SJames Wright } 33aa0b7f76SJames Wright 34aa0b7f76SJames Wright // @brief Create DM for storing data-drive SGS model inputs 35aa0b7f76SJames Wright static PetscErrorCode SGS_DD_TrainingCreateDM(DM dm_source, DM *dm_dd_training, PetscInt degree, PetscInt q_extra, PetscInt *num_components) { 36aa0b7f76SJames Wright PetscSection section; 37aa0b7f76SJames Wright 38aa0b7f76SJames Wright PetscFunctionBeginUser; 39aa0b7f76SJames Wright *num_components = 12; 40aa0b7f76SJames Wright 41aa0b7f76SJames Wright PetscCall(DMClone(dm_source, dm_dd_training)); 42aa0b7f76SJames Wright PetscCall(PetscObjectSetName((PetscObject)*dm_dd_training, "Data-Driven SGS Training Data")); 43aa0b7f76SJames Wright 44aa0b7f76SJames Wright PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, degree, 1, q_extra, 1, num_components, *dm_dd_training)); 45aa0b7f76SJames Wright 46aa0b7f76SJames Wright PetscCall(DMGetLocalSection(*dm_dd_training, §ion)); 47aa0b7f76SJames Wright PetscCall(PetscSectionSetFieldName(section, 0, "Data-Driven SGS Training Data")); 48aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 0, "SGSInput1")); 49aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 1, "SGSInput2")); 50aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 2, "SGSInput3")); 51aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 3, "SGSInput4")); 52aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 4, "SGSInput5")); 53aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 5, "SGSInput6")); 54aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 6, "FilteredSGSXX")); 55aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 7, "FilteredSGSYY")); 56aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 8, "FilteredSGSZZ")); 57aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 9, "FilteredSGSYZ")); 58aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 10, "FilteredSGSXZ")); 59aa0b7f76SJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 11, "FilteredSGSXY")); 60aa0b7f76SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 61aa0b7f76SJames Wright }; 62aa0b7f76SJames Wright 63aa0b7f76SJames Wright // @brief Create CeedOperator to calculate training data for data-drive SGS model at nodes 64aa0b7f76SJames Wright static PetscErrorCode SetupTrainingDataCalculation(Ceed ceed, User user, CeedData ceed_data, ProblemData *problem, 65aa0b7f76SJames Wright SGS_DD_TrainingSetupData sgs_dd_train_setup_data) { 66aa0b7f76SJames Wright SGS_DD_TrainingData sgs_dd_train = user->sgs_dd_train; 67aa0b7f76SJames Wright CeedQFunction qf_sgs_dd_train; 68aa0b7f76SJames Wright CeedOperator op_sgs_dd_train; 69aa0b7f76SJames Wright CeedInt num_comp_grad_velo, num_comp_grid_aniso; 70aa0b7f76SJames Wright CeedVector inv_multiplicity, filtered_fields; 71aa0b7f76SJames Wright CeedElemRestriction elem_restr_inv_multiplicity, elem_restr_grad_velo, elem_restr_sgs_train; 72aa0b7f76SJames Wright DMLabel domain_label = NULL; 73aa0b7f76SJames Wright PetscInt label_value = 0, height = 0, dm_field = 0; 74aa0b7f76SJames Wright 75aa0b7f76SJames Wright PetscFunctionBeginUser; 76aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(sgs_dd_train_setup_data->elem_restr_grid_aniso, &num_comp_grid_aniso)); 77aa0b7f76SJames Wright 78aa0b7f76SJames Wright PetscCall(DMPlexCeedElemRestrictionCreate(ceed, sgs_dd_train->dm_dd_training, domain_label, label_value, height, dm_field, &elem_restr_sgs_train)); 79aa0b7f76SJames Wright 80aa0b7f76SJames Wright { // -- Create inverse multiplicity for correcting nodal assembly 81aa0b7f76SJames Wright CeedVector multiplicity; 82aa0b7f76SJames Wright CeedQFunction qf_multiplicity; 83aa0b7f76SJames Wright CeedOperator op_multiplicity; 84aa0b7f76SJames Wright CeedInt num_comp_q; 85aa0b7f76SJames Wright 86aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q)); 87aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &multiplicity, NULL)); 88aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, multiplicity)); 89aa0b7f76SJames Wright PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, sgs_dd_train->dm_dd_training, domain_label, label_value, height, 1, 90aa0b7f76SJames Wright &elem_restr_inv_multiplicity)); 91aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_inv_multiplicity, &inv_multiplicity, NULL)); 92aa0b7f76SJames Wright 93aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, InverseMultiplicity, InverseMultiplicity_loc, &qf_multiplicity)); 94aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_multiplicity, "multiplicity", num_comp_q, CEED_EVAL_NONE)); 95aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_multiplicity, "inverse multiplicity", 1, CEED_EVAL_NONE)); 96aa0b7f76SJames Wright 97aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_multiplicity, NULL, NULL, &op_multiplicity)); 98aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorSetName(op_multiplicity, "SGS DD Training Inputs - Create Multiplicity Scaling")); 99c6271fa9SJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_multiplicity, "multiplicity", ceed_data->elem_restr_q, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 100c6271fa9SJeremy L Thompson PetscCallCeed(ceed, 101c6271fa9SJeremy L Thompson CeedOperatorSetField(op_multiplicity, "inverse multiplicity", elem_restr_inv_multiplicity, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 102aa0b7f76SJames Wright 103aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorApply(op_multiplicity, multiplicity, inv_multiplicity, CEED_REQUEST_IMMEDIATE)); 104aa0b7f76SJames Wright 105aa0b7f76SJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&multiplicity)); 106aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_multiplicity)); 107aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_multiplicity)); 108aa0b7f76SJames Wright } 109aa0b7f76SJames Wright 110aa0b7f76SJames Wright CeedElemRestriction elem_restr_filtered_state; 111aa0b7f76SJames Wright CeedInt num_comp_filtered_state; 112aa0b7f76SJames Wright { // -- Setup filtered velocity gradient projection 113aa0b7f76SJames Wright CeedBasis basis_filtered_state; 114aa0b7f76SJames Wright CeedOperatorField op_field; 115aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorGetFieldByName(user->diff_filter->op_rhs_ctx->op, "v0", &op_field)); 116aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(op_field, &elem_restr_filtered_state)); 117aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_filtered_state, &num_comp_filtered_state)); 118aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorFieldGetBasis(op_field, &basis_filtered_state)); 119aa0b7f76SJames Wright PetscCall(VelocityGradientProjectionSetup(ceed, user, ceed_data, problem, STATEVAR_PRIMITIVE, elem_restr_filtered_state, basis_filtered_state, 120aa0b7f76SJames Wright &sgs_dd_train->filtered_grad_velo_proj)); 121aa0b7f76SJames Wright // Get velocity gradient information 122aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorGetFieldByName(sgs_dd_train->filtered_grad_velo_proj->l2_rhs_ctx->op, "velocity gradient", &op_field)); 123aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(op_field, &elem_restr_grad_velo)); 124aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_grad_velo, &num_comp_grad_velo)); 125aa0b7f76SJames Wright } 126aa0b7f76SJames Wright 127aa0b7f76SJames Wright CeedElemRestriction elem_restr_filtered_velo_prod; 128aa0b7f76SJames Wright CeedInt num_comp_filtered_velo_prod; 129aa0b7f76SJames Wright { // Get filtered velocity product information 130aa0b7f76SJames Wright CeedOperatorField op_field; 131aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorGetFieldByName(user->diff_filter->op_rhs_ctx->op, "v1", &op_field)); 132aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(op_field, &elem_restr_filtered_velo_prod)); 133aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_filtered_velo_prod, &num_comp_filtered_velo_prod)); 134aa0b7f76SJames Wright } 135aa0b7f76SJames Wright 136aa0b7f76SJames Wright // -- Create operator for generating training data at nodes 137aa0b7f76SJames Wright // Differential Filter only provides filtered primitive variables 138aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, ComputeSGS_DDAnisotropicTrainingDataNodal_Prim, 139aa0b7f76SJames Wright ComputeSGS_DDAnisotropicTrainingDataNodal_Prim_loc, &qf_sgs_dd_train)); 140aa0b7f76SJames Wright 141aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionSetContext(qf_sgs_dd_train, sgs_dd_train_setup_data->sgs_dd_train_qfctx)); 142aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_train, "q", num_comp_filtered_state, CEED_EVAL_NONE)); 143aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_train, "velocity product", num_comp_filtered_velo_prod, CEED_EVAL_NONE)); 144aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_train, "gradient velocity", num_comp_grad_velo, CEED_EVAL_NONE)); 145aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_train, "anisotropy tensor", num_comp_grid_aniso, CEED_EVAL_NONE)); 146aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_sgs_dd_train, "inverse multiplicity", 1, CEED_EVAL_NONE)); 147aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_sgs_dd_train, "training data", sgs_dd_train->num_comp_dd_inputs, CEED_EVAL_NONE)); 148aa0b7f76SJames Wright 149aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_filtered_state, &filtered_fields, NULL)); 150aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_sgs_dd_train, NULL, NULL, &op_sgs_dd_train)); 151c6271fa9SJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_train, "q", elem_restr_filtered_state, CEED_BASIS_NONE, filtered_fields)); 152c6271fa9SJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_train, "velocity product", elem_restr_filtered_velo_prod, CEED_BASIS_NONE, filtered_fields)); 153c6271fa9SJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_train, "gradient velocity", elem_restr_grad_velo, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 154c6271fa9SJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_train, "anisotropy tensor", sgs_dd_train_setup_data->elem_restr_grid_aniso, CEED_BASIS_NONE, 155c6271fa9SJeremy L Thompson sgs_dd_train_setup_data->grid_aniso_ceed)); 156c6271fa9SJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_train, "inverse multiplicity", elem_restr_inv_multiplicity, CEED_BASIS_NONE, inv_multiplicity)); 157c6271fa9SJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_sgs_dd_train, "training data", elem_restr_sgs_train, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 158aa0b7f76SJames Wright 159aa0b7f76SJames Wright PetscCall(OperatorApplyContextCreate(sgs_dd_train->filtered_grad_velo_proj->dm, sgs_dd_train->dm_dd_training, ceed, op_sgs_dd_train, NULL, NULL, 160aa0b7f76SJames Wright NULL, NULL, &sgs_dd_train->op_training_data_calc_ctx)); 161aa0b7f76SJames Wright 162aa0b7f76SJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&inv_multiplicity)); 163aa0b7f76SJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&filtered_fields)); 164aa0b7f76SJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_inv_multiplicity)); 165aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_sgs_dd_train)); 166aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_sgs_dd_train)); 167aa0b7f76SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 168aa0b7f76SJames Wright } 169aa0b7f76SJames Wright 170aa0b7f76SJames Wright PetscErrorCode SGS_DD_TrainingSetup(Ceed ceed, User user, CeedData ceed_data, ProblemData *problem) { 171aa0b7f76SJames Wright SGS_DDTrainingContext sgsdd_train_qfctx; 172aa0b7f76SJames Wright SGS_DD_TrainingSetupData sgs_dd_train_setup_data; 173aa0b7f76SJames Wright 174aa0b7f76SJames Wright PetscFunctionBeginUser; 175aa0b7f76SJames Wright if (!user->diff_filter) PetscCall(DifferentialFilterSetup(ceed, user, ceed_data, problem)); 176aa0b7f76SJames Wright if (!user->smartsim) PetscCall(SmartSimSetup(user)); 177aa0b7f76SJames Wright 178aa0b7f76SJames Wright PetscCall(PetscNew(&sgsdd_train_qfctx)); 179aa0b7f76SJames Wright PetscCall(PetscNew(&sgs_dd_train_setup_data)); 180aa0b7f76SJames Wright PetscCall(PetscNew(&user->sgs_dd_train)); 181aa0b7f76SJames Wright SGS_DD_TrainingData sgs_dd_train = user->sgs_dd_train; 182aa0b7f76SJames Wright 183aa0b7f76SJames Wright sgs_dd_train->overwrite_training_data = PETSC_TRUE; 184aa0b7f76SJames Wright sgs_dd_train->write_data_interval = 1; 185aa0b7f76SJames Wright PetscOptionsBegin(user->comm, NULL, "SGS Data-Driven Training Options", NULL); 186aa0b7f76SJames Wright PetscCall(PetscOptionsInt("-sgs_train_write_data_interval", "Number of timesteps between writing data into database", NULL, 187aa0b7f76SJames Wright sgs_dd_train->write_data_interval, &sgs_dd_train->write_data_interval, NULL)); 188aa0b7f76SJames Wright PetscCall(PetscOptionsBool("-sgs_train_overwrite_data", "Overwrite old training data in the database", NULL, sgs_dd_train->overwrite_training_data, 189aa0b7f76SJames Wright &sgs_dd_train->overwrite_training_data, NULL)); 190aa0b7f76SJames Wright PetscOptionsEnd(); 191aa0b7f76SJames Wright 192aa0b7f76SJames Wright // -- Create DM for storing training data 193aa0b7f76SJames Wright PetscCall(SGS_DD_TrainingCreateDM(user->dm, &sgs_dd_train->dm_dd_training, user->app_ctx->degree, user->app_ctx->q_extra, 194aa0b7f76SJames Wright &sgs_dd_train->num_comp_dd_inputs)); 195aa0b7f76SJames Wright 196aa0b7f76SJames Wright { // -- Create QFunction Context 197aa0b7f76SJames Wright NewtonianIdealGasContext gas; 198aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionContextGetDataRead(problem->apply_vol_ifunction.qfunction_context, CEED_MEM_HOST, &gas)); 199aa0b7f76SJames Wright sgsdd_train_qfctx->gas = *gas; 200aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionContextRestoreDataRead(problem->apply_vol_ifunction.qfunction_context, &gas)); 201aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &sgs_dd_train_setup_data->sgs_dd_train_qfctx)); 202aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionContextSetData(sgs_dd_train_setup_data->sgs_dd_train_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, 203aa0b7f76SJames Wright sizeof(*sgsdd_train_qfctx), sgsdd_train_qfctx)); 204aa0b7f76SJames Wright PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(sgs_dd_train_setup_data->sgs_dd_train_qfctx, CEED_MEM_HOST, FreeContextPetsc)); 205aa0b7f76SJames Wright } 206aa0b7f76SJames Wright 207aa0b7f76SJames Wright { // -- Send training data array info to SmartRedis database 208aa0b7f76SJames Wright PetscMPIInt rank, num_ranks; 209aa0b7f76SJames Wright SmartSimData smartsim = user->smartsim; 210aa0b7f76SJames Wright PetscCallMPI(MPI_Comm_rank(user->comm, &rank)); 211aa0b7f76SJames Wright PetscCallMPI(MPI_Comm_size(user->comm, &num_ranks)); 212aa0b7f76SJames Wright 213aa0b7f76SJames Wright { 214aa0b7f76SJames Wright PetscSection global_section; 215aa0b7f76SJames Wright PetscInt num_dofs, num_comps; 216aa0b7f76SJames Wright PetscCall(DMGetGlobalSection(sgs_dd_train->dm_dd_training, &global_section)); 217aa0b7f76SJames Wright PetscCall(DMGetGlobalVectorInfo(sgs_dd_train->dm_dd_training, &num_dofs, NULL, NULL)); 218aa0b7f76SJames Wright PetscCall(PetscSectionGetFieldComponents(global_section, 0, &num_comps)); 219aa0b7f76SJames Wright sgs_dd_train->training_data_array_dims[0] = num_dofs / num_comps; 220aa0b7f76SJames Wright sgs_dd_train->training_data_array_dims[1] = num_comps; 221aa0b7f76SJames Wright } 222aa0b7f76SJames Wright 223aa0b7f76SJames Wright if (rank % smartsim->collocated_database_num_ranks == 0) { 224aa0b7f76SJames Wright size_t array_info_dim = 6; 225*f4632befSRiccardo Balin PetscInt64 array_info[6] = {0}, num_features = 6; 226aa0b7f76SJames Wright 227aa0b7f76SJames Wright array_info[0] = sgs_dd_train->training_data_array_dims[0]; 228aa0b7f76SJames Wright array_info[1] = sgs_dd_train->training_data_array_dims[1]; 229aa0b7f76SJames Wright array_info[2] = num_features; 230aa0b7f76SJames Wright array_info[3] = num_ranks; 231aa0b7f76SJames Wright array_info[4] = smartsim->collocated_database_num_ranks; 232aa0b7f76SJames Wright array_info[5] = rank; 233aa0b7f76SJames Wright 234ad2e713eSRiccardo Balin PetscCall(PetscLogEventBegin(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 235*f4632befSRiccardo Balin PetscSmartRedisCall(put_tensor(smartsim->client, "sizeInfo", 8, array_info, &array_info_dim, 1, SRTensorTypeInt64, SRMemLayoutContiguous)); 236aa0b7f76SJames Wright PetscCall(SmartRedisVerifyPutTensor(smartsim->client, "sizeInfo", 8)); 237ad2e713eSRiccardo Balin PetscCall(PetscLogEventEnd(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 238aa0b7f76SJames Wright 239aa0b7f76SJames Wright // -- Send array that communicates if tensors are overwritten in database 240*f4632befSRiccardo Balin PetscInt64 tensor_overwrite[2] = {sgs_dd_train->overwrite_training_data}; 241aa0b7f76SJames Wright size_t dim_2[1] = {2}; 242ad2e713eSRiccardo Balin PetscCall(PetscLogEventBegin(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 243*f4632befSRiccardo Balin PetscSmartRedisCall(put_tensor(smartsim->client, "tensor-ow", 9, tensor_overwrite, dim_2, 1, SRTensorTypeInt64, SRMemLayoutContiguous)); 244aa0b7f76SJames Wright PetscCall(SmartRedisVerifyPutTensor(smartsim->client, "tensor-ow", 9)); 245ad2e713eSRiccardo Balin PetscCall(PetscLogEventEnd(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 246aa0b7f76SJames Wright } 247aa0b7f76SJames Wright } 248aa0b7f76SJames Wright 249aa0b7f76SJames Wright // -- Compute and store anisotropy tensor 250aa0b7f76SJames Wright PetscCall(GridAnisotropyTensorProjectionSetupApply(ceed, user, ceed_data, &sgs_dd_train_setup_data->elem_restr_grid_aniso, 251aa0b7f76SJames Wright &sgs_dd_train_setup_data->grid_aniso_ceed)); 252aa0b7f76SJames Wright 253aa0b7f76SJames Wright // -- Create Nodal Evaluation Operator 254aa0b7f76SJames Wright PetscCall(SetupTrainingDataCalculation(ceed, user, ceed_data, problem, sgs_dd_train_setup_data)); 255aa0b7f76SJames Wright 256aa0b7f76SJames Wright PetscCall(SGS_DD_TrainingSetupDataDestroy(sgs_dd_train_setup_data)); 257aa0b7f76SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 258aa0b7f76SJames Wright } 259aa0b7f76SJames Wright 260aa0b7f76SJames Wright PetscErrorCode TSMonitor_SGS_DD_Training(TS ts, PetscInt step_num, PetscReal solution_time, Vec Q, void *ctx) { 261aa0b7f76SJames Wright User user = (User)ctx; 262aa0b7f76SJames Wright Ceed ceed = user->ceed; 263aa0b7f76SJames Wright SGS_DD_TrainingData sgs_dd_train = user->sgs_dd_train; 264aa0b7f76SJames Wright SmartSimData smartsim = user->smartsim; 265aa0b7f76SJames Wright Vec TrainingData; 266aa0b7f76SJames Wright 267aa0b7f76SJames Wright PetscFunctionBeginUser; 268aa0b7f76SJames Wright if (step_num % sgs_dd_train->write_data_interval != 0) PetscFunctionReturn(PETSC_SUCCESS); 269aa0b7f76SJames Wright PetscCall(DMGetGlobalVector(sgs_dd_train->dm_dd_training, &TrainingData)); 270aa0b7f76SJames Wright 271ad2e713eSRiccardo Balin PetscCall(PetscLogEventBegin(FLUIDS_TrainDataCompute, 0, 0, 0, 0)); 272aa0b7f76SJames Wright { // -- Compute and assemble training data 273aa0b7f76SJames Wright Vec FilteredVelocityGradient, FilteredFields, FilteredFields_loc; 274aa0b7f76SJames Wright PetscMemType filtered_fields_mem_type; 275aa0b7f76SJames Wright CeedVector filtered_fields; 276aa0b7f76SJames Wright 277aa0b7f76SJames Wright PetscCall(DMGetGlobalVector(user->diff_filter->dm_filter, &FilteredFields)); 278aa0b7f76SJames Wright PetscCall(DMGetLocalVector(user->diff_filter->dm_filter, &FilteredFields_loc)); 279aa0b7f76SJames Wright 280aa0b7f76SJames Wright PetscCall(DifferentialFilterApply(user, solution_time, Q, FilteredFields)); 281aa0b7f76SJames Wright PetscCall(DMGlobalToLocal(user->diff_filter->dm_filter, FilteredFields, INSERT_VALUES, FilteredFields_loc)); 282aa0b7f76SJames Wright 283aa0b7f76SJames Wright PetscCall(DMGetGlobalVector(sgs_dd_train->filtered_grad_velo_proj->dm, &FilteredVelocityGradient)); 284aa0b7f76SJames Wright PetscCall(VelocityGradientProjectionApply(sgs_dd_train->filtered_grad_velo_proj, FilteredFields_loc, FilteredVelocityGradient)); 285aa0b7f76SJames Wright 286aa0b7f76SJames Wright { 287aa0b7f76SJames Wright CeedOperatorField op_field; 288aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorGetFieldByName(sgs_dd_train->op_training_data_calc_ctx->op, "q", &op_field)); 289aa0b7f76SJames Wright PetscCallCeed(ceed, CeedOperatorFieldGetVector(op_field, &filtered_fields)); 290aa0b7f76SJames Wright } 291aa0b7f76SJames Wright PetscCall(VecP2C(FilteredFields_loc, &filtered_fields_mem_type, filtered_fields)); // filtered_fields is an implicit input 292aa0b7f76SJames Wright 293aa0b7f76SJames Wright PetscCall(ApplyCeedOperatorGlobalToGlobal(FilteredVelocityGradient, TrainingData, sgs_dd_train->op_training_data_calc_ctx)); 294aa0b7f76SJames Wright 295aa0b7f76SJames Wright PetscCall(VecC2P(filtered_fields, filtered_fields_mem_type, FilteredFields_loc)); 296aa0b7f76SJames Wright 297aa0b7f76SJames Wright PetscCall(DMRestoreGlobalVector(sgs_dd_train->filtered_grad_velo_proj->dm, &FilteredVelocityGradient)); 298aa0b7f76SJames Wright PetscCall(DMRestoreGlobalVector(user->diff_filter->dm_filter, &FilteredFields)); 299aa0b7f76SJames Wright PetscCall(DMRestoreLocalVector(user->diff_filter->dm_filter, &FilteredFields_loc)); 300aa0b7f76SJames Wright } 301ad2e713eSRiccardo Balin PetscCall(PetscLogEventEnd(FLUIDS_TrainDataCompute, 0, 0, 0, 0)); 302aa0b7f76SJames Wright 303aa0b7f76SJames Wright { // -- Send training data to SmartSim 304aa0b7f76SJames Wright char array_key[PETSC_MAX_PATH_LEN]; 305aa0b7f76SJames Wright size_t array_key_len; 306aa0b7f76SJames Wright PetscMPIInt rank; 307aa0b7f76SJames Wright 308aa0b7f76SJames Wright PetscCallMPI(MPI_Comm_rank(user->comm, &rank)); 309aa0b7f76SJames Wright 310aa0b7f76SJames Wright if (sgs_dd_train->overwrite_training_data) { 311aa0b7f76SJames Wright PetscCall(PetscSNPrintf(array_key, sizeof array_key, "%s", smartsim->rank_id_name)); 312aa0b7f76SJames Wright } else { 313aa0b7f76SJames Wright PetscCall(PetscSNPrintf(array_key, sizeof array_key, "%s.%" PetscInt_FMT, smartsim->rank_id_name, step_num)); 314aa0b7f76SJames Wright } 315aa0b7f76SJames Wright PetscCall(PetscStrlen(array_key, &array_key_len)); 316aa0b7f76SJames Wright 317aa0b7f76SJames Wright { 318aa0b7f76SJames Wright const PetscScalar *training_data; 319aa0b7f76SJames Wright PetscCall(VecGetArrayRead(TrainingData, &training_data)); 320ad2e713eSRiccardo Balin PetscCall(PetscLogEventBegin(FLUIDS_SmartRedis_Train, 0, 0, 0, 0)); 321ff6b888aSJames Wright PetscSmartRedisCall(put_tensor(smartsim->client, array_key, array_key_len, (void *)training_data, sgs_dd_train->training_data_array_dims, 2, 322aa0b7f76SJames Wright SRTensorTypeDouble, SRMemLayoutContiguous)); 323ad2e713eSRiccardo Balin PetscCall(PetscLogEventEnd(FLUIDS_SmartRedis_Train, 0, 0, 0, 0)); 324aa0b7f76SJames Wright PetscCall(VecRestoreArrayRead(TrainingData, &training_data)); 325aa0b7f76SJames Wright } 326ad2e713eSRiccardo Balin PetscCall(PetscLogEventBegin(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 327aa0b7f76SJames Wright PetscCall(SmartRedisVerifyPutTensor(smartsim->client, array_key, array_key_len)); 328ad2e713eSRiccardo Balin PetscCall(PetscLogEventEnd(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 329aa0b7f76SJames Wright 330aa0b7f76SJames Wright if (rank % smartsim->collocated_database_num_ranks == 0) { 331aa0b7f76SJames Wright size_t dim_2[1] = {2}; 332*f4632befSRiccardo Balin PetscInt64 step_array[2] = {step_num, step_num}; 333ad2e713eSRiccardo Balin PetscCall(PetscLogEventBegin(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 334*f4632befSRiccardo Balin PetscSmartRedisCall(put_tensor(smartsim->client, "step", 4, step_array, dim_2, 1, SRTensorTypeInt64, SRMemLayoutContiguous)); 335ad2e713eSRiccardo Balin PetscCall(PetscLogEventEnd(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 336aa0b7f76SJames Wright } 337aa0b7f76SJames Wright } 338aa0b7f76SJames Wright 339aa0b7f76SJames Wright PetscCall(DMRestoreGlobalVector(user->sgs_dd_train->dm_dd_training, &TrainingData)); 340aa0b7f76SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 341aa0b7f76SJames Wright } 342aa0b7f76SJames Wright 343632a41e1SJames Wright PetscErrorCode TSPostStep_SGS_DD_Training(TS ts) { 344632a41e1SJames Wright User user; 345632a41e1SJames Wright const char check_run_key[] = "check-run"; 346632a41e1SJames Wright PetscReal check_run[2] = {1}; 347632a41e1SJames Wright const size_t check_run_dims[1] = {2}; 348632a41e1SJames Wright size_t check_run_key_size; 349632a41e1SJames Wright 350632a41e1SJames Wright PetscFunctionBeginUser; 351632a41e1SJames Wright PetscCall(PetscStrlen(check_run_key, &check_run_key_size)); 352632a41e1SJames Wright PetscCall(TSGetApplicationContext(ts, &user)); 353632a41e1SJames Wright SmartSimData smartsim = user->smartsim; 354632a41e1SJames Wright 355ad2e713eSRiccardo Balin PetscCall(PetscLogEventBegin(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 356ff6b888aSJames Wright PetscSmartRedisCall( 357632a41e1SJames Wright unpack_tensor(smartsim->client, check_run_key, check_run_key_size, check_run, check_run_dims, 1, SRTensorTypeDouble, SRMemLayoutContiguous)); 358ad2e713eSRiccardo Balin PetscCall(PetscLogEventEnd(FLUIDS_SmartRedis_Meta, 0, 0, 0, 0)); 359632a41e1SJames Wright if (check_run[0] == 0) { 360632a41e1SJames Wright PetscCall(PetscPrintf(user->comm, "-- Simulation stopped by 'check-run' tensor in Redis database\n")); 361632a41e1SJames Wright PetscCall(TSSetConvergedReason(ts, TS_CONVERGED_USER)); 362632a41e1SJames Wright } 363632a41e1SJames Wright 364632a41e1SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 365632a41e1SJames Wright } 366632a41e1SJames Wright 367aa0b7f76SJames Wright PetscErrorCode SGS_DD_TrainingDataDestroy(SGS_DD_TrainingData sgs_dd_train) { 368aa0b7f76SJames Wright PetscFunctionBeginUser; 369aa0b7f76SJames Wright if (!sgs_dd_train) PetscFunctionReturn(PETSC_SUCCESS); 370aa0b7f76SJames Wright 371aa0b7f76SJames Wright PetscCall(OperatorApplyContextDestroy(sgs_dd_train->op_training_data_calc_ctx)); 372aa0b7f76SJames Wright PetscCall(NodalProjectionDataDestroy(sgs_dd_train->filtered_grad_velo_proj)); 373aa0b7f76SJames Wright PetscCall(DMDestroy(&sgs_dd_train->dm_dd_training)); 374aa0b7f76SJames Wright PetscCall(PetscFree(sgs_dd_train)); 375aa0b7f76SJames Wright 376aa0b7f76SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 377aa0b7f76SJames Wright } 378