def _pEqn( runTime, mesh, UEqn, thermo, p, psi, U, rho, phi, DpDt, g, initialMass, totalVolume, corr, nCorr, nNonOrthCorr, cumulativeContErr ): closedVolume = p.needReference() rho.ext_assign( thermo.rho() ) # Thermodynamic density needs to be updated by psi*d(p) after the # pressure solution - done in 2 parts. Part 1: thermo.rho().ext_assign( thermo.rho() - psi * p ) rUA = 1.0/UEqn.A() from Foam.OpenFOAM import word from Foam.finiteVolume import surfaceScalarField from Foam import fvc rhorUAf = surfaceScalarField( word( "(rho*(1|A(U)))" ), fvc.interpolate( rho * rUA ) ) U.ext_assign( rUA * UEqn.H() ) phiU = fvc.interpolate( rho ) * ( (fvc.interpolate( U ) & mesh.Sf() ) + fvc.ddtPhiCorr( rUA, rho, U, phi ) ) phi.ext_assign( phiU + rhorUAf * fvc.interpolate( rho ) * (g & mesh.Sf() ) ) for nonOrth in range( nNonOrthCorr+1 ): from Foam import fvm from Foam.finiteVolume import correction pEqn = fvc.ddt( rho ) + psi * correction( fvm.ddt( p ) ) + fvc.div( phi ) - fvm.laplacian( rhorUAf, p ) if corr == nCorr-1 and nonOrth == nNonOrthCorr: pEqn.solve( mesh.solver( word( str( p.name() ) + "Final" ) ) ) pass else: pEqn.solve( mesh.solver( p.name() ) ) pass if nonOrth == nNonOrthCorr: phi.ext_assign( phi + pEqn.flux() ) pass # Second part of thermodynamic density update thermo.rho().ext_assign( thermo.rho() + psi * p ) U.ext_assign( U + rUA * fvc.reconstruct( ( phi - phiU ) / rhorUAf ) ) U.correctBoundaryConditions() DpDt.ext_assign( fvc.DDt( surfaceScalarField( word( "phiU" ), phi / fvc.interpolate( rho ) ), p ) ) from Foam.finiteVolume.cfdTools.compressible import rhoEqn rhoEqn( rho, phi ) from Foam.finiteVolume.cfdTools.compressible import compressibleContinuityErrs cumulativeContErr = compressibleContinuityErrs( rho, thermo, cumulativeContErr ) # For closed-volume cases adjust the pressure and density levels # to obey overall mass continuity if closedVolume: p.ext_assign( p + ( initialMass - fvc.domainIntegrate( psi * p ) ) / fvc.domainIntegrate( psi ) ) thermo.rho().ext_assign( psi * p ) rho.ext_assign( rho + ( initialMass - fvc.domainIntegrate( rho ) ) / totalVolume ) pass return cumulativeContErr
def fun_pEqn( i, mesh, p, rho, turb, thermo, thermoFluid, K, UEqn, U, phi, psi, DpDt, initialMass, p_rgh, gh, ghf, \ nNonOrthCorr, oCorr, nOuterCorr, corr, nCorr, cumulativeContErr ) : closedVolume = p_rgh.needReference() rho.ext_assign( thermo.rho() ) rUA = 1.0 / UEqn.A() from Foam import fvc from Foam.OpenFOAM import word from Foam.finiteVolume import surfaceScalarField rhorUAf = surfaceScalarField( word( "(rho*(1|A(U)))" ) , fvc.interpolate( rho * rUA ) ) U.ext_assign( rUA * UEqn.H() ) from Foam import fvc phiU = ( fvc.interpolate( rho ) * ( ( fvc.interpolate( U ) & mesh.Sf() ) + fvc.ddtPhiCorr( rUA, rho, U, phi ) ) ) phi.ext_assign( phiU - rhorUAf * ghf * fvc.snGrad( rho ) * mesh.magSf() ) from Foam import fvm for nonOrth in range ( nNonOrthCorr + 1 ): p_rghEqn = ( fvm.ddt( psi, p_rgh) + fvc.ddt( psi, rho ) * gh + fvc.div( phi ) - fvm.laplacian( rhorUAf, p_rgh ) ) p_rghEqn.solve( mesh.solver( p_rgh.select( ( oCorr == nOuterCorr-1 and corr == ( nCorr-1 ) and nonOrth == nNonOrthCorr ) ) ) ) if nonOrth == nNonOrthCorr : phi.ext_assign( phi + p_rghEqn.flux() ) pass pass # Correct velocity field U.ext_assign( U + rUA * fvc.reconstruct( ( phi - phiU ) / rhorUAf ) ) U.correctBoundaryConditions() p.ext_assign( p_rgh + rho * gh ) #Update pressure substantive derivative DpDt.ext_assign( fvc.DDt( surfaceScalarField( word( "phiU" ), phi / fvc.interpolate( rho ) ), p ) ) # Solve continuity from Foam.finiteVolume.cfdTools.compressible import rhoEqn rhoEqn( rho, phi ) # Update continuity errors cumulativeContErr = compressibleContinuityErrors( i, mesh, rho, thermo, cumulativeContErr ) # For closed-volume cases adjust the pressure and density levels # to obey overall mass continuity if closedVolume : p.ext_assign( p + ( initialMass - fvc.domainIntegrate( psi * p ) ) / fvc.domainIntegrate( psi ) ) rho.ext_assign( thermo.rho() ) p_rgh.ext_assign( p - rho * gh ) pass #Update thermal conductivity K.ext_assign( thermoFluid[ i ].Cp() * turb.alphaEff() ) return cumulativeContErr
def fun_pEqn( mesh, p, rho, psi, p_rgh, U, phi, ghf, gh, DpDt, UEqn, thermo, nNonOrthCorr, corr, nCorr, finalIter, cumulativeContErr ): rho.ext_assign( thermo.rho() ) # Thermodynamic density needs to be updated by psi*d(p) after the # pressure solution - done in 2 parts. Part 1: thermo.rho().ext_assign( thermo.rho() - psi * p_rgh ) rUA = 1.0 / UEqn.A() from Foam.finiteVolume import surfaceScalarField from Foam.OpenFOAM import word from Foam import fvc rhorUAf = surfaceScalarField( word( "(rho*(1|A(U)))" ), fvc.interpolate( rho * rUA ) ) U.ext_assign( rUA*UEqn.H() ) phi.ext_assign( fvc.interpolate( rho ) * ( ( fvc.interpolate( U ) & mesh.Sf() ) + fvc.ddtPhiCorr( rUA, rho, U, phi ) ) ) buoyancyPhi = -rhorUAf * ghf * fvc.snGrad( rho ) * mesh.magSf() phi.ext_assign( phi + buoyancyPhi ) from Foam import fvm from Foam.finiteVolume import correction for nonOrth in range( nNonOrthCorr +1 ): p_rghEqn = fvc.ddt( rho ) + psi * correction( fvm.ddt( p_rgh ) ) + fvc.div( phi ) - fvm.laplacian( rhorUAf, p_rgh ) p_rghEqn.solve( mesh.solver( p_rgh.select( ( finalIter and corr == nCorr-1 and nonOrth == nNonOrthCorr ) ) ) ) if nonOrth == nNonOrthCorr: # Calculate the conservative fluxes phi.ext_assign( phi + p_rghEqn.flux() ) # Explicitly relax pressure for momentum corrector p_rgh.relax() # Correct the momentum source with the pressure gradient flux # calculated from the relaxed pressure U.ext_assign( U + rUA * fvc.reconstruct( ( buoyancyPhi + p_rghEqn.flux() ) / rhorUAf ) ) U.correctBoundaryConditions() pass p.ext_assign( p_rgh + rho * gh ) # Second part of thermodynamic density update thermo.rho().ext_assign( thermo.rho() + psi * p_rgh ) DpDt.ext_assign( fvc.DDt( surfaceScalarField( word( "phiU" ), phi / fvc.interpolate( rho ) ), p ) ) from Foam.finiteVolume.cfdTools.compressible import rhoEqn rhoEqn( rho, phi ) from Foam.finiteVolume.cfdTools.compressible import compressibleContinuityErrs cumulativeContErr = compressibleContinuityErrs( rho, thermo, cumulativeContErr ) return cumulativeContErr
def fun_pdEqn( corr, nCorr, nNonOrthCorr, closedVolume, pd, pRef, rho, psi, rUA, gh, phi ): closedVolume = pd.needReference() for nonOrth in range( nNonOrthCorr + 1): from Foam import fvc, fvm pdEqn = fvm.ddt( psi, pd ) + fvc.ddt( psi ) * pRef + fvc.ddt(psi, rho) * gh + fvc.div( phi ) - fvm.laplacian( rho * rUA, pd ) pdEqn.solve() if corr == nCorr-1 and nonOrth == nNonOrthCorr : from Foam.OpenFOAM import word pdEqn.solve( pd.mesh().solver( word( str( pd.name() ) + "Final" ) ) ) pass else: pdEqn.solve( pd.mesh().solver( pd.name() ) ) pass if nonOrth == nNonOrthCorr: phi.ext_assign(phi + pdEqn.flux() ) return pdEqn, closedVolume
def fun_pEqn( i, mesh, p, rho, turb, thermo, thermoFluid, K, UEqn, U, phi, psi, DpDt, initialMass, p_rgh, gh, ghf, \ nNonOrthCorr, oCorr, nOuterCorr, corr, nCorr, cumulativeContErr ) : closedVolume = p_rgh.needReference() rho.ext_assign(thermo.rho()) rUA = 1.0 / UEqn.A() from Foam import fvc from Foam.OpenFOAM import word from Foam.finiteVolume import surfaceScalarField rhorUAf = surfaceScalarField(word("(rho*(1|A(U)))"), fvc.interpolate(rho * rUA)) U.ext_assign(rUA * UEqn.H()) from Foam import fvc phiU = (fvc.interpolate(rho) * ( (fvc.interpolate(U) & mesh.Sf()) + fvc.ddtPhiCorr(rUA, rho, U, phi))) phi.ext_assign(phiU - rhorUAf * ghf * fvc.snGrad(rho) * mesh.magSf()) from Foam import fvm for nonOrth in range(nNonOrthCorr + 1): p_rghEqn = (fvm.ddt(psi, p_rgh) + fvc.ddt(psi, rho) * gh + fvc.div(phi) - fvm.laplacian(rhorUAf, p_rgh)) p_rghEqn.solve( mesh.solver( p_rgh.select((oCorr == nOuterCorr - 1 and corr == (nCorr - 1) and nonOrth == nNonOrthCorr)))) if nonOrth == nNonOrthCorr: phi.ext_assign(phi + p_rghEqn.flux()) pass pass # Correct velocity field U.ext_assign(U + rUA * fvc.reconstruct((phi - phiU) / rhorUAf)) U.correctBoundaryConditions() p.ext_assign(p_rgh + rho * gh) #Update pressure substantive derivative DpDt.ext_assign( fvc.DDt(surfaceScalarField(word("phiU"), phi / fvc.interpolate(rho)), p)) # Solve continuity from Foam.finiteVolume.cfdTools.compressible import rhoEqn rhoEqn(rho, phi) # Update continuity errors cumulativeContErr = compressibleContinuityErrors(i, mesh, rho, thermo, cumulativeContErr) # For closed-volume cases adjust the pressure and density levels # to obey overall mass continuity if closedVolume: p.ext_assign(p + (initialMass - fvc.domainIntegrate(psi * p)) / fvc.domainIntegrate(psi)) rho.ext_assign(thermo.rho()) p_rgh.ext_assign(p - rho * gh) pass #Update thermal conductivity K.ext_assign(thermoFluid[i].Cp() * turb.alphaEff()) return cumulativeContErr
def main_standalone( argc, argv ): from Foam.OpenFOAM.include import setRootCase args = setRootCase( argc, argv ) from Foam.OpenFOAM.include import createTime runTime = createTime( args ) from Foam.OpenFOAM.include import createMesh mesh = createMesh( runTime ) thermo, p, e, T, psi, mu, U, pbf, rhoBoundaryTypes, rho, rhoU, rhoE, pos, neg, inviscid = _createFields( runTime, mesh ) thermophysicalProperties, Pr = readThermophysicalProperties( runTime, mesh ) from Foam.finiteVolume.cfdTools.general.include import readTimeControls adjustTimeStep, maxCo, maxDeltaT = readTimeControls( runTime ) fluxScheme = readFluxScheme( mesh ) from Foam.OpenFOAM import dimensionedScalar, dimVolume, dimTime, word v_zero = dimensionedScalar( word( "v_zero" ) ,dimVolume/dimTime, 0.0) from Foam.OpenFOAM import ext_Info, nl ext_Info() << "\nStarting time loop\n" << nl while runTime.run() : # --- upwind interpolation of primitive fields on faces from Foam import fvc rho_pos = fvc.interpolate( rho, pos, word( "reconstruct(rho)" ) ) rho_neg = fvc.interpolate( rho, neg, word( "reconstruct(rho)" ) ) rhoU_pos = fvc.interpolate( rhoU, pos, word( "reconstruct(U)" ) ) rhoU_neg = fvc.interpolate( rhoU, neg, word( "reconstruct(U)" ) ) rPsi = 1.0 / psi rPsi_pos = fvc.interpolate( rPsi, pos, word( "reconstruct(T)" ) ) rPsi_neg = fvc.interpolate( rPsi, neg, word( "reconstruct(T)" ) ) e_pos = fvc.interpolate( e, pos, word( "reconstruct(T)" ) ) e_neg = fvc.interpolate( e, neg, word( "reconstruct(T)" ) ) U_pos = rhoU_pos / rho_pos U_neg = rhoU_neg / rho_neg p_pos = rho_pos * rPsi_pos p_neg = rho_neg * rPsi_neg phiv_pos = U_pos & mesh.Sf() phiv_neg = U_neg & mesh.Sf() c = ( thermo.Cp() / thermo.Cv() * rPsi ).sqrt() cSf_pos = fvc.interpolate( c, pos, word( "reconstruct(T)" ) ) * mesh.magSf() cSf_neg = fvc.interpolate( c, neg, word( "reconstruct(T)" ) ) * mesh.magSf() ap = ( phiv_pos + cSf_pos ).ext_max( phiv_neg + cSf_neg ).ext_max( v_zero ) am = ( phiv_pos - cSf_pos ).ext_min( phiv_neg - cSf_neg ).ext_min( v_zero ) a_pos = ap / ( ap - am ) from Foam.finiteVolume import surfaceScalarField amaxSf = surfaceScalarField( word( "amaxSf" ), am.mag().ext_max( ap.mag() ) ) CoNum, meanCoNum = compressibleCourantNo( mesh, amaxSf, runTime ) from Foam.finiteVolume.cfdTools.general.include import readTimeControls adjustTimeStep, maxCo, maxDeltaT = readTimeControls( runTime ) from Foam.finiteVolume.cfdTools.general.include import setDeltaT runTime = setDeltaT( runTime, adjustTimeStep, maxCo, maxDeltaT, CoNum ) runTime.increment() ext_Info() << "Time = " << runTime.timeName() << nl << nl aSf = am * a_pos if str( fluxScheme ) == "Tadmor": aSf.ext_assign( -0.5 * amaxSf ) a_pos.ext_assign( 0.5 ) pass a_neg = 1.0 - a_pos phiv_pos *= a_pos phiv_neg *= a_neg aphiv_pos = phiv_pos - aSf aphiv_neg = phiv_neg + aSf phi = None phi = surfaceScalarField( word( "phi" ), aphiv_pos * rho_pos + aphiv_neg * rho_neg ) phiUp = ( aphiv_pos * rhoU_pos + aphiv_neg * rhoU_neg) + ( a_pos * p_pos + a_neg * p_neg ) * mesh.Sf() phiEp = aphiv_pos * ( rho_pos * ( e_pos + 0.5*U_pos.magSqr() ) + p_pos ) + aphiv_neg * ( rho_neg * ( e_neg + 0.5 * U_neg.magSqr() ) + p_neg )\ + aSf * p_pos - aSf * p_neg from Foam.finiteVolume import volTensorField from Foam import fvc tauMC = volTensorField( word( "tauMC" ) , mu * fvc.grad(U).T().dev2() ) # --- Solve density from Foam.finiteVolume import solve from Foam import fvm solve( fvm.ddt( rho ) + fvc.div( phi ) ) # --- Solve momentum solve( fvm.ddt( rhoU ) + fvc.div( phiUp ) ) U.dimensionedInternalField().ext_assign( rhoU.dimensionedInternalField() / rho.dimensionedInternalField() ) U.correctBoundaryConditions() rhoU.ext_boundaryField().ext_assign( rho.ext_boundaryField() * U.ext_boundaryField() ) rhoBydt = rho / runTime.deltaT() if not inviscid: solve( fvm.ddt( rho, U ) - fvc.ddt( rho, U ) - fvm.laplacian( mu, U ) - fvc.div( tauMC ) ) rhoU.ext_assign( rho * U ) pass # --- Solve energy sigmaDotU = ( fvc.interpolate( mu ) * mesh.magSf() * fvc.snGrad( U ) + ( mesh.Sf() & fvc.interpolate( tauMC ) ) ) & ( a_pos * U_pos + a_neg * U_neg ) solve( fvm.ddt( rhoE ) + fvc.div( phiEp ) - fvc.div( sigmaDotU ) ) e.ext_assign( rhoE / rho - 0.5 * U.magSqr() ) e.correctBoundaryConditions() thermo.correct() from Foam.finiteVolume import volScalarField rhoE.ext_boundaryField().ext_assign( rho.ext_boundaryField() * ( e.ext_boundaryField() + 0.5 * U.ext_boundaryField().magSqr() ) ) if not inviscid: k = volScalarField( word( "k" ) , thermo.Cp() * mu / Pr ) # The initial C++ expression does not work properly, because of # 1. the order of expression arguments computation differs with C++ #solve( fvm.ddt( rho, e ) - fvc.ddt( rho, e ) - fvm.laplacian( thermo.alpha(), e ) \ # + fvc.laplacian( thermo.alpha(), e ) - fvc.laplacian( k, T ) ) solve( -fvc.laplacian( k, T ) + ( fvc.laplacian( thermo.alpha(), e ) \ + (- fvm.laplacian( thermo.alpha(), e ) + (- fvc.ddt( rho, e ) + fvm.ddt( rho, e ) ) ) ) ) thermo.correct() rhoE.ext_assign( rho * ( e + 0.5 * U.magSqr() ) ) pass p.dimensionedInternalField().ext_assign( rho.dimensionedInternalField() / psi.dimensionedInternalField() ) p.correctBoundaryConditions() rho.ext_boundaryField().ext_assign( psi.ext_boundaryField() * p.ext_boundaryField() ) runTime.write() ext_Info() << "ExecutionTime = " << runTime.elapsedCpuTime() << " s" << \ " ClockTime = " << runTime.elapsedClockTime() << " s" << nl << nl pass ext_Info() << "End\n" import os return os.EX_OK
def _pEqn(runTime, mesh, UEqn, thermo, p, psi, U, rho, phi, DpDt, g, initialMass, totalVolume, corr, nCorr, nNonOrthCorr, cumulativeContErr): closedVolume = p.needReference() rho.ext_assign(thermo.rho()) # Thermodynamic density needs to be updated by psi*d(p) after the # pressure solution - done in 2 parts. Part 1: thermo.rho().ext_assign(thermo.rho() - psi * p) rUA = 1.0 / UEqn.A() from Foam.OpenFOAM import word from Foam.finiteVolume import surfaceScalarField from Foam import fvc rhorUAf = surfaceScalarField(word("(rho*(1|A(U)))"), fvc.interpolate(rho * rUA)) U.ext_assign(rUA * UEqn.H()) phiU = fvc.interpolate(rho) * ( (fvc.interpolate(U) & mesh.Sf()) + fvc.ddtPhiCorr(rUA, rho, U, phi)) phi.ext_assign(phiU + rhorUAf * fvc.interpolate(rho) * (g & mesh.Sf())) for nonOrth in range(nNonOrthCorr + 1): from Foam import fvm from Foam.finiteVolume import correction pEqn = fvc.ddt(rho) + psi * correction( fvm.ddt(p)) + fvc.div(phi) - fvm.laplacian(rhorUAf, p) if corr == nCorr - 1 and nonOrth == nNonOrthCorr: pEqn.solve(mesh.solver(word(str(p.name()) + "Final"))) pass else: pEqn.solve(mesh.solver(p.name())) pass if nonOrth == nNonOrthCorr: phi.ext_assign(phi + pEqn.flux()) pass # Second part of thermodynamic density update thermo.rho().ext_assign(thermo.rho() + psi * p) U.ext_assign(U + rUA * fvc.reconstruct((phi - phiU) / rhorUAf)) U.correctBoundaryConditions() DpDt.ext_assign( fvc.DDt(surfaceScalarField(word("phiU"), phi / fvc.interpolate(rho)), p)) from Foam.finiteVolume.cfdTools.compressible import rhoEqn rhoEqn(rho, phi) from Foam.finiteVolume.cfdTools.compressible import compressibleContinuityErrs cumulativeContErr = compressibleContinuityErrs(rho, thermo, cumulativeContErr) # For closed-volume cases adjust the pressure and density levels # to obey overall mass continuity if closedVolume: p.ext_assign(p + (initialMass - fvc.domainIntegrate(psi * p)) / fvc.domainIntegrate(psi)) thermo.rho().ext_assign(psi * p) rho.ext_assign(rho + (initialMass - fvc.domainIntegrate(rho)) / totalVolume) pass return cumulativeContErr
def fun_pEqn(mesh, p, rho, psi, p_rgh, U, phi, ghf, gh, DpDt, UEqn, thermo, nNonOrthCorr, corr, nCorr, finalIter, cumulativeContErr): rho.ext_assign(thermo.rho()) # Thermodynamic density needs to be updated by psi*d(p) after the # pressure solution - done in 2 parts. Part 1: thermo.rho().ext_assign(thermo.rho() - psi * p_rgh) rUA = 1.0 / UEqn.A() from Foam.finiteVolume import surfaceScalarField from Foam.OpenFOAM import word from Foam import fvc rhorUAf = surfaceScalarField(word("(rho*(1|A(U)))"), fvc.interpolate(rho * rUA)) U.ext_assign(rUA * UEqn.H()) phi.ext_assign( fvc.interpolate(rho) * ((fvc.interpolate(U) & mesh.Sf()) + fvc.ddtPhiCorr(rUA, rho, U, phi))) buoyancyPhi = -rhorUAf * ghf * fvc.snGrad(rho) * mesh.magSf() phi.ext_assign(phi + buoyancyPhi) from Foam import fvm from Foam.finiteVolume import correction for nonOrth in range(nNonOrthCorr + 1): p_rghEqn = fvc.ddt(rho) + psi * correction( fvm.ddt(p_rgh)) + fvc.div(phi) - fvm.laplacian(rhorUAf, p_rgh) p_rghEqn.solve( mesh.solver( p_rgh.select((finalIter and corr == nCorr - 1 and nonOrth == nNonOrthCorr)))) if nonOrth == nNonOrthCorr: # Calculate the conservative fluxes phi.ext_assign(phi + p_rghEqn.flux()) # Explicitly relax pressure for momentum corrector p_rgh.relax() # Correct the momentum source with the pressure gradient flux # calculated from the relaxed pressure U.ext_assign(U + rUA * fvc.reconstruct( (buoyancyPhi + p_rghEqn.flux()) / rhorUAf)) U.correctBoundaryConditions() pass p.ext_assign(p_rgh + rho * gh) # Second part of thermodynamic density update thermo.rho().ext_assign(thermo.rho() + psi * p_rgh) DpDt.ext_assign( fvc.DDt(surfaceScalarField(word("phiU"), phi / fvc.interpolate(rho)), p)) from Foam.finiteVolume.cfdTools.compressible import rhoEqn rhoEqn(rho, phi) from Foam.finiteVolume.cfdTools.compressible import compressibleContinuityErrs cumulativeContErr = compressibleContinuityErrs(rho, thermo, cumulativeContErr) return cumulativeContErr
def main_standalone(argc, argv): from Foam.OpenFOAM.include import setRootCase args = setRootCase(argc, argv) from Foam.OpenFOAM.include import createTime runTime = createTime(args) from Foam.OpenFOAM.include import createMesh mesh = createMesh(runTime) thermo, p, e, T, psi, mu, U, pbf, rhoBoundaryTypes, rho, rhoU, rhoE, pos, neg, inviscid = _createFields( runTime, mesh) thermophysicalProperties, Pr = readThermophysicalProperties(runTime, mesh) from Foam.finiteVolume.cfdTools.general.include import readTimeControls adjustTimeStep, maxCo, maxDeltaT = readTimeControls(runTime) fluxScheme = readFluxScheme(mesh) from Foam.OpenFOAM import dimensionedScalar, dimVolume, dimTime, word v_zero = dimensionedScalar(word("v_zero"), dimVolume / dimTime, 0.0) from Foam.OpenFOAM import ext_Info, nl ext_Info() << "\nStarting time loop\n" << nl while runTime.run(): # --- upwind interpolation of primitive fields on faces from Foam import fvc rho_pos = fvc.interpolate(rho, pos, word("reconstruct(rho)")) rho_neg = fvc.interpolate(rho, neg, word("reconstruct(rho)")) rhoU_pos = fvc.interpolate(rhoU, pos, word("reconstruct(U)")) rhoU_neg = fvc.interpolate(rhoU, neg, word("reconstruct(U)")) rPsi = 1.0 / psi rPsi_pos = fvc.interpolate(rPsi, pos, word("reconstruct(T)")) rPsi_neg = fvc.interpolate(rPsi, neg, word("reconstruct(T)")) e_pos = fvc.interpolate(e, pos, word("reconstruct(T)")) e_neg = fvc.interpolate(e, neg, word("reconstruct(T)")) U_pos = rhoU_pos / rho_pos U_neg = rhoU_neg / rho_neg p_pos = rho_pos * rPsi_pos p_neg = rho_neg * rPsi_neg phiv_pos = U_pos & mesh.Sf() phiv_neg = U_neg & mesh.Sf() c = (thermo.Cp() / thermo.Cv() * rPsi).sqrt() cSf_pos = fvc.interpolate(c, pos, word("reconstruct(T)")) * mesh.magSf() cSf_neg = fvc.interpolate(c, neg, word("reconstruct(T)")) * mesh.magSf() ap = (phiv_pos + cSf_pos).ext_max(phiv_neg + cSf_neg).ext_max(v_zero) am = (phiv_pos - cSf_pos).ext_min(phiv_neg - cSf_neg).ext_min(v_zero) a_pos = ap / (ap - am) from Foam.finiteVolume import surfaceScalarField amaxSf = surfaceScalarField(word("amaxSf"), am.mag().ext_max(ap.mag())) aSf = am * a_pos if str(fluxScheme) == "Tadmor": aSf.ext_assign(-0.5 * amaxSf) a_pos.ext_assign(0.5) pass a_neg = 1.0 - a_pos phiv_pos *= a_pos phiv_neg *= a_neg aphiv_pos = phiv_pos - aSf aphiv_neg = phiv_neg + aSf # Reuse amaxSf for the maximum positive and negative fluxes # estimated by the central scheme amaxSf.ext_assign(aphiv_pos.mag().ext_max(aphiv_neg.mag())) CoNum, meanCoNum = compressibleCourantNo(mesh, amaxSf, runTime) from Foam.finiteVolume.cfdTools.general.include import readTimeControls adjustTimeStep, maxCo, maxDeltaT = readTimeControls(runTime) from Foam.finiteVolume.cfdTools.general.include import setDeltaT runTime = setDeltaT(runTime, adjustTimeStep, maxCo, maxDeltaT, CoNum) runTime.increment() ext_Info() << "Time = " << runTime.timeName() << nl << nl phi = None phi = surfaceScalarField(word("phi"), aphiv_pos * rho_pos + aphiv_neg * rho_neg) phiUp = (aphiv_pos * rhoU_pos + aphiv_neg * rhoU_neg) + (a_pos * p_pos + a_neg * p_neg) * mesh.Sf() phiEp = aphiv_pos * ( rho_pos * ( e_pos + 0.5*U_pos.magSqr() ) + p_pos ) + aphiv_neg * ( rho_neg * ( e_neg + 0.5 * U_neg.magSqr() ) + p_neg )\ + aSf * p_pos - aSf * p_neg from Foam.finiteVolume import volTensorField from Foam import fvc tauMC = volTensorField(word("tauMC"), mu * fvc.grad(U).T().dev2()) # --- Solve density from Foam.finiteVolume import solve from Foam import fvm solve(fvm.ddt(rho) + fvc.div(phi)) # --- Solve momentum solve(fvm.ddt(rhoU) + fvc.div(phiUp)) U.dimensionedInternalField().ext_assign( rhoU.dimensionedInternalField() / rho.dimensionedInternalField()) U.correctBoundaryConditions() rhoU.ext_boundaryField().ext_assign(rho.ext_boundaryField() * U.ext_boundaryField()) rhoBydt = rho / runTime.deltaT() if not inviscid: solve( fvm.ddt(rho, U) - fvc.ddt(rho, U) - fvm.laplacian(mu, U) - fvc.div(tauMC)) rhoU.ext_assign(rho * U) pass # --- Solve energy sigmaDotU = (fvc.interpolate(mu) * mesh.magSf() * fvc.snGrad(U) + (mesh.Sf() & fvc.interpolate(tauMC))) & (a_pos * U_pos + a_neg * U_neg) solve(fvm.ddt(rhoE) + fvc.div(phiEp) - fvc.div(sigmaDotU)) e.ext_assign(rhoE / rho - 0.5 * U.magSqr()) e.correctBoundaryConditions() thermo.correct() from Foam.finiteVolume import volScalarField rhoE.ext_boundaryField().ext_assign( rho.ext_boundaryField() * (e.ext_boundaryField() + 0.5 * U.ext_boundaryField().magSqr())) if not inviscid: k = volScalarField(word("k"), thermo.Cp() * mu / Pr) # The initial C++ expression does not work properly, because of # 1. the order of expression arguments computation differs with C++ #solve( fvm.ddt( rho, e ) - fvc.ddt( rho, e ) - fvm.laplacian( thermo.alpha(), e ) \ # + fvc.laplacian( thermo.alpha(), e ) - fvc.laplacian( k, T ) ) solve( -fvc.laplacian( k, T ) + ( fvc.laplacian( thermo.alpha(), e ) \ + (- fvm.laplacian( thermo.alpha(), e ) + (- fvc.ddt( rho, e ) + fvm.ddt( rho, e ) ) ) ) ) thermo.correct() rhoE.ext_assign(rho * (e + 0.5 * U.magSqr())) pass p.dimensionedInternalField().ext_assign( rho.dimensionedInternalField() / psi.dimensionedInternalField()) p.correctBoundaryConditions() rho.ext_boundaryField().ext_assign(psi.ext_boundaryField() * p.ext_boundaryField()) runTime.write() ext_Info() << "ExecutionTime = " << runTime.elapsedCpuTime() << " s" << \ " ClockTime = " << runTime.elapsedClockTime() << " s" << nl << nl pass ext_Info() << "End\n" import os return os.EX_OK