Cold Front Calculator With Loop#

Adapted from: “Modern Fortran: Building Efficient Parallel Applications” by Milan Curcic (Manning)

Program to Calculate the Temperature due to a Cold Front Using a Loop#

program cold_front

    implicit none
    integer :: n
    real    :: nhours
    
    do n = 6, 48, 6
        nhours = real(n)
        print *, "Temperature after ", &
            nhours, " hours is ", &
            cold_front_temperature(12., 24., 20., 960., nhours), " degrees."
    end do
    
contains

    real function cold_front_temperature ( &
        temp1, temp2, c, dx, dt) result(res)
        
        real, intent(in) :: temp1, temp2, c, dx, dt
        res = temp2 - c * (temp2 - temp1) / dx * dt
        
    end function cold_front_temperature
    
end program cold_front

Program Explanation#

The following explanation is given by “Code Copilot” within ChatGPT 4.

The Fortran program named cold_front is designed to calculate and display the temperature at different time intervals as a cold front moves through a region, altering the temperature linearly based on its speed and the initial temperature gradient over a given distance. The core computation is encapsulated within a function cold_front_temperature, which calculates the resulting temperature after the cold front has affected the area for a specified duration.

Program Structure and Explanation#

  1. Variable Declarations:

    • n: An integer used as a counter in the loop to represent time in hours.

    • nhours: A real number that converts n into a real data type appropriate for passing to the cold_front_temperature function.

  2. Loop Execution:

    • The do loop runs from 6 to 48 hours in increments of 6 hours. For each iteration, it calculates the temperature after nhours using the cold_front_temperature function and prints the result.

  3. Function Definition (cold_front_temperature):

    • Input Parameters:

      • temp1 (Real): The initial temperature at the starting point (e.g., 12 degrees Celsius).

      • temp2 (Real): The initial temperature at a distance dx from the starting point (e.g., 24 degrees Celsius).

      • c (Real): The speed of the cold front in km/hr (e.g., 20 km/hr).

      • dx (Real): The distance over which the initial temperature difference temp2 - temp1 is observed (e.g., 960 km).

      • dt (Real): The time in hours for which the effect of the cold front is being calculated (e.g., nhours).

    • Calculation and Result:

      • The formula temp2 - c * (temp2 - temp1) / dx * dt is used to calculate the temperature after the cold front has moved for dt hours. This calculation effectively adjusts the temperature at the point temp2 by considering how the temperature gradient changes linearly with distance, scaled by the speed of the cold front and the time elapsed.

Calculation Explanation#

  • The function calculates the change in temperature as a function of time due to the movement of the cold front:

    • (temp2 - temp1) / dx computes the rate of temperature change per kilometer (temperature gradient).

    • Multiplying this gradient by the cold front’s speed c gives the rate of temperature change per hour.

    • Multiplying further by dt scales this hourly change by the number of hours nhours to determine the total change in temperature over that period.

  • The result is subtracted from temp2 because as the cold front moves, it brings the characteristics (temperature) of the starting point (temp1) towards the point initially at temp2.

Usage and Output#

  • The program is particularly useful for educational purposes or simple meteorological predictions to understand the impact of a moving cold front over time on the local temperature.

  • It provides a straightforward and clear output showing how the temperature changes every 6 hours from 6 to 48 hours as a cold front progresses through the area.

This program is an excellent example of using procedural abstraction (cold_front_temperature function) to encapsulate the logic for a specific calculation, making the main program more readable and easier to manage.

Program Compilation and Execution#

The above program is compiled and run using Fortran Package Manager (fpm):

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Cold_Front_Calculator_with_Loop"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")
exec_status = os.system("fpm run 2>/dev/null")
 Temperature after    6.00000000      hours is    22.5000000      degrees.
 Temperature after    12.0000000      hours is    21.0000000      degrees.
 Temperature after    18.0000000      hours is    19.5000000      degrees.
 Temperature after    24.0000000      hours is    18.0000000      degrees.
 Temperature after    30.0000000      hours is    16.5000000      degrees.
 Temperature after    36.0000000      hours is    15.0000000      degrees.
 Temperature after    42.0000000      hours is    13.5000000      degrees.
 Temperature after    48.0000000      hours is    12.0000000      degrees.