
    _-Ph>                     <    d Z ddlZddlmZ ej        dz  ddfdZdS )zQGenerate matrix and right-hand side for upwind FD discretization of 2D advection.    N   )stencil_gridg      @g      ?c                     t                      t                     dk    rt          d          |dk    s|t          j        dz  k    rt          d          t          j        |          }t          j        |          }t          j        g d| ||z   dgd| dgg          }t          |           	                                }t          j         fdt          d d                   D                       }t          j         fdt          d d                   D                       }	t          j        ||	f          fd	t          d|j        d                   D             }
t          j        |          rt          j         d         |          }n&|j        d          d         k    rt          d
          t          j        |          rt          j         d         |          }n&|j        d          d         k    rt          d          t          j        |                                |                                f          }||
ddf         ddf          |z  }||
ddf         dd|
f         }||fS )am  
    Generate matrix and RHS for upwind FD discretization of 2D advection.

        (cos(theta),sin(theta)) dot grad(u) = 0,

    with inflow boundaries on the left and bottom of the domain. Assume uniform
    grid spacing, dx=dy, even for grid[0] != grid[1].

    Parameters
    ----------
    grid  : tuple
        (ny, nx) number of points in y and x
    theta : float, optional
        Rotation angle `theta` in radians defines direction of advection
        (cos(theta),sin(theta))
    l_bdry : float, array
        left boundary value. If float, then constant in-flow boundary value
        applied. If array, then length of array must be equal to ny = grid[0],
        and this array defines non-constant boundary value on the left.
    b_bdry : float, array
        bottom boundary value. If float, then constant in-flow boundary value
        applied. If array, then length of array must be equal to nx = grid[1],
        and this array defines non-constant boundary value on the bottom.

    Returns
    -------
    A : csr_matrix
        Defines 2D FD upwind discretization, with boundary
    rhs : array
        Defines right-hand-side with boundary contributions

    See Also
    --------
    poisson

    Examples
    --------
    >>> from numpy import pi
    >>> from pyamg.gallery import advection_2d
    >>> A, rhs = advection_2d( (4,4), theta=pi/4)
    >>> print(A.toarray().round(4))
        [[ 1.4142  0.      0.     -0.7071  0.      0.      0.      0.      0.    ]
         [-0.7071  1.4142  0.      0.     -0.7071  0.      0.      0.      0.    ]
         [ 0.     -0.7071  1.4142  0.      0.     -0.7071  0.      0.      0.    ]
         [ 0.      0.      0.      1.4142  0.      0.     -0.7071  0.      0.    ]
         [ 0.      0.      0.     -0.7071  1.4142  0.      0.     -0.7071  0.    ]
         [ 0.      0.      0.      0.     -0.7071  1.4142  0.      0.     -0.7071]
         [ 0.      0.      0.      0.      0.      0.      1.4142  0.      0.    ]
         [ 0.      0.      0.      0.      0.      0.     -0.7071  1.4142  0.    ]
         [ 0.      0.      0.      0.      0.      0.      0.     -0.7071  1.4142]]

       zCgrid must be a length 2 tuple, describe number of points in x and yr   ztheta must be in (0, pi/2))r   r   r   c                 &    g | ]}|d          z  S )r    .0igrids     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/gallery/advection.py
<listcomp>z advection_2d.<locals>.<listcomp>N   s!    ===a$q'	===    c                 >    g | ]}d          d         d z
  z  |z   S )r   r   r   r	   s     r   r   z advection_2d.<locals>.<listcomp>O   s/    III!Qa+A-IIIr   r   c                     g | ]}|v|	S r   r   )r
   r   	all_bdofss     r   r   z advection_2d.<locals>.<listcomp>Q   s#    FFFa1I3E3E3E3E3Er   z/left boundary data does not match boundary sizez1bottom boundary data does not match boundary sizeN)tuplelen
ValueErrornppicossinarrayr   tocsrrangeconcatenateshapeisscalarfullflatten)r   thetal_bdryb_bdryw1w2stAl_bdofsb_bdofsint_dofsbdryrhsr   s   `            @r   advection_2dr.      s   j ;;D
4yyA~~ @ A A 	AzzUbeAg%%5666 
B	B	999sBrE1oB3{;	<	<BR$$&&A h====5DG+<+<===>>GhIIIIuQQ7H7HIIIJJG122IFFFF5AGAJ//FFFH 
{6 La&))	aDG	#	#JKKK	{6 Na&))	aDG	#	#LMMM >6>>++V^^-=-=>??DXqqq[>!!!Y,'
'
,C	(AAA+qqq({#Ac6Mr   )__doc__numpyr   stencilr   r   r.   r   r   r   <module>r2      s\    W W     ! ! ! ! ! !  U3Ys3 \ \ \ \ \ \r   