|
| 1 | +! //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ! |
| 2 | +! |
| 3 | +! Maintainers : support@fluidnumerics.com |
| 4 | +! Official Repository : https://github.com/FluidNumerics/self/ |
| 5 | +! |
| 6 | +! Copyright © 2026 Fluid Numerics LLC |
| 7 | +! |
| 8 | +! Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 9 | +! |
| 10 | +! 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 11 | +! |
| 12 | +! 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in |
| 13 | +! the documentation and/or other materials provided with the distribution. |
| 14 | +! |
| 15 | +! 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from |
| 16 | +! this software without specific prior written permission. |
| 17 | +! |
| 18 | +! //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ! |
| 19 | +module lineareuler2d_boneandmarrow_model |
| 20 | +!! Heterogeneous-medium linear-acoustics test on the |
| 21 | +!! share/mesh/MultiMaterial2D/BoneAndMarrow.mesh ISM-MM mesh. |
| 22 | +!! |
| 23 | +!! The mesh tags every element with one of three materials: |
| 24 | +!! "Muscle" (background annulus, r in roughly [6.5, 14]), |
| 25 | +!! "Bone" (disk of radius 6.5 about (-0.5, 0)), and |
| 26 | +!! "Marrow" (smaller disk of radius 1.5 about (-1.5, 0.5)) |
| 27 | +!! nested inside the bone region. We map each material to a |
| 28 | +!! representative sound speed and write that into solution(...,5), |
| 29 | +!! which is held fixed in time by the LinearEuler2D model. |
| 30 | +!! |
| 31 | +!! Initial condition: a small Gaussian pressure / density bump is |
| 32 | +!! placed in the Muscle region, well outside the bone region. The |
| 33 | +!! transient acoustic pulse propagates outward, refracts/reflects |
| 34 | +!! at the material interfaces (because c is discontinuous across |
| 35 | +!! them), and radiates out through the outer boundary. |
| 36 | + |
| 37 | + use self_lineareuler2d |
| 38 | + |
| 39 | + implicit none |
| 40 | + |
| 41 | + type,extends(lineareuler2d) :: lineareuler2d_boneandmarrow |
| 42 | + ! Sound speeds in normalized units, preserving real-tissue ratios |
| 43 | + ! (bone is ~2.3x faster than muscle, marrow is ~0.92x). |
| 44 | + real(prec) :: c_muscle = 1.0_prec |
| 45 | + real(prec) :: c_bone = 2.3_prec |
| 46 | + real(prec) :: c_marrow = 0.92_prec |
| 47 | + real(prec) :: bump_x0 = 10.0_prec ! Pulse center, well into the muscle annulus |
| 48 | + real(prec) :: bump_y0 = 0.0_prec |
| 49 | + real(prec) :: bump_L = 0.6_prec ! Halfwidth (e-folding length) of the bump |
| 50 | + real(prec) :: bump_amp = 1.0e-3_prec ! Density-perturbation amplitude |
| 51 | + |
| 52 | + contains |
| 53 | + |
| 54 | + ! AdditionalInit is inherited from `lineareuler2d`, which registers both |
| 55 | + ! the no-normal-flow and radiation BCs using the correct CPU/GPU wrappers |
| 56 | + ! for the current build. The radiation wrapper zeros (rho', u, v, p) and |
| 57 | + ! preserves c on the same host/device buffer the rest of the pipeline |
| 58 | + ! reads from, which is what this disk-shaped scatterer test requires. |
| 59 | + procedure :: setInitialCondition |
| 60 | + |
| 61 | + endtype lineareuler2d_boneandmarrow |
| 62 | + |
| 63 | +contains |
| 64 | + |
| 65 | + subroutine setInitialCondition(this) |
| 66 | + !! Material-aware initial condition: stamp `c` from the per- |
| 67 | + !! element material id, then add a Gaussian rho/p bump only in |
| 68 | + !! Muscle elements (so the pulse starts cleanly in a uniform |
| 69 | + !! medium and the bone/marrow inclusions are seen as scatterers). |
| 70 | + implicit none |
| 71 | + class(lineareuler2d_boneandmarrow),intent(inout) :: this |
| 72 | + integer :: i,j,iel,matid |
| 73 | + real(prec) :: c_mat,x,y,r2,shape |
| 74 | + character(LEN=64) :: matname |
| 75 | + |
| 76 | + do iel = 1,this%mesh%nElem |
| 77 | + matid = this%mesh%elemMaterial(iel) |
| 78 | + matname = this%mesh%materialNames(matid) |
| 79 | + select case(trim(matname)) |
| 80 | + case("Muscle"); c_mat = this%c_muscle |
| 81 | + case("Bone"); c_mat = this%c_bone |
| 82 | + case("Marrow"); c_mat = this%c_marrow |
| 83 | + case default; c_mat = this%c_muscle |
| 84 | + endselect |
| 85 | + |
| 86 | + do j = 1,this%solution%N+1 |
| 87 | + do i = 1,this%solution%N+1 |
| 88 | + x = this%geometry%x%interior(i,j,iel,1,1) |
| 89 | + y = this%geometry%x%interior(i,j,iel,1,2) |
| 90 | + |
| 91 | + if(trim(matname) == "Muscle") then |
| 92 | + r2 = (x-this%bump_x0)**2+(y-this%bump_y0)**2 |
| 93 | + shape = this%bump_amp*exp(-r2/(this%bump_L*this%bump_L)) |
| 94 | + else |
| 95 | + shape = 0.0_prec |
| 96 | + endif |
| 97 | + |
| 98 | + this%solution%interior(i,j,iel,1) = shape ! density perturbation |
| 99 | + this%solution%interior(i,j,iel,2) = 0.0_prec ! u |
| 100 | + this%solution%interior(i,j,iel,3) = 0.0_prec ! v |
| 101 | + this%solution%interior(i,j,iel,4) = shape*c_mat*c_mat ! p = rho * c^2 (acoustic) |
| 102 | + this%solution%interior(i,j,iel,5) = c_mat ! sound speed for this material |
| 103 | + enddo |
| 104 | + enddo |
| 105 | + enddo |
| 106 | + |
| 107 | + call this%solution%UpdateDevice() |
| 108 | + |
| 109 | + endsubroutine setInitialCondition |
| 110 | + |
| 111 | +endmodule lineareuler2d_boneandmarrow_model |
| 112 | + |
| 113 | +program LinearEuler_BoneAndMarrow |
| 114 | + |
| 115 | + use self_data |
| 116 | + use lineareuler2d_boneandmarrow_model |
| 117 | + |
| 118 | + implicit none |
| 119 | + |
| 120 | + character(SELF_INTEGRATOR_LENGTH),parameter :: integrator = 'rk3' |
| 121 | + integer,parameter :: controlDegree = 3 |
| 122 | + integer,parameter :: targetDegree = 6 |
| 123 | + ! CFL for DGSEM: dt < dx_min / (c_max * (N+1)^2). With curved elements |
| 124 | + ! near the material interfaces dx_min can be much smaller than the |
| 125 | + ! background 1.0, so we run conservatively. |
| 126 | + real(prec),parameter :: dt = 1.0e-3_prec |
| 127 | + real(prec),parameter :: endtime = 0.5_prec |
| 128 | + real(prec),parameter :: iointerval = 0.1_prec |
| 129 | + real(prec) :: e0,ef |
| 130 | + type(lineareuler2d_boneandmarrow) :: modelobj |
| 131 | + type(Lagrange),target :: interp |
| 132 | + type(Mesh2D),target :: mesh |
| 133 | + type(SEMQuad),target :: geometry |
| 134 | + character(LEN=255) :: WORKSPACE |
| 135 | + |
| 136 | + ! Read the multi-material ISM-MM mesh. Boundary names from the |
| 137 | + ! .mesh file (e.g. "outer") become bc indices 1..nBCs; remap them |
| 138 | + ! all to SELF_BC_RADIATION since the disk has one transparent |
| 139 | + ! outer boundary. |
| 140 | + call get_environment_variable("WORKSPACE",WORKSPACE) |
| 141 | + call mesh%Read_HOHQMesh(trim(WORKSPACE)//"/share/mesh/MultiMaterial2D/BoneAndMarrow.mesh") |
| 142 | + call mesh%ResetBoundaryConditionType(SELF_BC_RADIATION) |
| 143 | + |
| 144 | + call interp%Init(N=controlDegree, & |
| 145 | + controlNodeType=GAUSS, & |
| 146 | + M=targetDegree, & |
| 147 | + targetNodeType=UNIFORM) |
| 148 | + |
| 149 | + call geometry%Init(interp,mesh%nElem) |
| 150 | + call geometry%GenerateFromMesh(mesh) |
| 151 | + |
| 152 | + call modelobj%Init(mesh,geometry) |
| 153 | + modelobj%prescribed_bcs_enabled = .false. |
| 154 | + modelobj%tecplot_enabled = .false. |
| 155 | + modelobj%rho0 = 1.0_prec |
| 156 | + |
| 157 | + call modelobj%setInitialCondition() |
| 158 | + |
| 159 | + call modelobj%WriteModel() |
| 160 | + call modelobj%IncrementIOCounter() |
| 161 | + |
| 162 | + call modelobj%CalculateEntropy() |
| 163 | + call modelobj%ReportEntropy() |
| 164 | + e0 = modelobj%entropy |
| 165 | + |
| 166 | + call modelobj%SetTimeIntegrator(integrator) |
| 167 | + call modelobj%ForwardStep(endtime,dt,iointerval) |
| 168 | + ef = modelobj%entropy |
| 169 | + |
| 170 | + if(ef /= ef) then |
| 171 | + print*,"Error: Final entropy is inf or nan",ef |
| 172 | + stop 1 |
| 173 | + endif |
| 174 | + if(ef > 10.0_prec*e0) then |
| 175 | + print*,"Error: Final entropy grew unphysically (e0=",e0," ef=",ef,")" |
| 176 | + stop 1 |
| 177 | + endif |
| 178 | + |
| 179 | + call modelobj%free() |
| 180 | + call mesh%free() |
| 181 | + call geometry%free() |
| 182 | + call interp%free() |
| 183 | + |
| 184 | +endprogram LinearEuler_BoneAndMarrow |
0 commit comments