1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 /// @file 9 /// Utility functions for setting up Blasius Boundary Layer 10 11 #include "../navierstokes.h" 12 #include "../qfunctions/blasius.h" 13 #include "stg_shur14.h" 14 15 static PetscErrorCode GetYNodeLocs(const MPI_Comm comm, 16 const char path[PETSC_MAX_PATH_LEN], PetscReal **pynodes, 17 PetscInt *nynodes) { 18 PetscErrorCode ierr; 19 PetscInt ndims, dims[2]; 20 FILE *fp; 21 const PetscInt char_array_len = 512; 22 char line[char_array_len]; 23 char **array; 24 PetscReal *node_locs; 25 PetscFunctionBeginUser; 26 27 ierr = PetscFOpen(comm, path, "r", &fp); CHKERRQ(ierr); 28 ierr = PetscSynchronizedFGets(comm, fp, char_array_len, line); CHKERRQ(ierr); 29 ierr = PetscStrToArray(line, ' ', &ndims, &array); CHKERRQ(ierr); 30 31 for (PetscInt i=0; i<ndims; i++) dims[i] = atoi(array[i]); 32 if (ndims<2) dims[1] = 1; // Assume 1 column of data is not otherwise specified 33 *nynodes = dims[0]; 34 ierr = PetscMalloc1(*nynodes, &node_locs); CHKERRQ(ierr); 35 36 for (PetscInt i=0; i<dims[0]; i++) { 37 ierr = PetscSynchronizedFGets(comm, fp, char_array_len, line); CHKERRQ(ierr); 38 ierr = PetscStrToArray(line, ' ', &ndims, &array); CHKERRQ(ierr); 39 if (ndims < dims[1]) SETERRQ(comm, -1, 40 "Line %d of %s does not contain enough columns (%d instead of %d)", i, 41 path, ndims, dims[1]); 42 43 node_locs[i] = (PetscReal) atof(array[0]); 44 } 45 ierr = PetscFClose(comm, fp); CHKERRQ(ierr); 46 *pynodes = node_locs; 47 PetscFunctionReturn(0); 48 } 49 50 /* \brief Modify the domain and mesh for blasius 51 * 52 * Modifies mesh such that `N` elements are within `refine_height` with a 53 * geometric growth ratio of `growth`. Excess elements are then distributed 54 * linearly in logspace to the top surface. 55 * 56 * The top surface is also angled downwards, so that it may be used as an 57 * outflow. It's angle is controlled by `top_angle` (in units of degrees). 58 * 59 * If `node_locs` is not NULL, then the nodes will be placed at `node_locs` 60 * locations. 61 */ 62 static PetscErrorCode ModifyMesh(MPI_Comm comm, DM dm, PetscInt dim, 63 PetscReal growth, PetscInt N, 64 PetscReal refine_height, PetscReal top_angle, 65 PetscReal node_locs[], PetscInt num_node_locs) { 66 PetscInt ierr, narr, ncoords; 67 PetscReal domain_min[3], domain_max[3], domain_size[3]; 68 PetscScalar *arr_coords; 69 Vec vec_coords; 70 PetscFunctionBeginUser; 71 72 PetscReal angle_coeff = tan(top_angle*(M_PI/180)); 73 74 // Get domain boundary information 75 ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr); 76 for (PetscInt i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 77 78 // Get coords array from DM 79 ierr = DMGetCoordinatesLocal(dm, &vec_coords); CHKERRQ(ierr); 80 ierr = VecGetLocalSize(vec_coords, &narr); CHKERRQ(ierr); 81 ierr = VecGetArray(vec_coords, &arr_coords); CHKERRQ(ierr); 82 83 PetscScalar (*coords)[dim] = (PetscScalar(*)[dim]) arr_coords; 84 ncoords = narr/dim; 85 86 // Get mesh information 87 PetscInt nmax = 3, faces[3]; 88 ierr = PetscOptionsGetIntArray(NULL, NULL, "-dm_plex_box_faces", faces, &nmax, 89 NULL); CHKERRQ(ierr); 90 // Get element size of the box mesh, for indexing each node 91 const PetscReal dybox = domain_size[1]/faces[1]; 92 93 if (!node_locs) { 94 // Calculate the first element height 95 PetscReal dy1 = refine_height*(growth-1)/(pow(growth, N)-1); 96 97 // Calculate log of sizing outside BL 98 PetscReal logdy = (log(domain_max[1]) - log(refine_height)) / (faces[1] - N); 99 100 for (PetscInt i=0; i<ncoords; i++) { 101 PetscInt y_box_index = round(coords[i][1]/dybox); 102 if (y_box_index <= N) { 103 coords[i][1] = (1 - (coords[i][0] - domain_min[0])*angle_coeff/domain_max[1]) 104 * dy1 * (pow(growth, coords[i][1]/dybox)-1)/(growth-1); 105 } else { 106 PetscInt j = y_box_index - N; 107 coords[i][1] = (1 - (coords[i][0] - domain_min[0])*angle_coeff/domain_max[1]) 108 * exp(log(refine_height) + logdy*j); 109 } 110 } 111 } else { 112 // Error checking 113 if (num_node_locs < faces[1] +1) 114 SETERRQ(comm, -1, "The y_node_locs_path has too few locations; " 115 "There are %d + 1 nodes, but only %d locations given", 116 faces[1]+1, num_node_locs); 117 if (num_node_locs > faces[1] +1) { 118 ierr = PetscPrintf(comm, "WARNING: y_node_locs_path has more locations (%d) " 119 "than the mesh has nodes (%d). This maybe unintended.", 120 num_node_locs, faces[1]+1); CHKERRQ(ierr); 121 } 122 PetscScalar max_y = node_locs[faces[1]]; 123 124 for (PetscInt i=0; i<ncoords; i++) { 125 // Determine which y-node we're at 126 PetscInt y_box_index = round(coords[i][1]/dybox); 127 // coords[i][1] = (1 - ((coords[i][0] - domain_min[0])/domain_size[0])*angle_coeff) 128 coords[i][1] = (1 - (coords[i][0] - domain_min[0])*angle_coeff/max_y) 129 * node_locs[y_box_index]; 130 } 131 } 132 133 ierr = VecRestoreArray(vec_coords, &arr_coords); CHKERRQ(ierr); 134 ierr = DMSetCoordinatesLocal(dm, vec_coords); CHKERRQ(ierr); 135 136 PetscFunctionReturn(0); 137 } 138 139 PetscErrorCode NS_BLASIUS(ProblemData *problem, DM dm, void *ctx) { 140 141 PetscInt ierr; 142 User user = *(User *)ctx; 143 MPI_Comm comm = PETSC_COMM_WORLD; 144 PetscBool use_stg = PETSC_FALSE; 145 BlasiusContext blasius_ctx; 146 NewtonianIdealGasContext newtonian_ig_ctx; 147 CeedQFunctionContext blasius_context; 148 149 PetscFunctionBeginUser; 150 ierr = NS_NEWTONIAN_IG(problem, dm, ctx); CHKERRQ(ierr); 151 ierr = PetscCalloc1(1, &blasius_ctx); CHKERRQ(ierr); 152 153 // ------------------------------------------------------ 154 // SET UP Blasius 155 // ------------------------------------------------------ 156 problem->ics.qfunction = ICsBlasius; 157 problem->ics.qfunction_loc = ICsBlasius_loc; 158 problem->apply_inflow_jacobian.qfunction = Blasius_Inflow_Jacobian; 159 problem->apply_inflow_jacobian.qfunction_loc = Blasius_Inflow_Jacobian_loc; 160 161 CeedScalar Uinf = 40; // m/s 162 CeedScalar delta0 = 4.2e-4; // m 163 CeedScalar theta0 = 288.; // K 164 CeedScalar P0 = 1.01e5; // Pa 165 PetscBool weakT = PETSC_FALSE; // weak density or temperature 166 PetscReal mesh_refine_height = 5.9e-4; // m 167 PetscReal mesh_growth = 1.08; // [-] 168 PetscInt mesh_Ndelta = 45; // [-] 169 PetscReal mesh_top_angle = 5; // degrees 170 char mesh_ynodes_path[PETSC_MAX_PATH_LEN] = ""; 171 172 PetscOptionsBegin(comm, NULL, "Options for CHANNEL problem", NULL); 173 ierr = PetscOptionsBool("-weakT", "Change from rho weak to T weak at inflow", 174 NULL, weakT, &weakT, NULL); CHKERRQ(ierr); 175 ierr = PetscOptionsScalar("-Uinf", "Velocity at boundary layer edge", 176 NULL, Uinf, &Uinf, NULL); CHKERRQ(ierr); 177 ierr = PetscOptionsScalar("-delta0", "Boundary layer height at inflow", 178 NULL, delta0, &delta0, NULL); CHKERRQ(ierr); 179 ierr = PetscOptionsScalar("-theta0", "Wall temperature", 180 NULL, theta0, &theta0, NULL); CHKERRQ(ierr); 181 ierr = PetscOptionsScalar("-P0", "Pressure at outflow", 182 NULL, P0, &P0, NULL); CHKERRQ(ierr); 183 ierr = PetscOptionsBoundedInt("-platemesh_Ndelta", 184 "Velocity at boundary layer edge", 185 NULL, mesh_Ndelta, &mesh_Ndelta, NULL, 1); CHKERRQ(ierr); 186 ierr = PetscOptionsScalar("-platemesh_refine_height", 187 "Height of boundary layer mesh refinement", 188 NULL, mesh_refine_height, &mesh_refine_height, NULL); CHKERRQ(ierr); 189 ierr = PetscOptionsScalar("-platemesh_growth", 190 "Geometric growth rate of boundary layer mesh", 191 NULL, mesh_growth, &mesh_growth, NULL); CHKERRQ(ierr); 192 ierr = PetscOptionsScalar("-platemesh_top_angle", 193 "Geometric top_angle rate of boundary layer mesh", 194 NULL, mesh_top_angle, &mesh_top_angle, NULL); CHKERRQ(ierr); 195 ierr = PetscOptionsString("-platemesh_y_node_locs_path", 196 "Path to file with y node locations. " 197 "If empty, will use the algorithmic mesh warping.", NULL, 198 mesh_ynodes_path, mesh_ynodes_path, 199 sizeof(mesh_ynodes_path), NULL); CHKERRQ(ierr); 200 ierr = PetscOptionsBool("-stg_use", "Use STG inflow boundary condition", 201 NULL, use_stg, &use_stg, NULL); CHKERRQ(ierr); 202 PetscOptionsEnd(); 203 204 PetscScalar meter = user->units->meter; 205 PetscScalar second = user->units->second; 206 PetscScalar Kelvin = user->units->Kelvin; 207 PetscScalar Pascal = user->units->Pascal; 208 209 theta0 *= Kelvin; 210 P0 *= Pascal; 211 Uinf *= meter / second; 212 delta0 *= meter; 213 214 PetscReal *mesh_ynodes = NULL; 215 PetscInt mesh_nynodes = 0; 216 if (strcmp(mesh_ynodes_path, "")) { 217 ierr = GetYNodeLocs(comm, mesh_ynodes_path, &mesh_ynodes, &mesh_nynodes); 218 CHKERRQ(ierr); 219 } 220 ierr = ModifyMesh(comm, dm, problem->dim, mesh_growth, mesh_Ndelta, 221 mesh_refine_height, mesh_top_angle, mesh_ynodes, 222 mesh_nynodes); CHKERRQ(ierr); 223 224 // Some properties depend on parameters from NewtonianIdealGas 225 CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, 226 CEED_MEM_HOST, &newtonian_ig_ctx); 227 228 blasius_ctx->weakT = weakT; 229 blasius_ctx->Uinf = Uinf; 230 blasius_ctx->delta0 = delta0; 231 blasius_ctx->theta0 = theta0; 232 blasius_ctx->P0 = P0; 233 newtonian_ig_ctx->P0 = P0; 234 blasius_ctx->implicit = user->phys->implicit; 235 blasius_ctx->newtonian_ctx = *newtonian_ig_ctx; 236 237 { 238 PetscReal domain_min[3]; 239 ierr = DMGetBoundingBox(dm, domain_min, NULL); CHKERRQ(ierr); 240 blasius_ctx->x_inflow = domain_min[0]; 241 } 242 243 CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, 244 &newtonian_ig_ctx); 245 246 CeedQFunctionContextCreate(user->ceed, &blasius_context); 247 CeedQFunctionContextSetData(blasius_context, CEED_MEM_HOST, 248 CEED_USE_POINTER, 249 sizeof(*blasius_ctx), blasius_ctx); 250 CeedQFunctionContextSetDataDestroy(blasius_context, CEED_MEM_HOST, 251 FreeContextPetsc); 252 253 CeedQFunctionContextDestroy(&problem->ics.qfunction_context); 254 problem->ics.qfunction_context = blasius_context; 255 CeedQFunctionContextReferenceCopy(blasius_context, 256 &problem->apply_inflow_jacobian.qfunction_context); 257 if (use_stg) { 258 ierr = SetupSTG(comm, dm, problem, user, weakT, theta0, P0, mesh_ynodes, 259 mesh_nynodes); CHKERRQ(ierr); 260 } else { 261 problem->apply_inflow.qfunction = Blasius_Inflow; 262 problem->apply_inflow.qfunction_loc = Blasius_Inflow_loc; 263 CeedQFunctionContextReferenceCopy(blasius_context, 264 &problem->apply_inflow.qfunction_context); 265 } 266 ierr = PetscFree(mesh_ynodes); CHKERRQ(ierr); 267 PetscFunctionReturn(0); 268 } 269