
    _-Ph&                     X    d Z ddlZddlZddlmZmZmZ ddlm	Z	m
Z
 	 ddZdd	Zdd
ZdS )a#  Visualization tools for coarse grids, both C/F splittings and aggregation.

Output is either to file (VTK) or to the screen (matplotlib).

vis_splitting:        visualize C/F splittings through vertex elements
vis_aggregate_groups: visualize aggregation through groupins of edges, elements

    N)
csr_matrix
coo_matrixtriu   )write_basic_mesh	write_vtu
output.vtuc           	         t          | |||           ddddd}||vrt          d|           ||         }t          |          }|                                |j        d         k    r+||                    d	
          |j        d         k              }t          |j                  |j        d         k    r|j        d	d         |j        dd         z
  dk                                    d         }t          j
        |                    d	
          t                                                    }|j        ||d	k    <   |j        d	         ||dk    <   ||         }	n|j        |         }	t          j        t          t          j        |	                                        d	
          dk              d         }
||
ddf         }|j        d         }|                                }t          j        t          j        d|j        d                   t          j        |j        d	         ft                              }t          j        t          |          f          }t          |          t          |          k    rt          d          t)          |||ff|j        d         |                                d	z   f          }|j        |z  }t-          |d	                                          }t          j        |j        |j        f          j        }||j        |j                 |j        |j                 k             }|j        d         }t          j
        |                    d
                                                    }t          j        t          j        |j        t          j        |d	k              d                                                             }t          |          }dt          j        |f          z  }dt          j        |f          z  }d	t          j        |f          z  }d	|d|||i}d	|d|||i}t;          | |||           dS )a  Coarse grid visualization of aggregate groups.

    Create .vtu files for use in Paraview or display with Matplotlib.

    Parameters
    ----------
    V : {array}
        coordinate array (N x D)
    E2V : {array}
        element index array (Nel x Nelnodes)
    AggOp : {csr_matrix}
        sparse matrix for the aggregate-vertex relationship (N x Nagg)
    mesh_type : {string}
        type of elements: vertex, tri, quad, tet, hex (all 3d)
    fname : {string, file object}
        file to be written, e.g. 'output.vtu'

    Returns
    -------
    Writes data to .vtu file for use in paraview (xml 0.1 format) or
    displays to screen using matplotlib

    Notes
    -----
    Works for both 2d and 3d elements.  Element groupings are colored
    with data equal to 2.0 and stringy edges in the aggregate are colored
    with 3.0

    Examples
    --------
    >>> from pyamg.aggregation import standard_aggregation
    >>> from pyamg.vis.vis_coarse import vis_aggregate_groups
    >>> from pyamg.gallery import load_example
    >>> data = load_example('unit_square')
    >>> A = data['A'].tocsr()
    >>> V = data['vertices']
    >>> E2V = data['elements']
    >>> AggOp = standard_aggregation(A)[0]
    >>> vis_aggregate_groups(V=V, E2V=E2V, AggOp=AggOp,
    ...                      mesh_type='tri', fname='output.vtu')
    >>> from pyamg.aggregation import standard_aggregation
    >>> from pyamg.vis.vis_coarse import vis_aggregate_groups
    >>> from pyamg.gallery import load_example
    >>> data = load_example('unit_cube')
    >>> A = data['A'].tocsr()
    >>> V = data['vertices']
    >>> E2V = data['elements']
    >>> AggOp = standard_aggregation(A)[0]
    >>> vis_aggregate_groups(V=V, E2V=E2V, AggOp=AggOp,
    ...                      mesh_type='tet', fname='output.vtu')

    )VE2VAggOp	mesh_type   	   
      )triquadtethexzUnknown mesh_type=r   r   )axisNdtypez)Problem constructing vertex-to-vertex map)shape      )r   cellsfnamecdata)check_input
ValueErrorr   maxr   lenindicesindptrnonzeronparraysumintravelwhereabsdiffkronarangeonesr   Tr   tocoovstackrowcolargwhereisinr   )r   r   r   r   r   map_type_to_keykey	full_aggsnew_aggsElementAggsmaskE2V_aNel_ar7   r6   dataV2VedgesE2V_bNel_bsumsE2V_cNel_ccolors_acolors_bcolors_cr   r    s                               T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/vis/vis_coarse.pyvis_aggregate_groupsrN      s   l !EY????2bAAO''9i99:::
)
$CuE wwyyEKN""#''q'//EKN23 5=U[^++l122&crc)::q@IIKKAN	8EII1I--S999??AA#(=a #(;q>a smmC(
 8C,,--11q199Q>??BDaaaLEKNE ))++C
'")Asy|,,'39Q</5557 7C7CHH;D
3xx3s88DEEE
dS#J'	!cggiik/J
K
K
KC
%#+C
sA,,



C Isw())+E %-(EM#',BBCEKNE 8EII1I%%&&,,..DKrx	/B/B1/EFFGGMMOOEJJE %"""H%"""H%"""Hq%e,E!XsH5Ee5999999    vtkc           
         t          | |           | j        d         }t          t          |          |z            }t	          j        d|t                    }|                    d          }t          |          dk     r|d         }d}	n%d                    |dd                   }|d         }	|}
t          d|          D ]}|d	k    r| d
|d	z    d|	 }
|||z  |d	z   |z           }|dvrt          d          |dk    rt          | |d||
           W|dk    r%	 ddlm} t	          j        |dk              d         }t	          j        |d	k              d         }| |df         }| |d	f         }| |df         }| |d	f         }|                                 |                    ||d||dd           |                    d           |                    d           |                    d           |                    d           |                                 c# t*          $ r t-          d           Y w xY wdS )aM  Coarse grid visualization for C/F splittings.

    Parameters
    ----------
    V : {array}
        coordinate array (N x D)
    splitting : {array}
        coarse(1)/fine(0) flags
    fname : {string, file object}
        file to be written, e.g. 'output.vtu'
    output : {string}
        'vtk' or 'matplotlib'

    Returns
    -------
    Displays in screen or writes data to .vtu file for use in paraview
    (xml 0.1 format)

    Notes
    -----
    D :
        dimension of coordinate space
    N :
        # of vertices in the mesh represented in V
    Ndof :
        # of dof (= ldof * N)

        - simply color different points with different colors.  This works
          best with classical AMG.

        - writes a file (or opens a window) for each dof

        - for Ndof>1, they are assumed ordered [...dof1..., ...dof2..., etc]

    Examples
    --------
    >>> import numpy as np
    >>> from pyamg.vis.vis_coarse import vis_splitting
    >>> V = np.array([[0.0,0.0],
    ...               [1.0,0.0],
    ...               [0.0,1.0],
    ...               [1.0,1.0]])
    >>> splitting = np.array([0,1,0,1,1,0,1,0])    # two variables
    >>> vis_splitting(V,splitting,output='vtk',fname='output.vtu')

    >>> from pyamg.classical.split import RS
    >>> from pyamg.vis.vis_coarse import vis_splitting
    >>> from pyamg.gallery import load_example
    >>> data = load_example('unit_square')
    >>> A = data['A'].tocsr()
    >>> V = data['vertices']
    >>> E2V = data['elements']
    >>> splitting = RS(A)
    >>> vis_splitting(V=V,splitting=splitting,output='vtk', fname='output.vtu')

    r   r   .r   z.vtu Nr   r   _)rP   
matplotlibzproblem with outputtyperP   vertex)r   r   r   r    r   rU   zr.zb.T)clip_onz%C/F splitting (red=coarse, blue=fine)xyoffz)
Note: matplotlib is needed for plotting.)r!   r   r+   r$   r(   r1   splitjoinranger"   r   matplotlib.pyplotpyplotr-   figureplottitlexlabelylabelr   showImportErrorprint)r   	splittingoutputr   NNdofr   afname1fname2	new_fnamedr    pltcdataFcdataCxCyCxFyFs                       rM   vis_splittingrx      s   r 9	
As9~~!""D
)Aq
$
$
$C 	CA
1vvzz13B32I1d^^ D D !88!22AaC22&22I1Q3!A#q/*...6777U??qcX#(	; ; ; ; ;|##D//////%1*--a0%1*--a0vqy\vqy\vqy\vqy\

Rr2tTBBB		ABBB

3

3



 D D DBCCCCCD! $D Ds   "DH&&IIc                 d   | 3t          j        | j        t           j                  st	          d          |vt          j        |j        t           j                  st	          d          |                                dk    r+t          j        d|                                 d           |+|j	        d         |j	        d         k    rt	          d	          ||t	          d
          |I|G|j	        d         |j	        d         k    s|j	        d         |j	        d         k    rt	          d          || t	          d
          |F|
                                }| 0t          |          | j	        d         z  dk    rt	          d          |d}||vrt	          d|           dS dS )z Check input for local functions.NzV should be of type floatzE2V should be of type integerr   zElement indices begin at r   )
stacklevelr   z AggOp should be of size N x Naggzproblem with check_inputz2expected square matrix A and compatible with AggOpz!splitting must be a multiple of N)rV   r   r   r   r   zmesh_type should one of )r(   
issubdtyper   floatingr"   integerminwarningswarnr   r,   r$   )r   r   r   Arh   r   valid_mesh_typess          rM   r!   r!      s   }R]17BK@@}4555
}SY
33 	><===7799>>MAcggiiAAaPPPPU[^ek!n<<;<<<}3444	%+WQZ171:%%171:Q+G+GMNNN3444OO%%	=c)nnqwqz9a??@AAAB,,,J8HJJKKK ,,rO   )r	   )rP   r	   )NNNNNN)__doc__r   numpyr(   scipy.sparser   r   r   
vtk_writerr   r   rN   rx   r!    rO   rM   <module>r      s          5 5 5 5 5 5 5 5 5 5 3 3 3 3 3 3 3 3  ,z: z: z: z:ziD iD iD iDX L  L  L  L  L  LrO   