Python for Fortran Programmers – Chapter 2

See the index page for context.


#!/usr/bin/python

# Translation of program "CONVRT" from D.M. Etter, _Structured FORTRAN
# 77 for Engineers and Scientists_, p.46

# This program converts kilowatt-hours to Joules.

kwh = float(raw_input("Enter energy in kilowatt-hours: "))
joules = 3.6e6*kwh

print "%6.2f kilowatt-hours is %9.2e Joules" % (kwh, joules)

# Notes:
#
# 1. raw_input reads keyboard input as a string. float(somestring)
# converts somestring into a floating-point representation. Similarly,
# int(somestring) converts somestring into an integer representation
# and long(somestring) converts somestring into a long integer
# representation.
#
# 2. Etter's text shows 0.20e+08 Joules as the result, when 1.98e+07
# is actually correct. Real variables must have been much lower
# precision in the original text.

#!/usr/bin/python

# Translation of program "GROWTH" from D.M. Etter, _Structured FORTRAN
# 77 for Engineers and Scientists_, p.49

# This program predicts bacterial growth

from math import exp

yold = float(raw_input("Enter initial population: "))
time = float(raw_input("Enter time elapsed in hours: "))

ynew = yold*exp(1.386*time)

print "Initial population = %9.4f" % (yold)
print "Time elapsed (hours) = %9.4f" % (time)
print "Predicted population = %9.4f" % (ynew)

# Notes:
#
# Any math beyond the most basic operations (addition, subtraction,
# multiplication, division) requires the math module to be
# available. You can either use the form 'from math import exp, log,
# log10, pow, ...' to import specific functions from the math module
# and call them by name as 'exp', 'log', etc. , or the form 'import
# math' to import the entire module, and call them as 'math.exp',
# 'math.log', etc.
#
# Option 1:
#
# from math import exp
# ...
# ynew = yold*exp(1.386*time)
#
# Option 2:
#
# import math
# ...
# ynew = yold*math.exp(1.386*time)
#
# See http://docs.python.org/lib/module-math.html for other functions.

#!/usr/bin/python

# Translation of program "DATE" from D.M. Etter, _Structured FORTRAN
# 77 for Engineers and Scientists_, p.51

# This program estimates the age of an artifact from the proportion of
# carbon remaining in the artifact.

from math import log

carbon = float(raw_input("Enter proportion remaining for carbon dating: "))

age = (-log(carbon))/0.0001216

print "Estimated age of artifact is = %6.1f years" % (age)

#!/usr/bin/python

# Translation of program "TRAIN" from D.M. Etter, _Structured FORTRAN
# 77 for Engineers and Scientists_, p.53

# This program computes the horizontal force generated by a train on a
# level curve.

from math import pow

weight = float(raw_input("Enter weight of train in tons: "))
mph = float(raw_input("Enter speed of train in miles per hour: "))
radius = float(raw_input("Enter radius of curve in feet: "))

force = (weight*2000.0/32.0)*(pow((mph*1.4667),2)/radius)

print "Train weight - %8.2f tons" % (weight)
print "Train speed - %8.2f mph" % (mph)
print "Curve radius - %8.2f feet" % (radius)
print
print "Resulting horizontal force - %8.2f pounds" % (force)

#!/usr/bin/python

# Translation of program "RELY" from D.M. Etter, _Structured FORTRAN
# 77 for Engineers and Scientists_, p.57

# This program computes the reliability of instrumentation using a
# Bernoulli equation.

from math import pow

print "Enter reliability of single component"
p = float(raw_input("(Use percentage between 0.0 and 100.0): "))

n = int(raw_input("Enter number of components in equipment: "))

perc = pow((p/100.0),n)*100.0

print "Percent of the time that the equipment"
print "should work without failure is %6.2f%%" % (perc)

# Notes:
#
# See
# http://docs.python.org/lib/typesseq-strings.html#typesseq-strings
# for more information on formatting strings in Python.

Leave a comment

Your email address will not be published. Required fields are marked *