python, scipy - f2py -...

52
Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize scipy.linalg scipy.sparse f2py Fortran C Une application Caisson acoustique Maillage Main.py MaillageEtEF.py Bords.py Matrices.py Spmat2csc.py Dirichlet.py Visu_0.py Résultat Python, Scipy - f2py - ... O. Wilk Calcul scientifique/Math/Cnam Février 2011 O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Upload: hoangquynh

Post on 22-May-2018

275 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Python,Scipy - f2py - ...

O. Wilk

Calcul scientifique/Math/Cnam

Février 2011

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 2: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Plusieurs parties :Python, une introduction :des bases, du calcul scientifique (numpu, scipy,...), de la visualisation.Le module scipy plus en détails, couplagefortran-python (f2py) et le C ...Une application : avec GetFem, ...

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 3: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Scipy

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 4: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

ScipySurvol de :

scipy.cluster : Cluster algorithms(cluster analysis (k-means, ...))(une extension : scipy-cluster) :

Source : Wikipedia.

scipy.constants : Physical and mathematicalconstants :>>> from scipy.constants import codata>>> codata.unit(u’proton mass’)’kg’

scipy.fftpack : Fast Fourier Transform routines :# 1D (fft, ifft),# 2D (fft2, ifft2),# nD (fftn, ifftn), ...>>> import numpy as np>>> import scipy.fftpack as sp_fft>>> x = np.arange(5) # [0 1 2 3 4]>>> np.all(np.abs(x-sp_fft.ifft(sp_fft.fft(x)) < 1.e-15))True

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 5: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

ScipySurvol de :

scipy.integrate : Integration of functions (librairiefortran QuadPack) and ordinary differentialequation solvers (librairie fortran OdePack) :>>> import numpy as np>>> import scipy.integrate as sp_integ>>> f = lambda x : np.exp(-x**2)>>> sp_integ.quad(f, -np.inf, np.inf)(1.7724538509055159, 1.420263678441437e-08)

scipy.interpolate : Interpolation and smoothingsplines,>>> import numpy as np>>> import scipy.interpolate as sp_intep>>> import matplotlib.pyplot as plt>>> x1 = np.linspace(0, 1, 10) ; y1 = np.sin(2*np.pi*x1)>>> cubic = sp_intep.interp1d(x1, y1, kind=’cubic’)>>> x2 = np.linspace(0, 1, 50)>>> y2 = cubic(x2)>>> plt.plot(x1, y1, ’o’)>>> plt.plot(x2, y2, ’-’)>>> plt.show()

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 6: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

ScipySurvol de :

scipy.io : voir numpy ...scipy.maxentropy : Maximum entropy method(theorie de l’information),scipy.ndimage : N-dimensional image processing :# convolution, corrélation, filtre, morphologie, ...>>> import scipy as sp>>> import scipy.ndimage as sp_im>>> import matplotlib.pyplot as plt>>> lena = sp.lena()>>> lena_floue = sp_im.gaussian_filter(lena, 3)>>> imgplot = plt.imshow(lena_floue)>>> imgplot.set_cmap(’gray’) ; plt.show()

scipy.odr : Orthogonal distance regression (librairiefortran ODRpack "based on a trust-regionLevenberg-Marquardt method").

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 7: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

ScipySurvol de :

scipy.signal : Signal processing :# convolution, B-splines, filtre : wiener, ..., wavelets, ...

scipy.special : Special functions :# Bessel (harmoniques cylindriques),Orthogonal polynomials, Kelvin, ...

Source : Linux Mag HS 40.

scipy.stats : Statistical distributions and functions :# continuous and discrete distributions : Random Variates, Probability

Density, Cumulative Distribution, Survival, Percent Point, ... ;

Univariate and multivariate kernel density, ...

scipy.weave : CC++ integration :# Python ⇔ CC++.

Add-ons Scipy : http ://www.scipy.org/Topical_Software.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 8: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Scipy

En détails :

scipy.optimize : Optimization and root-findingroutines,scipy.linalg : Linear algebra,scipy.sparse (scipy.sparse.linalg) : Sparse matricesand associated routines.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 9: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Optimisation et recherche de zéros

Importer le module "scipy.optimize" :

>>> import scipy.optimize as sp_o

Les différentes fonctions,un problème d’optimisation utilisant "BFGS",une recherche de zéros.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 10: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Les différentes fonctions (1/3)Optimisation sans contrainte :

fmin : Nelder-Mead Simplex algorithm (f ),

fmin_powell : Powell’s (modified) level set method (f ),

fmin_cg : Non-linear conjugate gradient algorithm (f , ∇) (Polak-Ribiere),

fmin_bfgs : Quasi-Newton method (BFGS) (f , ∇),

fmin_ncg : Line-search Newton Conjugate Gradient (f , ∇, H),

leastsq : Minimize the sum of squares of M equations in N unknowns.

Optimisation avec contraintes :fmin_l_bfgs_b : Zhu, Byrd, and Nocedal’s L-BFGS-B,

fmin_tnc : Truncated Newton Code originally (S. Nash ... J.-S. Roy),

fmin_cobyla : Constrained Optimization by Linear Approximation.

Optimisation globale :anneal : Simulated Annealing,

brute : Brute force searching optimizer.

Et quelques fonctions dédiées à la minimisation de fonctions scalaires.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 11: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Optimisation (2/3)

minx

f (x) =

N−1Xi=1

100 ∗ (xi − x2i−1)2 + (1− xi−1)2

, f (x) = 0 pour xi = 1, ∀i = 1, N.

CodePython.py

import numpy as npimport sc ipy as spimport sc ipy . l i n a l g as s p _ l i nimport t ime

from sc ipy . op t im ize import fmin # Nelder−Mead Simplex a lgo r i t hmfrom sc ipy . op t im ize import fmin_cg # Non−l i n e a r ( Polak−Rib ie re )

# conjugate g rad ien t a lgo r i t hmfrom sc ipy . op t im ize import fmin_bfgs # Broyden−Fle tcher−Goldfarb−Shanno

# a lgo r i t hm

def rosen ( x ) :" " " The Rosenbrock f u n c t i o n " " "return sum(100.0∗ ( x [1:]−x [:−1]∗∗2.0)∗∗2.0 + (1−x [ :−1])∗∗2.0)

def rosen_der ( x ) :xm_m1 = x [:−2] ; xm = x [1:−1] ; xm_p1 = x [ 2 : ]der = x∗0.0der [1:−1] = 200∗(xm−xm_m1∗∗2) − 400∗(xm_p1 − xm∗∗2)∗xm − 2∗(1−xm)der [ 0 ] = −400∗x [ 0 ]∗ ( x[1]−x [0 ]∗∗2) − 2∗(1−x [ 0 ] )der [−1] = 200∗(x[−1]−x[−2]∗∗2)return der

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 12: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Optimisation (2/3)CodePython.py (suite)

n = 30 ; x0 = 1.0 − ( sp . rand ( n ) − 0 . 5 ) / 3 ; x r e f = np . ones ( n )

pr in t " " ; pr in t " fmin "t1 = t ime . c lock ( ) ;xopt = fmin ( rosen , x0 , x t o l =1e−8, maxi ter = 1000000 , maxfun = 1000000)t2 = t ime . c lock ( ) − t1

pr in t " | | x − x r e f | | = %f , f ( x ) / f ( x r e f ) = %e / %f en %f s " . . .% ( s p _ l i n . norm ( xopt−x r e f ) , rosen ( xopt ) , rosen ( x r e f ) , t2 )

pr in t " " ; pr in t " fmin_cg "t1 = t ime . c lock ( ) ;xopt = fmin_cg ( rosen , x0 , fp r ime=rosen_der )t2 = t ime . c lock ( ) − t1

pr in t " | | x − x r e f | | = %f , f ( x ) / f ( x r e f ) = %e / %f en %f s " . . .% ( s p _ l i n . norm ( xopt−x r e f ) , rosen ( xopt ) , rosen ( x r e f ) , t2 )

pr in t " " ; pr in t " fmin_bfgs "t1 = t ime . c lock ( ) ;xopt = fmin_bfgs ( rosen , x0 , fp r ime=rosen_der )t2 = t ime . c lock ( ) − t1

pr in t " | | x − x r e f | | = %f , f ( x ) / f ( x r e f ) = %e / %f en %f s " . . .% ( s p _ l i n . norm ( xopt−x r e f ) , rosen ( xopt ) , rosen ( x r e f ) , t2 )

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 13: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Optimisation 3/3)

[wilk@localhost ∼]$ python CodePython.py

fminOpt im iza t ion terminated suc cess fu l l y .

Current f u n c t i o n value : 0.000051I t e r a t i o n s : 155631Funct ion eva lua t ions : 184620

| | x − x r e f | | = 0.003303 , f ( x ) / f ( x r e f ) = 5.0849e−05 / 0.0000 en 32.0100 s

fmin_cgOpt im iza t ion terminated suc cess fu l l y .

Current f u n c t i o n value : 0.000000I t e r a t i o n s : 74Funct ion eva lua t ions : 132Gradient eva lua t ions : 132

| | x − x r e f | | = 0.000014 , f ( x ) / f ( x r e f ) = 4.7954e−11 / 0.0000 en 0.0400 s

fmin_bfgsOpt im iza t ion terminated suc cess fu l l y .

Current f u n c t i o n value : 0.000000I t e r a t i o n s : 39Funct ion eva lua t ions : 60Gradient eva lua t ions : 60

| | x − x r e f | | = 0.000000 , f ( x ) / f ( x r e f ) = 1.4356e−15 / 0.0000 en 0.0300 s

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 14: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Recherche de zéros (1/3)newton : algorithme de Newton,

fixed_point : méthode du point fixe.

Fonctions scalaires, recherche dans un intervalle :brentq : 1 méthode de la sécante (extrapolation quadratique inverse),

brenth : méthode de la sécante (extrapolation hyperbolique),

ridder : 2,

bisect : méthode de la bissection (lent mais robuste).

MINPACK :fsolve : algorithmes de l’ensemble MINPACK (hybrd, hybrj).

Inverse de la Jacobienne ou une approximation :broyden1, broyden2, broyden3 : méthodes de Broyden,

broyden_generalized : méthode généralisé de Broyden,

anderson, anderson2 : méthodes d’Anderson.1

Brent R. P., Algo. for Min. Without Derivatives. Englewood Cliffs, NJ : Prentice-Hall, 1973,2

Ridders, C. F. J. “A New Algo. for Comp. a Single Root of a Real Continuous Fction.” IEEE

Trans. Circuits Systems 26, 979-980, 1979

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 15: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Recherche de zéros (2/3)

CodePython.py

import numpy as npimport sc ipy as spimport sc ipy . l i n a l g as s p _ l i nimport sc ipy . sparse as sp_spimport sc ipy . sparse . l i n a l g as sp_splimport sc ipy . op t im ize as sp_o

import m a t p l o t l i b . pyp lo t as p l t

# y_prim_prim + x∗y + c∗y∗∗2 = 6x , y (0)= 0 , y (5)= 0

x1 = 0.xN = 5.N = 100

dx = (xN − x1 ) / ( N−1)x = sp . arange ( 0 , N, 1)∗dxun = sp . ones (N−2)

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 16: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Recherche de zéros (2/3)

CodePython.py (suite)

# c = 0

A1 = sp_sp . spdiags ( [ un , −2∗un + x [1:−1]∗dx∗∗2, un ] , [−1, 0 , 1 ] , N−2, N−2)

A = np . zeros ( [ N,N ] )A[ 0 , 0 ] = 1.0A [N−1,N−1] = 1.0A [ 1 :N−1,1:N−1] = A1 . todense ( )

A = sp_sp . csc_matr ix (A)

b = 6∗x∗dx∗∗2 ; b [ 0 ] = 0 . ; b [N−1] = 0 .

def f ( y , x , dx , A, c ) :y [ 0 ] = 0 . ; y [N−1] = 0 .x [ 0 ] = 0 . ; x [N−1] = 0 .return A. _mul_vector ( y ) + ( c∗y∗∗2 − 6∗x )∗dx∗∗2

def f_pr im ( y , x , dx , A, c ) :y [ 0 ] = 0 . ; y [N−1] = 0 .mat = np . diag ( y )mat = sp_sp . csc_matr ix ( mat )return (A + 2∗c∗(dx∗∗2)∗mat ) . todense ( )

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 17: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Recherche de zéros (2/3)CodePython.py (suite)

[ y0 , i n f o ] = sp_spl . cg (A, b , x0 = np . zeros (N) , t o l = 1 .e−12, maxi ter = 2∗N)

c1 = 2.e−1 ; c2 = 5.e−1 ; c3 = 1.0y1 = sp_o . f s o l v e ( f , y0 , args = ( x , dx , A, c1 ) , x t o l = 1 .e−12)y2 = sp_o . f s o l v e ( f , y1 , args = ( x , dx , A, c2 ) , x t o l = 1 .e−12)y3 = sp_o . f s o l v e ( f , y2 , args = ( x , dx , A, c3 ) , fp r ime = f_pr im , x t o l = 1 .e−12)

y4 = sp_o . f s o l v e ( f , y0 , args = ( x , dx , A, c2 ) , x t o l = 1 .e−12)y5 = sp_o . f s o l v e ( f , y4 , args = ( x , dx , A, c3 ) , fp r ime = f_pr im , x t o l = 1 .e−12)

p l t . i o f f ( )p l t . f i g u r e ( )

p l t . p l o t ( x , y0 , co l o r = ’ b lack ’ )p l t . p l o t ( x , y1 , co l o r = ’ green ’ )p l t . p l o t ( x , y2 , co l o r = ’ b lue ’ )p l t . p l o t ( x , y3 , co l o r = ’ red ’ )p l t . p l o t ( x , y4 , ’−o ’ , co l o r = ’ b lue ’ )p l t . p l o t ( x , y5 , ’−o ’ , co l o r = ’ red ’ )

chaine0 = ’ y0 , f = %8.2e ’ % s p _ l i n . norm ( f ( y0 , x , dx , A, 0 . ) )chaine1 = ’ c1 , y0 −> y1 , f = %8.2e ’ % s p _ l i n . norm ( f ( y1 , x , dx , A, c1 ) )chaine2 = ’ c2 , y1 −> y2 , f = %8.2e ’ % s p _ l i n . norm ( f ( y2 , x , dx , A, c2 ) )chaine3 = ’ c3 , y2 −> y3 , f = %8.2e ’ % s p _ l i n . norm ( f ( y3 , x , dx , A, c3 ) )chaine4 = ’ c2 , y0 −> y4 , f = %8.2e ’ % s p _ l i n . norm ( f ( y4 , x , dx , A, c2 ) )chaine5 = ’ c3 , y2 −> y5 , f = %8.2e ’ % s p _ l i n . norm ( f ( y5 , x , dx , A, c3 ) )

p l t . legend ( [ chaine0 , chaine1 , chaine2 , chaine3 , chaine4 , chaine5 ] , l oc= ’ best ’ )

p l t . t i t l e ( ’ y ( x ) ’ ) ; p l t . draw ( ) ; p l t . save f ig ( ’ Zeros1 . png ’ ) ; p l t . ion ( )

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 18: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Recherche de zéros (3/3)

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 19: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Algèbre linéaire

Importer le module "scipy.linalg" :

>>> import scipy.linalg as sp_lin

Les différentes fonctions,un petit exemple.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 20: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Les différentes fonctions (1/2)inv : inverse de matrice,

solve : fonction résolvant A x = b,

solve_banded : même chose pour une matrice "bande",

solveh_banded : cas hermitien déf. pos.,

solve_triangular : pour une matrice triangulaire,

det : déterminant d’une matrice (LU),

norm : normes d’une matrice ou d’un vecteur,

lstsq : résolution "moindres carrés" de Ax = b,

pinv, pinv2 : pseudo-inverse (Moore-Penrose).

Valeurs (vecteurs) propres :eig : valeurs et vecteurs d’une matrice carré,

eigvals : que les valeurs propres,

eigh : valeurs et vecteurs d’une matrice hermitienne,

eigvalsh : que les valeurs propres,

eig_banded : valeurs et vecteurs d’une matrice "bande",

eigvals_banded : que les valeurs propres.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 21: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Les différentes fonctions (2/2)Décompositions, résolutions :

lu : décomposition LU,

lu_factor : décomposition LU (Lapack),

lu_solve : résolution de A x = b, en utilisant la décomposition LU,

svd : décomposition en valeurs singulières,

svdvals : que les valeurs singulières,

diagsvd : construction de la matrice diagonale associée,

orth : construction d’une base orthonormée (SVD),

cholesky : décomposition de Cholesky,

cholesky_banded : même chose pour une matrice "bande",

cho_factor : décompostion de Cholesky,

cho_solve : résolution de A x = b, en utilisant cho_factor„

cho_solve_banded : même chose pour une matrice "bande",

qr : décomposition QR,

schur : décomposition de Schur,

hessenberg : détermination de la forme "hessenberg" d’une matrice.

et : exponentielle de matrice, matrice d’Hadamart, ...

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 22: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Un exemple

Pour A une matrice stockée pleine :

>>> import numpy as np>>> import scipy.linalg as sp_lin

>>> print " Déterminant de A = ", sp_lin.det(A)

>>> print " Calcul des valeurs propres">>> [W,V] = sp_lin.eig(A)>>> index = np.argsort(W)>>> print " Affichage des 10 premières valeurs propres">>> print " ordonnées par ordre croissant :", W[index[0 :10]]

>>> print " Factorisation LU et résolution">>> [LU_A,P_A] = sp_lin.lu_factor(A)>>> x = sp_lin.lu_solve( (LU_A,P_A), b )

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 23: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Stockage morse

Importer le module "scipy.sparse" :

>>> import scipy.sparse as sp_sp

Les différentes classes, modules et fonctions,un petit exemple.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 24: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Les différentes classes

Différents stockages morses :csc_matrix : Compressed Sparse Column format,

csr_matrix : Compressed Sparse Row format,

bsr_matrix : Block Sparse Row format,

lil_matrix : List of Lists format,

dok_matrix : Dictionary of Keys format,

coo_matrix : COOrdinate format,

dia_matrix : DIAgonal format.

Les formats "csc_matrix" et "csr_matrix" sont àprivilégier lors des opérations de factorisation, derésolution, ...Les autres sont plus souples pour construire oumanipuler les matrices.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 25: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Quelques fonctions

et aussi pour construire une matrice morse :

eye : 1 sur la k ième diagonale, sinon 0,

identity : matrice identité stockée morse,

kron, kronsum : produit et somme de kronecker de matrices morses,

spdiags : matrice diagonale stockée morse,

tril, triu : partie "lower" et "upper" d’une matrice morse,

bmat : création d’une matrice bloc à partir de matrices morses, voiraussi hstack et vstack.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 26: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Exemplesimport numpy as npimport sc ipy . sparse as sp_sp

row = np . ar ray ( [ 0 , 2 , 1 ] ) ; co l = np . ar ray ( [ 0 , 2 , 1 ] ) ; data = np . ar ray ( [ 4 , 5 , 7 ] )pr in t sp_sp . coo_matr ix ( ( data , ( row , co l ) ) , shape =(3 ,3) ) . todense ( )# [ [ 4 0 0 ]# [0 7 0 ]# [0 0 5 ] ]

pr in t sp_sp . eye (3 ,3 , k =1 ) . todense ( )# [ [ 0 . 1 . 0 . ]# [ 0 . 0 . 1 . ]# [ 0 . 0 . 0 . ] ]

data = np . ar ray ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ) ; d iags = np . ar ray ( [0 ,−1 ,1] )pr in t sp_sp . spdiags ( data , diags , 3 , 3 ) . todense ( )# [ [ 1 8 0 ]# [4 2 9 ]# [0 5 3 ] ]

A = sp_sp . coo_matr ix ( [ [ 1 , 2 ] , [ 3 , 4 ] ] )B = sp_sp . coo_matr ix ( [ [ 5 ] , [ 6 ] ] ) ; C = sp_sp . coo_matr ix ( [ [ 7 ] ] )pr in t A. todense ( ) ; pr in t B. todense ( ) ; pr in t C. todense ( )# [ [ 1 2 ] [ [ 5 ] [ [ 7 ] ]# [3 4 ] ] [ 6 ] ]

pr in t sp_sp . bmat ( [ [ A ,B ] , [ None ,C ] ] ) . todense ( )# [ [ 1 2 5 ]# [3 4 6 ]# [0 0 7 ] ]

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 27: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Des modules

extract : pour extraire différentes parties d’une matrice morse,

sparsetools : différentes routines pour manipuler les matrices ...produits,

...

en relation avec (A une matrice stockée morse, x un vecteur) :

LaDiagonale = A.diagonal(),UnProduitMatriceVecteur = A._mul_vector(x),...

linalg : algèbre linéaire pour des matrices morses (sous-modules :

eigen (valeurs propres), isolve (méthodes itératives), dsolve

(méthodes directes).

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 28: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

scipy.sparse.linalg

matmat, matvec,

cg : gradient conjugué,

bicg : gradient bi-conjugué,

bicgstab : gradient biconjugué stabilisé,

cgs : gradient conjugué carré,

eigs, eigsh : valeurs et vecteurs propres d’une matrice carrée stockéemorse non herm. et herm. (Arpack),

factorized : LU,

gmres, lgmres : minimisation du résidu généralisé,

lobpcg : valeurs propres (utilisant un préconditionnement),

lsqr : Large sparse QR,

minres : (experimental)

qmr : quasi-minimisation du résidu,

splu, spilu : LU et LU incomplet (SuperLU).

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 29: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Qq exemples

Pour A une matrice stockée morse(stockage au format CSC ou CSR pour Umfpack) :

>>> [Val, Vect] = sp_sp.linalg.eigs(A, k = 10) # les 10 premières v.p.,

>>> sp_sp.linalg.use_solver(useUmfpack = True) # ... sinon SuperLU>>> solve = sp_sp.linalg.factorized( A ) # factorisation LU>>> x1 = solve( b1 ) # descente-remontée, b1 scd. membre,>>> x2 = solve( b2 ) # même chose sur un autre scd membre.

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 30: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

f2py

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 31: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

f2py (1/3)

La commande "f2py" permet d’utiliser des subroutinesfortran (77/90/95) dans python.

Compilation d’une subroutine "essai.f" (-c essai.f) etcréation d’un module pour python (-m essai) :

Au niveau d’un prompt shell (pas sous python)

[ w i lk@loca lhos t ~]$ f2py −c essai . f −m essai

CodePython.py

sys . path . append ( " / home / w i l k / essai " )

import essai

# l e nom de l a subrou t ine est i den t i que au nom du f i c h i e r# en enlevant l a te rmina ison " . f " évidemment , a i n s i :

pr in t essai . essai . __doc__ # on r écupère des i n f o ( v a r i a b l e s . . . )

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 32: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

f2py (2/3)Les commandes suivantes commencant par "cf2py"permettent de bien identifier les variables importantes :essai.f

subroutine essai ( n , longa , pcola , cola , a , b , x )

i m p l i c i t none

integer n , longa , i , j , kinteger pcola ( 0 : n ) , co la ( longa )

rea l∗8 a ( longa ) , b ( n ) , x ( n )

cf2py in tent ( in ) n , longa , pcola , cola , a , bcf2py in tent ( out ) x

. . .

end

F90/F95

subroutine norme ( a , b , c )rea l ( 8 ) , in tent ( in ) : : a , brea l ( 8 ) , in tent ( out ) : : cc = s q r t ( a∗a+b∗b )end subroutine norme

La commande "intent" peut utiliser d’autres options : in | inout | out | hide | in,out | inout,out | c | copy |cache | callback | inplace | aux .

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 33: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

f2py (3/3)Pour le C :test.c

void t e s t ( i n t n , double ∗x , double ∗y ) i n t i ;for ( i =0; i <n ; i ++)

y [ i ] = x [ i ] + i ;

m.pyf (le fichier permettant le lien C => python)

python module min ter face

subroutine t e s t ( n , x , y )in tent ( c ) t e s t ! t e s t es t l a f o n c t i o n Cin tent ( c )integer in tent ( h ide ) , depend ( x ) : : n= len ( x )double precision intent ( in ) : : x ( n )double precision intent ( out ) : : y ( n )

end subroutine t e s tend inter faceend python module m

Compilation et création du module "m" à importer dans python

f2py m. pyf −c t e s t . c

(plus d’info : exposé (CNRS) de Pierre Navaro - Autrans 6-10 décembre 2010)

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 34: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

SwigAutre solution pour le C/C++ :/∗ F i l e : header . h ∗ / /∗ F i l e : i n t e r f a c e . i ∗ /

%module mymodule# inc lude < s t d i o . h> %# inc lude <math . h> ==> # inc lude " header . h "

%extern i n t foo ( double ) ; ex tern i n t foo ( double ) ;ex tern double bar ( i n t , i n t ) ; ex tern double bar ( i n t , i n t ) ;ex tern vo id dump( FILE ∗ f ) ; ex tern vo id dump( FILE ∗ f ) ;

ou/∗ F i l e : i n t e r f a c e . i ∗ /%module mymodule%

==> # inc lude " header . h "%%inc lude " header . h "

[ ~ ] $ swig −python i n t e r f a c e . i −o in te r face_wrap . c[ ~ ] $ gcc −c example . c in te r face_wrap . c −I / usr / i nc lude / python2 .7[ ~ ] $ l d −shared example . o in te r face_wrap . o −o _example . so

Sous python :

>>> impor t example>>> [ . . . ] = example . UneDeVosFonction ( . . . )

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 35: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Une application

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 36: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Un caisson acoustique 3

8>>>>>>>>>>>><>>>>>>>>>>>>:

∂2ϕ

∂t2 − c2f ∆ϕ = 0 dans Ω×]0,T ],

ϕ = 0 sur ∂Ω \ Γs,

∂ϕ

∂ν=∂u∂t

sur Γs×]0,T ],

ϕ(x , 0) = ϕ0(x) et∂ϕ

∂t(x , 0) = ϕ1(x) dans Ω.

8>>>>>>><>>>>>>>:

2ε[ρs∂2u∂t2 − c2

s ∆su] = −∂ϕ∂t

sur ∂Γs×]0,T ],

u = 0 sur ∂Ω \ Γs,

u(s, 0) = u0(s) et∂u∂t

(s, 0) = u1(s) sur Γs.

Γs : la membrane de surface.

3Ph. Destuynder - Vibrations des structures et des systèmes couplés - Hermes-science, 2007

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 37: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Maillage (1/2)

Maillage créé à l’aide de gmsh :

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 38: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Maillage => code gmsh (2/2)l x = 2 ; nx = 100 ; nxh = nx + 1 ; nxb = nx + 1 ;l y = 2 ; ny = 50 ;

Po in t ( 1 ) = − l x , 0 , 0 ;Po in t ( 2 ) = lx , 0 , 0 ;Po in t ( 3 ) = lx , l y , 0 ;Po in t ( 4 ) = − l x , l y , 0 ;

L ine ( 1 ) = 1 , 2 ;L ine ( 2 ) = 2 , 3 ;L ine ( 3 ) = 3 , 4 ;L ine ( 4 ) = 4 , 1 ;

L ine Loop ( 5 ) = 1 , 2 , 3 , 4 ;

/ / generat ion des contours

T r a n s f i n i t e Line 3 = nxh Using Progression 1 ;T r a n s f i n i t e Line 1 = nxb Using Progression 1 ;

T r a n s f i n i t e Line 2 , −4 = ny Using Progression 1 ;

Plane Surface ( 7 ) = 5 ;T r a n s f i n i t e Surface 7 = 1 ,2 ,3 ,4 Aternate ;

Phys ica l L ine ( 3 ) = 3 ;Phys ica l L ine ( 2 ) = 1 , 2 , 4 ;

Phys ica l Surface ( 1 ) = 7 ;

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 39: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Main.py (1/)# −∗− coding : u t f 8 −∗−

impor t numpy as npimpor t getfem as gf

impor t sc ipy . sparse as sp_spimpor t sc ipy . sparse . l i n a l g as sp_sp_ l in

impor t m a t p l o t l i b as mplimpor t enthought . mayavi . mlab as mlab

impor t Mail lageEtEF , Bords , Matr ices , D i r i c h l e t , Visu_0

impor t t ime , os

# ======================================================================

rho_f = 1.0 ; c_f = 100.0rho_s = 100.0 ; c_s = 1.0 ; eps i l on = 1.e−3

dt = 5 .e−5 ; dt2 = dt∗dt ; duree = 0.05 ; i ter_max = i n t ( duree / d t )

f i l e _ g e o = ’Mesh / BassinVide_P1 . geo ’f i le_msh = ’Mesh / BassinVide_P1 . msh ’

p r i n t " "p r i n t " Creat ion du mai l lage (gmsh ) "p r i n t ’ gmsh −2 ’+ f i l e _ g e o + ’ −o ’+ f i le_msh + ’ > trace_gmsh ’os . system ( ’ gmsh −2 ’+ f i l e _ g e o + ’ −o ’+ f i le_msh + ’ > trace_gmsh ’ )

Label_s = 3 ; Label_Autres_Bords = 2

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 40: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Main.py (2/)[ nsom_f , m_f , mfu_f , mim_f , x_f , y_f , z_f , t r i _ f ] = . . .Mai l lageEtEF . Mai l lageEtEF ( f i le_msh )[ Numeros_s , index_s , nsom_s , x_s , y_s , z_s , Numeros_Diri ] = . . .Bords . Bords ( mfu_f , nsom_f , Label_s , Label_Autres_Bords , x_f , y_f , z_ f )[ M_f , M_s , K_f , K_s , M_sc1 , M_sc2 ] = . . .Mat r ices . Matr ices ( mim_f , mfu_f , nsom_f , Label_s , Numeros_s )

CoeffM_s = 2∗eps i l on∗rho_s / rho_f ; CoeffK_s = 2∗eps i l on∗c_s∗∗2/ rho_f

M = sp_sp . bmat ( [ [ M_f ,None ] , [ None , CoeffM_s∗M_s ] ] )K = sp_sp . bmat ( [ [ c_ f∗c_f∗K_f , None ] , [ None , CoeffK_s∗K_s ] ] )C = sp_sp . bmat ( [ [ None ,−M_sc2 ] , [ M_sc1 , None ] ] )

p r i n t " "p r i n t " On f i n a l i s e les matr ices l i e e s au schema numerique "p r i n t " "p r i n t " M∗d2u^k / dt2 + C∗du^k / d t + L∗u^k = 0"p r i n t " "p r i n t " d2u^k / dt2 = ( u ^ ( k+1) − 2∗u^k + u ^ ( k−1) ) / de l taT ^2"p r i n t " du^k / d t = ( u ^ ( k+1) − u ^ ( k−1) ) / ( 2∗ DeltaT ) "p r i n t " u^k = ( u ^ ( k+1) + u ^ ( k−1) ) / 2 "p r i n t " "p r i n t " A1∗u ^ ( k+1) = A2∗u^k + A3∗u ^ ( k−1)"

A1 = M + 0.5∗C∗dt + 0.5∗K∗dt2A2 = 2.∗MA3 = −1.∗M + 0.5∗C∗dt − 0.5∗K∗dt2

A1 = D i r i c h l e t . D i r i c h l e t ( nsom_f , nsom_s , A1 , Numeros_Diri )

A1 = A1 . tocsc ( ) ; A2 = A2 . tocsc ( ) ; A3 = A3 . tocsc ( )

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 41: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Main.py (3/)# Condi t ions i n i t i a l e s :

X0 = np . zeros ( nsom_f + nsom_s )X1 = np . zeros ( nsom_f + nsom_s )

chaine = ’ exp(−100∗((x[0]− %.9 f )∗∗2 + ( x[1]− %.9 f )∗∗2 + . . .( x [2]− %.9 f )∗∗2) ) ’ % ( 0 . , 1 . 5 , 0 . )

va l = mfu_f . eva l ( chaine )

X0 [ 0 : nsom_f ] = va l ; X1 [ 0 : nsom_f ] = va l

# Pré pa ra t i on à l a v i s u a l i s a t i o n :

X_f = X1 [ 0 : nsom_f ]X_s = X1 [ nsom_f : nsom_f + nsom_s ] ; X_s_ord = X_s [ index_s ]

[ m lab_ t r i , mlab_surf ] = Visu_0 . Visu_0 ( x_f , y_f , z_f , t r i _ f , . . .X_f , X_s_ord , x_s , y_s , z_s )

p r i n t " "p r i n t " F a c t o r i s a t i o n de l a matr ice A1 "

t1So lver = t ime . c lock ( ) ;

solveA1 = sp_sp_ l in . f a c t o r i z e d (A1)

p r i n t " en " , t ime . c lock ( ) − t 1So lve r ;

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 42: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Main.py (4/)

p r i n t " "p r i n t " On resout "

T = 0.0 ; compteur = 0 ; compteur_max = 10 ; k = 0

f o r i i n xrange (0 , i ter_max ) :

T = T + dt

b = A2 . _mul_vector (X1) + A3 . _mul_vector (X0)

b [ Numeros_Diri ] = 0.0∗b [ Numeros_Diri ]

X2 = solveA1 ( b )

X_s = X2 [ nsom_f : nsom_f + nsom_s ]X_f = X2 [ 0 : nsom_f ]

. . .

. . .

. . .

X0 = X1X1 = X2

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 43: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Main.py (5/). . .compteur = compteur + 1

i f ( compteur == compteur_max ) :

compteur = 0

X1_point = (X2−X0) / ( 2∗ dt )Ec = 0.5∗np . dot ( np . t ranspose ( X1_point ) ,M. _mul_vector ( X1_point ) )Ep = 0.5∗np . dot ( np . t ranspose (X1 ) ,K . _mul_vector (X1 ) )

m lab_ t r i . se t ( sca la rs = X_f )

y_s_new = y_s + 100.0∗X_s [ index_s ]mlab_surf . se t ( y = y_s_new )

chaine = ’ T = %.4 f , X_f : %.1e <=> %.1e , X_s : %.1e <=> %.1e , . . .E : %.1 f . ’ % (T , X_f . min ( ) , X_f .max ( ) , X_s . min ( ) , X_s .max ( ) , Ec + Ep)

mlab . t i t l e ( chaine , s ize = 0.25 , he igh t = 0.93 )

k = k + 1

i f ( k < 10) : mlab . save f ig ( ’ Trace / V_000 ’+ s t r ( k ) + ’ . png ’ )i f (10 <= k ) and ( k < 100 ) : mlab . save f ig ( ’ Trace / V_00 ’+ s t r ( k ) + ’ . png ’ )i f (100 <= k ) and ( k < 1000 ) : mlab . save f ig ( ’ Trace / V_0 ’+ s t r ( k ) + ’ . png ’ )i f (1000 <= k ) and ( k < 10000 ) : mlab . save f ig ( ’ Trace / V_ ’+ s t r ( k ) + ’ . png ’ )

. . .# Creat ion d ’ une videoos . system ( ’ mencoder mf : / / Trace / V_∗ . png −mf type=png :w=700:h=368: fps=5 . . .−ovc lavc −l avcop ts vcodec=mpeg4 −oac copy −o Videos / Video . avi ’ )

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 44: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => MaillageEtEF.py (1/2)# −∗− coding : u t f 8 −∗−

impor t numpy as npimpor t getfem as gf

impor t sc ipy . sparse as sp_sp

def Mai l lageEtEF ( f i le_msh ) :

p r i n t " "p r i n t " Impor ta t i on du mai l lage ( format gmsh ) "

m = gf . Mesh ( ’ import ’ , ’ gmsh ’ , f i le_msh )

p r i n t " Les d i f f é ren ts l a b e l s : " , m. reg ions ( )

p r i n t " "p r i n t " Cré a t i on des ob je t s E . F . "#Creat ion d ’ un ob je t MeshFemmfu = gf .MeshFem(m, 1 )

# A f f e c t a t i o n de FEM_P1 a tous les convexesmfu . set_fem ( g f .Fem( ’FEM_PK( 2 , 1 ) ’ ) )

p r i n t " Les f c t i o n s de forme : " , g f .Fem( ’FEM_PK( 2 , 1 ) ’ ) . p o l y _ s t r ( )

#Methode d ’ i n t e g r a t i o n u t i l i s emim = gf . MeshIm (m, g f . I n teg ( ’ IM_TRIANGLE ( 2 ) ’ ) )

# ==========================================

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 45: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => MaillageEtEF.py (2/2)

# ==========================================

nsom = mfu . nbdof ( ) ; n e l t = m. nbcvs ( )

p r i n t " "p r i n t " On recupere l a c o n n e c t i v i t e ( u t i l e pour l a v isu ) "

[ pid , cvx ] = mfu . bas ic_dof_f rom_cvid ( )

t r i a n g l e s = [ np . ar ray ( [ p id [ cvx [ i ] : cvx [ i + 1 ] ] [ 0 ] , . . .p id [ cvx [ i ] : cvx [ i + 1 ] ] [ 1 ] , p id [ cvx [ i ] : cvx [ i + 1 ] ] [ 2 ] ] ) f o r i i n xrange ( n e l t ) ]

P _ t r i = mfu . basic_dof_nodes ( )

x = P _ t r i [ 0 ] [ : ]y = P _ t r i [ 1 ] [ : ]z = P _ t r i [ 2 ] [ : ]

r e t u r n nsom , m, mfu , mim, x , y , z , t r i a n g l e s

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 46: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Bords.py# −∗− coding : u t f 8 −∗−impor t numpy as np

def Bords ( mfu_f , nsom_f , Label_s , Label_Autres_Bords , x_f , y_f , z_ f ) :

p r i n t " On recupere les noeuds du bord haut "

Numeros_s = mfu_f . dof_on_region ( Label_s )nsom_s = Numeros_s . s ize

index_s = np . a rgso r t ( x_ f [ Numeros_s ] )x_s = x_f [ Numeros_s [ index_s ] ]y_s = y_f [ Numeros_s [ index_s ] ]z_s = z_f [ Numeros_s [ index_s ] ]

p r i n t " Le bord haut es t c o n s t i t u é de " , nsom_s , " sommets "p r i n t " d ’ abscisses ordonnées : " , x_s . min ( ) , x_s .max ( )p r i n t " d ’ ordonnées : " , y_s . min ( ) , y_s .max ( )

p r i n t " On recupere les noeuds des aut res bords ( D i r i c h l e t = 0 ) "

Numeros_Diri_0 = mfu_f . dof_on_region ( Label_Autres_Bords )

dim = Numeros_Diri_0 . s izeNumeros_Diri = np . zeros ( dim + 2 , dtype = ’ in t64 ’ )Numeros_Diri [ 0 : dim ] = Numeros_Diri_0

Numeros_Diri [ dim ] = nsom_f + index_s [ 0 ]Numeros_Diri [ dim +1] = nsom_f + index_s [ nsom_s−1]

r e t u r n Numeros_s , index_s , nsom_s , x_s , y_s , z_s , Numeros_Diri

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 47: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Matrices.py# −∗− coding : u t f 8 −∗−impor t getfem as gfimpor t Spmat2csc

def Matr ices ( mim_f , mfu_f , nsom_f , Label_s , Numeros_s ) :

p r i n t " Ca lcu l des matr ices de masse"

M_f = g f . asm( ’ volumic ’ , . . .’M(#1 ,#1)+=sym(comp( Base ( # 1 ) . Base ( # 1 ) ) ( : , : ) ) ’ , mim_f , mfu_f )M_s = gf . asm( ’ boundary ’ , Label_s , . . .’M(#1 ,#1)+=sym(comp( Base ( # 1 ) . Base ( # 1 ) ) ( : , : ) ) ’ , mim_f , mfu_f )

p r i n t " Ca lcu l des matr ices de ra ideu r "

K_f = g f . asm( ’ volumic ’ , . . .’M(#1 ,#1)+= sym(comp( Grad ( # 1 ) . Grad ( # 1 ) ) ( : , i , : , i ) ) ’ , mim_f , mfu_f )K_s = gf . asm( ’ boundary ’ , Label_s , . . .’M(#1 ,#1)+= sym(comp( Grad ( # 1 ) . Grad ( # 1 ) ) ( : , 1 , : , 1 ) ) ’ , mim_f , mfu_f )

M_f = Spmat2csc . Spmat2csc ( M_f , nsom_f ) ; M_s = Spmat2csc . Spmat2csc (M_s , nsom_f )K_f = Spmat2csc . Spmat2csc ( K_f , nsom_f ) ; K_s = Spmat2csc . Spmat2csc ( K_s , nsom_f )

M_sc1 = M_s . copy ( ) ; M_sc2 = M_s . copy ( )

M_sc1 = M_sc1 [ Numeros_s , : ] ; M_sc2 = M_sc2 [ : , Numeros_s ]

M_s = M_s [ Numeros_s , : ] ; M_s = M_s [ : , Numeros_s ]K_s = K_s [ Numeros_s , : ] ; K_s = K_s [ : , Numeros_s ]

r e t u r n M_f , M_s , K_f , K_s , M_sc1 , M_sc2

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 48: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Spmat2csc.py

# −∗− coding : u t f 8 −∗−

impor t sc ipy . sparse as sc

def Spmat2csc (A,N) :

[ Ndiag , Ind ices ] = A. csc_ind ( ) ; VA = A. csc_val ( )A = sc . csc_matr ix ( ( VA, Ind ices , Ndiag ) , shape = (N,N) )

r e t u r n A

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 49: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Dirichlet.py (1/2)

# −∗− coding : u t f 8 −∗−impor t numpy as npimpor t sc ipy . sparse as sp_sp

def D i r i c h l e t ( nsom_f , nsom_s , A1 , Numeros_Diri ) :

N = nsom_f+nsom_sZeros = np . zeros (N)Tout = np . arange (N)

p r i n t " D i r i c h l e t nb . sommets : " , Numeros_Diri . s i ze

f o r k i n xrange (0 , Numeros_Diri . s i ze ) :# On enlève les termes sur les colonnes ( sauf sur l a diag )Uns = np . ones (N)row = Uns∗Numeros_Diri [ k ]co l = Toutdata = Unsdata [ Numeros_Diri [ k ] ] = 0.0A = sp_sp . coo_matr ix ( ( data , ( row , co l ) ) , shape =(N,N) )A1 = A1 − A1 . m u l t i p l y (A)

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 50: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Dirichlet.py (2/2)

# On enlève les termes sur les l i g n e s ( sauf sur l a diag )Uns = np . ones (N)row = Toutco l = Uns∗Numeros_Diri [ k ]data = Unsdata [ Numeros_Diri [ k ] ] = 0.0A = sp_sp . coo_matr ix ( ( data , ( row , co l ) ) , shape =(N,N) )A1 = A1 − A1 . m u l t i p l y (A)

# On met les uns sur l a diag ( pour les sommets D i r i c h l e t )Uns = np . ones (N)row = Numeros_Dirico l = Numeros_Diridata = Uns [ Numeros_Diri ]A = sp_sp . coo_matr ix ( ( data , ( row , co l ) ) , shape =(N,N) )A1 = A1 − A1 . m u l t i p l y (A) + A

r e t u r n A1

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 51: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Code => Visu_0.py# −∗− coding : u t f 8 −∗−impor t enthought . mayavi . mlab as mlab

def Visu_0 ( x_f , y_f , z_f , t r i _ f , X_f , X_s_ord , x_s , y_s , z_s ) :

mlab . f i g u r e ( s ize =(700 ,400))

maxV = 0.25∗X_f .max ( ) ; minV = −1.0∗maxV

ml = mlab . t r iangular_mesh ( x_f , y_f , z_f , t r i _ f , sca la rs = X_f , . . .colormap = ’ j e t ’ , vmin = minV , vmax = maxV)

mlab . axes ( nb_labels = 3 , x _ a x i s _ v i s i b i l i t y = True , . . .y _ a x i s _ v i s i b i l i t y = False , z _ a x i s _ v i s i b i l i t y = True )

mlab . co lo rba r ( o r i e n t a t i o n = ’ v e r t i c a l ’ )

y_s_new = y_s + X_s_ordml2 = mlab . p lo t3d ( x_s , y_s_new , z_s , X_s_ord )

x0 = −0.4 ; y0 = 1.0 ; z0 = 4.0mlab . view ( 0 . , 0 . , 1 . , [ x0 , y0 , z0 ] ) # pour r e p o s i t i o n n e r l e mai l lage

T i t r e = ’ X_f : %.2e <=> %.2e ’ % ( minV , maxV)mlab . t i t l e ( T i t r e , s i ze = 0.25 , he igh t = 0.93 )mlab . save f ig ( " Trace / V_0000 . png " )

m lab_ t r i = ml . mlab_source # pour changer les va leurs associeesmlab_surf = ml2 . mlab_source # pour changer les va leurs associees

r e t u r n mlab_ t r i , mlab_surf

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam

Page 52: Python, Scipy - f2py - maths.cnam.frmaths.cnam.fr/Membres/wilk/Cours_Python/Expose_Python_Scipy-f2… · Python, Scipy - f2py - ... O. Wilk Scipy Survol Plus en détails scipy.optimize

Python,Scipy - f2py - ...

O. Wilk

ScipySurvol

Plus en détails

scipy.optimize

scipy.linalg

scipy.sparse

f2pyFortran

C

Une applicationCaisson acoustique

Maillage

Main.py

MaillageEtEF.py

Bords.py

Matrices.py

Spmat2csc.py

Dirichlet.py

Visu_0.py

Résultat

Résultat

O. Wilk: Python, Scipy - f2py - ... Calcul scientifique/Math/Cnam