
    _-Phw?                     @    d Z ddlZddlZ	 	 d	dZ	 	 	 d
dZd Zd ZdS )a  VTK output functions.

Create coarse grid views and write meshes/primitives to .vtu files.  Use the
XML VTK format for unstructured meshes (.vtu)

This will use the XML VTK format for unstructured meshes, .vtu

See here for a guide:  http://www.vtk.org/pdf/file-formats.pdf
    N
output.vtkc           
         g d}t          |t                    st          d          | j        \  }}	|	dk    r*t	          j        | t	          j        |df          f          } |D ]}
t          |
t                    r|
t          t          dd                    vrt          d          ||
         ||
         t          d          ||
         t          d	|
           t	          j        ||
                   dk    r+||
                             ||
         j        df          ||
<   ||
         ||
         j        d         k    r-t          d
||
         j        d          d||
          d          d}|vt	          j        |          dk    r|j        d         }nd}|                    |j        df          }|j        d         |k    r!t          d| d|j        d          d          d}||t	          j        |          dk    r|j        d         }nd}|                    |j        df          }|j        d         d|z  k    r$t          dd|z   d|j        d          d          d}||D ]	}
t	          j        ||
                   dk    rA|dk    r||
         j        d         }nT|||
         j        d         k    rt          d          n-d}||
                             ||
         j        df          ||
<   ||
         j        d         ||
         j        d         k    r8t          d||
         j        d          d||
         j        d          d          ||
         t          d|
           d}||D ]}
t	          j        ||
                   dk    rA|dk    r||
         j        d         }nT|||
         j        d         k    rt          d          n-d}||
                             ||
         j        df          ||
<   ||
         j        d         d||
         j        d         z  k    rt          d          ||
         t          d|
           d}t	          j        dt          j                  }t	          j        dd          }t	          j        dd          }d}d}|D ]}
||
         j        d         }||
         j        d         }||z  }t	          j        |fd          }t	          j        |||
                                         f          }t	          j        |||z  f          }t	          j        ||
|z  f          }|'|	||
         }nt	          j        |||
         f          }|'|	||
         }t	          j        |||
         f          }t&          j        j                                        }|                    dd           }d!d"d#d$}t1          ||           |                    dd!          }|                    dd%          }t          |          t          |          d&}t1          ||           |                    dd'          }|                    dd(          }d)d*d+d,d-}t1          ||           |                    t5          |                     }|                    dd.          }|                    dd(          } d/d0d,d1}t1          ||            |                    t5          |                    }!|                    dd(          }"d/d2d,d1}t1          ||"           |                    t5          |                                                    }#|                    dd(          }$d3d4d,d1}t1          ||$           |                    t5          |                    }%|                    dd5          }&g }'g }(t          d|          D ]})|'                    |                    dd(                     d)d6|) d7d,d-}t1          ||'|)                    |(                    |                    t5          |dd|)f                                        g }*g }+t          d|          D ]})|*                    |                    dd(                     d)d8|) d+d,d-}t1          ||*|)                    |+                    |                    t5          |dd|)f                                        |                    dd9          },g }-g }.t          d|          D ]})|-                    |                    dd(                     d)d:|) d7d,d-}t1          ||-|)                    |.                    |                    t5          |dd|)f                                        g }/g }0t          d|          D ]})|/                    |                    dd(                     d)d;|) d+d,d-}t1          ||/|)                    |0                    |                    t5          |dd|)f                                        |                    |           |                    |           |                    |           |                    |           |                    |           |                    |           |                    |           |                    |            |                    |"           |                    |$           |                     |!           |"                    |#           |$                    |%           |                    |&           t          d|          D ]>})|&                    |'|)                    |'|)                             |(|)                    ?t          d|          D ]>})|&                    |*|)                    |*|)                             |+|)                    ?|                    |,           t          d|          D ]>})|,                    |-|)                    |-|)                             |.|)                    ?t          d|          D ]>})|,                    |/|)                    |/|)                             |0|)                    ?	 t=          |d<d=>          5 }1|                    |1d?@           ddd           dS # 1 swxY w Y   dS # t@          $ r*}2tC          dA|2j"         dB|2j#                    Y d}2~2dS d}2~2ww xY w)Ca,  Write a .vtu file in xml format.

    Parameters
    ----------
    fname : {string}
        file to be written, e.g. 'mymesh.vtu'
    V : {array}
        Ndof x 3 (if 2, then expanded by 0)
        list of (x,y,z) point coordinates
    cells : {dictionary}
        Dictionary of with the keys
    pdata : {array}
        Ndof x Nfields array of scalar values for the vertices
    pvdata : {array}
        Nfields*3 x Ndof array of vector values for the vertices
    cdata : {dictionary}
        scalar valued cell data
    cvdata : {dictionary}
        vector valued cell data

    Returns
    -------
    writes a .vtu file for use in Paraview

    Notes
    -----
    - Poly data not supported
    - Non-Poly data is stored in Numpy array: Ncell x vtk_cell_info
    - Each I1 must be >=3
    - pdata = Ndof x Nfields
    - pvdata = 3*Ndof x Nfields
    - cdata,cvdata = list of dictionaries in the form of cells


    =====  =================== ============= ===
    keys   type                n points      dim
    =====  =================== ============= ===
       1   VTK_VERTEX:         1 point        2d
       2   VTK_POLY_VERTEX:    n points       2d
       3   VTK_LINE:           2 points       2d
       4   VTK_POLY_LINE:      n+1 points     2d
       5   VTK_TRIANGLE:       3 points       2d
       6   VTK_TRIANGLE_STRIP: n+2 points     2d
       7   VTK_POLYGON:        n points       2d
       8   VTK_PIXEL:          4 points       2d
       9   VTK_QUAD:           4 points       2d
       10  VTK_TETRA:          4 points       3d
       11  VTK_VOXEL:          8 points       3d
       12  VTK_HEXAHEDRON:     8 points       3d
       13  VTK_WEDGE:          6 points       3d
       14  VTK_PYRAMID:        5 points       3d
    =====  =================== ============= ===

    Examples
    --------
    >>> from pyamg.vis import write_vtu
    >>> import numpy as np
    >>> V = np.array([[0.0,0.0],
    ...               [1.0,0.0],
    ...               [2.0,0.0],
    ...               [0.0,1.0],
    ...               [1.0,1.0],
    ...               [2.0,1.0],
    ...               [0.0,2.0],
    ...               [1.0,2.0],
    ...               [2.0,2.0],
    ...               [0.0,3.0],
    ...               [1.0,3.0],
    ...               [2.0,3.0]])
    >>> E2V = np.array([[0,4,3],
    ...                 [0,1,4],
    ...                 [1,5,4],
    ...                 [1,2,5],
    ...                 [3,7,6],
    ...                 [3,4,7],
    ...                 [4,8,7],
    ...                 [4,5,8],
    ...                 [6,10,9],
    ...                 [6,7,10],
    ...                 [7,11,10],
    ...                 [7,8,11]])
    >>> E2edge = np.array([[0,1]])
    >>> E2point = np.array([2,3,4,5])
    >>> cells = {5:E2V,3:E2edge,1:E2point}
    >>> pdata=np.ones((12,2))
    >>> pvdata=np.ones((12*3,2))
    >>> cdata={5:np.ones((12,2)),3:np.ones((1,2)),1:np.ones((4,2))}
    >>> cvdata={5:np.ones((3*12,2)),3:np.ones((3*1,2)), 1:np.ones((3*4,2))}
    >>> write_vtu(V=V, cells=cells, fname='test.vtu')

    See Also
    --------
    write_mesh

    )   N   N   NN   r	   r	      r
         zfname is assumed to be a stringr   r      z4cell array must have positive integer keys in [1,14]NzPoly Data not implemented yetz#Cell array cannot be empty for key zCell array has z columns. Expected .r   zpdata array should be length z (not )r   zpvdata array should be of size z (or multiples) (it is now z).zcdata dimension problemzSize mismatch with cdata z and cells z$cdata array cannot be empty for key zcvdata dimension problemz#size mismatch with cvdata and cellsz%cvdata array cannot be empty for key )r   )dtypeuint8VTKVTKFileUnstructuredGridz0.1LittleEndian)typeversion
byte_orderPiece)NumberOfPointsNumberOfCellsPoints	DataArrayFloat32vertices3ascii)r   NameNumberOfComponentsformatCellsInt32connectivity)r   r"   r$   offsetsUInt8types	PointDatazpdata 1zpvdata CellDatazcdata zcvdata wzutf-8)encoding
)newlz.vtu error (z): )$
isinstancestr
ValueErrorshapenphstackzerosintlistrangeNotImplementedErrorndimreshapesizeemptyint32onesravelvstackxmldomminidomDocumentcreateElementNS_set_attributescreateTextNode_a2scumsumappendappendChildopenwritexmlOSErrorprinterrnostrerror)3Vcellspdatapvdatacdatacvdatafnamevtk_cell_infoNdofdimkeyn_pdatan_pvdatan_cdatan_cvdataNcellscell_indcell_offset	cell_type	cdata_all
cvdata_allszoffsetuudocrootdgridpiecepointspoints_datapoints_data_str
cells_datacells_data_strcells_offset_datacells_offset_data_strcells_type_datacells_type_data_str	pointdata	pdata_obj	pdata_stri
pvdata_obj
pvdata_strcelldata	cdata_obj	cdata_str
cvdata_obj
cvdata_strfes3                                                      T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/vis/vtk_writer.py	write_vtur      s   D ONNM eS!! <:;;; ID#
axxIq"(D!9--.//
  @ @C%% 	U3d5B<<6H6H+H+HSTTT#&U3Z-C%&EFFF:H3HHIII75:!##s++U3Z_a,@AAE#Js!1!!444 ?uSz/?/B ? ?)6s);? ? ? @ @ @ 5 G75>>Ak!nGGGMM5:q/22E;q>T!!ZTZZUVZZZ[[[ H76??Q|AHHH^^V[!$455F<?af$$ Nqv N N:@,q/N N N O O O
 G 	O 	OCwuSz""Q&&a<<#Cj.q1GGc
 0 333$%>??? 4 "3Z//s!0DEEc
Sz"eCj&6q&999  "EU3Z=Ma=P "E "E.3Cj.>q.A"E "E "E F F FSz! !M!M!MNNN "
 H 	P 	PCwvc{##a''q==%c{03HH!21!555$%?@@@ 6 $Sk116#;3CQ2GHHsc{ #q5:+;A+>'>>> !FGGGc{" !N!N!NOOO # FxBH---H(4w///KW---IIJ B B3Za s!!$"WbU'***9hc
(8(8(:(:;<<ifrk :;;Iy#(344	 !#J		Iy%*&=>>	!#C[

Y
F3K'@AA
 '/
"
"
$
$C ui00D#%	' 	'AAt u&899D w//Et99s6{{CCAAu   11F%%e[99KJc	 	AA{###((a11O w//E$$UK88J.GDDAAz"""''X77N++E;??)w??AA()))..tK4F4F4H4H/I/IJJ))%==O'W==AA''',,T)__== ##E;77III1g @ @,,UK@@AAA#&'; ;9Q<(((++Dqqq!t,=,=>>????JJ1h B B#--e[AABBB!#&'; ;:a=)))#,,T&A,-?-?@@AAAA ""5*55HII1g D D,,UK@@AAA#&'; ;9Q<(((++D111a4,A,ABBCCCCJJ1h F F#--e[AABBB!#&'; ;:a=)))#,,T*QQQT2B-C-CDDEEEEOODTU	f
{###O,,,	e	j!!!	'(((	o&&&>***!!"7888 3444	i   1g / /il+++!  1....1h 1 1jm,,,1!!*Q-0000	h1g / /Yq\***!  1....1h 1 1Z]+++1!!*Q-00007%w/// 	'1LLL&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 7 7 75QW55556666666667s<   :t> t1$t> 1t55t> 8t59t> >
u2u--u2tric           	      <   |d}dddddd}||vrt          d	|           ||         }	|dk    rEt          j        d
| j        d
                                       | j        d
         df          }
|	|
i}n|	|i}||	|i}||	|i}t          | ||||||           dS )aI  Write mesh file for basic types of elements.

    Parameters
    ----------
    fname : {string}
        file to be written, e.g. 'mymesh.vtu'
    V : {array}
        coordinate array (N x D)
    E2V : {array}
        element index array (Nel x Nelnodes)
    mesh_type : {string}
        type of elements: tri, quad, tet, hex (all 3d)
    pdata : {array}
        scalar data on vertices (N x Nfields)
    pvdata : {array}
        vector data on vertices (3*Nfields x N)
    cdata : {array}
        scalar data on cells (Nfields x Nel)
    cvdata : {array}
        vector data on cells (3*Nfields x Nel)

    Returns
    -------
    writes a .vtu file for use in Paraview

    Notes
    -----
    The difference between write_basic_mesh and write_vtu is that write_vtu is
    more general and requires dictionaries of cell information.
    write_basic_mesh calls write_vtu

    Examples
    --------
    >>> import numpy as np
    >>> from pyamg.vis import write_basic_mesh
    >>> V = np.array([[0.0,0.0],
    ...               [1.0,0.0],
    ...               [2.0,0.0],
    ...               [0.0,1.0],
    ...               [1.0,1.0],
    ...               [2.0,1.0],
    ...               [0.0,2.0],
    ...               [1.0,2.0],
    ...               [2.0,2.0],
    ...               [0.0,3.0],
    ...               [1.0,3.0],
    ...               [2.0,3.0]])
    >>> E2V = np.array([[0,4,3],
    ...                 [0,1,4],
    ...                 [1,5,4],
    ...                 [1,2,5],
    ...                 [3,7,6],
    ...                 [3,4,7],
    ...                 [4,8,7],
    ...                 [4,5,8],
    ...                 [6,10,9],
    ...                 [6,7,10],
    ...                 [7,11,10],
    ...                 [7,8,11]])
    >>> pdata=np.ones((12,2))
    >>> pvdata=np.ones((12*3,2))
    >>> cdata=np.ones((12,2))
    >>> cvdata=np.ones((3*12,2))
    >>> write_basic_mesh(V, E2V=E2V, mesh_type='tri',pdata=pdata,
    ...                  pvdata=pvdata, cdata=cdata, cvdata=cvdata,
    ...                  fname='test.vtu')

    See Also
    --------
    write_vtu

    Nvertexr   r   	   
      )r   r   quadtethexzUnknown mesh_type=r   )rV   rW   rX   rY   rZ   r[   r\   )r4   r6   aranger5   r>   r   )rV   E2V	mesh_typerX   rY   rZ   r[   r\   map_type_to_keyr`   uidxs              r   write_basic_meshr   o  s    V {	!"1a2NNO''9i99:::
)
$CHyAGAJ''//Q@@DkCjevE&&7 7 7 7 7 7    c                 H    | D ]}|                     || |                    dS )z)Set attributes from dictionary of values.N)setAttribute)rp   elmr`   s      r   rJ   rJ     s8     & &af%%%%& &r   c                 h    d}|                     d |                                 D                       S )zConvert to string. c                     g | ]}| d S )  ).0vs     r   
<listcomp>z_a2s.<locals>.<listcomp>  s    333A1333r   )joinrC   )anewstrs     r   rL   rL     s0    F;;33333444r   )NNNNr   )Nr   NNNNr   )	__doc__xml.dom.minidomrE   numpyr6   r   r   rJ   rL   r   r   r   <module>r      s             EI ]7 ]7 ]7 ]7@ -2(,4@b7 b7 b7 b7J& & &5 5 5 5 5r   