Skip to content

asherchok/fortran-multiplicity-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Motivation

To explore the syntax of FORTRAN and learn a legacy language.

Original Problem

In Schroeder's Thermal Physics Problem 2.9, we were tasked by the author to recreate the same Figure 2.4 and then determine the most and least probable macrostate, along with their respective probabilities.

Problem 2.9

Problem 2.9

Figure 2.4

Figure 2.4

For our FORTRAN exercise, we aim to become familiar with FORTRAN syntax by solving the Total Multiplicity for six harmonic oscillators and four harmonic oscillators in each Einstein Solid, with a total of six energy units.

Original Solution

The original approach to solving this problem (back in October 2021) involved using Excel to generate the figures and address the remaining challenges.

Original Solution

Original Solution done in Excel

It was relatively simple to set up and generate figures without the need for pip install pandas or other Python packages. To tackle this problem using a more challenging approach, we considered doing it the FORTRAN way.

FORTRAN Solution

The FORTRAN approach (Checkout the .f90 file here!) successfully achieved the same Total Multiplicity as the original way (a total multiplicity of 5005) with the same input. Below is a pseudocode representation of the actual FORTRAN code.

    
Program MultiplicityCalculator
  Initialize n_A, n_B, q_A, q_B, q_Total, Omega_A, Omega_B, Multiplicity, TotalMultiplicity
  
  Input n_A, n_B, q_Total
  
  q_A = 0
  q_B = q_Total
  TotalMultiplicity = 0
  
  while q_A is not equal to q_Total + 1
    Calculate Omega_A using comb(q_A + n_A - 1, q_A)
    Calculate Omega_B using comb(q_B + n_B - 1, q_B)
    Calculate Multiplicity as Omega_A * Omega_B
    
    Print q_A, Omega_A, q_B, Omega_B, Multiplicity
    
    Increment q_A by 1
    Decrement q_B by 1
    Add Multiplicity to TotalMultiplicity
    
  Print the total Multiplicity
  
Contains
  
  Define comb(n, r) function
    // Combination function implementation
    
  Define fact(n1) function
    // Factorial function implementation

End Program MultiplicityCalculator

    
Figure 2.4

FORTRAN solution output for Problem 2.9

This concludes the FORTRAN exercise.

Problems with the FORTRAN Code

Similar code structure was implemented in Python and was able to yield the same Total Multiplicity for Problem 2.9, validating its functionality. The Python code successfully ran for both Problem 2.9 and Problem 2.10. However, the FORTRAN code failed to achieve the same Total Multiplicity for Problem 2.10, particularly when handling larger integer numbers.

Problem 2.10

Problem 2.10

Error Msg

FORTRAN compiler error message for Problem 2.10

The error is likely due to the fact that 50! is a very large factorial number, and it exceeds the capacity of the integer variable, resulting in a floating-point error message.

Fixes?

If anyone can find a solution to this problem, please feel free to submit a pull request.

About

Solving Schroeder's Thermal Physics Problem 2.9 with FORTRAN

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published