Computational Astrophysics AS 3013

Similar documents
Introduction to Fortran Programming. -External subprograms-

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Review More Arrays Modules Final Review

Numerical Modelling in Fortran: day 2. Paul Tackley, 2017

Introduction to Modern Fortran

printf( Please enter another number: ); scanf( %d, &num2);

Subroutines and Functions

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017

ME1107 Computing Y Yan.

Practical Numerical Methods in Physics and Astronomy. Lecture 1 Intro & IEEE Variable Types and Arithmetic

Fortran Coding Standards and Style

Table 2 1. F90/95 Data Types and Pointer Attributes. Data Option. (Default Precision) Selected-Int-Kind

Exercise 1.1 Hello world

AMath 483/583 Lecture 8

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Introduction to Fortran Programming. - Module -

Lecture V: Introduction to parallel programming with Fortran coarrays

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

3. Simple Types, Variables, and Constants

Visual C# Instructor s Manual Table of Contents

Lecture 2 Tao Wang 1

Review Functions Subroutines Flow Control Summary

LECTURE 0: Introduction and Background

Numerical Methods in Physics. Lecture 1 Intro & IEEE Variable Types and Arithmetic

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Introduction to Modern Fortran

Scientific Programming in C VI. Common errors

A quick guide to Fortran

Variable A variable is a value that can change during the execution of a program.

Examining the Code. [Reading assignment: Chapter 6, pp ]

CS110: PROGRAMMING LANGUAGE I

Data Types and Variables in C language

gfortran - Linux Command

2. Basic Elements of Fortran

Reusing this material

1. User-Defined Functions & Subroutines Part 1 Outline

Casting in C++ (intermediate level)

Introduction to Fortran Programming. -Internal subprograms (1)-

Our Strategy for Learning Fortran 90

7. Procedures and Structured Programming

Fortran. (FORmula TRANslator) History

CSc Introduction to Computing

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Week 3 Lecture 2. Types Constants and Variables

NO CALCULATOR ALLOWED!!

C interfaces to HSL routines. J. D. Hogg. Version 1.0 5th December Numerical Analysis Group Internal Report

An Introduction to Fortran

CS313D: ADVANCED PROGRAMMING LANGUAGE

Exercise: Inventing Language

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

Independent Representation

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Old Questions Name: a. if b. open c. output d. write e. do f. exit

Programming in C++ 5. Integral data types

COMP 202 Java in one week

Introduction to Programming using C++

Outline of Fortran 90 Topics

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Preprocessor Directives

An introduction to Fortran. Daniel Price School of Physics and Astronomy Monash University Melbourne, Australia

Programming Language 2 (PL2)

Chapter 2 THE STRUCTURE OF C LANGUAGE

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

ANSI C Programming Simple Programs

CS240: Programming in C. Lecture 2: Overview

Fundamental Data Types

Exercise: Using Numbers

PACKAGE SPECIFICATION HSL 2013

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Basic Types and Formatted I/O

Fortran90 Bindings for Charm++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Subroutines, Functions and Modules

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Lecture 2 FORTRAN Basics. Lubna Ahmed

10. Additional Intrinsic Data Types

CS Programming In C

Modern Fortran OO Features

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

3. Java - Language Constructs I

Data Types and Basic Calculation

EL2310 Scientific Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Tokens, Expressions and Control Structures

CS112 Lecture: Primitive Types, Operators, Strings

Fundamentals of Programming

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Functions in C C Programming and Software Tools

Fortran Introduction

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

A First Program - Greeting.cpp

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Programming with Python

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Fortran programming for beginner seismologists Lesson 6

Fortran classes and data visibility

Transcription:

Computational Astrophysics AS 3013 Lecture 2: 1) F90 variable types 2) variable declaration 3) good programming style AS3013: F90 lecture2 1

Fortran 90 variable types integer whole numbers: 3, 244, -10, default is integer*4 (compiler-dependent!) 4 bytes = 32 bits => ±(2 31-1) = -2147483647 +2147483647 otherwise overflow you can also use integer*2, or integer*8 real floating point numbers: 0.0333, 0.0, -5.421, 3.56e+8, default is real*4 = single precision which gives about 7 significant digits, and exponents -38 +38 otherwise overflow or underflow AS3013: F90 lecture2 2

Fortran 90 variable types double and quadrupole precision you can switch to real*8 = double precision e.g. 0.d0, 1.2d+0, 3.756793451752398d-156 about 15 significant digits, and exponents -308 +308 real*8 constants are specified by d or D or _8 otherwise overflow or underflow somewhat slower (on 32-bit architecture) you can switch to real*16 = quadrupole precision real*16 constants are specified by q or Q or _16 real :: pi1 real*8 :: pi2 real*16 :: pi3 pi1 = ACOS(-1.0) pi2 = ACOS(-1.d0) pi3 = ACOS(-1.q0) print*,pi1 print*,pi2 print*,pi3 3.141593 3.14159265358979 AS3013: F90 lecture2 3.14159265358979323846264338327950 3

Fortran 90 variable types complex two single precision floating point numbers (Re,Im), c=re+i*im very convenient, intrinsic numerical functions for complex numbers you can switch to complex*8, or complex*16 complex :: const const = (-1.0,0.0) print *,const, print *,SQRT(const) ( -1.0000000, 0.0000000 ) ( 0.0000000, 1.0000000 ) AS3013: F90 lecture2 4

character Fortran 90 variable types a string of characters with maximum length N: character(len=n) all ASCII characters allowed, e.g. 'A', '...', 'Peter', 'signal/noise' logical character(len=20) :: YourName print *, "What's your name?" read *, YourName print *, "Hello "//trim(yourname)//", how are you today?" What's your name? Peter Hello Peter, how are you today? a logical value, can only be.true. or.false. often the result of a logical expression logical :: larger real :: a,b larger = (a>b) if (larger) then... AS3013: F90 lecture2 5

Be careful when combining variables of different types expressions containing different types of variables are a frequent source of errors and inaccuracies depending on the Compiler, automatic type conversions will be performed, sometimes with quite unexpected results use your own type conversions to say explicitly what you want real() real*4 dble() real*8 int() integer integer powers are OK though, and actually recommended program TEST implicit none real*8 :: pi=acos(-1.d0) real*16 :: pi16=acos(-1.q0) character(len=4) :: name='peter' print *, '1: ',name print *, '2: ',2/3*pi print *, '3: ',2*pi/3 print *, '4: ',2.0/3.0*pi print *, '5: ',2.d0/3.d0*pi print *, '6: ',2.q0/3.q0*pi print *, '7: ',2.q0/3.q0*pi16 print *, '8: ',int(pi) end program TEST AS3013: F90 lecture2 6

program F90 units the main program, execution starts here there may only be one program-unit subroutine a general block of code can be called from anywhere may have an arbitrary number of input and output variables function a general block of code may have any number of input, but only one output variable module a list of variables in a block of memory (selected) access can be granted in any program / subroutine / function by the use command AS3013: F90 lecture2 7

general structure of F90 units module NATURE real*8,parameter :: cl=2.9979245800d+08! [m/s] real*8,parameter :: bk=1.3806581200d-23! [J/K] real*8,parameter :: hplanck = 6.62607554d-34! [J s] real*8,parameter :: nm=1.d-9! 1nm [m] end module NATURE program MYPROG use NATURE,ONLY: cl,nm implicit none real*8 :: Temp,freq,lambda,Bnu,BPLANCK read *,Temp,lambda freq = cl/(lambda*nm) Bnu = BPLANCK(Temp,freq) end program MYPROG real*8 function BPLANCK(T,nu) use NATURE,ONLY: hplanck,cl,bk implicit none real*8,intent(in) :: T,nu real*8 :: x x = hplanck*nu/(bk*t) BPLANCK = 2.d0*hplanck*nu**3/cl**2 / (EXP(x)-1.d0) end function BPLANCK unit starts... } variable declaration unit ends unit starts... variable declaration executable statements unit ends unit starts... variable declaration executable statements unit ends AS3013: F90 lecture2 8

type variable declaration type, modifier1, modifier2,... :: name1, name2,... basic type of a variable, for example integer, real, double precision, logical, character modifier parameter unchangable constant dimension(3) three of them, e.g. for vectors external function to be passed in argument list allocatable decide about dimensions later intent(in), intent(out), intent(inout) kind of variable in argument list there are more modifiers name up to 31 characters allowed: A-Z, a-z, 0-9, and '_' first character must be a letter, for example speed_0 Fortran does not distinguish between upper and lower case letters AS3013: F90 lecture2 9

variable declaration all variables used in a unit must be declared declarations take effect only in the local unit for example, variable names declared in the main program are unknown / undefined in a subroutine implicit none switch off old-fashioned and dangerous implicit declaration use this command at the start of each Fortran unit! use MyModule,ONLY: name1, name2,... grant access to selected variables in a module very convenient AS3013: F90 lecture2 10

Programming today's astronomy is unthinkable without computers developing your programming skills will be crucial for your career in astronomy skills required: 1. critical reading what's the requested functionality? 2. analytical thinking (take it apart into simple tasks...) 3. innovative solutions for the simple tasks 4. creative synthesis (... and put it back together!) 5. attention to detail AS3013: F90 lecture2 11

Programming Tips keep it simple: simple is beautiful...... and more likely to be correct - complexity leads to errors clear code structure: meaningful variable names, well-defined short sub-units (this will be assessed in the exercises!) include comments & headers... while writing the program: comments, headers, units (this will be assessed in the exercises!) often better than external manual think before programming will minimise your debugging time base code on what is required to reach goal not on what makes an elegant code AS3013: F90 lecture2 12

How to write a program? 1. analyse the problem what are the basic tasks, functionalities, goals? which physical assumptions, approximations? 2. decide on overall code structure break up the problem into smaller and smaller sub-units decide on algorithms, draw a flow chart! names and structure of variables 3. ask for advice, early! anyone solved similar problems before? are there alternatives? (ready-to-use codes? better algorithms?) 4. code it, and test it!!! debug small units first most important parts of code writing! most time spent here! AS3013: F90 lecture2 13