Física Computacional Conceptos de programación. F. A. Velázquez-Muñoz Departamento de Física CUCEI UdeG 6 de febrero de 2015
Sistemas Operativos ms-dos windows linux suse; redhat; ubuntu; etc. unix Mac OSx
usuario aplicación sistema operativo hardware
Lenguajes de Programacón: Matlab Fortran C++ Pascal Basic Java etc.
MatLab (MATrix LABoratory) The Language of Technical Computing High-level language for numerical computation, visualization, and application development Interactive environment for iterative exploration, design, and problem solving. Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, numerical integration, and solving ordinary differential equations. Built-in graphics for visualizing data and tools for creating custom plots Development tools for improving code quality and maintainability and maximizing performance Tools for building applications with custom graphical interfaces Functions for integrating MATLAB based algorithms with external applications and languages such as C, Java,.NET, and Microsoft R Excel R
Fortran (Formula Translating System). is a general-purpose, imperative programming language that is especially suited to numeric computation and scientific computing
Declaración y uso de Variables
Tipos de Variables INTEGER: entero REAL: real COMPLEX: complejo LOGICAL: logica (verdadero o falso) CHARACTER: caracter
En un programa de fortran, la forma de escribir la declaración de variables es: program v a r s i n t e g e r : : i r e a l : : a complex : : img l o g i c a l : : i o c h a r a c t e r (10) : : fecha i =1 a =2.33 img=cmplx ( 1, 2 ) i o = t r u e fecha = 2015/01/01 w r i t e ( 6, ) w r i t e ( 6, ) w r i t e ( 6, ) w r i t e ( 6, ) w r i t e ( 6, ) i a img i o f e c h a end program v a r s
El resultado que muestra el programa en pantalla es: compu$./a.out 1 2.330000 (1.000000,2.000000) F 2015/01/01
c l e a r a l l i =1 a =2.33 img=1+2 i i o=f a l s e f e c h a = 2015/01/01
i = a = 1 img = 3 i o = 2.3300 0 f e c h a = 2015/01/01
program prog r e a l : : a, b, c a =1.2 b=3.4 c=a+b w r i t e ( 6, ) c end program prog
program prog r e a l : : a, b, c r e a d (, ) r e a d (, ) a b c=a+b w r i t e ( 6, ) c end program prog
program prog r e a l : : a, b, c open ( u n i t =100, f i l e = d a t o s. dat ) r e a d (100, ) a r e a d (100, ) b c l o s e ( 1 0 0 ) c=a+b w r i t e ( 6, ) c end program prog
program prog r e a l : : a, b, c open ( u n i t =100, f i l e = d a t o s. dat ) r e a d ( 1 0 0, 1 1 0 ) a r e a d ( 1 0 0, 1 1 0 ) b c l o s e ( 1 0 0 ) 110 format ( F7. 2 ) c=a+b w r i t e ( 6, ) a w r i t e ( 6, ) b w r i t e ( 6, ) c open ( u n i t =200, f i l e = d a t o s. out ) w r i t e (200, ) c c l o s e ( 2 0 0 ) end program prog
Formato para datos de entrada y salida en fortran sintaxis: write/read (UNIT,FORMAT) variables (UNIT LABEL,FORMAT LABEL) variables label format (format-code) ejemplo: write(*,900) i,x 900 format(i4,f8.3) write/read
A: text string D: double precision numbers, exponent notation E: real numbers, exponent notation F: real numbers, fixed point format I: integer X: horizontal skip (space) /: vertical skip (new line) n FC w.d
Los indices se usan para identificar a un elemento de un arreglo de variables. por ejemplo: Se se define a(1)=10; a(2)=20; a(3)=30 el arreglo es de dimensión 1 x 3. Si queremos multiplicar el arreglo de numeros a por 2, tenemos que hacer: b(1)=2*a(1) b(2)=2*a(2) b(3)=2*a(3) para n datos, la forma general es: b(j) = 2 a(j) para j = 1, 2, 3,..., n.
Cuando se requiere hacer una tarea de forma repetitiva utilizamos un ciclo. para j=1 hasta n TAREA termina En fortran do j=1,n b(j)=2*a(j) enddo En MatLab foro j=1:n b(j)=2*a(j); end
program prog i n t e g e r : : i r e a l : : a, b, c c h a r a c t e r ( 3 0 ) : : head c h a r a c t e r ( 3 0 ) : : b a s u r a open ( u n i t =100, f i l e = e s t a c i o n. dat ) r e a d ( 1 0 0, 1 0 1 ) head r e a d ( 1 0 0, (A) ) b a s u r a r e a d ( 1 0 0, 1 0 2 ) i, a, b c l o s e ( 1 0 0 ) 101 format (A) 102 format ( I1, F5. 1, F6. 2 ) c=a+b w r i t e ( 6, ) w r i t e ( 6, ) head i, a, b, c open ( u n i t =200, f i l e = e s t a c i o n. out ) w r i t e (200, ) d a t o s de s a l i d a w r i t e ( 2 0 0, ( I1, 3 F6. 2 ) ) i, a, b, c c l o s e ( 2 0 0 ) end program prog
program prog i n t e g e r : : i r e a l : : a, b, c c h a r a c t e r ( 3 0 ) : : head c h a r a c t e r ( 3 0 ) : : b a s u r a d i m e n s i o n i ( 5 ), a ( 5 ), b ( 5 ), c ( j ) open ( u n i t =100, f i l e = e s t a c i o n 1. dat ) r e a d ( 1 0 0, 1 0 1 ) head r e a d ( 1 0 0, (A) ) b a s u r a do j =1,5 r e a d ( 1 0 0, 1 0 2 ) i ( j ), a ( j ), b ( j ) enddo c l o s e ( 1 0 0 ) 101 format (A) 102 format ( I1, F5. 1, F6. 2 ) do j =1,5 c ( j )=a ( j )+b ( j ) enddo w r i t e ( 6, ) head do j =1,5 w r i t e ( 6, ( I1, 3 F6. 2 ) ) i ( j ), a ( j ), b ( j ), c ( j ) enddo open ( u n i t =200, f i l e = e s t a c i o n 1. out ) write (200,201) datos de s a li da do j =1,5 w r i t e ( 2 0 0, 2 0 2 ) i ( j ), a ( j ), b ( j ), c ( j ) enddo c l o s e ( 2 0 0 ) 201 format (A) 202 format ( I3, 3 F8. 4 ) end program prog
Ejemplo. Programa para evaluar y graficar una función
program f u n c i o n i n t e g e r : : i, n r e a l, a, b r e a l, d i m e n s i o n ( 1 0 ) : : x, f a =0. e0 b=2. e0 n=10 dx=(b a )/ n x (1)= a f (1)=x 2. e0 do i =1,n x ( i )=a+i dx f ( i )=x 2 w r i t e ( 6, ) x, f enddo end program f u n c i o n
program f u n c i o n p a r a m e t e r ( n=10) i n t e g e r : : i r e a l, a, b r e a l, d i m e n s i o n ( n ) : : x, f a =0. e0 b=2. e0 dx=(b a )/ n x (1)= a f (1)= x ( 1 ) 2. e0 do i =1,n x ( i )=a+i dx f ( i )=x ( i ) 2 enddo open ( u n i t =100, f i l e = f u n c d a t a. dat ) do i =1,n w r i t e ( 1 0 0, 2 0 0 ) x ( i ), f ( i ) enddo c l o s e ( 1 0 0 ) 200 format (2 f 1 6. 4 ) end program f u n c i o n
program f u n c i o n c l e a r a l l p a r a m e t e r ( n=10) i n t e g e r : : i r e a l, a, b r e a l, d i m e n s i o n ( n ) : : x, f a =0. e0 b=2. e0 dx=(b a )/ n load funcdata. dat x=f u n c d a t a ( :, 1 ) ; f=f u n c d a t a ( :, 2 ) ; c l f p l o t ( x, f, o ) x (1)= a f (1)= x ( 1 ) 2. e0 do i =1,n x ( i )=a+i dx f ( i )=x ( i ) 2 enddo open ( u n i t =100, f i l e = f u n c d a t a. dat ) do i =1,n w r i t e ( 1 0 0, 2 0 0 ) x ( i ), f ( i ) enddo c l o s e ( 1 0 0 ) 200 format (2 f 1 6. 4 ) end program f u n c i o n
program f u n c i o n c l e a r a l l p a r a m e t e r ( n=10) i n t e g e r : : i r e a l, a, b r e a l, d i m e n s i o n ( n ) : : x, f a =0. e0 b=2. e0 dx=(b a )/ n x (1)= a f (1)= x ( 1 ) 2. e0 do i =1,n x ( i )=a+i dx f ( i )=x ( i ) 2 enddo open ( u n i t =100, f i l e = f u n c d a t a. dat ) do i =1,n w r i t e ( 1 0 0, 2 0 0 ) x ( i ), f ( i ) enddo c l o s e ( 1 0 0 ) 200 format (2 f 1 6. 4 ) end program f u n c i o n load funcdata. dat x=f u n c d a t a ( :, 1 ) ; f=f u n c d a t a ( :, 2 ) ; c l f p l o t ( x, f, o ) 4 3.5 3 2.5 2 1.5 1 0.5 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
sub rutinas
programas: principal.f ; subrutina rutina.f compilacion: pgf90 principal.f rutina.f -o principal.out program p r i n c i p a l s u b r o u t i n e r u t i n a ( x, y, z ) r e a l : : a, b a=3 b=4 c a l l r u t i n a ( a, b ) end program p r i n c i p a l r e a l : : x, y, z z=s q r t ( x 2+y 2) w r i t e ( 6, ) r e t u r n end z
compu$ pgf90 principal.f rutina.f -o prin.out principal.f: rutina.f: compu$./prin.out 5.000000
programas: principal.f ; subrutina rutina.f compilacion: pgf90 principal.f rutina.f -o principal.out program p r i n c i p a l s u b r o u t i n e r u t i n a ( x, y, z ) r e a l : : a, b, c a=3 b=4 c a l l r u t i n a ( a, b, c ) c a l l wrt ( c ) end program p r i n c i p a l c s u b r o u t i n e wrt ( v a r ) r e a l : : x, y, z z=s q r t ( x 2+y 2) w r i t e ( 6, ) r e t u r n end z w r i t e ( 6, ) w r i t e ( 6, ) v a r c end
program p r i n c i p a l program p r i n c i p a l p a r a m e t e r ( n=10) r e a l : : a, b, x, f a =0. e0 b=10. e0 dt =(b a )/ n open ( u n i t =100, f i l e = f u n c d a t a 3. dat ) do i =1,n t=t+dt c a l l e v a l ( t, f ) p a r a m e t e r ( n=10) r e a l dt, a, b r e a l, d i m e n s i o n ( n ) : : t, f a =0. e0 b=10. e0 dt =(b a )/ n do i =1,n t ( i )=( i 1) dt c a l l e v a l ( t ( i ), f ( i ) ) w r i t e (1 0 0,2 00) enddo 200 format (2 F11. 4 ) t, f enddo open ( u n i t =100, f i l e = f u n c d a t a 4. dat ) do i =1,n w r i t e ( 1 0 0, 2 0 0 ) t ( i ), f ( i ) enddo c l o s e ( 1 0 0 ) 200 format (2 F11. 4 ) end program p r i n c i p a l C s u b r o u t i n e e v a l ( t, f ) r e a l : : x, f r e t u r n end C f = 9.81 t 2 end program p r i n c i p a l C s u b r o u t i n e e v a l ( t, f ) r e a l : : x, f r e t u r n end C f = 9.81 t 2
archivo de salida funcdata3.dat c l e a r a l l dat=l o a d ( f u n c d a t a 4. dat ) ; 1.0000 9.8100 2.0000 39.2400 3.0000 88.2900 4.0000 156.9600 5.0000 245.2500 6.0000 353.1600 7.0000 480.6900 8.0000 627.8400 9.0000 794.6100 10.0000 981.0001 t=dat ( :, 1 ) ; f=dat ( :, 2 ) ; c l f p l o t ( t, f,. k, MarkerSize, 2 0, L i n e w i d t h, 2 ) t i t l e ( c a i d a l i b r e, FontSize, 1 8 ) x l a b e l ( tiempo [ s eg ], FontSize, 1 8 ) y l a b e l ( d i s t a n c i a [m], FontSize, 1 8 ) s e t ( gca, FontSize, 1 8 ) g r i d on p r i n t depsc p r i n c i p a l 4
0 caida libre 100 200 distancia [m] 300 400 500 600 700 800 0 2 4 6 8 10 tiempo [seg]