Fortran چیست؟ آموزش کامل و جامع Fortran
فهرست مطالب
Fortran چیست؟
Fortran (FORmula TRANslation) یکی از قدیمیترین زبانهای برنامهنویسی سطح بالا است که برای محاسبات علمی و مهندسی طراحی شده است. این زبان بهویژه در محاسبات عددی، فیزیک محاسباتی و هواشناسی کاربرد گستردهای دارد.
program hello
implicit none
print *, "سلام دنیا!"
end program hello
تاریخچه Fortran
Fortran در سال 1957 توسط تیمی در IBM به رهبری جان بکوس توسعه یافت. از آن زمان تاکنون، نسخههای مختلفی از Fortran منتشر شده که هر کدام ویژگیهای جدیدی را معرفی کردهاند.
ساختار پایه Fortran
برنامههای Fortran از یک ساختار مشخص پیروی میکنند که شامل تعریف برنامه، متغیرها و دستورات اجرایی است.
program example
implicit none
! تعریف متغیرها
integer :: x, y
real :: result
! دستورات اجرایی
x = 5
y = 3
result = x + y
print *, "Result:", result
end program example
انواع داده در Fortran
Fortran دارای انواع داده متنوعی است که برای محاسبات علمی و مهندسی بهینه شدهاند.
program data_types
implicit none
! انواع داده عددی
integer :: i = 42
real :: x = 3.14
double precision :: d = 3.14159265359
complex :: c = (1.0, 2.0)
! نوع منطقی
logical :: flag = .true.
! رشتهها
character(len=10) :: str = "Fortran"
print *, "Integer:", i
print *, "Real:", x
print *, "Double:", d
print *, "Complex:", c
print *, "Logical:", flag
print *, "String:", str
end program data_types
آرایهها و ماتریسها
آرایهها و ماتریسها از قویترین ویژگیهای Fortran هستند که برای محاسبات علمی بهینه شدهاند.
program arrays
implicit none
! تعریف آرایهها
integer, dimension(5) :: vector = [1, 2, 3, 4, 5]
real, dimension(3,3) :: matrix
integer :: i, j
! مقداردهی ماتریس
do i = 1, 3
do j = 1, 3
matrix(i,j) = i + j
end do
end do
! عملیات برداری
print *, "Vector sum:", sum(vector)
print *, "Matrix trace:", sum([(matrix(i,i), i=1,3)])
end program arrays
ساختارهای کنترلی
Fortran دارای ساختارهای کنترلی قدرتمندی برای کنترل جریان برنامه است.
program control_structures
implicit none
integer :: i, sum
! حلقه DO
sum = 0
do i = 1, 10
sum = sum + i
end do
! شرط IF
if (sum > 50) then
print *, "مجموع بزرگتر از 50 است"
else if (sum == 50) then
print *, "مجموع برابر 50 است"
else
print *, "مجموع کوچکتر از 50 است"
end if
! CASE ساختار
select case (sum)
case (1:10)
print *, "مجموع بین 1 تا 10 است"
case (11:20)
print *, "مجموع بین 11 تا 20 است"
case default
print *, "مجموع بزرگتر از 20 است"
end select
end program control_structures
توابع و زیربرنامهها
توابع و زیربرنامهها در Fortran امکان نوشتن کد ماژولار و قابل استفاده مجدد را فراهم میکنند.
module math_operations
implicit none
contains
! تعریف تابع
function factorial(n) result(fact)
integer, intent(in) :: n
integer :: fact, i
fact = 1
do i = 1, n
fact = fact * i
end do
end function factorial
! تعریف زیربرنامه
subroutine swap(a, b)
integer, intent(inout) :: a, b
integer :: temp
temp = a
a = b
b = temp
end subroutine swap
end module math_operations
program main
use math_operations
implicit none
integer :: x = 5, y = 10
print *, "Factorial of 5:", factorial(5)
print *, "Before swap:", x, y
call swap(x, y)
print *, "After swap:", x, y
end program main
ورودی و خروجی در Fortran
Fortran دارای امکانات گستردهای برای خواندن و نوشتن دادهها است.
program io_examples
implicit none
integer :: age
real :: height
character(len=20) :: name
! خواندن از ورودی
print *, "Enter your name:"
read *, name
print *, "Enter your age and height:"
read *, age, height
! نوشتن در فایل
open(unit=10, file="data.txt", status="replace")
write(10,*) "Name:", trim(name)
write(10,*) "Age:", age
write(10,*) "Height:", height
close(10)
print *, "Data has been written to data.txt"
end program io_examples
ویژگیهای مدرن Fortran
نسخههای جدید Fortran قابلیتهای مدرنی را برای برنامهنویسی معاصر ارائه میدهند.
! برنامهنویسی شیءگرا
module shape_module
implicit none
type :: Shape
real :: area
contains
procedure :: calculate_area
end type Shape
type, extends(Shape) :: Rectangle
real :: width
real :: height
contains
procedure :: calculate_area => rectangle_area
end type Rectangle
contains
function rectangle_area(this) result(area)
class(Rectangle), intent(in) :: this
real :: area
area = this%width * this%height
end function rectangle_area
end module shape_module
! برنامهنویسی موازی
program parallel_example
use omp_lib
implicit none
integer :: i
!$omp parallel do
do i = 1, 1000
call heavy_computation(i)
end do
!$omp end parallel do
contains
subroutine heavy_computation(n)
integer, intent(in) :: n
! محاسبات سنگین
end subroutine heavy_computation
end program parallel_example
بهترین شیوههای کدنویسی Fortran
رعایت اصول و بهترین شیوههای کدنویسی در Fortran به افزایش خوانایی، نگهداری و کارایی کد کمک میکند.
۱. سازماندهی کد
! استفاده از ماژولها
module utilities
implicit none
private ! همه چیز خصوصی است مگر موارد مشخص شده
public :: helper_function ! فقط این تابع عمومی است
contains
function helper_function(x) result(y)
real, intent(in) :: x
real :: y
y = x * 2
end function helper_function
end module utilities
! برنامه اصلی
program main
use utilities
implicit none
! متغیرها در ابتدا
integer :: i
real :: result
! کد اصلی
result = helper_function(3.14)
end program main
۲. بهینهسازی کارایی
program optimization
implicit none
real, dimension(1000,1000) :: matrix
integer :: i, j
! روش کارآمد - دسترسی ستونی در Fortran
do j = 1, 1000
do i = 1, 1000
matrix(i,j) = i + j
end do
end do
end program optimization
کاربردهای Fortran
Fortran در بسیاری از حوزههای علمی و مهندسی کاربرد دارد:
۱. محاسبات علمی
program numerical_integration
implicit none
real :: a = 0.0, b = 1.0 ! بازه انتگرالگیری
integer :: n = 1000 ! تعداد نقاط
real :: result
result = trapezoidal_integration(a, b, n)
print *, "Integral result:", result
contains
function trapezoidal_integration(a, b, n) result(integral)
real, intent(in) :: a, b
integer, intent(in) :: n
real :: integral, h, x
integer :: i
h = (b - a) / n
integral = 0.5 * (func(a) + func(b))
do i = 1, n-1
x = a + i * h
integral = integral + func(x)
end do
integral = integral * h
end function
function func(x) result(y)
real, intent(in) :: x
real :: y
y = x**2 ! تابع مورد نظر برای انتگرالگیری
end function
end program numerical_integration
۲. شبیهسازی فیزیکی
program particle_motion
implicit none
real :: position(3) = [0.0, 0.0, 0.0]
real :: velocity(3) = [1.0, 2.0, -0.5]
real :: dt = 0.01
integer :: i
do i = 1, 100
call update_position(position, velocity, dt)
print *, "Time:", i*dt, "Position:", position
end do
contains
subroutine update_position(pos, vel, dt)
real, intent(inout) :: pos(3)
real, intent(in) :: vel(3), dt
pos = pos + vel * dt
end subroutine
end program particle_motion
۳. هواشناسی و اقلیمشناسی
Fortran در مدلهای پیشبینی آب و هوا و تحلیلهای اقلیمی استفاده میشود.
۴. مهندسی هوافضا
محاسبات دینامیک سیالات محاسباتی (CFD) و شبیهسازیهای آیرودینامیکی
ابزارهای مفید برای کار با Fortran
کامپایلرها
– Intel Fortran Compiler (ifort)
– GNU Fortran (gfortran)
– NAG Fortran Compiler
– PGI Fortran
محیطهای توسعه
– Visual Studio با افزونه Intel Fortran
– Eclipse با افزونه Photran
– Code::Blocks با پشتیبانی Fortran
کتابخانههای مفید
– LAPACK: برای محاسبات جبر خطی
– BLAS: توابع پایه جبر خطی
– NetCDF: برای کار با دادههای علمی
– MPI: برای برنامهنویسی توزیعشده