Shell und UNIX
Typischer Datei-Baum unter UNIX
/
/bin
/dev
/bin/sh /bin/ksh /bin/bash /bin/csh /bin/tcsh /bin/zsh
Shells.. /bin/cp /bin/mv /bin/man
/etc
/tmp
/usr
/lib
usr/local
/usr/users
usr/local/bin
/usr/users/wolfi
/usr/local/bin/f95
Compiler..
/usr/users/heinz /usr/users/roger
Benutzer..
Kommandos Die Shell ist der Kommandozeilen-Interpreter vom Betriebssystem UNIX. Sie ist ein (normales) Programm das dem Super-User (Administrator) geh¨ ort. Der Benutzer kann es nicht l¨ oschen! Je nach Installation hat der Benutzer nach dem einloggen eine Shell. Es gibt komfortable aber wenig verbreitete Shells wie zsh und tcsh.
1
Shell und UNIX
Verschiedene Shells: Merkmale Bourne-Shell (S.R. Bourne): sh • unkomfortabel: Tippfehler = neu tippen • auf jedem UNIX System vorhanden !! C-Shell: csh • Eingabe komfortabler • Kommandos angelehnt an C (vorwiegend Kleinbuchstaben) • aufw¨ arts-kompatibel dazu ist Tenex-Shell: tcsh Korn-Shell (Bolsky and Korn): ksh • Mindestens so komfortable wie C-Shell • absolut aufw¨ arts-kompatibel zur sh Bourne (borne) Again Shell: bash • absolut aufw¨ arts-kompatibel zur sh • fast aufw¨ arts-kompatibel zur ksh und csh • Bei Suse Linux Standard; verbreitet sich rasend 2
Shell und UNIX
Shell Stammbaum
1977 sh csh
1979 1988
ksh (88)
1993
ksh (93)
1995
tcsh
1998
bash
2004 überall
Linux
Rechenzentren
MM5
???
Ablauf wenn ich ein Terminal (Konsole) o ¨ffne Shell sh ksh bash
csh tcsh
login-Shell /etc/profile, $HOME/.profile /etc/profile, $HOME/.profile, ENV Datei /etc/profile, $HOME/.bash_profile, $HOME/.bash_login, $HOME/.profile $HOME/.cshrc, $HOME/.login /etc/csh.cshrc, /etc/csh.login, $HOME/.cshrc, $HOME/.login
nichtlogin-Shell — ENV Datei, ENV=datei gesetzt in $HOME/.profile $HOME/.bashrc
$HOME/.cshrc /etc/csh.cshrc, ~/.login, ~/.tcshrc, ~/.tcshrc 3
Shell und UNIX
¨ Anderungen wirksam machen ¨ nderungen in .profile bzw .login gemacht und Ich habe A m¨ ochte dass diese sofort wirksam werden • Ich habe sh, ksh oder bash: . .profile • Ich habe csh oder tcsh: source .login Ich habe sh, ksh, bash und m¨ ochte meine Anpassungen auf ein separates File schreiben: # dies ist die letzte Zeile von .profile . $HOME/.mysettings # in .mysettings sind meine Anpassungen Typische Anpassung: Such-Pfad verl¨ angern PATH=$PATH:. set path = ( $path . )
# in .profile # in .login
sh ksh bash csh tcsh
4
Shell und UNIX
Der Anfang: einige UNIX Standard Kommandos: Kommando which man ls cp mv rm echo date chmod
Wirkung welches Kommando wird benutzt ? Kurz-Referenz des Kommandos Dateien und Rechte listen copy umbenennen l¨ oschen Textausgabe Datum ausgeben Datei-Attribute setzen
Beispiel which f95 man man ls -alF cp von nach mv alt nachneu rm -i datei echo "Text ausgeben", echo $HOME, echo $PATH date +%j chmod ugo+r
Shell-Skripts In der Datei todo steht: # ich bin ein shell-script echo "Compile and run demo.f90" f95 demo.f90 ls -l a.out a.out echo "fertig" Start in Sub-Shell sh todo ksh todo csh todo bash todo oder einfach f¨ ur den user (mich) ausf¨ uhrbar machen: chmod u+x todo todo 5
Shell und UNIX
Start auf gleicher Ebene Start als Prozess auf gleicher Ebene . todo in sh, ksh, bash bzw. source todo in csh oder tcsh. Dies ist normalerweise unerw¨ unscht, da die aktuelle Umgebung beeinflusst wird, wohingegen der Kind-Prozess nur intern etwas an der Umgebung ¨ andert. Ausnahme: .profile bzw, .login Pseudokommentar: Shell in der todo l¨ auft bei Aufruf mit Namen durch Pseudokommentar erzwingen #!/bin/bash # laeuft unter Bourne-Again Shell falls mit todo gerufen # sh todo oder ksh todo .... ignorieren diesen Kommentar # ebenso Aufruf mit . todo echo "Compile and run demo.f90" ... Argumente an Skript ¨ ubergeben #!/bin/sh # filename: argumente f95 $3 a.out echo "$1 Jahre w${2}der" echo "ende $0" Aufruf etwa: argumente Alle ie demo.f90
6
Shell und UNIX
Metazeichen f¨ ur Dateinamen Zeichen * ? [s] [!s] [c1-cn]
Bedeutung Wird zu einem oder mehreren beliebigen Zeichen (auch leer) expandiert. Ausnahme: . am Anfang, sog. versteckte Datei Wird zu 1 beliebigen Zeichen expandiert. Wird mit einem Zeichen aus der Zeichenkette s ersetzt. ls t[mu]p listet tmp und tup Wird mit einem Zeichen aus der Zeichenkette ersetzt, das nicht in s enthalten ist. wird durch genau ein Zeichen aus c1-cn ersetzt, z.B. [a-z]
Weitere Metazeichen Zeichen $ ‘kommando‘
Bedeutung Beginn einer Variablen, die substituiert werden soll, z.B. echo $HOME oder echo $1 oder echo $PATH Wird durch die Ausgabe (stdout), die kommando erzeugt ersetzt. Bsp. ‘date +%j‘, auch ‘a.out‘
7
Shell und UNIX
Sonder-Bedeutung von Metazeichen aufheben: Quoten Quote-Zeichen \ ’ ’ " "
Bedeutung Zeichen rechts davon keine SonderBedeutung Text innerhalb einfacher Anf¨ uhrungszeichen wird nicht ver¨ andert. $ bleibt Sonderzeichen. Alle anderen verlieren innerhalb der Anf¨ uhrungszeichen ihre Bedeutung
Umlenkung von input output und error Umlenkung von stdin stdout stderr stdout and stderr getrennte Umlenkung stdout und stderr stdout and file anh¨ angen stdout und stderr an file anh¨ angen
sh ksh bash < file > file 2> file >file 2>&1 >file 2>file1
csh tcsh < file > file nicht m¨ oglich >& file nicht m¨ oglich
>> file
>> file
>>file 2>&1
>>& file
8
Shell und UNIX
Konzept der Pipes Kommando1
stdout
|
stdin
Kommando2
stdout
stderr
stderr
Beispiele: ls -lR | grep tm cat demo.f90 | tr "[A-Z]" "[a-z]"
Gruppierung von Kommandos Symbol ; k1 && k2 k1 || k2 (kommando)
Bedeutung trennt Kommandos k2 wird ausgef¨ uhrt wenn k1 den R¨ uckgabewert 0 liefert (=Erfolg) k2 wird ausgef¨ uhrt wenn k1 einen R¨ uckgabewert ungleich 0 liefert kommando wird in einer Sub-Shell ausgef¨ uhrt
Beispiele: ls; cd ..; ls cd ls && echo "ok" ls /00 && echo "’ok" pwd; (cd /tmp; pwd): pwd sh exit
9
Shell und UNIX
Shell-Variablen Variable ? $ * @ # HOME PWD PATH PS1
Bedeutung R¨ uckgabewert des letzten Kommandos Prozess-Nummer der Shell Alle Argumente die an die Shell u ¨bergeben wurden als einzige Zeichenkette Alle Argumente die an die Shell u ¨bergeben wurden als einzelne Zeichenkette Enth¨ alt die Anzahl der Argumente die an die Shell u ¨bergeben wurden als einzelne Zeichenkette Pfad zum Home Verzeichnis de Benutzers Enth¨ alt das aktuelle Arbeitsverzeichnis Suchpfad in dem nach Programmen gesucht wird Steuerung der Eingabeaufforderung
Environment Variablen wie Shell-Variablen m¨ ussen exportiert werden um in Sub-Shells verf¨ ugbar zu sein (sonst sind sie nicht verf¨ ugbar). # Lizenzinformation f95 LM_LICENSE_FILE=/usr/local/lib/NAGWare/NAG.demo; export LM_LICENSE_FILE setenv LM_LICENSE_FILE /usr/local/lib/NAGWare/NAG.demo # csh
10
Shell und UNIX
Kontrollstrukturen (nur in Beispielen) Bedingte Anweisung in sh (neue Zeilen k¨ onnen durch ; ersetzt werden) if test then echo exit else echo fi ..
$# -ne 1 "$0 falsche Anzahl von Parametern -- Abbruch" 1 # Rueckgabewert "Uebergeben wurde $1"
alternativ f¨ ur test k¨ onnen eckige Klammern mit Leerzeichen (!) geschrieben werden if [ $# -ne 1 ]; then echo "$0 falsche Anzahl von Parametern -- Abbruch" exit 1 # Rueckgabewert fi .. Die while Schleife while [ -n "$1" ]; do # prueft ob "string" leer ist echo "Argument: $1" shift # jetzt ist $1 das zweite Argument done
11
Shell und UNIX Die for Schleife for i in wolfi heinz roger 2 3; do echo $i done Die case-Anweisung echo "Ihre Anwort bitte: " read ANSWER # belegt variable ANSWER case $ANSWER in [jJ]a|[yY]es) echo "Antwort war ja/Ja oder yes/Yes" exit 0 ;; [nN]ein|[nN]o) echo "Antwort war nein/Nein/ oder no/No" exit 1 ;; *) echo "unpassende Antwort" ;; esac
12
Shell und UNIX
Operatoren des Kommandos test Operator n1 -eq n2 n1 -lt n2 n1 -le n2 n1 -gt n2 n1 -ge n2 s1 = s2 s1 != s2 -z s1 -n s1 -e datei -f datei -r datei -w datei -x datei -s datei a1 -a a2 a1 -o a2 !a ( a )
Bedeutung wahr wenn Zahl n1 gleich Zahl n2 wahr wenn Zahl n1 kleiner als Zahl n2 wahr wenn Zahl n1 kleiner gleich Zahl n2 wahr wenn Zahl n1 gr¨ oßer als Zahl n2 wahr wenn Zahl n1 gr¨ oßer gleich Zahl n2 wahr wenn Zeichenkette s1 gleich Zeichenkette s2 wahr wenn Zeichenkette s1 nicht gleich Zeichenkette s2 wahr wenn Zeichenkette s1 null Zeichen ist wahr wenn Zeichenkette s1 gr¨ oßer null Zeichen ist wahr wenn datei existiert wahr wenn datei eine normale datei ist (kein Verzeichnis, kein Link) wahr wenn datei lesbar ist (Permission) wahr wenn datei beschreibbar ist (Permission) wahr wenn datei ausf¨ uhrbar ist (Permission) wahr wenn datei l¨ anger als Null Zeichen ist AND Verkn¨ upfung zweier TestAusdrucke a1 a2 OR Verkn¨ upfung zweier Test-Ausdr¨ ucke a1 a2 Negation, wahr wenn nicht a wahr ist Klammerung zur Kl¨ arung von Priorit¨ aten, wie ( a1 -o a2 ) -a a3 im Gegensatz zu a1 -o ( a2 -a a3 ) 13
Shell und UNIX
Shell (Skript) Optionen Option set -e set -f set -n set -v set -x
Wirkung Shell wird bei Fehler beendet Dateinamenersetzung aus Testlauf - keine Ausf¨ uhrung verbose zur Fehlersuche jedes Kommando wird interpretiert ausgegeben
Nicht in der sh verf¨ ugbar sind: set -o emacs set -o vi
Kommandozeile im emacs-Mode editieren Kommandozeile im vi-Mode editieren
14
Shell Beispiele wolfi@ramses2:~/Shells> cat .bash_profile # # all created data are private # umask 077 # remove permissions 0 4+2+1 4+2+1 4=-r 2=-w 1=-x # # on ramses only the old version ifc is available # case ‘uname -n‘ in ramses) alias ifort=ifc ;; esac wolfi@ramses2:~/Shells> cat findshort #!/bin/sh max=1000 for name in * do size=‘wc -c < $name‘ # count words if [ $size -le $max ] then echo "$name is a short file: $size characters" fi done wolfi@ramses2:~/Shells> cat forloop #!/bin/sh ingred=’apple berry cake dough’ for word in $ingred do echo $word done
15
Shell Beispiele wolfi@ramses2:~/Shells> cat forset #!/bin/sh set ‘ls‘ for file do echo $file done echo "This is equivalent.." for file in * do echo $file done wolfi@ramses2:~/Shells> cat function # # make available with . function # rename () { echo "Current name: " read file echo "new name: " read new mv $file $new echo "file $file has been renamed $new " } wolfi@ramses2:~/Shells> cat untilloop #!/bin/sh i=1 until i=‘expr $i - 1‘ do echo Counter is $i done wolfi@ramses2:~/Shells> cat whileloop #!/bin/sh i=5 while i=‘expr $i - 1‘ # Loop while true do echo Counter is $i done 16
GMT
Generic Mapping Tools Graphics http://gmt.soest.hawaii.edu • Paul Wessel and Walter H.F. Smith • follows UNIX command line philosophy not Windows • why so popular - the price is right (free) • output is postscript Die -B Option (Eier legende Wollmilchsau)
grid
10
20 anotation
30 frame
17
GMT Postscript im Baukasten Prinzip
−O ommits header
HEADER BODY−1
−O −K BODY−n TRAILER
−K ommits trailer
Das Koordinatensystem y default is Landscape xoff=1 inch yoff=1 inch
−X −Y yoff
x xoff
18
GMT
Beispiel: pscontour Struktur des input-files: Chan and Williams vortex -250.00 -250.00 0.00 -245.00 -250.00 0.00 ... ... ... -50.00 0.00 47.18 -45.00 0.00 49.25 -40.00 0.00 50.00 ... ... ... MODULE VORTEXPROFILE CONTAINS ELEMENTAL SUBROUTINE ChanWilliamsVortex(v,r,vm,rm,b) ! elemental: subroutine may be called with conformal arrays ! restricted to "pure" (=simple) subroutines without ! if structures inside. Interface must be explicit, ! e.g. module procedure. REAL, REAL, REAL, REAL, REAL,
INTENT(out) INTENT(in) INTENT(in) INTENT(in) INTENT(in)
:: :: :: :: ::
v r vm rm b
! ! ! ! !
azimuthal wind radius max azimuthal wind radius of max wind shape parameter use 1 or 2
REAL :: s; s=r/rm v=vm*s*EXP((1.0-s**b)/b) END SUBROUTINE ChanWilliamsVortex END MODULE VORTEXPROFILE 19
GMT PROGRAM VortexDemo ! 3.12.2004 USE VORTEXPROFILE INTEGER, PARAMETER :: NX=50, NY=50, iu=7 REAL, PARAMETER :: dx=5.E3, dy=dx, rm=40.E3, vm=50. REAL, DIMENSION(-NX:NX,-NY:NY) :: wind, r REAL, DIMENSION(-NX:NX) :: x REAL, DIMENSION(-NY:NY) :: y x=(/(i*dx,i=-NX,NX)/) y=(/(i*dy,i=-NY,NY)/) xc=0.; yc=0.
! centre of vortex
DO j=-NY,NY DO i=-NX,NX r(i,j)=SQRT((x(i)-xc)**2+(y(j)-yc)**2) ENDDO ENDDO b=2. CALL ChanWilliamsVortex(wind,r,vm,rm,b) CALL togmt(’CWvortex.xyz’) CONTAINS SUBROUTINE togmt(filename) CHARACTER(LEN=*) :: filename OPEN(UNIT=iu,FILE=filename) WRITE(UNIT=iu,FMT="(A)") "Chan and Williams vortex" DO j=-NY,NY DO i=-NX,NX WRITE(UNIT=iu,FMT="(2F10.2,F10.2)") & x(i)/1000.,y(j)/1000.,wind(i,j) ! x y z ENDDO ENDDO CLOSE(UNIT=iu) END SUBROUTINE togmt END PROGRAM VortexDemo 20
GMT
Demo-script: gmtscript.bash #!/bin/bash ####################################################################### # bash script demo to plot xyz files # W.Ulrich 23.11.2004 ####################################################################### # # settings # set -e # stop on any error gmtset PAPER_MEDIA A4 # A4 paper PSOUT=wind.ps # postscript output XYZ=CWvortex.xyz # data file with structure x y z if test -e $PSOUT; then # if $PSOUT alreay exists delete it rm $PSOUT fi head -1 $XYZ # display first line of $XYZ minmax -H $XYZ # GMT: display minimum and maximum of data LO=0; HI=60; INC=5 # contour settings XWEST=-250; XEAST=250 # X-axis YSOUTH=-250; YNORTH=250 # Y-axis TITLE=’WIND SPEED’ XTITLE=’WEST-EAST (km)’ YTITLE=’SOUTH-NORTH (km)’ XWIDTH=16c # size of diagram in cm YWIDTH=16c # use an even number, see below XTICKS=a50f10g50 # aanotate fticks ggrid YTICKS=a50f10g50 COLOURPALETTE=rainbow # this is a recommended one LABELS=1 # # write a colour table (rainbow sequence) to file palette.cpt # makecpt -C${COLOURPALETTE} -T${LO}/${HI}/${INC} > palette.cpt # # colour shading # 21
GMT pscontour $XYZ -H \ -I -Cpalette.cpt \ -JX${XWIDTH}/${YWIDTH} \ -R${XWEST}/${XEAST}/${YSOUTH}/${YNORTH} \ -B${XTICKS}:"${XTITLE}":/${YTICKS}:"${YTITLE}"::."${TITLE}":WeSn \ -X4c -K > $PSOUT # # contour labels if LABELS=1 # if test $LABELS -eq 1; then pscontour $XYZ -H \ -W -Cpalette.cpt \ -JX \ -R \ -O -K >> $PSOUT fi # # colour bar finishes figure # XPOS=‘expr ${XWIDTH%c} + 2‘c YPOS=‘expr ${YWIDTH%c} / 2‘c echo "XPOS=$XPOS YPOS=$YPOS" LENGTH=$YWIDTH if test ‘expr 2 \* ${YPOS%c}‘ -ne ${YWIDTH%c} then LENGTH=‘expr ${YWIDTH%c} - 1‘c fi WIDTH=1c UNITS=’ms@+-1@+’ psscale -Cpalette.cpt \ -D${XPOS}/${YPOS}/${LENGTH}/${WIDTH} \ -Ba${INC}::/:${UNITS}: \ -O -U >> $PSOUT # # display $PSOUT with postscript viewer gv # gv -landscape $PSOUT 22
GMT
GMT - gmtscript.bash
WIND SPEED ms-1
250
60 55
200
5
50 45
15
20
100
40
5
50
40
10
35
10
35
15 35
25
20
45
-50
30
25
40
20
30
35 45
40
30 25
0 5
15
SOUTH-NORTH (km)
150
20
-100 15 -150
10
5
-200 -250 -250
10 5 0
-200
2004 Dec 4 11:19:25
-150
-100
-50
0
50
100
150
200
250
WEST-EAST (km)
23
GMT
Leicht zu erstellen: x-y-plot # # axes ticks and label texts # psbasemap -JX${XWIDTH}/${YWIDTH} \ -R${XLEFT}/${XRIGHT}/${YBOT}/${YTOP} \ -B${XTICKS}:"${XTITLE}":/${YTICKS}:"${YTITLE}"::."${TITLE}":WeSn -K > $PSOUT # # use e.g. default thick thicker fat fatter # psxy $XY -H -JX -R -B -Wthicker,red -O >> $PSOUT
50
V (m/s)
40
30
20
10
0 0
50
100
150
200
250
300
RADIUS (km)
24
Gnuplot
Gnuplot http://www.gnuplot.info Gnuplot is a portable command-line driven interactive datafile (text or binary) and function plotting utility for UNIX, IBM OS/2, MS Windows, DOS, Apple Macintosh, VMS, Atari and many other platforms. The software is copyrighted but freely distributed (i.e., you don’t have to pay for it). It was originally intended as graphical program which would allow scientists and students to visualize mathematical functions and data. It does this job pretty well, and in addition it serves as non-interactive plotting engine for miscellaneous portable third-party applications, like Octave. Gnuplot is developed and supported since 1986, and having its scripts and commands easy to understand text files, it is time-portable as well. Demonstration mit dem input-file: p_th_TK.dat #
p (hPa) 100.0 200.0 300.0 400.0 500.0 600.0 700.0 800.0 900.0 1000.0
theta (K) 353.4 339.2 330.1 323.3 317.9 313.3 309.4 305.9 302.8 300.0
TK(k) 183.2 214.2 234.1 248.9 260.8 270.8 279.4 287.0 293.8 300.0
Das # wird als Kommentarzeile interpretiert.
25
Gnuplot
Gnuplot besitzt eine einfache (Script-)Sprache > cat theta_p.gpi # Ausgabe # # gnuplot input # set terminal postscript eps enh color solid "Times-Roman" 24 set output "th_p.eps" set size ratio 0.6 set title "constant N^{2} profile" set xlabel "{/Symbol q} in K" set ylabel "p in hPa" set autoscale x set yrange [1000:100] set key right bottom plot "p_th_TK.dat" using 2:1 title "[{/Symbol q}(p)]_{N^{2}=const}" \ w l lt 1 lw 5 pause 1 !gv th_p.eps &
constant N2 profile 100 200 300 p in hPa
400 500 600 700 800 900 1000 300
[θ(p)]N2=const 310
320
330 θ in K
340
350
360
Ergebnis von load "theta_p.gpi" 26
Gnuplot
Qualit¨ at h¨ angt stark vom terminal ab > cat theta_p.pnggpi # # gnuplot input # set terminal png color medium set output "th_p.png" set size ratio 0.6 set title "constant N^{2} profile" set xlabel "{/Symbol q} in K" set ylabel "p in hPa" set autoscale x set yrange [1000:100] set key right bottom plot "p_th_TK.dat" using 2:1 title "{/Symbol q}" w l lt 1 lw 5 pause 1 !xv th_p.png &
Ergebnis von load "theta_p.pnggpi" 27
Gnuplot
St¨ arke: Automatische Wahl der Achsenbeschriftung > cat theta_TK_p.gpi # # gnuplot input # set terminal postscript eps enh color solid "Times-Roman" 24 set output "th_TK_p.eps" set size ratio 0.6 set title "constant N^{2} profiles" set xlabel "{/Symbol q} and T_{K} in K" set ylabel "p in hPa" set autoscale x set yrange [1000:100] set key top plot "p_th_TK.dat" using 2:1 title "[{/Symbol q}(p)]_{N^{2}=const}" \ w l lt 1 lw5, \ "" using 3:1 title "[T_{K}(p)]_{N^{2}=const}" w lp lt 3 lw 1 pause 1 !gv th_TK_p.eps &
constant N2 profiles 100
[θ(p)]N2=const [TK(p)]N2=const
200 300 p in hPa
400 500 600 700 800 900 1000 180
200
220
240
260 280 300 θ and TK in K
320
340
360
Ergebnis von load "theta_TK_p.gpi" 28
GrADS
GrADS http://grads.iges.org/grads/grads.html The Grid Analysis and Display System (GrADS) is an interactive desktop tool that is used for easy access, manipulation, and visualization of earth science data. The format of the data may be either binary, GRIB, NetCDF, or HDF-SDS (Scientific Data Sets). GrADS has been implemented worldwide on a variety of commonly used operating systems and is freely distributed over the Internet. • GrADS uses a 4-Dimensional data environment: longitude, latitude, vertical level, and time t(=4.DIM)
t=3
z(=3.DIM)
t=1
t=2 y(=2.DIM)
x(=1.DIM)
• Data sets are placed within the 4-D space by use of a data descriptor file • Data from different data sets may be graphically overlaid, with correct spatial and time registration • Operations are executed interactively by entering FORTRAN-like expressions at the command line • GrADS has a programmable interface (scripting language) that allows for sophisticated analysis and display applications 29
GrADS Example Descriptor file example.ctl DSET ^gridded_data_sample.dat TITLE Gridded Data Sample UNDEF -9.99E33 XDEF 180 LINEAR 0.0 2.0 YDEF 90 LINEAR -90 2.0 ZDEF 10 LEVELS 1000 850 700 500 400 300 250 200 150 100 TDEF 4 LINEAR 0Z10apr1991 12hr VARS 4 slp 0 99 sea level pressure hgt 10 99 heights temp 10 99 temperature shum 6 99 specific humidity ENDVARS Eine kleine function auf dem file myd.gs im selben Verzeichnis kann mit dem Aufruf myd ps den Bodendruck mit Farbe und u ¨berlagerten Isolinien darstellen. function myd(var) ******************************************* * merkwuerdig, die 1. function auf dem file * darf nur 1 Argument haben ******************************************* ’set gxout shaded’ ’d ’var cbarn ’set gxout contour’ ’d ’var return Hinweis: Soll die function so einfach bleiben ist ein exec vorzuziehen, das keine ’ ’ braucht. set gxout shaded d &0 cbarn set gxout contour d &0 30
GrADS
Erg¨ anzungen zum GrADS Tutorial Eine einfache M¨ oglichkeit der Animation function stepit(var) * animate time evolution of var rc = gsfallow("on") t=1 ’set t ’t _tmax=gettmax() while(t 0 x < 0.
F (x) =
n
c1 = c(ρ1 ), c2 = c(ρ2 ),
x>0 x c1 tritt sofort Brechen auf. F¨ ur c2 < c1 gilt aber f¨ ur die Kurven-Familie (8): c = F, x = F t ⇒ c =
x , c2 < F < c 1 t
(16)
Die L¨ osung ist dann c=
(
c1 , x , t c2 ,
c1 < xt , c2 < xt < c1 , x < c2 . t
(17)
Der Fall c2 < c1 ist im Gegensatz zu c2 > c1 eine physikalische L¨ osung.
37
Advektion Kompression c2 > c1
t
x=c1 t x=c2 t
c=c2
c=c1
x
ρ ρ2 ρ1 x t
c=c2
Expansion c2 < c1
c=c1
x
ρ ρ1 ρ2 x 38
Advektion
Flussform Sei ρ(x, t) eine Dichte pro L¨ angeneinheit und q(x, t) ein Fluss pro Zeiteinheit. Dann ist eine Geschwindigkeit v(x, t) v=
q . ρ
(18)
Materielle Erhaltung D Dt
Z
x1 x2
ρ(x, t)dx + q(x1 , t) − q(x2 , t) = 0.
(19)
Mit x1 → x2 erh¨ alt man die Erhaltungsgleichung ∂ρ ∂q + =0 ∂t ∂x
(20)
Sei q = Q(ρ) ⇒ ρt + c(ρ)ρx = 0 mit c(ρ) = Q0 (ρ). Beispiele: • ρ(x, t): Autos pro L¨ angeneinheit q(x, t): Autos die x im Zeitintervall kreuzen. • ρ(x, t): Kanal-Querschnitt q(x, t): Volumenfluss durch Querschnitt q = Q(A). • ρ(x, t): Dicke von Gletscher-Eis q(x, t): Vorbeifluss bei x.
39
Advektion
Schock (Brechen) Es gelte q = Q(ρ) und die Erhaltung (19). Sei ein Sprung (Schock) da: x2 < s(t) < x1 (Konvention in dieser Literatur). Aus (19) folgt D q(x2 , t) − q(x1 , t) = Dt
Z
s(t)
x2
= ρ(s− , t)s˙ − ρ(s+ , t)s˙ +
Z
D ρ(x, t)dx + Dt
s(t)
ρt (x, t)dx +
Z
Z
x1
ρ(x, t)dx
s(t) x1
ρt (x, t)dx
(21)
s(t)
x2
Mit s˙ = Ds . F¨ ur x1 → s+ und x2 → s− verschwinden die beiden Dt letzten Integrale weil ρt begrenzt. Somit q(s− , t) − q(s+ , t) = s˙ ρ(s− , t) − ρ(s+ , t)
|
{z
}
q2 −q1
{z
|
U (ρ2 −ρ1 )
oder mit[..] als Symbol f¨ ur den Sprung
}
(22)
−U [ρ] + [q] = 0
| {z } → ∂ρ ∂t
(23)
|{z} ∂q → ∂x
Wo q = Q(ρ) erf¨ ullt ist gilt also U =
Q(ρ2 ) − Q(ρ1 ) q2 − q 1 = ρ2 − ρ 1 ρ2 − ρ 1
(24)
Wir machen den Fall c2 > c1 physikalisch: ρ=
n
ρ1 , ρ2 ,
x > Ut x < Ut
(25)
In Schock-relativen Koordinaten: q1 − U ρ 1 = q2 − U ρ 2
(26)
40
Advektion
moving
relative
U
ρ=ρ 2
ρ=ρ 1
q=q2
q=q1
ρ=ρ 2 q=q2 −Uρ2
ρ=ρ 1 q=q1 −Uρ1
Schock-Struktur (Physikalischer) Ansatz: q = Q(ρ) − νρx ,
ν = const.
(27)
c(ρ) = Q0 (ρ)
(28)
eingesetzt in (20): ρt + c(ρ)ρx = νρxx , Anmerkung: Die L¨ osung von ρt = νρxx
(29)
ist ρ = ρ2 +
ρ1 − ρ 2 √ π
Z
√ x/ 4νt
exp(−ζ 2)dζ.
(30)
U = const.
(31)
−∞
L¨ osungsansatz f¨ ur (28): ρ = ρ(X),
X = x − U t,
Eingesetzt (c (ρ) − U ) ρX = νρXX .
(32)
41
Advektion Integriert nach ρ: Q(ρ) − U ρ + A = νρX
(33)
Gesucht: ρ → ρ1 f¨ ur X → ∞, ρ → ρ2 f¨ ur X → −∞. Sei auch ρX → 0 f¨ ur X → ±∞. Q(ρ1 ) − U ρ1 + A = Q(ρ2 ) − U ρ2 + A = 0 Q(ρ2 ) − Q(ρ1 ) ⇒U = ρ2 − ρ 1
(34) (35)
Dies ist gerade die (richtige) Schock-Bedingung! Man kann mit dρ = ρX dX (33) umformen zu X = ν
Z
dρ Q(ρ) − U ρ + A
(36)
F¨ ur bestimmte Q(ρ) z.B. Q(ρ) = αρ2 + βρ + γ ist dies integrierbar. U und A folgen aus (34). Daf¨ ur X=
ρ2 − ρ ν log α(ρ2 − ρ1 ) ρ − ρ1
(37)
¨ bergangszone ist Ein Maß f¨ ur die Breite der U ν α(ρ2 − ρ1 )
(38)
ρ ρ=ρ 2 ρ=ρ 1
}
x
∼ν
42
Advektion
Numerische L¨ osung der Advektionsgleichung Die Erhaltungsform ∂ρ ∂F (ρ) + = 0, ∂t ∂x
F (ρ) = vρ
• umgesetzt in ein (versetztes) Gitter = ρti − ρt+∆t i
i−1
t t Fi+ 1 − F i− 1
i−1/2
2
2
∆x
i
!
i+1/2
(39)
∆t.
(40)
i+1
• umgesetzt in zentrierte finite Differenzen t Fi+ 1 = 2
t Fi+1 + Fit
2
⇒ ρt+∆t = ρti − i
t t Fi+1 − Fi−1
2∆x
∆t. (41)
Diese Form ist numerisch instabil! (Beweis mit Neumannscher Stabilit¨ atsanalyse) Mathematisches Musterbeispiel: Lax-Wendroff 1. Ausgangspunkt: Taylor-Entwicklung ∂ρ ∂ 2 ρ ∆t2 ρ(x, t + ∆t) = ρ(x, t) + ∆t + 2 + O(∆t3 ) ∂t ∂t 2
(42)
2. Ersetze Zeitableitung durch Ortsableitung. Ort und Zeit sind also nicht unabh¨ angig! ∂F ∂ ρ(x, t + ∆t) = ρ(x, t) − ∆t + ∂x ∂x
∂F ∂F ∂ρ ∂ρ ∂ρ ∂x
∆t2 + O(∆t3 )(43) 2 43
Advektion • mit zentrierten Differenzen erh¨ alt man ρt+∆t = ρtn − n
t t Fi+1 − Fi−1
2∆x
v∆t2 ∆t + 2
t t Fi+1 − 2Fit + Fi−1
∆x2
• oder in Erhaltungsform: t Fi+ 1 2
1 t t t t v∆t = F + Fi − Fi+1 − Fi 2 i+1 2∆x
(45)
• F¨ ur diese Formen gilt das Courant-Friedrichs-Lewy (CFL) Stabilit¨ atskriterium v∆t/∆x ≤ 1. Upwind Methoden: Der Natur nachempfunden 1. Ordnung: t Fi+ 1 2
=
n
Fit t Fi+1
v>0 v 0: u = 2c0 folgt aus u + 2c = 2c0 mit c = 0 (wegen h = 0). Integriert: x = 2c0 t. • Ruhezone x < 0: x = −c0 t
Dx Dt
= −c0 folgt aus u = 0. Integriert:
51
Dammbruch Der Bereich −c0t < x < 2c0 t heisst centered simple wave. Auf einer negativen Charakteristik gilt dann: Dx = u − c = x/t. Aus Dt u + 2c = 2c0 und u − c = x/t folgt die L¨ osung f¨ ur c und u: 1 (2c0 − 3 2 u = (c0 + 3
c=
x ) t x ) t
(61) (62)
Die L¨ osung f¨ ur h ist dann:
c=
p
gh =
c0 = 1 3
0,
p
gh0 ,
2c0 −
x t
,
−∞ < x ≤ −t
p
gh0 p p −t gh0 ≤ x ≤ 2t gh0 p
2t
(63)
gh0 < x < ∞
Bemerkung: Bei x = 0 gilt h = 49 h0 und u = c.
dam break h0=1.00 g=1.00 1.2
t=0 t=1 t=2 t=3 t=4
1
h
0.8 0.6 0.4 0.2 0 -10
-5
0 x
5
10
52
Dammbruch
Erhaltungs-Gleichungen f¨ ur Flachwasser ∂ ∂ P (u, h) + Q(u, h) = 0 ∂t ∂x
(64)
Man kann immer neue Erhaltungsformen generieren falls: Qu = uPu + hPh ,
Qh = gPu + uPh
(65)
Die ersten sind: Erhaltung Impuls/Einheitsmasse Masse Impuls Energie Namenlos
P u h uh 1 2 u + 21 gh2 2 1 3 u h + ugh2 3
Q + gh uh 2 u h + 21 gh2 1 3 u h + ugh2 2 1 4 u h + 32 u2gh2 + 13 g 2 h3 3 1 2 u 2
Bei einem Schock bleibt die Energie nicht erhalten! Die Schock-Bedingungen f¨ ur Masse und Impuls sind:
h
i
1 −U [uh] + u h + gh2 = 0 2 −U [h] + [uh] = 0 2
(66) (67)
Hier z.B. tritt sofort Shock auf auf mit U = const. Damm
h1 h0 x
Es ist u0 = 0, somit wird z.B aus (67) −U (h0 − h2 ) + u0h0 = 0
(68) 53
Dammbruch
Dammbruch mit Schock Es gibt mehr Unbekannte als Gleichungen, man kann berechnen: u2 U c0 = − c0 c0 4U
1+
r
1+8
2 U c0
!
c2 c0
,
t
v u u1 =t 2
x=(u2−c2)t
x=−c1t
r
1+8
2 U c0
x=Ut t=t0
(1)
(3)
x
(0)
(2)
h1 h2
h0 x
Aus u + 2c = const folgt in Gebiet (3) wegen u1 = 0: u + 2c = 2c1,
u + 2c = u2 + 2c2 ⇒ u2 + 2c2 = 2c1
(70)
Die L¨ osung von (70) f¨ ur U als Funktion von (h1 /h0 ) muß grafisch oder iterativ erfolgen. In Gebiet (3) gilt wieder Dx 3 = u − c = 2c1 − 3c = u − c1 Dt 2
(71)
und die L¨ osung in (3) ist 1 c = 9 2
x 2c1 − t
2
2 u= 3
x c1 + t
(72) 54
!
(69)
Dammbruch dam break with shock 2.5
u2/c0 c2/c0 h0/h1 u/c0
2
u2/c0=1.79
1.5 1 U/c0=2.60 0.5 h0/h1=0.1384
0 1
1.5
2
2.5
3
3.5
U/c0
• L¨ osung (72) ist in Gebiet (3) eine Parabel. Sie hat keine horizontale Tangente zu Gebiet (1) und (2)! • F¨ ur u2 > c2 liegt x = 0 in Gebiet (3). Es gilt h = 49 h1 und u = 23 c1 und somit der Durchfluss uh unabh¨ angig von t und von h0 . Es gilt auch c = u, somit kann keine Information stromauf wandern. • u2 = c2 ⇒ h0 /h1 = 0.1384. • F¨ ur h0 /h1 < 0.1384 liegt x = 0 im Gebiet (2). Auch hier ist der Durchfluss unabh¨ angig von t weil u2 = const, aber abh¨ angig von h0 .
55
Dammbruch
Durchfluss (dQ/dt)/(h1 c1) 0.35 0.3
dQ/dt=0.29 h1 c1
0.25 0.2 0.15 h0/h1=0.138
0.1 0.05 0 0
0.2
0.4
0.6
0.8
1
h0/h1
Schockfront (h2-h0)/h1 0.35 h2-h0=0.32 h1
0.3 0.25 0.2 0.15 h0/h1=0.176
0.1 0.05 0 0
0.2
0.4
0.6
0.8
1
h0/h1
56
Dammbruch
Die Grenzf¨ alle f¨ ur h0 /h1 t
t x=+c1t
x=Ut
x=−c1t
x=Ut
x=2c 1t
x=−c1t
t=t0
(1)
(3)
t=t0
x
(2)
(1)
(0)
h1
(3)
h1
(2)
h2
h0
(0)
x
h0
x
x
Zusammenfssung der L¨ osung
h(x, t)
u(x, t)
=
=
h1 − x/t)2 h2 0
1 (2c1 9g
0 + x/t) u2 0
2 (c 3 1
=
gh0 U− 4U
c2
=
(
h2
=
c22/g,
u2
u2
=
gh0 2
"
"
1+
1+
x < −c1t, −c1 t < x < (u2 − c2 )t, (73) (u2 − c2 )t < x < U t, U t < x, x < −c1t, −c1 t < x < (u2 − c2 )t, (74) (u2 − c2 )t < x < U t, U t < x,
8U 2 1+ gh0
8U 2 gh0
21
21 #
−1
#) 21
,
(75)
,
(76) (77)
1 2
2(gh1) − 2c2.
(78)
57
Dammbruch
Bewegter Damm mit U0 Es gelte 0 < U0 < 2c1 .
c1 3/ U −c 2 0 1
h1
U0 x
F¨ ur x + c1 t > 0 haben wir die Invariante u + 2c = 2c1. F¨ ur den bewegten Damm gilt x = U0 t . Aus der Invariante folgt: U0 + 2c = 2c1 ⇒ c = c1 − 12 U0 . Die Verbindungsbedingung zwischen Front und Parabel ergibt sich aus der negativen Charakteristik: u = U0 , c = c 1 −
Die L¨ osung ist somit
1
c = (gh) 2
u
=
=
1 3
2 3
1 Dx 3 U0 ⇒ = u − c = U0 − c 1 , 2 Dt 2 3 ⇒x= U0 − c 1 t 2 c1 (2c1 − x/t) c1 − 21 U0 0 0 (c1 − x/t) U0 0
(79) (80)
x < −c1 t, 3 −c1 t < x < 2 U0 − c1 t, (81) 3 U − c t < x < U t, 0 1 0 2 x > U0 t,
x < −c1t, −c1 t < x < 23 U0 − c1 t, (82) 3 U − c 1 t < x < U0 t, 2 0 x > U0 t 58
Dammbruch
Vergleiche mit Wasserkanal Dammbruch-L¨ osung unrealistisch. Realistischer Ansatz: 1 ∗ uf = k(g hf ) 2 , g ∗ = g∆ρ/ρ, k ≈ 1. Damit 2kc1 2+k 2 2 hf = h 1 2+k
(83)
uf =
(84)
Unser Wasserkanal: h1 =15 cm, beobachtete Dicke der Dichtestr¨ omung hf ≈ 2.2 cm. Bemerkung: Passt nicht-Erkl¨ arungsbedarf! Zu den numerischen Modellen gibt es eine Beilage!
59