a
    /§»aÈ^  ã                   @   s–   d Z ddlZddlmZmZmZmZmZm	Z	m
Z
mZmZ ddd„Zddd„Zddd„Zddd„Zddd„Zddd„Zd dd„Zd!dd„Zd"dd„ZdS )#z<High level Python library to export data to binary VTK file.é    Né   )	ÚVtkFileÚVtkUnstructuredGridÚVtkImageDataÚVtkRectilinearGridÚVtkStructuredGridÚ	VtkVertexÚVtkLineÚVtkPolyLineÚVtkPixelc           	         s:  ˆrxt ˆ ¡ ƒ}t‡fdd„|D ƒd ƒ}t‡fdd„|D ƒd ƒ}| jd||d� |D ]}ˆ| }|  ||¡ qT|  d¡ ˆ rðt ˆ  ¡ ƒ}t‡ fdd„|D ƒd ƒ}t‡ fdd„|D ƒd ƒ}| jd||d� |D ]}ˆ | }|  ||¡ qÌ|  d¡ |�r6t | ¡ ƒ}|  d	¡ |D ]}|| }|  ||¡ �q|  d	¡ d S )
Nc                 3   s"   | ]}t ˆ | tjƒr|V  qd S ©N©Ú
isinstanceÚnpZndarray©Ú.0Úkey©Ú	pointData© úC/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/pyevtk/hl.pyÚ	<genexpr>1   ó    z!_addDataToFile.<locals>.<genexpr>c                 3   s    | ]}t ˆ | tƒr|V  qd S r   ©r   Útupler   r   r   r   r   2   r   ZPoint)ÚscalarsÚvectorsc                 3   s"   | ]}t ˆ | tjƒr|V  qd S r   r   r   ©ÚcellDatar   r   r   =   r   c                 3   s    | ]}t ˆ | tƒr|V  qd S r   r   r   r   r   r   r   >   r   ZCellZField)ÚlistÚkeysÚnextZopenDataÚaddDataZ	closeData)	ÚvtkFiler   r   Ú	fieldDatar    r   r   r   Údatar   )r   r   r   Ú_addDataToFile,   s2    


r&   c                 C   s”   |d ur0t | ¡ ƒ}|D ]}|| }|  |¡ q|d ur`t | ¡ ƒ}|D ]}|| }|  |¡ qH|d ur�t | ¡ ƒ}|D ]}|| }|  |¡ qxd S r   )r   r    Ú
appendData)r#   r   r   r$   r    r   r%   r   r   r   Ú_appendDataToFileP   s    r(   ©ç        r*   r*   ©ç      ð?r,   r,   c                 C   st  |dus|dusJ ‚d}d}|dur„t | ¡ ƒ}||d  }	t|	dƒrN|	j}n4|	d jdkr‚|	d jdkr‚|	d jdkr‚|	d j}nŠ|du�rt | ¡ ƒ}||d  }	t|	dƒr¸|	j}n4|	d jdkrì|	d jdkrì|	d jdkrì|	d j}|d d |d d |d d f}t| tƒ}
|
j||||d� |
j||d	� t	|
|||ƒ |
 
¡  |
 ¡  t|
|||ƒ |
 ¡  |
 ¡ S )
a›  
    Export data values as a rectangular image.

    Parameters
    ----------
    path : str
        name of the file without extension where data should be saved.
    origin : tuple, optional
        grid origin.
        The default is (0.0, 0.0, 0.0).
    spacing : tuple, optional
        grid spacing.
        The default is (1.0, 1.0, 1.0).
    cellData : dict, optional
        dictionary containing arrays with cell centered data.
        Keys should be the names of the data arrays.
        Arrays must have the same dimensions in all directions and can contain
        scalar data ([n,n,n]) or vector data ([n,n,n],[n,n,n],[n,n,n]).
        The default is None.
    pointData : dict, optional
        dictionary containing arrays with node centered data.
        Keys should be the names of the data arrays.
        Arrays must have same dimension in each direction and
        they should be equal to the dimensions of the cell data plus one and
        can contain scalar data ([n+1,n+1,n+1]) or
        +1,n+1,n+1],[n+1,n+1,n+1],[n+1,n+1,n+1]).
        The default is None.
    fieldData : dict, optional
        dictionary with variables associated with the field.
        Keys should be the names of the variable stored in each array.

    Returns
    -------
    str
        Full path to saved file.

    Notes
    -----
    At least, cellData or pointData must be present
    to infer the dimensions of the image.
    N©r   r   r   r   Úshapeé   r   é   )ÚstartÚendÚoriginÚspacing©r1   r2   )r   r    Úhasattrr.   Úndimr   r   ÚopenGridÚ	openPiecer&   Ú
closePieceÚ	closeGridr(   ÚsaveÚgetFileName)Úpathr3   r4   r   r   r$   r1   r2   r    r%   Úwr   r   r   Ú
imageToVTKh   s6    1
*

*
"
r@   c                 C   s¸  d}d } }	}
|j dkrZ|j dkrZ|j dkrZ|jd |jd |jd   }}	}
d}t}n^|j dkr°|j dkr°|j dkr°|j}|d d |d d |d d   }}	}
d}t}nds¸J ‚||	|
f}t| |ƒ}|j||d� |j||d� |�r(| d	¡ | 	d
|¡ | 	d|¡ | 	d|¡ | 
d	¡ n&| d¡ | 	d|||f¡ | 
d¡ t||||ƒ | ¡  | ¡  |�rŠ| |¡ |¡ |¡ n| |||f¡ t||||ƒ | ¡  | ¡ S )a†  
    Write data values as a rectilinear or rectangular grid.

    Parameters
    ----------
    path : str
        name of the file without extension where data should be saved.
    x : array-like
        x coordinate axis.
    y : array-like
        y coordinate axis.
    z : array-like
        z coordinate axis.
    cellData : dict, optional
        dictionary containing arrays with cell centered data.
        Keys should be the names of the data arrays.
        Arrays must have the same dimensions in all directions and must contain
        only scalar data.
    pointData : dict, optional
        dictionary containing arrays with node centered data.
        Keys should be the names of the data arrays.
        Arrays must have same dimension in each direction and
        they should be equal to the dimensions of the cell data plus one and
        must contain only scalar data.
    fieldData : dict, optional
        dictionary with variables associated with the field.
        Keys should be the names of the variable stored in each array.

    Returns
    -------
    str
        Full path to saved file.

    Notes
    -----
    coordinates of the nodes of the grid. They can be 1D or 3D depending if
    the grid should be saved as a rectilinear or logically structured grid,
    respectively.
    Arrays should contain coordinates of the nodes of the grid.
    If arrays are 1D, then the grid should be Cartesian,
    i.e. faces in all cells are orthogonal.
    If arrays are 3D, then the grid should be logically structured
    with hexahedral cells.
    In both cases the arrays dimensions should be
    equal to the number of nodes of the grid.
    r-   r   r   Tr/   r0   Fr5   ZCoordinatesZx_coordinatesZy_coordinatesZz_coordinatesÚPointsÚpoints)r7   Úsizer   r.   r   r   r8   r9   ÚopenElementr"   ÚcloseElementr&   r:   r;   r'   r(   r<   r=   )r>   ÚxÚyÚzr   r   r$   r1   ZnxÚnyZnzZisRectZftypeÚsr2   r?   r   r   r   Ú	gridToVTK»   sD    0"(




rK   c                 C   sH  |j |j   kr|j ks n J ‚|j }tjd|d dd�}tj|dd�}tj|dd�}	tj|	dd…< t| tƒ}
|
 ¡  |
j	||d� |
 
d¡ |
 d	|||f¡ |
 d¡ |
 
d
¡ |
 d|¡ |
 d|¡ |
 d|	¡ |
 d
¡ t|
d||d� |
 ¡  |
 ¡  |
 |||f¡ |
 |¡ |¡ |	¡ t|
d||d� |
 ¡  |
 ¡ S )a  
    Export points and associated data as an unstructured grid.

    Parameters
    ----------
    path : str
        name of the file without extension where data should be saved.
    x : array-like
        x coordinates of the points.
    y : array-like
        y coordinates of the points.
    z : array-like
        z coordinates of the points.
    data : dict, optional
        dictionary with variables associated to each point.
        Keys should be the names of the variable stored in each array.
        All arrays must have the same number of elements.
    fieldData : dict, optional
        dictionary with variables associated with the field.
        Keys should be the names of the variable stored in each array.

    Returns
    -------
    str
        Full path to saved file.
    r   Úint32)r1   ÚstopÚdtype©rN   Úuint8N©ÚncellsÚnpointsrA   rB   ÚCellsÚconnectivityÚoffsetsÚtypes©r   r   r$   )rC   r   ÚarangeÚemptyr   Útidr   r   r8   r9   rD   r"   rE   r&   r:   r;   r'   r(   r<   r=   )r>   rF   rG   rH   r%   r$   rS   rV   rU   Ú
cell_typesr?   r   r   r   ÚpointsToVTK  s:     
ÿÿ




r]   c                 C   sf  |j |j   kr|j ks n J ‚|j d dks2J ‚|j }|j d }tjdd|d dd�}	tj|dd�}
tj|dd�}tj|dd…< t| tƒ}| ¡  |j	||d	� | 
d
¡ | d|||f¡ | d
¡ | 
d¡ | d|
¡ | d|	¡ | d|¡ | d¡ t||||d� | ¡  | ¡  | |||f¡ | |
¡ |	¡ |¡ t||||d� | ¡  | ¡ S )a  
    Export line segments that joint 2 points and associated data.

    Parameters
    ----------
    path : str
        name of the file without extension where data should be saved.
    x : array-like
        x coordinates of the points in lines.
    y : array-like
        y coordinates of the points in lines.
    z : array-like
        z coordinates of the points in lines.
    cellData : dict, optional
        dictionary with variables associated to each line.
        Keys should be the names of the variable stored in each array.
        All arrays must have the same number of elements.
    pointData : dict, optional
        dictionary with variables associated to each vertex.
        Keys should be the names of the variable stored in each array.
        All arrays must have the same number of elements.
    fieldData : dict, optional
        dictionary with variables associated with the field.
        Keys should be the names of the variable stored in each array.

    Returns
    -------
    str
        Full path to saved file.

    Notes
    -----
    x, y, z are 1D arrays with coordinates of the vertex of the lines.
    It is assumed that each line.
    is defined by two points,
    then the lenght of the arrays should be equal to 2 * number of lines.
    r0   r   r   rL   )r1   ÚsteprM   rN   rO   rP   NrQ   rA   rB   rT   rU   rV   rW   rX   )rC   r   rY   rZ   r	   r[   r   r   r8   r9   rD   r"   rE   r&   r:   r;   r'   r(   r<   r=   )r>   rF   rG   rH   r   r   r$   rS   rR   rV   rU   r\   r?   r   r   r   Ú
linesToVTK]  sF    & 
ÿÿ




ÿÿr_   c                 C   sn  |j |j   kr|j ks n J ‚|j }|j }	tj|	dd�}
d}t|	ƒD ]}||| 7 }||
|< qFtj|dd�}tj|dd�}tj|dd…< t| t	ƒ}| 
¡  |j|	|d� | d¡ | d|||f¡ | d¡ | d	¡ | d
|¡ | d|
¡ | d|¡ | d	¡ t||||d� | ¡  | ¡  | |||f¡ | |¡ |
¡ |¡ t||||d� | ¡  | ¡ S )a\  
    Export line segments that joint n points and associated data.

    Parameters
    ----------
    path : str
        name of the file without extension where data should be saved.
    x : array-like
        x coordinates of the points in lines.
    y : array-like
        y coordinates of the points in lines.
    z : array-like
        z coordinates of the points in lines.
    pointsPerLine : array-like
        Points in each poly-line.
    cellData : dict, optional
        dictionary with variables associated to each line.
        Keys should be the names of the variable stored in each array.
        All arrays must have the same number of elements.
    pointData : dict, optional
        dictionary with variables associated to each vertex.
        Keys should be the names of the variable stored in each array.
        All arrays must have the same number of elements.
    fieldData : dict, optional
        dictionary with variables associated with the field.
        Keys should be the names of the variable stored in each array.

    Returns
    -------
    str
        Full path to saved file.
    rL   rO   r   rP   NrQ   rA   rB   rT   rU   rV   rW   rX   )rC   r   ÚzerosÚrangerY   rZ   r
   r[   r   r   r8   r9   rD   r"   rE   r&   r:   r;   r'   r(   r<   r=   )r>   rF   rG   rH   ZpointsPerLiner   r   r$   rS   rR   rV   ÚiiÚirU   r\   r?   r   r   r   ÚpolyLinesToVTK´  sL    # ÿ
ÿ




ÿÿrd   c
                 C   s  |j |j   kr|j ks n J ‚|j }
|j }|j |ks:J ‚t| tƒ}| ¡  |j||
d� | d¡ | d|||f¡ | d¡ | d¡ | d|¡ | d|¡ | d|¡ | d¡ t||||	d� | 	¡  | 
¡  | |||f¡ | |¡ |¡ |¡ t||||	d� | ¡  | ¡ S )	a1  
    Export unstructured grid and associated data.

    Parameters
    ----------
    path : str
        name of the file without extension where data should be saved.
    x : array-like
        x coordinates of the vertices.
    y : array-like
        y coordinates of the vertices.
    z : array-like
        z coordinates of the vertices.
    connectivity : array-like
        1D array that defines the vertices associated to each element.
        Together with offset define the connectivity or topology of the grid.
        It is assumed that vertices in an element are listed consecutively.
    offsets : array-like
        1D array with the index of the last vertex of each element
        in the connectivity array.
        It should have length nelem,
        where nelem is the number of cells or elements in the grid..
    cell_types : TYPE
        1D array with an integer that defines the cell type of
        each element in the grid.
        It should have size nelem.
        This should be assigned from evtk.vtk.VtkXXXX.tid, where XXXX represent
        the type of cell.
        Please check the VTK file format specification for allowed cell types.
    cellData : dict, optional
        dictionary with variables associated to each cell.
        Keys should be the names of the variable stored in each array.
        All arrays must have the same number of elements.
    pointData : dict, optional
        dictionary with variables associated to each vertex.
        Keys should be the names of the variable stored in each array.
        All arrays must have the same number of elements.
    fieldData : dict, optional
        dictionary with variables associated with the field.
        Keys should be the names of the variable stored in each array.

    Returns
    -------
    str
        Full path to saved file.
    rQ   rA   rB   rT   rU   rV   rW   rX   )rC   r   r   r8   r9   rD   r"   rE   r&   r:   r;   r'   r(   r<   r=   )r>   rF   rG   rH   rU   rV   r\   r   r   r$   rS   rR   r?   r   r   r   ÚunstructuredGridToVTK
  s6    : 




ÿÿre   é   c           %      C   s  ddl }d|j | }t dd|j |¡}|t |¡ | }|t |¡ | }|| | }tj||| |d�}||d  }|| }t |¡}t |¡}t |¡}d}t|d ƒD ]>}t|ƒD ]0}|| ||< || ||< || ||< |d }q¼q°tjd| tjd�}d}t|ƒD ]–}t|ƒD ]†}|}|d |k�r8d}n|d }|||  }|||  }|| } || }!|||d < |||d < |!||d	 < | ||d
 < |d }�q�qtj|tjd�}"t|ƒD ]}#|#d d |"|#< �qÀt 	|¡t
j }$t| |||||"|$||	|
d�
S )aA  
    Export cylinder as VTK unstructured grid.

    Parameters
    ----------
    path : str
        name of the file without extension where data should be saved.
    x0 : float
        x-center of the cylinder.
    y0 : float
        y-center of the cylinder.
    z0 : float
        lower end of the cylinder.
    z1 : float
        upper end of the cylinder.
    radius : float
        radius of the cylinder.
    nlayers : int
        Number of layers in z direction to divide the cylinder..
    npilars : int, optional
        Number of points around the diameter of the cylinder.
        Higher value gives higher resolution to represent the curved shape.
        The default is 16.
    cellData : dict, optional
        dictionary with variables associated to each cell.
        Keys should be the names of the variable stored in each array.
        Arrays should have number of elements equal to
        ncells = npilars * nlayers.
    pointData : dict, optional
        dictionary with variables associated to each vertex.
        Keys should be the names of the variable stored in each array.
        Arrays should have number of elements equal to
        npoints = npilars * (nlayers + 1).
    fieldData : dict, optional
        dictionary with variables associated with the field.
        Keys should be the names of the variable stored in each array.

    Returns
    -------
    str
        Full path to saved file.

    Notes
    -----
    This function only export vertical shapes for now.
    However, it should be easy to
    rotate the cylinder to represent other orientations.
    r   Ng       @r*   )r^   r   é   rO   r0   r/   )rU   rV   r\   r   r   r$   )ÚmathÚpir   rY   ÚcosÚsinr`   ra   Úint64Zonesr   r[   re   )%r>   Zx0Zy0Zz0Zz1ZradiusZnlayersZnpilarsr   r   r$   ÚmZdpiZanglesrF   rG   ZdzrH   rS   rR   ÚxxÚyyÚzzrb   ÚkÚpÚconnÚlZp0Úp1Zn0Zn1Zn2Zn3rV   rc   Úctyper   r   r   ÚcylinderToVTKh  sh    =


örw   )N)N)r)   r+   NNN)NNN)NN)NNN)NNN)NNN)rf   NNN)Ú__doc__Únumpyr   Zvtkr   r   r   r   r   r   r	   r
   r   r&   r(   r@   rK   r]   r_   rd   re   rw   r   r   r   r   Ú<module>   s0   ,
$
     ú
S
^
D
X ÿ
^   ö
f    õ