💻 Fortran Source Code Library

We currently offer 172 open-source, production-grade Fortran codes for offline testing. Run calculations locally on your own machine, view code structure, read technical explanations, and download compilation packages including sample input files.

Convection Over a Sphere

Core Numerical Engine in Fortran 90 • 30 total downloads

sphere_convection.f90
! =========================================================================
! Source File: sphere_convection.f90
! =========================================================================

program sphere_convection
    implicit none
    double precision :: D, V, Ti, Ts, rho, mu, kf, Pr, cp, beta
    integer :: fl_type
    
    double precision :: Tf, dT, nu, Re, NuW, NuR, h, Q, As, g, pi, mu_s, temp_val
    integer :: iostat_val, i
    double precision :: Vs, Res, NuWs, NuRs, hs, Qs
    
    g = 9.80665d0
    pi = 3.141592653589793d0
    
    ! Read inputs sequentially
    read(*,*,iostat=iostat_val) D
    read(*,*,iostat=iostat_val) V
    read(*,*,iostat=iostat_val) Ti
    read(*,*,iostat=iostat_val) Ts
    read(*,*,iostat=iostat_val) fl_type
    
    ! Read custom parameters
    read(*,*,iostat=iostat_val) rho
    read(*,*,iostat=iostat_val) mu
    read(*,*,iostat=iostat_val) kf
    read(*,*,iostat=iostat_val) Pr
    read(*,*,iostat=iostat_val) cp
    read(*,*,iostat=iostat_val) temp_val ! mu_s from php
    
    if (iostat_val /= 0) then
        write(*,*) 'ERROR: Failed to read all input values.'
        stop
    end if
    
    ! Evaluate bulk properties at free-stream temperature T_inf = Ti
    if (fl_type == 1) then
        ! Air
        rho = 1.177d0
        mu = 1.85d-5
        kf = 0.0263d0
        Pr = 0.71d0
        cp = 1007d0
        mu_s = 1.85d-5 * ((Ts + 273.15d0) / (Ti + 273.15d0))**0.7d0
    else if (fl_type == 2) then
        ! Water
        rho = 997d0
        mu = 8.9d-4
        kf = 0.613d0
        Pr = 6.13d0
        cp = 4180d0
        mu_s = 2.414d-5 * 10.0d0**(247.8d0 / (Ts + 273.15d0 - 140.0d0))
    else
        ! Engine Oil
        rho = 870d0
        mu = 0.05d0
        kf = 0.14d0
        Pr = 500d0
        cp = 2000d0
        mu_s = 0.05d0 * exp(-0.04d0 * (Ts - Ti))
    end if
    
    ! Override custom mu_s if provided
    if (temp_val > 0.0d0) mu_s = temp_val
    
    nu = mu / rho
    Re = rho * V * D / mu
    
    ! Nusselt Whitaker correlation (Eq. 7.56)
    NuW = 2.0d0 + (0.4d0 * Re**0.5d0 + 0.06d0 * Re**(2.0d0/3.0d0)) * Pr**0.4d0 * (mu / mu_s)**0.25d0
    
    ! Nusselt Ranz-Marshall correlation
    NuR = 2.0d0 + 0.6d0 * Re**0.5d0 * Pr**(1.0d0/3.0d0)
    
    h = NuW * kf / D
    As = pi * D**2
    dT = abs(Ts - Ti)
    Q = h * As * dT
    
    write(*,'(A)') '============================================'
    write(*,'(A)') '  CONVECTION OVER SPHERE ENGINE'
    write(*,'(A)') '============================================'
    write(*,*)
    write(*,'(A,ES14.4)') '  Reynolds Re             = ', Re
    write(*,'(A,F12.4)')    '  Whitaker Nu             = ', NuW
    write(*,'(A,F12.4)')    '  Ranz Nu                 = ', NuR
    write(*,'(A,F12.4,A)')  '  Coeff h                 = ', h, ' W/m2K'
    write(*,'(A,F12.4,A)')  '  Transfer Q              = ', Q, ' W'
    write(*,*)
    
    write(*,'(A)') '--- VELOCITY SWEEP ---'
    write(*,'(A)') '  V[m/s]     Re           Nu_Whitaker  Nu_Ranz      h[W/m2K]   Q[W]'
    write(*,'(A)') '  -------------------------------------------------------------------------'
    do i=1,25
        Vs = 0.05d0 + (max(V,0.2d0)*4.0d0 - 0.05d0)*dble(i-1)/24.0d0
        Res = rho * Vs * D / mu
        NuWs = 2.0d0 + (0.4d0 * Res**0.5d0 + 0.06d0 * Res**(2.0d0/3.0d0)) * Pr**0.4d0 * (mu / mu_s)**0.25d0
        NuRs = 2.0d0 + 0.6d0 * Res**0.5d0 * Pr**(1.0d0/3.0d0)
        hs = NuWs * kf / D
        Qs = hs * As * dT
        write(*,'(2X,F8.3,2X,ES10.3,2X,F11.3,2X,F11.3,2X,F10.4,2X,F12.4)') Vs, Res, NuWs, NuRs, hs, Qs
    end do
    write(*,*)
end program sphere_convection


Solver Description

Models forced convection heat transfer over spheres. Computes Nusselt numbers using Whitaker correlation (which incorporates a correction for temperature-dependent viscosity variations across the boundary layer) and Ranz-Marshall correlation.

Key Numerical Methods & Architecture

  • Input Redirection: Reads parameters sequentially from standard input (`stdin`) using Fortran sequential read (`read(*,*)`), ensuring modular integration.
  • Modular Design: Formulated using pure mathematical routines, separation of equations from output formatting, and precise numerical solvers (e.g. bisection, Newton-Raphson).
  • Standard Compliant: Written in clean, standards-compliant Fortran 90 to ensure cross-compiler compatibility.

🛠️ Local Compilation

To test this code on your machine, compile the source code file(s) using a standard Fortran compiler (e.g., `gfortran`).

Compilation Command:

gfortran -O3 sphere_convection.f90 -o sphere_convection

Execution Command:

Execute the program by feeding the sample input file into the program using stdin redirection:

sphere_convection < input.txt

📥 Downloads & Local Files

Preview of the required input file (input.txt):

! Sphere diameter D [m]
0.05
! Free stream velocity V [m/s]
5.0
! Free stream temperature Tinf [C]
25.0
! Surface temperature Ts [C]
100.0
! Fluid type (1=Air, 2=Water, 3=Oil)
1
! Custom density [kg/m3] (0=auto)
0.0
! Custom viscosity [Pa-s] (0=auto)
0.0
! Custom thermal conductivity [W/m-K] (0=auto)
0.0
! Custom Prandtl number (0=auto)
0.0
! Custom specific heat [J/kg-K] (0=auto)
0.0
! Custom surface dynamic viscosity [Pa-s] (0=auto)
0.0