lecture 1 : introduction to unix and fortran · lecture 1 : introduction to unix and fortran . ......

32
Introduction to Computational Modeling Instructor : Cedric Weber Course : 4CCP1000 Lecture 1 : Introduction to UNIX and Fortran

Upload: voduong

Post on 16-May-2018

264 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

Introduction  to  Computational  Modeling  

Instructor  :    Cedric  Weber  Course        :    4CCP1000  

Lecture 1 : Introduction to UNIX and Fortran

Page 2: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

General  informations  

2  

Ø Lecture:    Thursday,  9-­‐10am  Ø Practice:  K3.16  (25C),  10am-­‐13pm    Ø Schedule  

Ø First  term:  10  Lectures,  10  practice  sessions  

Ø  1h  Lecture  /  3h  practice  Ø Lecture  :  short  overview  of  the  theory  

and  context  Ø Practice  :  programming  hands-­‐on      

Ø Office  :  4.02D  Ø Office  hours  :  Monday  pm  /  

Tuesday  am    Ø Website  :    keats.kcl.ac.uk  Ø Email:    [email protected]  

Ø Examination  1  Ø  1)  A  set  of  problems  to  be  solved  

in  the  computer  room  (3h),  codes  and  answers  collected  at  the  end  of  the  examination  and  marked  Ø Last  week  of  December  

Ø  2)    A  10  page  report  where  a  set  of  problems  is  solved  and  presented  Ø After  the  Christmas  break  

Ø   Contributes  25%  to  the  final  grade  of  the  Laboratory  module  

Ø Computers    Ø Your  KCL  login  Ø To  reset  it  :  kclpword.kcl.ac.uk    

Page 3: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

Class/Week   Chapter   Topic   Milestones  

1   Monte  Carlo   UNIX  system  /  Fortran  

2   Monte  Carlo   Fibonacci  sequence  

3   Monte  Carlo   Random  variables  

4   Monte  Carlo   Central  Limit  Theorem  

5   Monte  Carlo   “Monte  Carlo”  integration   Milestone  1  

6   Differential  equations   The  Pendulum  

7   Differential  equations   A  Quantum  Particle  in  a  box  

8   Differential  equations   The  Tacoma  bridge   Milestone  2  

9   Linear  Algebra   System  of  equations  

10   Linear  Algebra   Matrix  operations   Milestone  3  

3  

Schedule  

Page 4: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  Lecture:    *  Theory  –  historical  context    :  ~20  min  *  Programming      :  ~30  min  

*  The  lecture  is  merely  an  overview  of  the  topic  covered  by  the  practice  session  

*  You  will  be  confused  until  ~Week  5  and  really  wonder  what  we  are  doing,  that’s  to  be  expected  with  programming  

*  In  the  practice  session  you  can  ask  the  TA  any  questions  or  help  to  get  through  the  problems  

*  I  expect  from  you  to  :  *  Solve  problems  during  practice  sessions  *  Go  through  the  lecture  notes  before  the  lecture  starts  *  Spend  ~1h  on  the  lecture  notes  after  the  lecture,  

write  your  questions  down  and  contact  me  or  the  TA  to  go  through  those  points  

*  In  each  lecture  we  will  add  one  or  a  few  programming  features,  and  see  how  they  can  be  used  to  solve  important  problems  in  Physics  and  Maths  

4  

Buckle  up…  

0%  10%  20%  30%  40%  50%  60%  70%  80%  90%  100%  

wee

k  1  

wee

k  2  

wee

k  3  

wee

k  4  

wee

k  5  

wee

k  6  

wee

k  7  

wee

k  8  

wee

k  9  

wee

k  10  

Learning  curve  

Page 5: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  For  the  Theory  

5  

Reading  list  /  Literature  *  For  the  Programming  

Numerical  Analysis,  2nd  ed.,  Timothy  Sauer   Modern  Fortran  explained,  Metcalf,  Reid,  Cohen,  Oxford  

Available  at  the  KCL  library  

Page 6: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

Part  1  :  UNIX/Linux  Operative  system  

6  

Page 7: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

What  is  Unix/Linux  ?  

7  

Ø Multi-­‐tasking,  multi-­‐user  architecture  

Ø Stability,  portability  and  powerful  networking  capabilities  

Ø Available    for  laptops,  servers,  …  Ø Lightweight  :  does  not  require  

state  of  the  art    Ø Open-­‐source  (=free)  :  

Ø Open  office    =  microsoft  office  Ø firefox      =  internet  explorer  

Ø Mozzila      =  outlook  Ø …  

Ø Remote  access  (work  from  home,  supercomputers…)  

Page 8: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

Linux  File  system  

8  

Ø  Files  are  contained  in  directories  Ø  Directories  follow  a  tree  structure  Ø  The  source  directory  is  denoted    “  /  “  Ø  The  path  to  a  given  directory  is  denoted  by  its  

location  in  the  tree  :    

 /  book  /  chapter  /  page  /        

Ø  Means  the  directory  page  is  contained  in  the  directory  chapter,  itself  contained  in  the  directory  book  

Ø  Your  personal  file  are  contained  in      

Ø                   /home/”your_login”/  

Ø  An  equivalent  shorthand  notation  is  :              ~/    

Ø  You  can  access  your  files  from  any  computers  in  the  computer  room(shared  file  system)  

§  /etc/…  §  /opt/…  §  /usr/…  §  /home/your_login/            [  =        ~/      ]        §  /home/….  §  /tmp/…  §  /scratch/…    

Page 9: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

Ø You  will  be  first  prompted  to  enter  your  login  Ø Once  your  credentials  entered,  you  will  access  a  desktop  session,  with  various  menus  

Ø Find  and  open  the  program  “terminal”  in  utilities  

9  

Getting  started  with  Linux  

Ø You  can  open/close  programs  and  execute  commands  from  the  terminal  

Ø Example  :    Ø to  display  the  files  in  this  

directory,  type  “ls”,  and  press  enter  

Ø A  list  of  your  files  and  directories  will  be  shown  

Ø To  enter  the  directory  tmp  :  Ø Type  “  cd  tmp”  ,  press  enter  

Page 10: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  To  navigate  through  directories  

*  cd  [space]  name_of_directory  *  change  your  location  in  the  file  tree  from  the  current  directory  to  the  new  directory  

“name_of_directory”  

*  cd    *  bring  you  back  to  your  home  directory  (~/).  This  is  equivalent  to  :        cd          ~/        

*  cd  [space]  .    .      *  goes  one  directory  up  in  the  tree    

*  ls    *  list  the  content  of  “name_of_directory”  

*  pwd  *  gives  back  the  position  in  the  file  tree  

*  Important  :  if  you  hit  the  “tab”  key  and  entered  the  first  half  of  a  command  or  directory  name,  the  second  half  will  be  auto-­‐completed  

10  

File  system  navigation  

Page 11: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  Directory  operation    

* mkdir  [space]  name_of_directory    *  creates  a  directory  with  name  “name_of_directory”  

*  rm    [space]  -­‐r    [space]  name_of_directory    *  deletes  this  directory  and  all  its  content  

*  cp  [space]  –r  [space]  name_of_directory  [space]  name_of_target    *  Copies  the  directory  and  content  

 *  File  operation  

*  cp  [space]  file  [space]  file_copy  *  copies  the  files  named  “file”  to  a  file  named  “file_copy”  

*  rm    [space]    file_to_erase  *  erases  the  file  named  “file_to_erase”  

11  

File/directory  operations  

Page 12: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  To  manage  your  UNIX  account  :  

* quota  * will  give  you  the  amount  of  disk  space  that  you  can  use.  *  top  *  displays  the  list  of  running  processes,  the  left  column  is  a  job/process  identifier  (PID  number)  

* kill  -­‐9  PID  *  stops  a  running  calculation.  “PID”  is  the  job  identifier  obtained  by  top  

*  If  you  wish  to  stop  or  exit  a  running  calculation,  press  CTRL  and  C  at  the  same  time,  or  CTRL  and  D  

12  

Account  management  

Page 13: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  Nedit  /  Gedit  :  opens  a  text  editor  program  in  window  mode.    *  Type  in  the  terminal  :  gedit&  

*  Gnuplot  :  opens  a  plotting  tool  in  terminal  mode.  Once  in  this  program,  you  can  plot  functions.    *  Step  1,  type:  f(x)=x*x*3,  hit  enter  *  Step  2,  type:  p  f(x),  enter.  The  function  will  be  displayed.    *  Plotting  interval:  To  plot  the  function  from  x=-­‐3  to  x=+3,  *  Step  2,  type:  p  [-­‐3:3][]  f(x),  hit  enter.    

*  File  content  plotting:  to  plot  the  content  of  the  file  name_of_file,  which  has  as  its  first  column  the  x  coordinates,  and  as  a  second  column  the  y  coordinates,  *  Step  2,  type:  p  [-­‐3:3][]  ‘name_of_file’  

*  To  produce  an  output  file  of  the  plot,  type:  *  Set  term  postscript  *  Set  output  “name_of_the_file.ps”  *  rep  

*  Once  finished,  type  “exit”  to  return  to  the  terminal  *  To  display  the  produced  graphics,  type  in  the  terminal:  *  gv  name_of_the_file.ps  

*  Type  in  the  terminal:  gnuplot  *  xmgrace:  in  the  terminal,  type:  “xmgrace  name_of_file”  to  display  a  two  column  file  *  Type  in  the  terminal:  xmgrace&     13  

Text  editor,  simple  plotting  tool  

Page 14: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

14  

Ø What  does  “programming”  mean  ?    Ø  1)  Write  a  series  of  specific  instructions  (e.g.  a  

scientific  calculation)  in  a  given  language  (programming)  

Ø  2)  Transform  these  instructions  into  a  program  that  the  computer  can  execute  (compilation)  

Ø  3)  Execute  this  program  (execution)  Ø 4)  Obtain  results  (numbers,  plot)  as  a  result  of  

this  scientific  calculation  (post-­‐processing)  

Ø Fortran  programming  language  Ø Modern  programming  language  with  Python  

and  C    Ø Syntax  is  convenient  for  scientific  applications    Ø Fast  and  efficient  Ø open-­‐source  compiler  :  gfortran  Ø Large  number  of  available  scientific  libraries  Ø  Increasing  number  of  functionalities  and  

continuously  updated/improved  versions    

STEP  1  :  With  a  text  editor,  write  a  series  of  instruction  (programming)  

STEP  2  :  Transform  the  text  into  a  program  (compilation)  

STEP  3  :    execute  the  program  (execution)  

Page 15: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

Part  2  :  Fortran  Programming  

15  

Page 16: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

1.  Program  something  

2.  …  instruction  1  …  3.  …  instruction  2….  4.  …  instruction  3  …  

5.  end  program  

16  

What  is  a  program?  Ø  A  program  is  a  list  of  instructions  

Ø  Each  line  contains  one  instruction  

Ø  Instructions  are  executed  from  the  top  to  the  bottom,  one  by  one  

Ø  When  executing  your  software,  the  computer  executes  all  the  lines  until  it  reaches  the  “end  program”  instruction  

Ø  Vertical  axis  can  be  thought  of  as  a  timeline  

Ø  The  program  starts  at  the  first  line,  and  ends  at  the  last  line  

Ø  Each  instruction  has  a  very  precise  syntax,  every  coma,  space,  is  important.    

Page 17: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

1.  program  hello  

2.  write(*,*)    ‘      hi  there    !    ’  

3.  end  program  

17  

The  Hello  program  Ø  program  /  end  program  define  the  scope  of  the  series  

of  instructions  that  constitute  the  program  

Ø  In  between  these  two  lines,  you  can  add  the  instructions  that  you  want  to  execute,  the  program  stops  at  line  3  

Ø  A  name  is  given  to  the  program  scope,  in  this  case  hello.    

Ø  Write(*,*)  is  a  programming  command  :  it  displays  on  your  screen  the  text  given  as  an  argument,  so  here  the  string  of  characters  hi  there.    

Ø  The  text  shown  on  the  display  by  write(*,*)  is  given  within  quotes.  This  allows  the  computer  to  differentiate  this  string  of  characters  from  the  rest  of  your  program  

Ø  Line  starting  with  an  exclamation  mark  are  not  taken  into  account  by  the  program.  They  can  be  used  to  add  some  notes  or  reminder  for  yourself.  Those  are  called    “comments”  

1.  program  hello  2.       !    This  is  just  a  notice  …          3.  write(*,*)    ‘      hi  there    !    ’  

4.  end  program  

Page 18: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

1.  program  hello  2.     

3.     write(*,*)      ‘      2    ‘  4.  !  write(*,*)      ‘      1    ’  5.     write(*,*)      ‘      2    ‘  6.     write(*,*)        ‘    3    ‘  

7.  end  program  

18  

Spot  the  mistake  

Ø  If  I  want  to  display  1,2,3,  where  is  the  mistake  in  the  program  ?  What  do  I  need  to  change  ?    

Page 19: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  1)  write  your  program  with  any  text  editor  (the  hello  program)  *  2)  save  it  to  a  file,  the  filename  can  be  anything,  but  should  end  with  the  extension  f90  :    

*  myprogram.f90  

*  3)  open  a  terminal,  and  type  :    *  gfortran          [space]        myprogram.f90    

*  4)    Gfortran  is  a  command  (compiler)  that  converts  the  text  to  an  executable  software/program,  the  program  is  now  called  a.out  *  5)  to  execute  your  program,  type  in  the  terminal  :    

*   .  /  a.out  *  6)  If  you  modify  the  text/series  of  instructions  in  myprogram.f90,  you  need  to  compile  again,  obtain  a  new  program,  and  execute  it  again    

19  

Compilation  

Page 20: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  You  can  also  change  the  name  of  the  obtained  program  (instead  of  getting  a.out)  by  specifying  the  additional  option  –o    to  the  compiler  :      

 *  In  the  terminal,  type  :    

*  gfortran      [space  ]  –o    [space  ]  progname.out    [space  ]  myprogram.f90      

*  If  any  errors  in  your  code  (syntax,  misspells,  typos,  conceptual  problems),  your  will  receive  a  list  of  errors.  Correct  your  code  and  try  to  compile  again  

*  This  will  produce  the  executable  “prog_name.out”  

*  To  run  your  program,  write  in  the  terminal:  

*   ./progname.out     20  

Optional  :  Linking  and  executable  name  

Page 21: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

21  

Variables  We  want  to  perform  some  calculations  with  the  computer,  and  for  this  we  need  to  store  the  result  of  an  operation  in  the  memory.  A  variable  is  a  location  in  the  memory  where  you  can  store  the  result  of  an  addition,  subtraction  etcetera.  We  could  denote  this  location  with  a  number  (or  an  address),  but  it  is  more  convenient  to  just  give  it  a  name  (variable).    

There  are  different  types  of  variables  in  Fortran  :     integer            :    whole  numbers,  positive  and  negative  

     -­‐4            30  

real    :    rational  numbers  (floating  point  numbers)        1.30145        3.141592653589793        2.718281828459045  

character    :    a  string  of  character  (text)        ‘  hello  I  am  here  ‘        

Single  precision  

Double  precision  

Page 22: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  Both  integer  and  real  are  stored  in  the  computer’s  memory  in  the  binary  basis  (in  terms  of  0  and  1)  

 

*  Integers  are  decomposed  in  powers  of  2  :  

*  130 = 128 + 2 = 2^7 + 2^1 "*  Question 1 : How can you write this in terms of 0 and 1 ?"*  Question 2 : How would I represent -130 ? "*  Question 3 : How many bytes if 32 bits ? "

22  

Integer  /  real  representation  

……  

2^0"2^1"2^2"2^3"2^4"2^5"….." 2^6"2^7"2^29"2^30"2^31"

Byte  /  octet    

1"1"0"0" 0" 0"0" 0" 0" 0" 0"

Sign  bit  

bit  

Page 23: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  The single precision real number is represented as follows:"

         *  A constant shift is commonly applied to exp, so it runs from -127 to +128"*  Largest number : 2128=3.4 x 1038 smallest number : 2-127=5.8 x 10-39"*  Only the 7-8 digits after the coma are significant in single precision"

*  Example, 0.5000000 is obtained with : "*  s=0; sign=-10 " "exp=0; exp=20 "m1=1; mantissa=2-1"

23  

Representation  of  Floating  Number  

Variable   Definition   Number  of  bits  

s" sign" 1"

exp" Exponent between 0-255" 8"

mantissa" Coded as : m1 (2-1) + m2 (2-2) + … + m23 ( 2-23)" 23"

(−1)s × 2exp ×mantissa

Page 24: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

24  

Using  variables  

1.  program testingvariables"2.  Real(8) ":: " var1"3.  Integer(4) ":: " var2,var3"

4.  var1 = 1.10"5.  write(*,*) "var1 "6.  var2 = 2 "7.  write(*,*) "var2"

8.  end program"

Left  side  :  variable  type     Right  side  :  variable  names  (your  choice)  

Block  of    Variable  declarations  

Execution    Block  of  the  program  

You  can’t  declare  a  variable  in  the  execution  block  &  all  variables  need  to  be  declared  

Integer    à  integer(4)  

Floating    Single  precision  à  real(4)  

Floating    Double  precision  à  real(8)  

Page 25: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

25  

Spot  the  3  mistakes  1.  program testingvariables"2.  Integer(4) " ":: " var2"""3.  write(*,*) "var2"4.  var2 = 2 "5.  real(8) " " ":: " var4"6.  var4 = 1.10"7.  write(*,*) "var4 "8.  var2 = 2 "9.  write(*,*) "var3"10.  end program"

Block  of    Variable  declarations  

Execution    Block  of  the  program  

Page 26: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

26  

Variable  Assignment    1.  var1 = 1"2.  var1 = 2*var1 + 1"3.  write(*,*) var1 "*  Even  more  confusing  !!  *  Remember  :    

1.  Line  (2)  is  not  an  equation  2.  Line  1-­‐2-­‐3  are  executed  one  by  one  from  the  top  down  to  the  bottom.    3.  At  Line  (2),  the  computer  first  evaluates  all  the  operation  contained  on  the  

right  hand  side,  and  obtains  a  numerical  result  4.  This  numerical  result  is  stored  in  the  variable  appearing  on  the  left  hand  side,  

which  is  assigned  a  new  value  

=  ……  [Fill  in]  

Page 27: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  Basic  operations  :    *  addition:      a+b  *  substraction:    a-­‐b  *  division:      a/b  *  multiplication:    a*b  *  to  the  power  n:    a**n  *  to  the  power  1/n:    a**(1.0/2.0)    

*  Priorities:  *  Elements  in  brackets  are  done  first  *  Powers  are  done  before  multiplications  *  Multiplications  are  done  before  

additions  *  If  any  doubt,  use  a  bracket  

27  

Basic  operations  

var1 = 1 + 2 "

*  Extremely  confusing  !!    *  This  operation  is  not  an  equation  *  It  is  a  multi-­‐step  process  :    

1.  The  computer  always  first  carries  out  all  the  operation  contained  on  the  right  hand  side,  and  obtains  a  numerical  result  

2.  This  numerical  result  is  stored  in  the  variable  appearing  on  the  left  hand  side,  which  is  assigned  a  new  value  

Page 28: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

28  

Example  1.  program testingvariables"2.  Integer(4) " ":: " var1""3.  var1 = 1"4.  write(*,*) var1 "5.  var1= var1 + 2 "6.  write(*,*) var1 "7.  Var1= var1 * 2 "8.  Write(*,*) var1"9.  end program"

Block  of    Variable  declarations  

Execution    Block  of  the  program  

Questions  :    1)  How  many  outputs  displayed  ?  [Fill  in]      2)  Write  them  here  :  [Fill  in]  

Page 29: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

*  Type  are  conserved  during  operations,  so  if  “i”  and  “j”  are  integers,  i/j  is  also  an  integer  (floored  down)  *  i  =  2/3  will  give  i=0  *  i  =  4/3  will  give  i=1    

*  Integers  involved  in  floating  operations  are  automatically  converted  *  Example  :  a  =  10.0  /  3,  “3”  is  converted  internally  to  3.0  *  Warning:  some  ambiguous  cases  exists,  and  will  give  different  results  on  

different  compilers,  such  as  :  *  10.0  *  2  /  3  *  Why  is  it  ambiguous?      

*  To  avoid  any  confusion,  integer  can  be  transformed  to  real  numbers  with  the  function  “dble”  *  10.0  *  dble(2)/dble(3)  is  equivalent  to  10.0  *  2.0/3.0  *  a  =  2.0  *  b  /  dble(i),  where  “a,b”,  are  real  numbers  and  “i”  is  an  integer      29  

Operations  with  different  types  

Page 30: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

30  

Operations  mixing  different  types  Integer(4) ":: var2"Real(8) ":: var1" var1 = var2 / 2 "

*  If  var1  is  a  floating  number  and  var2  an  integer,  and  they  appear  in  the  same  operation,  we  need  to  convert  var2  to  a  floating  number  *  Integer  to  floating  type  conversion  :  *  Simply  insert  dble(    )  around  var2,  this  will  transform  var2  from  an  

integer  to  a  floating  number  

var1 = dble(var2) / 2 "*  Warnings  :  Integer  divisions  are  floored  down  *  Example  :  var2  =  1  /  2  will  give  the  value  zero  to  the  variable  var2  

*  To  distinguish  integer  values  from  floating  values,  we  always  append  explicitly  a  coma  to  floating  point  numbers  *  Example  :  var1  =  1.0  /  2.0      

[and  not  var1  =  1  ]    

Page 31: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

Functions readily available in Fortran:"Trigonometric functions:"" "à sin(x), cos(x), tan(x)"

Inverse functions:"" "à asin(x),acos(x),atan(x)"

Hyperbolic functions: "" "à sinh(x),cosh(x),tanh(x)"

units of x are radians above"Absolute value:"" "à abs(x)"

Exponentials:"" "à exp(x)"

Square root:"" "à Sqrt(x)"

31  

Available  functions  for  double  precision  floating  numbers  

1.  program  testop  2.  real(8)    ::    a  ,  b  3.  a=sin(2.0)  4.  b=cos(  a  )  5.  write(*,*)  ‘b=‘,  b  6.  end  program  

Page 32: Lecture 1 : Introduction to UNIX and Fortran · Lecture 1 : Introduction to UNIX and Fortran . ... Computers)! Your)KCL)login)! ... in!a!given!language! (programming)!

Practice  in  K3.16  /  25  C  

1st  Floor  -­‐  Kings  Building  

32