Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Programming

Description:

If you program like a dinosaur, your code will be outdated no matter what language you use. ... You may give names, which are obviously temporary: tmp, dummy ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 29
Provided by: astrono5
Category:

less

Transcript and Presenter's Notes

Title: Programming


1
Programming
2
Steps and Stages
  • Unix aliases, scripts, emacs
  • Programming languages
  • Algorithms
  • Running codes and handling data
  • Analysis
  • Astrophysics interpretation

3
Making life more comfortable
  • aliases are a great way to get organized and to
    make convenient short-cuts for system-independent
    commands

Examples making human commands
alias dirls -l alias
printlpr alias copycp almost mandatory
alias cpcp
-i alias mvmv -i alias rmrm -i short-cuts
alias
mmore alias xemacs alias
eemacs -nw alias hhistory connection
s alias leo1ssh -X
leo1.aip.de navigation alias CODEcd
HOME/CODE alias RUN1cd scratch/RUN1
4
Connecting to other computer(s)
Use ssh and scp To avoid typing you password
again and again, use ssh-keygen and then ssh-add.
In this case you type your pass only once.
Steps 1)ssh-keygen -t rsa 2)take ...pub file
and scp it to another computer 3)add it to a file
.ssh/authorized-keys(2) 4)run ssh-agent bash on
your computer 5)run ssh-add. You will be prompted
to give passphrase. Next time you login to your
computer make (4) and (5). Your system may be
setup so that you do not need (4). Connection
with the external computer may be slow. To edit
files use emacs -nw, which makes editing
possible with emacs even with very slow
connection. You lose some functionality of emacs
cannot use buttons. Yet, it is better than
using vi.
5
EMACS
  • Emacs is a standard editor, which you can find in
    any unix or Mac OS system. It has many options
    and can be adjusted to make your editing more
    efficient.
  • The best way to customize emacs is to make
    changes to .emacs

6
(No Transcript)
7
Shell Scripts
  • Simulations often create many files. For example,
    code may produce many snapshots.
  • Analysis of many snapshots requires special tools
    of handling repetitive tasks
  • Archiving and retrieving of many models and
    snapshots

8
Simple example
This script creates a list of snapshots
(variables file1, 2,3). Then it calls executable
PMgalaxy3 many times and feeds it with current
snapshot number. It works fine given the number
of the snapshots is not large. Place the text in
a file run.bat. Make it executable (chmod ax
run.bat). Then just run it as if it is a unix
command run.bat
9
  • Programming with Fortran
  • Style and rules
  • Statements
  • Examples
  • Compilers and options

10
General Assessment
Fortran is designed for number-crunching. It is
not for problems with sophisticated data
structures or logic. If you follow rules, Fortran
provides a very fast and easy to write code. Do
not use archaic features in Fortran. Fortran is
about speed fast codes is our goal.
11
Before we start coding
  • Check formulae and algorithms. It takes very long
    time to develop and to debug a code. If at the
    end we find that there was a mistake in
    equations, we lose too much.
  • More efficient algorithm has higher priority
    over pretty code writing.
  • Start with clearly defining data structures
    arrays, parameters, files.
  • Estimate time needed for code developing 10 -
    design, 10 - write, 80 - debug
  • High price for making errors write code nice and
    clear learn how find bugs.

12
Example
Declarations
Main body
Finish
13
The same example in Fortran-90 and C
14
Example Main program
Declarations are in file nbody1.h
Open files
Set parameters
Read data
Analyze and print
15
Example subroutine for direct summation
16
Style and rules
  • Write Fortran code as C objects and data flow
    are first, arithmetics is second.
  • Top down design start with the top level of the
    code. Write the logic of calls of different
    top-level routines. Then fill the gaps by writing
    routines of lower and lower levels.
  • Routines should be short. No side effects. If a
    routine is assigning density of particles to some
    array, do not modify potential.

17
Archaic Features, which should be avoided
  • Labels or line numbers should be avoided. Code
    can jump to any label (goto N). Because compiler
    does not know from where you can jump in to the
    labeled statement, it does not do many
    optimizations. This makes code very slow.
  • Compilers try to optimize pieces of the code,
    which do not contain labels.
  • Do not abuse the rule sometimes it is easier to
    use labels.

18
Implicit statement
  • It is often recommended (in strong words) not to
    use Implicit statement. The main motivation is
    that one can make a typo in a name of a variable,
    which is difficult to find. Implicit none is
    recommended for the beginners.
  • This recommendation comes from people, who do not
    write large and complicated codes.
  • Implicit is a powerful tool, which makes coding
    more efficient. Yet, one should always use it or
    never use it. Do not mix explicit declarations
    and implicit style.
  • Always follow implicit rules variables and
    arrays starting with letters (i-n) are INTEGER.
    Do not define Mass as real. aMass is as clear
    as Mass.

19
Writing comments
  • Rule Number One write text in such a way that
    just looking at it one sees the structure of the
    code. No comments are needed to understand what
    is written.
  • Rule Number Two follow rule N1, but then write
    short comments Always. Write them as you type.
    You will not come back to add a comment.
  • Rule Number Three comment every subroutine or
    function by putting dividing lines, which clearly
    separate the text.
  • Rule Number Four no dividing lines in the main
    text. Comments should not obstruct the text

20
More on Style calling names
  • Use long and natural names for global variables
    and arrays. E.g., Nparticles, Niterations.
  • For local temporary variables use short names
    x,y,i,k0
  • In a long subroutine one should be careful not
    to use names, which are already in use. You may
    give names, which are obviously temporary tmp,
    dummy
  • Use grab to check whether name is used.

21
Formal parameters and common blocks
  • Data can be passed to a subroutine either using
    formal arguments ( e.g. CALL MySUB(x,y,z) ) or by
    a common block ( e.g. COMMON /A/x,y,z) )
  • Those are very different mechanisms. Formal
    arguments are handled through stack and common
    blocks (also SAVE and ALLOCATE) go to the heap.
    Most of the computer RAM is allocated to the
    heap because by design stack is for small
    things.
  • Use formal arguments for small arrays, few
    arguments, and for constructs, which cannot be
    handled by common blocks.

22
Formatting I/O
  • Fortran has extensive tools and options to read
    and write files. There is nothing like that in C.

write (,10) a,b,c 10 format(f8.2,f9.2,f10.5)
Format can be given as a separate statement or as
an in-line specification
write (,(f8.2,f9.2,f10.5)) a,b,c
23
Implicit Do loops in formats
  • You can write in a file n1 an array Z of length
    N in two ways
  • write(5) Z or write(5) (Z(i),i1,N)
  • The first form is faster, but the second is more
    flexible if we want to modify it
  • write(5) (Z(i),i1,N-5,3)
  • More complicated example
  • write(5) a,b,c,((Z(i,j),i1,N,10),j2,M/2)

24
Formatting outputs
  • To produce nice-looking output tables it is
    convenient to use T format. For example,
  • write(,(f8.2,T30,a,g12.3)) r,Density,d
  • starts text Density at the position 30. This is
    useful for wide tables with many headers

25
Files
  • Formatted and Unformatted
  • Sequential and direct access
  • positionappend

Unit 20 is attached to sequential, formatted file
myfile.dat
Open(20,filemyfile.dat)
Open(20,filemyfile.dat, formunformatted)
Unit 20 is attached to sequential, unformatted
file myfile.dat
Write x,y,z in to Unit 20. Because it is attached
to file.dat, the write command modifies myfile.dat
write(20,) x,y,z
read(20,,error10,end10) x,y,z
Read from Unit 20. If error of end of file
happen, go to statement labeled 10
Close(20)
26
Compilation
Compile and link
ifort -fast file1.f file2.f -o file.exe
ifort -fast -c file1.f
Compile
ifort -fast -c file2.f
link
ifort -fast file1.o file2.o -o file.exe
27
(No Transcript)
28
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com