Simulaci on de Procesos F sicos (F sica...

Post on 20-Jul-2020

2 views 0 download

Transcript of Simulaci on de Procesos F sicos (F sica...

Simulacion de Procesos Fısicos

(Fısica Computacional).Conceptos de Programacion.

F. A. Velazquez-Munozfederico.vemu@gmail.com

Departamento de FısicaCUCEI UdeG

2018A

Sistemas Operativos

I ms-dos → windows

I linux → suse; redhat; ubuntu; etc.

I unix → Mac OSx

usuario

aplicacion

sistema operativo

hardware

Lenguajes de Programacon:

I Matlab

I Fortran

I C++

I Phyton

I

I Java

I etc.

MatLab (MATrix LABoratory)The Language of Technical Computing

I High-level language for numerical computation, visualization, andapplication development

I Interactive environment for iterative exploration, design, and problemsolving.

I Mathematical functions for linear algebra, statistics, Fourier analysis,filtering, optimization, numerical integration, and solving ordinarydifferential equations.

I Built-in graphics for visualizing data and tools for creating custom plots

I Development tools for improving code quality and maintainability andmaximizing performance

I Tools for building applications with custom graphical interfaces

I Functions for integrating MATLAB based algorithms with externalapplications and languages such as C, Java, .NET, and Microsoft R© Excel R©

Fortran (Formula Translating System).

is a general-purpose, imperative programming languagethat is especially suited to numeric computation andscientific computing

Declaracion y uso de Variables

Tipos de Variables

I INTEGER: entero

I REAL: real

I COMPLEX: complejo

I LOGICAL: logica (verdadero o falso)

I CHARACTER: caracter

En un programa de fortran, la forma de escribir ladeclaracion de variables es:

program vars

i n t e g e r : : ir e a l : : acomplex : : imgl o g i c a l : : i ocharac t e r ( 1 0 ) : : f echa

i=1a=2.33img=cmplx (1 ,2 )i o=’ true ’f echa = ’2015/01/01 ’

wr i t e (6 ,∗ ) iwr i t e (6 ,∗ ) awr i t e (6 ,∗ ) imgwr i t e (6 ,∗ ) i owr i t e (6 ,∗ ) f echa

end program vars

El resultado que muestra el programa en pantalla es:compu$ ./a.out

12.330000(1.000000,2.000000)T2015/01/01

En un programa de MatLab no es necesario declarar lasvariables.

c l e a r a l li=1a=2.33img=1+2∗ ii o=f a l s ef echa = ’2015/01/01 ’

i =1

a =2.3300

img =

3

i o =0

fecha =2015/01/01

Programas basicos

programa para sumar dos numeros:declarados en el programa.

prog.f

program prog

r e a l : : a , b , c

a=1.2b=3.4

c=a+b

wr i t e (6 ,∗ ) c

end program prog

programa para sumar dos numeros:que pide al usuario.

prog.f

program prog

r e a l : : a , b , c

read (∗ ,∗ ) aread (∗ ,∗ ) b

c=a+b

wr i t e (6 ,∗ ) c

end program prog

programa para sumar dos numeros:cargados de un archivo.

prog.f

program prog

r e a l : : a , b , c

open ( un i t =100 , f i l e =’datos . dat ’ )read (100 ,∗ ) aread (100 ,∗ ) bc l o s e (100)

c=a+b

wr i t e (6 ,∗ ) c

end program prog

archivo de datos datos.dat

10 .2520 .34

programa que ademas salva los datos a un archivo

prog.f

program prog

r e a l : : a , b , c

open ( un i t =100 , f i l e =’ dts . dat ’ )read (100 ,110) aread (100 ,110) bc l o s e (100)

110 format (F7 . 2 )

c=a+b

wr i te (6 ,∗ ) awr i t e (6 ,∗ ) bwr i t e (6 ,∗ ) copen ( un i t =200 , f i l e =’ dts . out ’ )wr i t e (200 ,∗ ) cc l o s e (200)

end program prog

archivo de datos datos.dat

10 .2520 .34

archivo de datos datos.dat

1 10 .2 20 .3 30 .5

Formato para datos de entrada y salida en fortransintaxis:

write/read (UNIT,FORMAT) variables

write/read (UNIT LABEL,FORMAT LABEL) variableslabel format (format-code)

ejemplo:

write(*,900) i,x900 format(I4,F8.3)

A: text stringD: double precision numbers, exponent notationE: real numbers, exponent notationF: real numbers, fixed point formatI: integerX: horizontal skip (space)/: vertical skip (new line)

n FC w.d

indices de variables

Los indices se usan para identificar a un elemento de unarreglo de variables.por ejemplo: Se si define a(1) = 10; a(2) = 20; a(3) = 30 elarreglo es de dimension 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.

ciclos

Cuando se requiere hacer una tarea de forma repetitivautilizamos un ciclo.

para j=1 hasta nTAREA

termina

En fortrando j=1,n

b(j)=2*a(j)enddo

En MatLabfor j=1:n

b(j)=2*a(j);end

Cuando se requiere hacer una tarea de forma repetitivautilizamos un ciclo.

para j=1 hasta nTAREA

termina

En fortrando j=1,n

b(j)=2*a(j)enddo

En MatLabfor j=1:n

b(j)=2*a(j);end

Cuando se requiere hacer una tarea de forma repetitivautilizamos un ciclo.

para j=1 hasta nTAREA

termina

En fortrando j=1,n

b(j)=2*a(j)enddo

En MatLabfor j=1:n

b(j)=2*a(j);end

program prog

i n t e g e r : : ir e a l : : a , b , ccharac t e r ( 3 0 ) : : headcharac t e r ( 3 0 ) : : basura

open ( un i t =100 , f i l e =’ e s t a c i on . dat ’ )read (100 ,101) headread (100 , ’ (A) ’ ) basuraread (100 ,102) i , a , bc l o s e (100)

101 format (A)102 format ( I1 , F5 . 1 , F6 . 2 )

c=a+b

wr i te (6 ,∗ ) headwr i t e (6 ,∗ ) i , a , b , c

open ( un i t =200 , f i l e =’ e s t a c i on . out ’ )wr i t e (200 ,∗ ) ’ datos de sa l i da ’wr i t e (200 , ’ ( I1 , 3F6 . 2 ) ’ ) i , a , b , cc l o s e (200)

end program prog

program prog

i n t e g e r : : ir e a l : : a , b , ccharac t e r ( 3 0 ) : : headcharac t e r ( 3 0 ) : : basuradimension i ( 5 ) , a ( 5 ) , b (5 ) , c ( j )

open ( un i t =100 , f i l e =’ e s t a c i on1 . dat ’ )read (100 ,101) headread (100 , ’ (A) ’ ) basurado j =1,5read (100 ,102) i ( j ) , a ( j ) , b ( j )enddoc l o s e (100)

101 format (A)102 format ( I1 , F5 . 1 , F6 . 2 )

do j =1,5c ( j )=a ( j )+b( j )enddo

wr i t e (6 ,∗ ) headdo j =1,5wr i t e ( 6 , ’ ( I1 , 3F6 . 2 ) ’ ) i ( j ) , a ( j ) , b ( j ) , c ( j )enddo

open ( un i t =200 , f i l e =’ e s t a c i on1 . out ’ )wr i t e (200 ,201) ’ datos de sa l i da ’do j =1,5wr i t e (200 ,202) i ( j ) , a ( j ) , b ( j ) , c ( j )enddoc l o s e (200)

201 format (A)202 format ( I3 , 3F8 . 4 )

end program prog

Ejemplo. Programa para evaluar y graficar una funcion

funcion.f

program func ion

i n t e g e r : : i , nr e a l a , brea l , dimension ( 1 0 ) : : x , f

a=0. e0b=2. e0n=10dx=(b−a )/n

x(1)=af (1)=x (1 )∗∗2 . e0

do i =2,nx ( i )=a + i ∗dxf ( i )=x( i )∗∗2wr i t e (6 ,∗ ) x ( i ) , f ( i )enddo

wr i t e (6 ,∗ ) xwr i t e (6 ,∗ ) f

end program func ion

funcion.f

program func ion

parameter (n=10)i n t e g e r : : ir e a l a , brea l , dimension (n ) : : x , f

a=0. e0b=2. e0dx=(b−a )/n

x(1)=af (1)=x (1 )∗∗2 . e0

do i =1,nx ( i )=a+i ∗dxf ( i )=x( i )∗∗2enddo

open ( un i t =90, f i l e =’dat . dat ’ )do i =1,nwr i t e (90 ,80) x ( i ) , f ( i )enddoc l o s e (90)

80 format (2 f16 . 4 )

end program func ion

funcion.m

c l e a r a l l

load funcdata . dat

x=funcdata ( : , 1 ) ;f=funcdata ( : , 2 ) ;

c l fp l o t (x , f , ’ o− ’)

0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

0.5

1

1.5

2

2.5

3

3.5

4

funcion.f

program func ion

parameter (n=10)i n t e g e r : : ir e a l a , brea l , dimension (n ) : : x , f

a=0. e0b=2. e0dx=(b−a )/n

x(1)=af (1)=x (1 )∗∗2 . e0

do i =1,nx ( i )=a+i ∗dxf ( i )=x( i )∗∗2enddo

open ( un i t =90, f i l e =’dat . dat ’ )do i =1,nwr i t e (90 ,80) x ( i ) , f ( i )enddoc l o s e (90)

80 format (2 f16 . 4 )

end program func ion

funcion.m

c l e a r a l l

load funcdata . dat

x=funcdata ( : , 1 ) ;f=funcdata ( : , 2 ) ;

c l fp l o t (x , f , ’ o− ’)

0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

0.5

1

1.5

2

2.5

3

3.5

4

funcion.f

program func ion

parameter (n=10)i n t e g e r : : ir e a l a , brea l , dimension (n ) : : x , f

a=0. e0b=2. e0dx=(b−a )/n

x(1)=af (1)=x (1 )∗∗2 . e0

do i =1,nx ( i )=a+i ∗dxf ( i )=x( i )∗∗2enddo

open ( un i t =90, f i l e =’dat . dat ’ )do i =1,nwr i t e (90 ,80) x ( i ) , f ( i )enddoc l o s e (90)

80 format (2 f16 . 4 )

end program func ion

funcion.m

c l e a r a l l

load funcdata . dat

x=funcdata ( : , 1 ) ;f=funcdata ( : , 2 ) ;

c l fp l o t (x , f , ’ o− ’)

0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

0.5

1

1.5

2

2.5

3

3.5

4

sub rutinas

Una subrutina es una parte del codigo de un programa quehace una tarea especıfica y que puede ser utilizada en variasocaciones a lo largo de un programa principal. Puede estar

escrita en el mismo archivo del programa principal, y por logeneral se localizan en la parte inferior, despues que terminael programa principla. Tambien pueden estar en otro archivo,

que por lo general se nombra con el nombre de la subrutinay con la extension propia del lenguaje de programacion.

programas: principal.f ; subrutina rutina.fcompilacion: pgf90 principal.f rutina.f -o principal.out

program p r i n c i p a l

r e a l : : a , b

a=3b=4

c a l l ru t ina (a , b)

end program p r i n c i p a l

subrout ine rut ina (x , y )

r e a l : : x , y , z

z=sq r t (x∗∗2+y∗∗2)

wr i t e (6 ,∗ ) z

re turnend

compu$ pgf90 principal.f rutina.f -o prin.out principal.f:rutina.f:compu$ ./prin.out 5.000000

programas: principal.f ; subrutina rutina.fcompilacion: pgf90 principal.f rutina.f -o principal.out

program p r i n c i p a l

r e a l : : a , b , c

a=3b=4

c a l l ru t ina (a , b , c )c a l l wrt ( c )

end program p r i n c i p a lc −−−

subrout ine wrt ( var )r e a l : : varwr i t e (6 ,∗ ) ’ ’wr i t e (6 ,∗ ) var

c −−−end

subrout ine rut ina (x , y , z )

r e a l : : x , y , z

z=sq r t (x∗∗2+y∗∗2)

wr i t e (6 ,∗ ) z

re turnend

program p r i n c i p a l

parameter (n=10)r e a l : : a , b , x , f

a=0. e0b=10. e0dt=(b−a )/n

open ( un i t =100 , f i l e =’d3 . dat ’ )do i =1,nt=t+dtc a l l eva l ( t , f )

wr i t e (100 ,200) t , fenddo

200 format (2F11 . 4 )

end program p r i n c i p a lC −−−

subrout ine eva l ( t , f )r e a l : : x , f

f =(−9.81/2. e0 )∗ t ∗∗2

returnend

C −−−

program p r i n c i p a l

parameter (n=10)r e a l dt , a , brea l , dimension (n ) : : t , f

a=0. e0b=4. e0dt=(b−a )/n

do i =1,nt ( i )=( i −1)∗dtc a l l eva l ( t ( i ) , f ( i ) )

enddo

open ( un i t =100 , f i l e =’d4 . dat ’ )do i =1,nwr i t e (100 ,200) t ( i ) , f ( i )enddoc l o s e (100)

200 format (2F11 . 4 )

end program p r i n c i p a lC −−−

subrout ine eva l ( t , f )r e a l : : x , f

f =(−9.81/2. e0 )∗ t ∗∗2

returnend

C −−−

archivo de salida funcdata3.dat

1.0000 −9.81002.0000 −39.24003.0000 −88.29004.0000 −156.96005.0000 −245.25006.0000 −353.16007.0000 −480.69008.0000 −627.84009.0000 −794.6100

10.0000 −981.0001

c l e a r a l l

dat=load ( ’ funcdata4 . dat ’ ) ;

t=dat ( : , 1 ) ;f=dat ( : , 2 ) ;

c l f

p l o t ( t , f , ’ .−k ’ , ’ MarkerSize ’ , 2 0 , ’ Linewidth ’ , 2 )

t i t l e ( ’ ca ida l i b r e ’ , ’ FontSize ’ , 1 8 )x l ab e l ( ’ tiempo [ seg ] ’ , ’ FontSize ’ , 1 8 )y l ab e l ( ’ d i s t an c i a [m] ’ , ’ FontSize ’ , 1 8 )

s e t ( gca , ’ FontSize ’ , 1 8 )

g r id on

pr in t −depsc p r i n c i p a l 4

0 2 4 6 8 10−800

−700

−600

−500

−400

−300

−200

−100

0caida libre

tiempo [seg]

dis

tancia

[m

]

CAIDA LIBRE

program caida

parameter (n=10)r e a l dt , to , t fi n t e g e r i

i n c lude ’ ca ida . c ’

vo= 0 . e0yo= 0 . e0

to=0. e0t f =4. e0dt=( t f−to )/n

open ( un i t =90, f i l e =’c . dat ’ )do i =1,n+1

t=(i −1)∗dtc a l l eva l ( t , y , v )wr i t e (90 ,80) t , y , v

enddo80 format (3F10 . 4 )

c l o s e (90)end program caida

C −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−subrout ine eva l ( t , y , v )inc lude ’ ca ida . c ’

g=−9.81e0y=(g /2 . e0 )∗ t∗∗2+vo∗ t+yov=g∗ t + voreturnend

C −−−

CC common blockC

r e a l y , vr e a l vo , yo , dt

common/blk /vo , yo , dt

C −−−−−−−−−−−−−−−−−

caida.c

caida.f

archivo de salida funcdata3.dat

0.0000 0.0000 0.00000.4000 −0.7848 −3.92400.8000 −3.1392 −7.84801.2000 −7.0632 −11.77201.6000 −12.5568 −15.69602.0000 −19.6200 −19.62002.4000 −28.2528 −23.54402.8000 −38.4552 −27.46803.2000 −50.2272 −31.39203.6000 −63.5688 −35.31604.0000 −78.4800 −39.2400

% Graf i ca de l o s datos de ca ida l i b r e% caida . dat% ca l cu l ado s con ca ida . f y ca ida . c

c l e a r a l l

dat=load ( ’ ca ida . dat ’ ) ;

t=dat ( : , 1 ) ;y=dat ( : , 2 ) ;v=dat ( : , 3 ) ;

c l fsubplot 211p lo t ( t , y , ’ k ’ , ’ LineWidth ’ , 2 )t i t l e ( ’ po s i c i on [m] ’ , ’ FontSize ’ , 1 6 )g r id ons e t ( gca , ’ Xtick ’ , [ 0 : 0 . 5 : 4 ] , ’ FontSize ’ , 1 6 )

subplot 212p lo t ( t , v , ’ k ’ , ’ LineWidth ’ , 2 )t i t l e ( ’ ve loc idad [m s ˆ{ −1} ] ’ , ’ FontSize ’ , 1 6 )g r id ons e t ( gca , ’ Xtick ’ , [ 0 : 0 . 5 : 4 ] , ’ FontSize ’ , 1 6 )

x l ab e l ( ’ tiempo [ s ] ’ )drawnow

pr in t −depsc c a i d a f i g

0 0.5 1 1.5 2 2.5 3 3.5 4−80

−60

−40

−20

0posicion [m]

0 0.5 1 1.5 2 2.5 3 3.5 4−40

−30

−20

−10

0velocidad [m s

−1]

tiempo [s]

−1

0

1

2

3

4

5

6

7

8

9

10

−1

0

1

2

3

4

5

6

7

8

9

10 program caida1

r e a l dt , g , tmin t e g e r i ,Nr e a l t , y , v , vo , yo

vo= 0 . e+0yo= 4 . e+1dt= 1 . e−2tm= 14.25+0

N=tm/dt+1

g=−9.81e0

open ( un i t =100 , f i l e =’ ca ida1 . dat ’ )do i =1,N

t=(i −1)∗dty=0.5 e0∗g∗dt∗∗2 + vo∗dt + yov= g∗dt + vo

wr i t e (100 ,200) t , y , v

yo=yvo=v

i f ( y . l t . 0 . e0 ) thenvo=−0.75∗vyo=0. e0

end i f

enddo

200 format (3F10 . 4 )

c l o s e (100)end program caida1

−1

0

1

2

3

4

5

6

7

8

9

10

Manejo de archivos.