
    _-PhE+                     2    d Z ddlZddZd Zd Z	 	 dd
ZdS )zGenerate a diffusion stencil.

Supports isotropic diffusion (FE,FD), anisotropic diffusion (FE, FD), and
rotated anisotropic diffusion (FD).

The stencils include redundancy to maintain readability for simple cases (e.g.
isotropic diffusion).

    N      ?        FEc                    t          |           }t          |          }t          j        |          }t          j        |          }||z  }|dz  }|dz  }|dk    rd|z  dz
  |z  d|z  dz
  |z  z   d|z  dz
  |z  z   }	d|z  dz
  |z  d|z  dz   |z  z   }
d|z  dz
  |z  d|z  dz
  |z  z   d|z  dz   |z  z   }d|z  dz   |z  d|z  dz
  |z  z   }d	|z  d	z   |z  d	|z  d	z   |z  z   }t          j        |	|
|g|||g||
|	gg          d
z  }n^|dk    rId|dz
  z  |z  }	||z  |z    }
|	 }||z  |z    }d|dz   z  }t          j        |	|
|g|||g||
|	gg          }nt          d          |S )a-  Rotated Anisotropic diffusion in 2d of the form.

        -div Q A Q^T grad u

        Q = [cos(theta) -sin(theta)]
            [sin(theta)  cos(theta)]

        A = [1          0        ]
            [0          eps      ]

    Parameters
    ----------
    epsilon  : float, optional
        Anisotropic diffusion coefficient: -div A grad u,
        where A = [1 0; 0 epsilon].  The default is isotropic, epsilon=1.0
    theta : float, optional
        Rotation angle `theta` in radians defines -div Q A Q^T grad,
        where Q = [cos(`theta`) -sin(`theta`); sin(`theta`) cos(`theta`)].
    type : {'FE','FD'}
        Specifies the discretization as Q1 finite element (FE) or 2nd order
        finite difference (FD)
        The default is `theta` = 0.0

    Returns
    -------
    stencil : numpy array
        A 3x3 diffusion stencil

    See Also
    --------
    stencil_grid, poisson

    Notes
    -----
    Not all combinations are supported.

    Examples
    --------
    >>> import scipy as sp
    >>> from pyamg.gallery.diffusion import diffusion_stencil_2d
    >>> sten = diffusion_stencil_2d(epsilon=0.0001,theta=sp.pi/6,type='FD')
    >>> print(sten)
    [[-0.2164847 -0.750025   0.2164847]
     [-0.250075   2.0002    -0.250075 ]
     [ 0.2164847 -0.750025  -0.2164847]]

    Consider a 2 x 4 grid ([x0, x1] x [y0, y1, y2, y3]).
    The first dimension of the stencil defines x.

    >>> nx, ny = (2, 4)
    >>> sten = pyamg.gallery.diffusion_stencil_2d(epsilon=0.1, type='FD')
    >>> A = pyamg.gallery.stencil_grid(sten, (nx, ny)).toarray()
    >>> print(sten)
        [[-0.  -1.   0. ]
         [-0.1  2.2 -0.1]
         [ 0.  -1.  -0. ]]
    >>> print(A)
        [[ 2.2 -0.1  0.   0.  -1.   0.   0.   0. ]
         [-0.1  2.2 -0.1  0.   0.  -1.   0.   0. ]
         [ 0.  -0.1  2.2 -0.1  0.   0.  -1.   0. ]
         [ 0.   0.  -0.1  2.2  0.   0.   0.  -1. ]
         [-1.   0.   0.   0.   2.2 -0.1  0.   0. ]
         [ 0.  -1.   0.   0.  -0.1  2.2 -0.1  0. ]
         [ 0.   0.  -1.   0.   0.  -0.1  2.2 -0.1]
         [ 0.   0.   0.  -1.   0.   0.  -0.1  2.2]]

       r               g      @FDg      ?g       @z.only stencil types "FE" and "FD" are supported)floatnpcossinarray
ValueError)epsilonthetatypeepsCSCSCCSSabcdestencils                  W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/gallery/diffusion.pydiffusion_stencil_2dr&      s   H ..C%LLE
uA
uA	
1B	
AB	
ABt||. VaZOr#vz2o-3B>sUQYNbfqj"_,VaZOr#vz2o-C!R?VaZOquqy"n,sUQYNaeai^+(Q1I1I1I' ( (*-. 
" qM""frkNB"frkNqM(Q1I1I1I' ( ( IJJJN    c                     ddl m} m}  | d          \  }} | d          \  }} | d          \  }} |||dg| |dgg dg          } |g dd||gd| |gg          }	 |||dg| |dgg dg          }
||	z  |
z  } | d          \  }} |g dd|dgdd|gg          }||z  |j        z  }t	          d	          D ]4}t	          d	          D ]"}t          d
| d| d|||f                     #5dS )z?Use SymPy to generate the 3D matrices for diffusion_stencil_3d.r   symbolsMatrixz
cpsi, spsizcth, sthz
cphi, sphi)r   r   r	   )r	   r   r   z
epsy, epszr
   zD[z, z] = N)sympyr*   r+   Trangeprint)r*   r+   cpsispsicthsthcphisphiRpsiRthRphiQepsyepszADijs                     r%   _symbolic_rotation_helperr@      s    &%%%%%%%&&JD$wz""HC&&JD$6D$?dUD!$4iii@AAD
&)))ac]QcN;
<
<C6D$?dUD!$4iii@AADs
TA&&JD$			AtQ<!Q677A	AA1XX . .q 	. 	.A,q,,A,,1QT7,,----	.. .r'   c            	          ddl m} m}  | d          \	  }}}}}}}}	}
 ||||g|||g||	|
gg          } |g dg          j        }|j        }||z  |z  }t	          |d                    dS )z?Use SymPy to generate the 3D products for diffusion_stencil_3d.r   r)   z+D11, D12, D13, D21, D22, D23, D31, D32, D33)dxdydzN)r,   r*   r+   r-   r/   )r*   r+   D11D12D13D21D22D23D31D32D33r=   graddivr   s                  r%   _symbolic_product_helperrP      s     &%%%%%%% 	=>> 0Cc3S#sC 	c3#sC3S/BCCA6%%%&'')D
&Ca$A	!A$KKKKKr'   r   c                 	   t          |           }t          |          }t          |          }t          |          }t          |          }t          j        d          }t          j        |          }	t          j        |          }
t          j        |          }t          j        |          }t          j        |          }t          j        |          }||	|z  |z  ||
z  z   dz  z  ||dz  z  |dz  z  z   |	|z  ||
z  |z  z
  dz  z   |d<   ||z  |z  |dz  z  ||	|z  |z  |
|z  z
  z  |	|z  |z  ||
z  z   z  z   |	|z  ||
z  |z  z
  |	 |z  ||z  |
z  z
  z  z   |d<   |	 |z  |z  |	|z  |z  ||
z  z   z  ||z  |z  |z  z   |
|z  |	|z  ||
z  |z  z
  z  z   |d<   ||z  |z  |dz  z  ||	|z  |z  |
|z  z
  z  |	|z  |z  ||
z  z   z  z   |	|z  ||
z  |z  z
  |	 |z  ||z  |
z  z
  z  z   |d<   |dz  |z  |dz  z  ||	|z  |z  |
|z  z
  dz  z  z   |	 |z  ||z  |
z  z
  dz  z   |d<   |	 |z  |z  |	|z  |z  |
|z  z
  z  ||z  |z  |z  z   |
|z  |	 |z  ||z  |
z  z
  z  z   |d<   |	 |z  |z  |	|z  |z  ||
z  z   z  ||z  |z  |z  z   |
|z  |	|z  ||
z  |z  z
  z  z   |d	<   |	 |z  |z  |	|z  |z  |
|z  z
  z  ||z  |z  |z  z   |
|z  |	 |z  ||z  |
z  z
  z  z   |d
<   |	dz  |z  |dz  z  |dz  |z  z   |
dz  |dz  z  z   |d<   t          j        d          }|dk    rt          d          |dk    rd\  }}}||dz
  ||dz   g||fxx         t          j        g d          |d         z  z  cc<   |||dz
  ||dz   g|fxx         t          j        g d          |d         z  z  cc<   ||||dz
  ||dz   gfxx         t          j        g d          |d         z  z  cc<   t          j        g d          }t          j        g d          }|||z   ||z   |fxx         dt          j        g d          z  |d         |d         z   z  z  cc<   |||z   |||z   fxx         dt          j        g d          z  |d	         |d         z   z  z  cc<   ||||z   ||z   fxx         dt          j        g d          z  |d
         |d         z   z  z  cc<   |S )a;	  Rotated Anisotropic diffusion in 3d of the form.

    -div Q A Q^T grad u

    Q = Rpsi Rtheta Rphi

    Rpsi = [ c   s   0 ]
           [-s   c   0 ]
           [ 0   0   1 ]
           c = cos(psi)
           s = sin(psi)

    Rtheta = [ 1   0   0 ]
             [ 0   c   s ]
             [ 0  -s   c ]
           c = cos(theta)
           s = sin(theta)

    Rphi = [ c   s   0 ]
           [-s   c   0 ]
           [ 0   0   1 ]
           c = cos(phi)
           s = sin(phi)

    Here Euler Angles are used:
    http://en.wikipedia.org/wiki/Euler_angles

    This results in

    Q = [   cphi*cpsi - cth*sphi*spsi, cpsi*sphi + cphi*cth*spsi, spsi*sth]
        [ - cphi*spsi - cpsi*cth*sphi, cphi*cpsi*cth - sphi*spsi, cpsi*sth]
        [                    sphi*sth,                 -cphi*sth,      cth]

    A = [1          0            ]
        [0          epsy         ]
        [0          0        epsz]

    D = Q A Q^T

    Parameters
    ----------
    epsilony  : float, optional
        Anisotropic diffusion coefficient in the y-direction
        where A = [1 0 0; 0 epsilon_y 0; 0 0 epsilon_z].  The default is
        isotropic, epsilon=1.0
    epsilonz  : float, optional
        Anisotropic diffusion coefficient in the z-direction
        where A = [1 0 0; 0 epsilon_y 0; 0 0 epsilon_z].  The default is
        isotropic, epsilon=1.0
    theta : float, optional
        Euler rotation angle `theta` in radians. The default is 0.0.
    phi : float, optional
        Euler rotation angle `phi` in radians. The default is 0.0.
    psi : float, optional
        Euler rotation angle `psi` in radians. The default is 0.0.
    type : {'FE','FD'}
        Specifies the discretization as Q1 finite element (FE) or 2nd order
        finite difference (FD)

    Returns
    -------
    stencil : numpy array
        A 3x3 diffusion stencil

    See Also
    --------
    stencil_grid, poisson, _symbolic_rotation_helper, _symbolic_product_helper

    Notes
    -----
    Not all combinations are supported.

    Examples
    --------
    >>> import scipy as sp
    >>> from pyamg.gallery.diffusion import diffusion_stencil_2d
    >>> sten = diffusion_stencil_2d(epsilon=0.0001,theta=sp.pi/6,type='FD')
    >>> print(sten)
    [[-0.2164847 -0.750025   0.2164847]
     [-0.250075   2.0002    -0.250075 ]
     [ 0.2164847 -0.750025  -0.2164847]]

    )r
   r
   r   )r   r   )r   r	   )r   r   )r	   r   )r	   r	   )r	   r   )r   r   )r   r	   )r   r   )r
   r
   r
   r   zFE not implemented yetr   )r	   r	   r	   r	   )r   r   r   )r   r   r	   r	   )r   r	   r   r	   g      ?)r	   r   r   r	   )r   r   zerosr   r   NotImplementedErrorr   )epsilonyepsilonzr   phipsir   r:   r;   r=   r4   r5   r2   r3   r0   r1   r$   r>   r?   kLMs                        r%   diffusion_stencil_3dr[      sP   j ??D??D%LLE
**C
**C
A6#;;D6#;;D
&--C
&--C6#;;D6#;;D DHTMDI-11DqLa4GG	dSXd]	"Q&'AdG4inS!V#d4imd4i'($s(4-$t)*CDE	dSXd]	"dU4Z$s(4-%?@AAdG eDjnd3htmd4i78Dc Hd4i#d(4-&?@AAdG4inS!V#d4imd4i'($s(4-$t)*CDE	dSXd]	"dU4Z$s(4-%?@AAdG Agdl36!D$t)C-$t)*Ca)G$GG
td3htm	#a'(AdGeDjnd4imd4i78Sc HteDj48D=&@ABAdGeDjnd3htmd4i783t8D=;LLS$t)c$htm+,-AdGeDjnd4imd4i7848D=;LLS4%*tCx},-.AdGAgdl36!CF4K/$'#q&.@AdGhy!!Gt||!":;;;t|| 1a 	1a1q!#$$$(=(=$(GG$$$ 	AaCAaC=!#$$$(=(=$(GG$$$ 	1qsAqsm#$$$(=(=$(GG$$$H^^^$$H^^^$$Aq1ua   bh~~~...!D'AdG2CD	E    	Aq!a%   bh~~~...!D'AdG2CD	E    	1q5!a%   bh~~~...!D'AdG2CD	E    Nr'   )r   r   r   )r   r   r   r   r   r   )__doc__numpyr   r&   r@   rP   r[    r'   r%   <module>r_      s        K K K K\. . .0  " EH'+c c c c c cr'   