rL i4dZddlZddlmZddlmZddlmZddl m Z dZ Gdd Z Gd d e Z Gd d e ZGddZGddZGddeZGddeZGddeZGddZd"dZdZdZdZdZdZd Zd!Zy)#z( Interpolation inside triangular grids. N)_api) Triangulation) TriFinder) TriAnalyzer)TriInterpolatorLinearTriInterpolatorCubicTriInterpolatorc2eZdZdZddZdZdZ d dZdZy) ram Abstract base class for classes used to interpolate on a triangular grid. Derived classes implement the following methods: - ``__call__(x, y)``, where x, y are array-like point coordinates of the same shape, and that returns a masked array of the same shape containing the interpolated z-values. - ``gradient(x, y)``, where x, y are array-like point coordinates of the same shape, and that returns a list of 2 masked arrays of the same shape containing the 2 derivatives of the interpolator (derivatives of interpolated z values with respect to x and y). Nctjt|||_t j ||_|j j|jjjk7r tdtjtdf||xs|jj|_ d|_ d|_d|_y)N) triangulationz=z array must have same length as triangulation x and y arrays) trifinder?)rcheck_isinstancer_triangulationnpasarray_zshapex ValueErrorr get_trifinder _trifinder_unit_x_unit_y _tri_renum)selfr zr s d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/matplotlib/tri/_triinterpolate.py__init__zTriInterpolator.__init__!s m=I+**Q- 77==D//1177 7-. . y$/9E#Jt':':'H'H'J   a Returns a masked array containing interpolated values at the specified (x, y) points. Parameters ---------- x, y : array-like x and y coordinates of the same shape and any number of dimensions. Returns ------- np.ma.array Masked array of the same shape as *x* and *y*; values corresponding to (*x*, *y*) points outside of the triangulation are masked out. a Returns a list of 2 masked arrays containing interpolated derivatives at the specified (x, y) points. Parameters ---------- x, y : array-like x and y coordinates of the same shape and any number of dimensions. Returns ------- dzdx, dzdy : np.ma.array 2 masked arrays of the same shape as *x* and *y*; values corresponding to (x, y) points outside of the triangulation are masked out. The first returned array contains the values of :math:`\frac{\partial z}{\partial x}` and the second those of :math:`\frac{\partial z}{\partial y}`. ctj|tj}tj|tj}|j}|j|jk7r%t d|jd|jtj |}tj |}||j z }||jz }tj|}||j||}n?|j|k7rt d|jd|tj |}|dk7} |j|| } n|j|| } || } || } g} |D]} ddd d |}d d |j z d |jz g|}tj|tj}tj|| <|j|| | | |z|| <| tjj!|j#|d gz } | S#t$r}t d |d}~wwxYw)a Versatile (private) method defined for all TriInterpolators. :meth:`_interpolate_multikeys` is a wrapper around method :meth:`_interpolate_single_key` (to be defined in the child subclasses). :meth:`_interpolate_single_key actually performs the interpolation, but only for 1-dimensional inputs and at valid locations (inside unmasked triangles of the triangulation). The purpose of :meth:`_interpolate_multikeys` is to implement the following common tasks needed in all subclasses implementations: - calculation of containing triangles - dealing with more than one interpolation request at the same location (e.g., if the 2 derivatives are requested, it is unnecessary to compute the containing triangles twice) - scaling according to self._unit_x, self._unit_y - dealing with points outside of the grid (with fill value np.nan) - dealing with multi-dimensional *x*, *y* arrays: flattening for :meth:`_interpolate_params` call and final reshaping. (Note that np.vectorize could do most of those things very well for you, but it does it by function evaluations over successive tuples of the input arrays. Therefore, this tends to be more time-consuming than using optimized numpy functions - e.g., np.dot - which can be used easily on the flattened inputs, in the child-subclass methods :meth:`_interpolate_single_key`.) It is guaranteed that the calls to :meth:`_interpolate_single_key` will be done with flattened (1-d) array-like input parameters *x*, *y* and with flattened, valid `tri_index` arrays (no -1 index allowed). Parameters ---------- x, y : array-like x and y coordinates where interpolated values are requested. tri_index : array-like of int, optional Array of the containing triangle indices, same shape as *x* and *y*. Defaults to None. If None, these indices will be computed by a TriFinder instance. (Note: For point outside the grid, tri_index[ipt] shall be -1). return_keys : tuple of keys from {'z', 'dzdx', 'dzdy'} Defines the interpolation arrays to return, and in which order. Returns ------- list of arrays Each array-like contains the expected interpolated values in the order defined by *return_keys* parameter. dtypez'x and y shall have same shapes. Given: z and NzItri_index array is provided and shall have same shape as x and y. Given: rrdzdxdzdyzT\\>778$  1-I&( ; 'uVH677+I? ?? "'0O"ooi.@AO7#7#% OJ D%&1=jI DLL"T\\/:<HEhhxrzz:G "GWH #;;OWg ?AF GGG  BEE(()@u(MN NC! O$  D ":;@CD Ds H77 I I  Ictd)a+ Interpolate at points belonging to the triangulation (inside an unmasked triangles). Parameters ---------- return_key : {'z', 'dzdx', 'dzdy'} The requested values (z or its derivatives). tri_index : 1D int array Valid triangle index (cannot be -1). x, y : 1D arrays, same shape as `tri_index` Valid locations where interpolation is requested. Returns ------- 1-d array Returned array of the same size as *tri_index* zCTriInterpolator subclassesshould implement _interpolate_single_key!NotImplementedErrorrrAr6rr5s rr1z'TriInterpolator._interpolate_single_keys&"#NO Or N)Nr) __name__ __module__ __qualname____doc__r_docstring__call___docstringgradientrFr1r rrrs1": $ ,6:+1iVOr rczeZdZdZdfd ZdZeje_dZeje_dZ xZ S)ra Linear interpolator on a triangular grid. Each triangle is represented by a plane so that an interpolated value at point (x, y) lies on the plane of the triangle containing (x, y). Interpolated values are therefore continuous across the triangulation, but their first derivatives are discontinuous at edges between triangles. Parameters ---------- triangulation : `~matplotlib.tri.Triangulation` The triangulation to interpolate over. z : (npoints,) array-like Array of values, defined at grid points, to interpolate between. trifinder : `~matplotlib.tri.TriFinder`, optional If this is not specified, the Triangulation's default TriFinder will be used by calling `.Triangulation.get_trifinder`. Methods ------- `__call__` (x, y) : Returns interpolated values at (x, y) points. `gradient` (x, y) : Returns interpolated derivatives at (x, y) points. c|t|||||jj|j|_yrK)superrrcalculate_plane_coefficientsr_plane_coefficients)rr rr __class__s rrzLinearTriInterpolator.__init__s6 95    < >),##Aq(3jj#nnTYYy1:  ))==sC! !""8,A))BBq#s$DV#Aq!G}$Aq!G}$r c|dk(r*| tdt||}|j S|dk(rt|}|j St|}|j S)aR Compute and return nodal dofs according to kind. Parameters ---------- kind : {'min_E', 'geom', 'user'} Choice of the _DOF_estimator subclass to estimate the gradient. dz : tuple of array-likes (dzdx, dzdy), optional Used only if *kind*=user; in this case passed to the :class:`_DOF_estimator_user`. Returns ------- array-like, shape (npts, 2) Estimation of the gradient at triangulation nodes (stored as degree of freedoms of reduced-HCT triangle elements). rlzQFor a CubicTriInterpolator with *kind*='user', a valid *dz* argument is expected.rprm)r_DOF_estimator_user_DOF_estimator_geom_DOF_estimator_min_Ecompute_dof_from_df)rrorqTEs rr{z!CubicTriInterpolator._compute_dofs$ 6>z "9::%Tb1B %%'' V^$T*B%%''&d+B%%''r c|jdz }|dddddf|dddddfz }|dddddf|dddddfz }tj||gd}t|}tj||gd|dddddfz }||z} t | } |ttj ||z} | | z} t d| ddddfz | ddddfz g| ddddfg| ddddfgg} | S)a2 Fast (vectorized) function to compute barycentric coordinates alpha. Parameters ---------- x, y : array-like of dim 1 (shape (nx,)) Coordinates of the points whose points barycentric coordinates are requested. tris_pts : array like of dim 3 (shape: (nx, 3, 2)) Coordinates of the containing triangles apexes. Returns ------- array of dim 2 (shape (nx, 3)) Barycentric coordinates of the points inside the containing triangles. r&Nr%rr$r)ndimrstack_transpose_vectorized_pseudo_inv22sym_vectorizedr_to_matrix_vectorized)rr5rrababTabOMmetric metric_invCovarksirs rrz#CubicTriInterpolator._get_alpha_vecs"&}}Q Q1W Aq 1 1 Q1W Aq 1 1hh1vB' "3 ' XXq!f1 %Aq(9 9c 18 *2>>"d+CDD5 % s1a7|^C1aL ( )C1aL>C1aL>'KL r ctj|dddddf|dddddfz }tj|dddddf|dddddfz }t|dddf|dddfg|dddf|dddfgg}|S)a Fast (vectorized) function to compute triangle jacobian matrix. Parameters ---------- tris_pts : array like of dim 3 (shape: (nx, 3, 2)) Coordinates of the containing triangles apexes. Returns ------- array of dim 3 (shape (nx, 2, 2)) Barycentric coordinates of the points inside the containing triangles. J[itri, :, :] is the jacobian matrix at apex 0 of the triangle itri, so that the following (matrix) relationship holds: [dz/dksi] = [J] x [dz/dx] with x: global coordinates ksi: element parametric coordinates in triangle first apex local basis. Nr%rr&)rarrayr)rrrrs rrz"CubicTriInterpolator._get_jacobians, HHXaAg&!Q'):: ; HHXaAg&!Q'):: ; !AadGQq!tW#5$%adGQq!tW#5#7 8r ctj|dddddf|dddddfz d}tj|dddddf|dddddfz d}tj|dddddf|dddddfz d}t||zddddf}t||zddddf}t||zddddf}t||z |z g||z |z g||z |z ggS)ax Compute triangle eccentricities. Parameters ---------- tris_pts : array like of dim 3 (shape: (nx, 3, 2)) Coordinates of the triangles apexes. Returns ------- array like of dim 2 (shape: (nx, 3)) The so-called eccentricity parameters [1] needed for HCT triangular element. Nr&r%rr)rrrr)rrrcdot_adot_bdot_cs rryz0CubicTriInterpolator._compute_tri_eccentricities!s NN8Aq!G,x1a/@@q I NN8Aq!G,x1a/@@q I NN8Aq!G,x1a/@@q I'q)A-q!Qw7&q)A-q!Qw7&q)A-q!Qw7%e u'<&=(-e u'<&=(-e u'<&=&?@ @r )rnNNrK)rMrNrOrPrr_rrQrcrRr1r{ staticmethodrrryrhris@rr r s[xBF'7RB'99HI'99H%$(<$$L6@@r r c eZdZdZej gdgdgdgdgdgdgdgd gd g Zej gd gd gd gd gd gd gdgd gdg Zej gdgd gdgd gd gd gdgdgd g Zej gdgdgd gdgd gdgd gd gd g Z ej ddgddgddgddgddgddggZ ej gdgdgdgdgdgdgdgdgd g Z d!Z ej gd"gd#gd$gd%gd&gd'gd(gd)gd*g ej+Zejd!gej+d,z Zej gdgdgd-gZej ddgddggZej ddgddggZd.Zd/Zd0Zd1Zd2Zd6d3Zd4Zy5)7r}aN Implementation of reduced HCT triangular element with explicit shape functions. Computes z, dz, d2z and the element stiffness matrix for bending energy: E(f) = integral( (d2z/dx2 + d2z/dy2)**2 dA) *** Reference for the shape functions: *** [1] Basis functions for general Hsieh-Clough-Tocher _triangles, complete or reduced. Michel Bernadou, Kamal Hassan International Journal for Numerical Methods in Engineering. 17(5):784 - 789. 2.01 *** Element description: *** 9 dofs: z and dz given at 3 apex C1 (conform) ) rr@rrrrrr) пrr??rrrrr) rrrrrrrrrr) rrrr@rrrr) rrrr?rrrrr) rrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rr?rrrrr) rrr?rrrrrr) rrrrrrrrrr) rrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rrrrrrrrrr) rrrrrrrrrrrrr)rrr)rrr)rrr)rrr)rr)rrr )qq?qq?qq?)rrr)98?rr)rrr)rrr)rrr)rrr)rrr)rrrr"g"@)rr@c tj|ddddf}t|| d}t|| d}|ddddf}|ddddf}|ddddf} ||z} ||z} | | z} t| |zg| |zg| | zg| | zg| |zg| |zg| | zg| |zg| |zg||z| zgg } |j| z}|t |ddddf|j | zz }|t |ddddf|j| zz }|t |ddddf|j| zz }t|d|zd}||zddddfS)a Parameters ---------- alpha : is a (N x 3 x 1) array (array of column-matrices) of barycentric coordinates, ecc : is a (N x 3 x 1) array (array of column-matrices) of triangle eccentricities, dofs : is a (N x 1 x 9) arrays (arrays of row-matrices) of computed degrees of freedom. Returns ------- Returns the N-array of interpolated function values. r%rNrr&) rargmin_roll_vectorizedrM_scalar_vectorizedM0M1M2)rrrdofssubtrirErr5rx_sqy_sqz_sqVprodss rrz'_ReducedHCT_Element.get_function_valuess5q)!Q$/uvgA6 S6' 2 1aL 1aL 1aLsss ! !VHtAvha46(T!VHtAvh !VHtAvha1Q3q5'#3 4vvz "1Q1W:tww{;; "1Q1W:tww{;; "1Q1W:tww{;; T1V8! 4q!Q'""r cntj|ddddf}t|| d}t|| d}|ddddf}|ddddf} |ddddf} ||z} | | z} | | z} td| zd| zgd| zdgdd| zgd |z| zd |z| z| zgd |z| z| zd |z| zgd |z| z| z | gd | z| z| g| d | z| zg| d |z| z| z g|| z| | zz || z| | zz gg }|t |j |dd z}|j |z}|t|ddddf|j|zz }|t|ddddf|j|zz }|t|ddddf|j|zz }t|d |zd}||z}t|}|t|z}|S) a Parameters ---------- *alpha* is a (N x 3 x 1) array (array of column-matrices of barycentric coordinates) *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at triangle first apex) *ecc* is a (N x 3 x 1) array (array of column-matrices of triangle eccentricities) *dofs* is a (N x 1 x 9) arrays (arrays of row-matrices) of computed degrees of freedom. Returns ------- Returns the values of interpolated function derivatives [dz/dx, dz/dy] in global coordinates at locations alpha, as a column-matrices of shape (N x 2 x 1). r%rNrr&rrrrr block_sizerr) rrrr_extract_submatrices rotate_dVrrrrr_safe_inv22_vectorizedr)rrrrrrrrrr5rrrrdVrdsdksidfdksiJ_invdfdxs rrz,_ReducedHCT_Element.get_function_derivativess8&5q)!Q$/uvgA6 S6' 2 1aL 1aL 1aLsss "Ts4x (T'R (4 (a%'3q574< ( U1WT\Aa (d1fTk4% (a464 (Aa (EBqDF4K (A#ac'!AaC ( $* +& NNFqq::vv{ "1Q1W:tww|<< "1Q1W:tww|<< "1Q1W:tww|<<!$&q9'q),V44 r cr|j||}||z}|j|}||z}t|S)a Parameters ---------- *alpha* is a (N x 3 x 1) array (array of column-matrices) of barycentric coordinates *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at triangle first apex) *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle eccentricities *dofs* is a (N x 1 x 9) arrays (arrays of row-matrices) of computed degrees of freedom. Returns ------- Returns the values of interpolated function 2nd-derivatives [d2z/dx2, d2z/dy2, d2z/dxdy] in global coordinates at locations alpha, as a column-matrices of shape (N x 3 x 1). )get_d2Sidksij2get_Hrot_from_Jr) rrrrrd2sdksi2d2fdksi2H_rotd2fdx2s rget_function_hessiansz)_ReducedHCT_Element.get_function_hessianssD&&&uc2(?$$Q'E!$V,,r cBtj|ddddf}t|| d}t|| d}|ddddf}|ddddf}|ddddf}td|zd|zd|zgd|zddgdd|zdgd|zd|zd |zz d|zd|zz gd|zd |zz d|zd|zd|zz gd|zd |zz dd |zgd|zdd|zgdd|zd|zgdd|zd |zz d |zgd |zd |z||z |z gg } | t |j |d d z} |j | z} | t|ddddf|j| zz } | t|ddddf|j| zz } | t|ddddf|j| zz } t| d |zd} | S) a Parameters ---------- *alpha* is a (N x 3 x 1) array (array of column-matrices) of barycentric coordinates *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle eccentricities Returns ------- Returns the arrays d2sdksi2 (N x 3 x 1) Hessian of shape functions expressed in covariant coordinates in first apex basis. r%rNrr&g@rrg@rrr) rrrrr rotate_d2Vrrrrr) rrrrrrrr5rd2Vrrs rrz"_ReducedHCT_Element.get_d2Sidksij2s5q)!Q$/uvgA6 S6' 2 1aL 1aL 1aL#Q$RT1 -Q$r" -RT" -Q$1RT 2a419 - T"Q$YRT2a419 - T"Q$Yrs1u -Q$r1 -RT1 -1RT s1u -QCEqs1u - %/ 0( OOV;;vv| "1Q1W:tww}== "1Q1W:tww}== "1Q1W:tww}==#D!F(;r cdtj|d}|j|z}|j|z}tj|ddgtj }d|ddddf<d|ddddf<d|ddddf<||ddddddf<||ddddddf<||ddd dd df<|j |d \}}tj|ddgtj } |j} |j} t|jD]} tj| | ddf|j|d} tj| d } | | }|j| |}||z}| |||jzt!|zzz } t!|| z|z} t#|| S) a Parameters ---------- *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at triangle first apex) *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle eccentricities Returns ------- Returns the element K matrices for bending energy expressed in GLOBAL nodal coordinates. K_ij = integral [ (d2zi/dx2 + d2zi/dy2) * (d2zj/dx2 + d2zj/dy2) dA] tri_J is needed to rotate dofs from local basis to global basis rrr"r%NrT) return_arear&)rr-J0_to_J1J0_to_J2zerosr+rgauss_w gauss_ptsrangen_gausstiler4rrrrr)rrrnJ1J2DOF_rotrareaKweightsptsigaussrweight d2Skdksi2d2Skdx2s rget_bending_matricesz(_ReducedHCT_Element.get_bending_matrices>s GGCO]]Q  ]]Q ((Aq!9BJJ71a1a1a 1Q3! !1Q3! !1Q3! **1$*? t HHaAYbjj 1,,nnDLL) NFGGC NA.66q!L!Aq!G*Qq!QwZ/!Aq!G*Qq!QwZ2GGHD$; r c tj|d}tj|tj}tj|dtj}gd}gd} t ||dddfdz|dddfdzdz||dddfdz|dddfdzdz||dddfdz|dddfdzdzg g} tj |d dgtj} t| | z} | | z} |j||}tj|tj|||}tj| tj|||}tj| tj|||}|tj||| }tj|d }||zdddddf }| tj|dg|dddddf}tjtj|tj| }||||fS) aF Build K and F for the following elliptic formulation: minimization of curvature energy with value of function at node imposed and derivatives 'free'. Build the global Kff matrix in cco format. Build the full Ff vec Ff = - Kfc x Uc. Parameters ---------- *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at triangle first apex) *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle eccentricities *triangles* is a (N x 3) array of nodes indexes. *Uc* is (N x 3) array of imposed displacements at nodes Returns ------- (Kff_rows, Kff_cols, Kff_vals) Kff matrix in coo format - Duplicate (row, col) entries must be summed. Ff: force vector - dim npts * 3 rr"r$)r%r&rr)rrrNr&r%rrr) rr-arangeint32fullronesrr r,ix_rbincount)rrr trianglesUcntri vec_range c_indicesf_dofc_dof f_dof_indicesexpand_indices f_row_indices f_col_indicesK_elemKff_valsKff_rowsKff_colsKfc_elemUc_elemFf_elem Ff_indicesFfs rget_Kff_and_Ffz"_ReducedHCT_Element.get_Kff_and_Ffs0wwsAIId"((3 GGD"BHH5 ". yAq()AqD/!*;A*= yAq()AqD/!*;A*= yAq()AqD/!*;A*=0?/@A $1RXX>-n}.LM &6 **1c288F266)UE#BCD88M"&&E5*IJK88M"&&E5*IJK"&&E59:..!,w&1a00"266)aS%#@A!Q'J [[*-rxx7H I8R//r N)F)rMrNrOrPrrrrrrrrrr+rrrrrrrrrrr rr-rSr rr}r}Bs* GGGGGGGGG I JA HHHHHHHHH J KB FFFFFFFFF H IB FFFFFFFFF H IBB*Rj*sCj*Rj23I<''HIJG5555555557?AjjJIbggqc,r1G , l;t')""5&!4r c |jd}tj|dgtj}tj |z}tj |z}|tj|dddddfdz}|tj|dddddfdz}|tj|dddddfdz} t|ddddf|ddddf| ddddfg|ddddf|ddddf| ddddfgg} ||ddddd f<| dddf|dddd d f<| dddf|ddddd f<|S) a\ Compute the dof vector of a triangle, from the value of f, df and of the local Jacobian at each node. Parameters ---------- tri_z : shape (3,) array f nodal values. tri_dz : shape (3, 2) array df/dx, df/dy nodal values. J Jacobian matrix in local basis of apex 0. Returns ------- dof : shape (9,) array For each apex ``iapex``:: dof[iapex*3+0] = f(Ai) dof[iapex*3+1] = df(Ai).(AiAi+) dof[iapex*3+2] = df(Ai).(AiAi-) rrr"Nr&rr%rrr) rrrr+r}rrrr) r7r8rnptrrrcol0col1col2rs rr6z_DOF_estimator.get_dof_vecsU0kk!nhhQxrzz2 ) )A - ) )A -2>>&Aq/::BNN6!Q'?;;BNN6!Q'?;;& !Q']DAqM41a= 9 !Q']DAqM41a= 9(;<Aq1uH q!t Aq1uH q!t Aq1uH  r N) rMrNrOrPrr2rrr6rSr rr/r/s* #"&&r r/ceZdZdZdZy)rz3dz is imposed by user; accounts for scaling if any.c|\}}||jz}||jz}tj||gjSrK)rrrvstackT)rrqr(r)s rr2z_DOF_estimator_user.compute_dz(s@ tdll"dll"yy$&(((r N)rMrNrOrPr2rSr rrr%s =)r rc"eZdZdZdZdZdZy)rz=Fast 'geometric' approximation, recommended for large arrays.c|j}|j}tjtj|j tj|}tj |}tj |}tdD]6}|dd|f|dddfz|dd|f<|dd|f|dddfz|dd|f<8tjtj|j tj|}tjtj|j tj|}||z } ||z } tj| | gjS)a self.df is computed as weighted average of _triangles sharing a common node. On each triangle itri f is first assumed linear (= ~f), which allows to compute d~f[itri] Then the following approximation of df nodal values is then proposed: f[ipt] = SUM ( w[itri] x d~f[itri] , for itri sharing apex ipt) The weighted coeff. w[itri] are proportional to the angle of the triangle itri at apex ipt rrNrr%) compute_geom_weightscompute_geom_gradsrrr,rt empty_likerrArB) r el_geom_w el_geom_grad w_node_sumdfx_el_wdfy_el_wiapex dfx_node_sum dfy_node_sum dfx_estim dfy_estims rr2z_DOF_estimator_geom.compute_dz2sL--/ ..0 [[$//!:)+))<> ==+==+1X HE!*1e8!4\!Q$5G!GHQX !*1e8!4\!Q$5G!GHQX  H{{288DOO#<+-88H+=? {{288DOO#<+-88H+=? !+  + yy)Y/0222r ctjtj|jddg}|j}t dD]}|dd|dzddf}|dd|dzdzddf}|dd|dz dzddf}tj |dddf|dddfz |dddf|dddfz }tj |dddf|dddfz |dddf|dddfz }tj||z tjz dz} dtj| dz z |dd|f<|S)z Build the (nelems, 3) weights coeffs of _triangles angles, renormalized so that np.sum(weights, axis=1) == np.ones(nelems) rrNr%r) rrr-rtrxrarctan2abspi) rrriptp0p1p2alpha1alpha2angles rrEz(_DOF_estimator_geom.compute_geom_weightsSsF ((BGGDOOQ7;<>>8 6C!S1Wa-(B!c!eq[!+,B!c!eq[!+,BZZ1a4AqD 12ad8Bq!tH3DEFZZ1a4AqD 12ad8Bq!tH3DEFFFVF]bee3q89E"BFF59$55GAsFO 6r cp|j}|j|j}|dddddf|dddddfz }|dddddf|dddddfz }tj||g}t |}|dddf|dddfz }|dddf|dddfz }tj ||gj} tj| } | dddf|ddddfz| dddf|ddddfzz| dddf<| dddf|ddddfz| dddf|ddddfzz| dddf<| S)z Compute the (global) gradient component of f assumed linear (~f). returns array df of shape (nelems, 2) df[ielem].dM[ielem] = dz[ielem] i.e. df = dz x dM = dM.T^-1 x dz Nr%rr&) rxrrtrdstackrrArBrG) rrtris_fdM1dM2dMdM_invdZ1dZ2dZdfs rrFz&_DOF_estimator_geom.compute_geom_gradsis^ >>(q!Qw(1a7"33q!Qw(1a7"33 YYSz "(+QTlVAqD\)QTlVAqD\) YYSz " $ $ ]]2 ad8F1a7O+bAhvaAg.FF1a4ad8F1a7O+bAhvaAg.FF1a4 r N)rMrNrOrPr2rErFrSr rrr/sG3B,r rc,eZdZdZfdZfdZxZS)rz The 'smoothest' approximation, df is computed through global minimization of the bending energy: E(f) = integral[(d2z/dx2 + d2z/dy2 + 2 d2z/dxdy)**2 dA] cF|j|_t| |yrK)rzrVr)r InterpolatorrYs rrz_DOF_estimator_min_E.__init__s!''  &r ct|}tj|}t }t j |j}|j}|j}|j|j}|j||||\}} } } d} | jd} t| || | | f}|jt|| || \}}tj j#|j%|| z }||krt'j(d|}tj*|j,jddgtj.}|ddd|dddf<|d dd|ddd f<|S) z{ Elliptic solver for bending energy minimization. Uses a dedicated 'toy' sparse Jacobi PCG solver. 绽|=r)r)Arx0tolzIn TriCubicInterpolator initialization, PCG sparse solver did not converge after 1000 iterations. `geom` approximation is used instead of `min_E`r&r"Nr%)rVr2rr,r}r rrxrzrtrr-r_Sparse_Matrix_coo compress_csc_cglinalgnormdotr warn_externalr/rwr+)rdz_initUf0reference_elementreccsrrr&r'r%r,ron_dofKff_cooUfrCerr0rqrYs rr2z_DOF_estimator_min_E.compute_dzsl '$&hhw/1 . .t~~ >zzOO VVDOO $,=+K+K tY,$((Hb $Xx,15>;2#37Cyy~~gkk#.34 #:    9 :BXXtyyq)1-RZZ @cc71a4add81a4 r )rMrNrOrPrr2rhris@rrrs ',,r rc@eZdZdZdZdZdZdZdZe dZ y) rpc|\|_|_tj|tj|_tj|tj |_tj|tj |_y)a4 Create a sparse matrix in coo format. *vals*: arrays of values of non-null entries of the matrix *rows*: int arrays of rows of non-null entries of the matrix *cols*: int arrays of cols of non-null entries of the matrix *shape*: 2-tuple (n, m) of matrix shape r"N) rmrrr+valsrrowscols)rrrrrs rrz_Sparse_Matrix_coo.__init__sSJJt2::6 JJt2884 JJt2884 r c|j|jfk(sJtj|j|j ||j z|jS)z Dot product of self by a vector *V* in sparse-dense to dense format *V* dense vector of shape (self.m,). )r minlength)rrrrrrr)rrs rruz_Sparse_Matrix_coo.dotsN ww466)###{{499#'99Qtyy\#9%)VV- -r c tj|j|j|jzzdd\}}}|j||_|j||_tj ||j |_y)zV Compress rows, cols, vals / summing duplicates. Sort for csc format. TrBreturn_inverserN)runiquerrrrrr_rindicess rrqz_Sparse_Matrix_coo.compress_cscso YY IItyy( (d467IIf% IIf% KK; r c tj|j|jz|jzdd\}}}|j||_|j||_tj ||j |_y)zV Compress rows, cols, vals / summing duplicates. Sort for csr format. TrrN)rrrrrrrrs r compress_csrz_Sparse_Matrix_coo.compress_csrso YY FF499 tyy (d467IIf% IIf% KK; r c4tj|j|jgtj}|j j }t|D]8}||j||j|fxx|j |z cc<:|S)zY Return a dense matrix representing self, mainly for debugging purposes. r") rrrrr+rr-rrr)rr@nvalsis rto_densez_Sparse_Matrix_coo.to_denseswhh'rzz: u |jjSrK)r__str__)rs rrz_Sparse_Matrix_coo.__str__s}}&&((r c|j|jk(}tjt |j |j tj }|j|||j|<|S)z3Return the (dense) vector of the diagonal elements.r")rrrrminrr+r)rin_diagdiags rrz_Sparse_Matrix_coo.diagsZ99 )xxDFFDFF+2::>#'99W#5TYYw   r N) rMrNrOrrurqrrrpropertyrrSr rrprps4 5- < <)r rpc|j}|j|k(sJ|j|k(sJtjj |}|j }tj|d}|tj|}n|}||j|z } | |z } tj|} d} tj| | } d}tjt| ||zkDr||kr| | | zz} |j| }| tj| |z }| ||zz } | |z } | }tj| | } ||| zz}| |z } |dz }tjt| ||zkDr||krtjj |j||z }||fS)a Use Preconditioned Conjugate Gradient iteration to solve A x = b A simple Jacobi (diagonal) preconditioner is used. Parameters ---------- A : _Sparse_Matrix_coo *A* must have been compressed before by compress_csc or compress_csr method. b : array Right hand side of the linear system. x0 : array, optional Starting guess for the solution. Defaults to the zero vector. tol : float, optional Tolerance to achieve. The algorithm terminates when the relative residual is below tol. Default is 1e-10. maxiter : int, optional Maximum number of iterations. Iteration will stop after *maxiter* steps even if the specified tolerance has not been achieved. Defaults to 1000. Returns ------- x : array The converged solution. err : float The absolute error np.linalg.norm(A.dot(x) - b) gư>rrr%) r-rrrrsrtrmaximumrrusqrtrT)rmrrnromaxiterrb_normkvecrrwpbetarhokrrrhooldrCs rrrrrs: A 33!8O8 33!8O8 YY^^A F 66D ::dD !D z HHQK  AEE!H A $A  A D &&A,C A 773s8 s6z )G QJ EE!HBFF1aL  aK dFffQl aK6z Q 773s8 s6z )G  ))..qA &C c6Mr cPtjd|tj|}|ddddf|ddddfz}||ddddf|ddddfzz }tj|dtj|zkD}tj |rd|z }n-tj |jd}d||z ||<|ddddf|z|ddddf<|ddddf |z|ddddf<|ddddf |z|ddddf<|ddddf|z|ddddf<|S)z Inversion of arrays of (2, 2) matrices, returns 0 for rank-deficient matrices. *M* : array of (2, 2) matrices to inverse, shape (n, 2, 2) Nr&r&rNrr%:0yE>r)r check_shaperrGrTallrr)rM_invprod1deltarank2 delta_invs rrrwsC \Q' MM! E aAgJqAqz !E AaAgJqAqz) )EVVE]T"&&-/ /E vve}uH HHQWWQZ( eEl? %q!QwZ )E!Q'N1aj[*E!Q'N1aj[*E!Q'Nq!QwZ )E!Q'N Lr ctjd|tj|}|ddddf|ddddfz}||ddddf|ddddfzz }tj|dtj|zkD}tj |rX|ddddf|z |ddddf<|ddddf |z |ddddf<|ddddf |z |ddddf<|ddddf|z |ddddf<|S||}||ddf|z ||ddf<||ddf |z ||ddf<||ddf |z ||ddf<||ddf|z ||ddf<|}||ddf||ddfz}tj|dk}d|z |dz|zz }||ddf|z||ddf<||ddf|z||ddf<||ddf|z||ddf<||ddf|z||ddf<|S) a Inversion of arrays of (2, 2) SYMMETRIC matrices; returns the (Moore-Penrose) pseudo-inverse for rank-deficient matrices. In case M is of rank 1, we have M = trace(M) x P where P is the orthogonal projection on Im(M), and we return trace(M)^-1 x P == M / trace(M)**2 In case M is of rank 0, we return the null matrix. *M* : array of (2, 2) matrices to inverse, shape (n, 2, 2) rrNrr%rrr&)rrrrGrTr) rrrrrrank01trtr_zeros sq_tr_invs rrrsi \Q' MM! E aAgJqAqz !E AaAgJqAqz) )E VVE]T"&&-/ /E vve}1a7e+aAgAq!G*u,aAgAq!G*u,aAg1a7e+aAg* L!e ua{^e3eQkq! n_u4eQkq! n_u4eQkua{^e3eQk vq!|_qA .FF2J&[RU8^4 1 o 9fal1 o 9fal1 o 9fal1 o 9fal Lr cT|ddtjtjf|zS)z6 Scalar product between scalars and matrices. N)rnewaxis)scalarrs rrrs# !RZZ+ ,Q ..r c2tj|gdS)z4 Transposition of an array of matrices *M*. )rr&r%)r transposers rrrs <<9 %%r c|dvsJ|j}|dk(sJ|j}|dk(sJ|j}|dd\}}|d|jdk(sJtj|dtj}tj |} |dk(r9t |D])} t |D]} ||| | z|z| f| dd| | f<+| St |D])} t |D]} ||| | | z|zf| dd| | f<+| S)z{ Roll an array of matrices along *axis* (0: rows, 1: columns) according to an array of indices *roll_indices*. rr%rr%Nrr")rrrrrrGr) r roll_indicesrr ndim_rollshrr vec_indicesM_rollirics rrrsT 6>> 66D 199!!I >> B bc7DAq a5L&&q) )) )))BqE2K]]1 F qy( OBAh O$%k\M"4D3I2&M$Nq"by! O O M( OBAh O$%k2 b8HA7M&M$Nq"by! O O Mr cFt|ttfsJtd|DsJt j |Dcgc] }t |c}}t j||dz dk(sJt |}|d}t j |dd}|j}|jd||g}t j||}t|D]5} t|D]%} t j || | |dd| | f<'7|Scc}w)a+ Build an array of matrices from individuals np.arrays of identical shapes. Parameters ---------- M ncols-list of nrows-lists of shape sh. Returns ------- M_res : np.array of shape (sh, nrow, ncols) *M_res* satisfies ``M_res[..., i, j] = M[i][j]``. c3HK|]}t|ttfywrK) isinstancetuplelist).0items r z(_to_matrix_vectorized..s=4z$ .=s "rr"N) rrrrrrlenr#rr/r) rrc_vecrrM00dtrM_retirowicols rrrs a% '' ' =1= == = JJa0dD 0 1E 66%a.A% && & AA aA **QqT!W C B ))A,1 B HHRr "Ea=!H =D#%::agdm#> 77DAq qy!!!$j! 4!!!$a 4 B HHRr "E qy # BBz!9"!rs(/0 NVOVOr3:O3:l_@?_@H P0P0n IIX).)R.Rj6.6vAAHCh<+\/&8<r