
    tjt                     H   d Z ddlmZ ddlZ	 ddlZddlmZ	 ddlm
Z ddlZ	 dd	lmZ dd
lmZ d ZdodZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Ze	j<                  dfdZdpdZ dpdZ!dpdZ"dpdZ#dpdZ$dpdZ%dpd Z&dpd!Z'dpd"Z(dpd#Z)d$ Z*d% Z+dqd&Z,dpd'Z-dpd(Z.d) Z/drd*Z0drd+Z1 eejd                        Z2d,e2_          eejf                        Z3d-e3_          eejh                        Z4d.e4_          eejj                        Z5d/e5_          eejl                        Z6d0e6_          eejn                        Z7d1e7_          eejp                        Z8d2e8_          eejr                        Z9d3e9_          eejt                        Z:d4e:_          eejv                        Z;d5e;_          eejx                        Z<d6e<_          eejz                        Z=d7e=_          eej|                        Z>d8e>_          eej~                        Z?d9e?_          eej                        Z@d:e@_          eej                        ZAd;eA_          eej                        ZBd<eB_          eej                        ZCd=eC_          eej                        ZDd>eD_          eej                        ZEd?eE_          eej                        ZFd@eF_          eej                        ZGdAeG_          eej                        ZHdBeH_          eej                        ZIdCeI_          eej                        ZJdDeJ_          eej                        ZKdEeK_          eej                        ZLdFeL_          eej                        ZMdGeM_          eej                        ZNdHeN_          eej                        ZOdIeO_          eej                        ZPdJeP_          eej                        ZQdKeQ_          eej                        ZRdLeR_          eej                        ZSdMeS_          eej                        ZTdNeT_          eej                        ZUdOeU_          eej                        ZVdOeV_          eej                        ZWdPeW_          eej                        ZXdQeX_          eej                        ZYdReY_          eej                        ZZdSeZ_          eej                        Z[dTe[_          eej                        Z\dUe\_          eej                        Z]dUe]_          eej                        Z^dVe^_          eej                        Z_dWe__          eej                        Z`dXe`_          eej                        ZadXea_          eej                        ZbdYeb_          eej                        ZcdZec_          eej                        Zdd[ed_          eej                        Zed\ee_          eej                        Zfd]ef_          eej                        Zgd^eg_          eej                        Zhd_eh_          eej                        Zid`ei_          eej                        Zjdaej_          eej                        Zkdbek_          eej                        Zldcel_          eej                        Zmddem_          eej                        Zndeen_          eej                        Zodfeo_          eej                        Zpdgep_          eej                        Zqdheq_          eej                        Zrdier_          eej                        Zsdjes_          eej                        Ztdket_          eej                        Zudleu_          eej                        Zvdmev_          eej                        Zwdnew_         y# e$ r	  ed      w xY w# e$ r dZdZY qw xY w)sa  
This module provides a number of algorithms that can be used with
the dataset classes defined in the dataset_adapter module.
See the documentation of the dataset_adapter for some examples.
These algorithms work in serial and in parallel as long as the
data is partitioned according to VTK data parallel execution
guidelines. For details, see the documentation of individual
algorithms.
    )absolute_importNzWThis module depends on the numpy module. Please makesure that it is installed properly.   )dataset_adapter)internal_algorithms   )vtkMultiProcessController)vtkMPI4PyCommunicatorc                     |t         j                  u rg S g }|j                  D ]L  }|t         j                  u r |j                  t         j                         5|j                   | |g|        N |S )zApply a function to each member of a VTKCompositeDataArray.
    Returns a list of arrays.

    Note that this function is mainly for internal use by this module.)dsa	NoneArrayArraysappend)funcarrayargsresas        R/DATA/.local/lib/python3.12/site-packages/vtkmodules/numpy_interface/algorithms.py_apply_func2r      s_    
 	
C\\JJs}}%JJtA~~&	 
 J    c                     |t         j                  u rt         j                  S t        |      t         j                  k(  r,t        j                  t	        | ||      |j
                        S  | |g| S )zmApply a function to each member of a VTKCompositeDataArray.
    VTKArray and numpy arrays are also supported.dataset)r   r   typeVTKCompositeDataArrayr   DataSet)r   r   r   s      r   apply_ufuncr   ,   s_     }}	e11	1((dE4)HTYTaTabbE!D!!r   c                       fd}|S )z Given a ufunc, creates a closure that applies it to each member
    of a VTKCompositeDataArray.

    Note that this function is mainly for internal use by this module.c                     t        | |      S N)r   )r   r   ufuncs     r   	new_ufuncz_make_ufunc.<locals>.new_ufunc;   s    5%..r    )r!   r"   s   ` r   _make_ufuncr$   6   s    
/r   c                    t        |      t        j                  k(  rt        |      t        j                  k(  rg }t        |j                  |j                        D ]}  \  }}|t        j
                  u s|t        j
                  u r |j                  t        j
                         Jt        j                  ||      }|j                   | |d   |d                 t        j                  ||j                        S t        |      t        j                  k(  rg }|j                  D ]h  }|t        j
                  u r |j                  t        j
                         5t        j                  ||      }|j                   | |d   |d                j t        j                  ||j                        S |t        j
                  u rt        j
                  S t        j                  ||      } | |d   |d         S )ag  Apply a two argument function to each member of a VTKCompositeDataArray
    and another argument The second argument can be a VTKCompositeDataArray, in
    which case a one-to-one match between arrays is assumed. Otherwise, the
    function is applied to the composite array with the second argument repeated.
    VTKArray and numpy arrays are also supported.r   r   r   )	r   r   r   zipr   r   r   reshape_append_onesr   )dfuncarray1val2r   a1a2lr   s           r   apply_dfuncr.   ?   sz    F|s000T$Z3C\C\5\&--5FBS]]"bCMM&9

3==)++B3

51qt,- 6 ((GG	f22	2ACMM!

3==)++At4

51qt,-  ((GG	3==	 }}##FD1QqT1Q4  r   c                       fd}|S )z Given a function that requires two arguments, creates a closure that
    applies it to each member of a VTKCompositeDataArray.

    Note that this function is mainly for internal use by this module.c                     t        | |      S r    )r.   )r)   r*   r(   s     r   	new_dfuncz_make_dfunc.<locals>.new_dfuncb   s    5&$//r   r#   )r(   r1   s   ` r   _make_dfuncr2   ]   s    
0r   c                      d fd	}|S )z Given a function that requires two arguments (one array, one dataset),
    creates a closure that applies it to each member of a VTKCompositeDataArray.
    Note that this function is mainly for internal use by this module.c                    t        |       t        j                  k(  r~g }| j                  D ]L  }|t        j                  u r |j                  t        j                         5|j                   ||             N t        j                  || j                        S | t        j                  u rt        j                  S  | |      S Nr   )r   r   r   r   r   r   r   )r   dsr   r   dsfuncs       r   
new_dsfuncz _make_dsfunc.<locals>.new_dsfuncj   s    ;#333C\\%JJs}}-JJva}-	 "
 ,,SEMMJJcmm#== %$$r   r    r#   )r7   r8   s   ` r   _make_dsfuncr9   f   s    % r   c                       fd}|S )z Given a function that requires a dataset, creates a closure that
    applies it to each member of a VTKCompositeDataArray.

    Note that this function is mainly for internal use by this module.c                     t        |       t        j                  k(  r7g }| D ]  }|j                   |              t        j                  ||       S  |       S r5   )r   r   CompositeDataSetr   r   )r6   r   r   r7   s      r   new_dsfunc2z"_make_dsfunc2.<locals>.new_dsfunc2~   sQ    8s+++C

6'?+ ,,SB??":r   r#   )r7   r=   s   ` r   _make_dsfunc2r>   y   s    
 r   c                     ddl m} | t        k(  rd}nt        j                  |       j
                  }	 |j                  |   S # t        $ r |j                  |   cY S w xY w)Nr   MPIb)	mpi4pyrA   boolnumpydtypechar__TypeDict__AttributeError	_typedict)ntyperA   typecodes      r   _lookup_mpi_typerM      sY    };;u%**')) '}}X&&'s   A AAc                    ddl m} t        j                  ddgt        j                        }| t
        j                  urt        |       }t        |      dk(  r(t        j                  ddgt        j                        }n^t        |      dk(  r+t        j                  |d   dgt        j                        }n%t        j                  |t        j                        }t        j                  |t        j                        }t        t        j                        }|j                  ||g||g|j                         |d   dk(  rt        j                  |d   f      }|d   }n|d   |d   z  }|d   dk(  rd}||fS )Nr   r@   rF   r   )rC   rA   rE   r   int32r   r   shapelenrM   	AllreduceMAX)r   commrA   dimsshpmax_dimsmpitypesizes           r   _reduce_dimsr[      s&   ;;1vU[[1DCMM!Els8q=;;1vU[[9DX];;A{%++>D;;s%++6D{{4u{{3Hu{{+GNND'?Xw$7A{a;;~.{{8A;&{adr   c                 B   t        |      t        j                  k(  r7||dk(  r| j                  ||      }net	        | j                         ||f      }nH | j                         ||      }|t        j                  ur|j                  t        j                        }||dk(  rc|t        t        j                         }|rD|j                  d      r2|j                         dkD  r	 ddlm} t%        j&                  |j)                               }t+        ||      \  }}|dk(  rt        j                  S |t        j                  u rKt        j,                  |      rd}t        j.                  |      }|j1                  | j3                                t        j4                  |      }	t7        |j8                        }
|j;                  ||
g|	|
g| j=                                |t        j                  u rt        j                  S |	}|S # t         $ r t#        d      w xY w)Nr   vtkMPIControllerr   r@   z3MPI4Py is required to perform multi-rank operationsr#   )r   r   r   serial_compositer   opr   astyperE   float64r   GetGlobalControllerIsAGetNumberOfProcessesrC   rA   ImportErrorRuntimeErrorr	   ConvertToPythonGetCommunicatorr[   isscalaremptyfilldefaultr   rM   rF   rS   mpi_op)implr   axis
controllerr   rA   rU   rX   rZ   res_recvmpi_types              r   _global_funcrs      s   E{c///<419''t4Cdggi8Cdggit$cmm#**U]]+C|tqy";"G2FFHJ*..);<A`A`AbefAfZ& )889S9S9UVD)#t4NHd qy}}$cmm#>>(+!Hkk(+({{3'H'		2HNNC?Xx,@$++-P%}}$CJ1  Z"#XYYZs   *H	 	Hc                 8   t        |       t        j                  k(  rt        |      t        j                  k(  rg }t        | j                  |j                        D ]<  \  }}t        j
                  ||      }|j                  t        |d   |d                > t        j                  || j                        S t        |       t        j                  k(  rkg }| j                  D ]9  }t        j
                  ||      }|j                  t        |d   |d                ; t        j                  || j                        S | t        j                  u r|S |t        j                  u r| S t        j
                  | |      }t        j                  |d   |d         S )zImplements element by element or (bitwise, | in C/C++) operation.
    If one of the arrays is a NoneArray, this will return the array
    that is not NoneArray, treating NoneArray as 0 in the or operation.r   r   r   )r   r   r   r&   r   r'   r   
bitwise_orr   r   rE   )r)   array2r   r+   r,   r-   r   s          r   ru   ru      sI    F|s000T&\SE^E^5^&--7FB''B/AJJz!A$!-. 8 ((GG	f22	2A''62AJJz!A$!-.  ((GG	3==	 	3==	 ##FF3!ad++r   c                 `    ddl m} | j                  |j                            }t	        ||      S )a8  This method will create a ghost array corresponding to an
    input with NaN values. For each NaN value, the output array will
    have a corresponding value of vtkmodules.vtkCommonDataModel.vtkDataSetAttributes.HIDDENPOINT.
    These values are also combined with any ghost values that the
    dataset may have.r   vtkDataSetAttributes)vtkCommonDataModelry   	PointDataGhostArrayNamemake_mask_from_NaNsr   r   ry   ghostss       r   make_point_mask_from_NaNsr      s.     :3BBDEFuf--r   c                 b    ddl m} | j                  |j                            }t	        ||d      S )a7  This method will create a ghost array corresponding to an
    input with NaN values. For each NaN value, the output array will
    have a corresponding value of vtkmodules.vtkCommonDataModel.vtkDataSetAttributes.HIDDENCELL.
    These values are also combined with any ghost values that the
    dataset may have.r   rx   T)rz   ry   CellDatar|   r}   r~   s       r   make_cell_mask_from_NaNsr      s0     :2AACDFufd33r   Fc                     ddl m} |r|j                  }n|j                  }t	        t        |       j                  t        j                        |z  |      S )a  This method will create a ghost array corresponding to an
    input with NaN values. For each NaN value, the output array will
    have a corresponding value of vtkmodules.vtkCommonDataModel.vtkDataSetAttributes.HIDDENPOINT or
    HIDDENCELL is the is_cell argument is true. If an input ghost_array
    is passed, the array is bitwise_or'ed with it, simply adding
    the new ghost values to it.r   rx   )	rz   ry   
HIDDENCELLHIDDENPOINTru   isnanr`   rE   uint8)r   ghost_arrayis_cellry   
mask_values        r   r}   r}     sJ     :)44
)55
eEl))%++6C r   c                 <     G d d      }t         |       | ||      S )a  Returns the sum of all values along a particular axis (dimension).
    Given an array of m tuples and n components:
    * Default is to return the sum of all values in an array.
    * axis=0: Sum values of all components and return a one tuple,
      n-component array.
    * axis=1: Sum values of all components of each tuple and return an
      m-tuple, 1-component array.

    When called in parallel, this function will sum across processes
    when a controller argument is passed or the global controller is
    defined. To disable parallel summing when running in parallel, pass
    a dummy controller as follows:

    sum(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    c                   $    e Zd Zd Zd Zd Zd Zy)sum.<locals>.SumImplc                 "    t         j                  S r    algssumselfs    r   r_   zsum.<locals>.SumImpl.op&      88Or   c                 &    ddl m} |j                  S Nr   r@   rC   rA   SUMr   rA   s     r   rm   zsum.<locals>.SumImpl.mpi_op)      "77Nr   c                     d }|j                   }|D ]d  }|t        j                  us|4t        j                  ||      j                  t        j                        }L|t        j                  ||      z  }f |S r    )r   r   r   r   r   r`   rE   ra   )r   r   ro   r   arraysr   s         r   r^   z%sum.<locals>.SumImpl.serial_composite-  sf    C\\FCMM){"hhq$/66u}}Etxx400  Jr   c                 ,    t        j                  d      S Nr   rE   ra   r   s    r   rl   zsum.<locals>.SumImpl.default8      ==##r   N__name__
__module____qualname__r_   rm   r^   rl   r#   r   r   SumImplr   %  s    					$r   r   rs   )r   ro   rp   r   s       r   r   r     s!     $ $, 	5$
;;r   c                 <     G d d      }t         |       | ||      S )a  Returns the max of all values along a particular axis (dimension).
    Given an array of m tuples and n components:
    * Default is to return the max of all values in an array.
    * axis=0: Return the max values of all tuples and return a
      one tuple, n-component array.
    * axis=1: Return the max values of all components of each tuple
      and return an m-tuple, 1-component array.

    When called in parallel, this function will compute the max across
    processes when a controller argument is passed or the global controller
    is defined. To disable parallel summing when running in parallel, pass a
    dummy controller as follows:

    max(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    c                   $    e Zd Zd Zd Zd Zd Zy)max.<locals>.MaxImplc                 "    t         j                  S r    r   maxr   s    r   r_   zmax.<locals>.MaxImpl.opN  r   r   c                 &    ddl m} |j                  S r   rC   rA   rT   r   s     r   rm   zmax.<locals>.MaxImpl.mpi_opQ  r   r   c                    t        t        j                  ||f      }g }|D ]&  }|t        j                  us|j                  |       ( |g u ry t        j                  |d      j                  t        j                        S Nr   )ro   )	r   r   r   r   r   r   r`   rE   ra   r   r   ro   r   
clean_listr   s         r   r^   z%max.<locals>.MaxImpl.serial_compositeU  l    txx8CJCMM)%%a(  R88JQ/66u}}EEr   c                 \    t        j                  t         j                        j                  S r    rE   finfora   minr   s    r   rl   zmax.<locals>.MaxImpl.default_      ;;u}}-111r   Nr   r#   r   r   MaxImplr   M      			F	2r   r   r   )r   ro   rp   r   s       r   r   r   =  s!     2 2* 	5$
;;r   c                 <     G d d      }t         |       | ||      S )a  Returns the min of all values along a particular axis (dimension).
    Given an array of m tuples and n components:
    * Default is to return the min of all values in an array.
    * axis=0: Return the min values of all tuples and return a one
      tuple, n-component array.
    * axis=1: Return the min values of all components of each tuple and
      return an m-tuple, 1-component array.

    When called in parallel, this function will compute the min across processes
    when a controller argument is passed or the global controller is defined.
    To disable parallel summing when running in parallel, pass a dummy controller as follows:

    min(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    c                   $    e Zd Zd Zd Zd Zd Zy)min.<locals>.MinImplc                 "    t         j                  S r    r   r   r   s    r   r_   zmin.<locals>.MinImpl.opt  r   r   c                 &    ddl m} |j                  S r   rC   rA   MINr   s     r   rm   zmin.<locals>.MinImpl.mpi_opw  r   r   c                    t        t        j                  ||f      }g }|D ]&  }|t        j                  us|j                  |       ( |g u ry t        j                  |d      j                  t        j                        S r   )	r   r   r   r   r   r   r`   rE   ra   r   s         r   r^   z%min.<locals>.MinImpl.serial_composite{  r   r   c                 \    t        j                  t         j                        j                  S r    rE   r   ra   r   r   s    r   rl   zmin.<locals>.MinImpl.default  r   r   Nr   r#   r   r   MinImplr   s  r   r   r   r   r   ro   rp   r   s       r   r   r   d  s!    2 2* 	5$
;;r   c                    ||dkD  r | j                         |||      S 	 |j                  }t        |      }|t        j
                  k(  s|t        j                  k(  rAddlm	} t	        j                  |g      } |       }|j                  d|j                         |}t        | j                         ||f      }|t        t        j                          }|r|j#                  d      rddlm}	 t)        j*                  |j-                               }
t        j.                  }|D ]  }|t        j.                  us n t1        ||
      \  }}|dk(  rt        j.                  S g }t        j2                  d      }||j5                         }|j7                  d        |j9                  d       |j;                         s|j=                         }t        j>                  ||f      jA                  t        j2                        }|jC                         |jE                  |       |jG                          |j;                         st        jH                  dt        j2                        }tK        t        j2                        }|
jM                  ||g||g|	jN                         t        jP                  |d	z   t        j2                        }|D ]  }d	||<   	 t        jH                  |      }|
jM                  ||g||g|	jR                         t        jT                  |d	k        rt	        j                  ||
      S g }tW        tY        |            D ]  }||   d	kD  s|jE                  |        tY        |      }|dk(  rt	        j                  ||
      S t        jZ                  ||z        }|j]                  | j_                                |s|j5                         }|j7                  d        g }|j;                         s@|jE                  |j=                                |jG                          |j;                         s@ta        ||      D ]K  \  }}d}	 |jc                  |      }|s|t        j.                  us0|jg                         ||z  |d	z   |z   M t        jH                  |      }tK        t        jh                        }|
jM                  ||g||g| jk                                |t        j.                  u rt        j.                  S tW        |      D ]_  }||   }d}	 |jc                  |      }|s|d	k(  rt	        j
                  ||         |<   @|||z  |d	z   |z   jm                  |      |<   a t	        j                  ||
      S # t        $ r d }Y yw xY w# td        $ r d}Y Mw xY w# td        $ r d}Y w xY w)Nr   )ro   rp   r   )vtkMultiBlockDataSetr]   r@   FrO   r   r   T)7r_   r   rI   r   r   VTKArrayrE   ndarrayrz   r   r   SetBlock	VTKObjectr   op2r   rb   rc   rC   rA   r	   rg   rh   r   r[   rP   NewIterator
UnRegisterSetSkipEmptyNodesIsDoneWithTraversalGetCurrentFlatIndexr   r`   GetCurrentDataObjectr   GoToNextItemr   rM   rS   rT   zerosr   allrangerR   rj   rk   rl   r&   index
ValueErrorflattendoublerm   reshape)rn   r   ro   rp   r   tr   r6   resultsrA   rU   r   rX   rZ   idslmax_idit_idmax_idrY   has_idsid_count
reduce_ids	to_reducelresults_ressuccesslocrresultsis                                 r   _global_per_blockr     s    D1HtwwyTjAA-- 	UACLLA.=))5'2!#
Aw(()488:utg6G7C.BBD
jnn%78$44Z5O5O5QR mmC#--'  &c40$ 19==  ++a.$$&BMM$  ',,.,,.))WcN3::5;;G**,8JJsO! ,,. Qekk2"5;;/)FG+<cggF++fQhekk:CGCL ;;w')Hg+>H99X]#,,WgFF 
X'C}q !!#& ( 
O	 >,,WgFF
 ;;tI~.dlln%
 $$&BMM$C,,.

21134! ,,.
 S'*ICG  &&s+ s}},6:llnHSXs1udl3 + ;;x("5<<0'*Xw,?OCMM!==  y!AQ-CG iin 19#&<<#<GCL#+AdFAaC:#>#F#Fx#PGCL " $$Wg>>y  @    (    s5   V V++V=V('V(+V:9V:=W
Wc                 <     G d d      }t         |       | ||      S )a  Returns the sum of all values along a particular axis (dimension) for
    each block of an VTKCompositeDataArray.

    Given an array of m tuples and n components:
    * Default is to return the sum of all values in an array.
    * axis=0: Sum values of all components and return a one tuple,
      n-component array.
    * axis=1: Sum values of all components of each tuple and return an
      m-tuple, 1-component array.

    When called in parallel, this function will sum across processes
    when a controller argument is passed or the global controller is
    defined. To disable parallel summing when running in parallel, pass
    a dummy controller as follows:

    sum_per_block(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    c                   $    e Zd Zd Zd Zd Zd Zy)&sum_per_block.<locals>.SumPerBlockImplc                     t         S r    )r   r   s    r   r_   z)sum_per_block.<locals>.SumPerBlockImpl.op!      Jr   c                 "    t         j                  S r    r   r   s    r   r   z*sum_per_block.<locals>.SumPerBlockImpl.op2$  r   r   c                 &    ddl m} |j                  S r   r   r   s     r   rm   z-sum_per_block.<locals>.SumPerBlockImpl.mpi_op'  r   r   c                 ,    t        j                  d      S r   r   r   s    r   rl   z.sum_per_block.<locals>.SumPerBlockImpl.default+  r   r   Nr   r   r   r_   r   rm   rl   r#   r   r   SumPerBlockImplr      s    				$r   r   r   )r   ro   rp   r   s       r   sum_per_blockr     s"    $$ $ _.tZHHr   c                 `    ||dkD  rt        d       G d d      }t         |       | ||      S )zReturn the number of elements of each block in a VTKCompositeDataArray
    along an axis.

    - if axis is None, the number of all elements (ntuples * ncomponents) is
    returned.
    - if axis is 0, the number of tuples is returned.
    r   z1Only axis=None and axis=0 are supported for countc                   $    e Zd Zd Zd Zd Zd Zy)*count_per_block.<locals>.CountPerBlockImplc                     t         S r    )_array_countr   s    r   r_   z-count_per_block.<locals>.CountPerBlockImpl.op=  s    r   c                     t         S r    )_local_array_countr   s    r   r   z.count_per_block.<locals>.CountPerBlockImpl.op2@  s    %%r   c                 &    ddl m} |j                  S r   r   r   s     r   rm   z1count_per_block.<locals>.CountPerBlockImpl.mpi_opC  r   r   c                 ,    t        j                  d      S r   r   r   s    r   rl   z2count_per_block.<locals>.CountPerBlockImpl.defaultG  r   r   Nr   r#   r   r   CountPerBlockImplr   <  s    	 	&		$r   r  )r   r   )r   ro   rp   r  s       r   count_per_blockr  0  s=     D1HLMM$ $ .0%zJJr   c                 `    ||dk(  rt        | ||      t        | ||      z  S t        | ||      S )a  Returns the mean of all values along a particular axis (dimension)
    for each block of a VTKCompositeDataArray.

    Given an array of m tuples and n components:
    * Default is to return the mean of all values in an array.
    * axis=0: Return the mean values of all components and return a one
      tuple, n-component array.
    * axis=1: Return the mean values of all components of each tuple and
      return an m-tuple, 1-component array.

    When called in parallel, this function will compute the mean across
    processes when a controller argument is passed or the global controller
    is defined. To disable parallel summing when running in parallel, pass a
    dummy controller as follows:

    mean(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    r   )r   r  r   r   ro   rp   s      r   mean_per_blockr
  L  s<    $ |tqyUD*5tU_8```5$
++r   c                 <     G d d      }t         |       | ||      S )a  Returns the max of all values along a particular axis (dimension)
    for each block of a VTKCompositeDataArray.
    Given an array of m tuples and n components:
    * Default is to return the max of all values in an array.
    * axis=0: Return the max values of all components and return a one
      tuple, n-component array.
    * axis=1: Return the max values of all components of each tuple and return
      an m-tuple, 1-component array.

    When called in parallel, this function will compute the max across
    processes when a controller argument is passed or the global controller
    is defined. To disable parallel summing when running in parallel, pass a
    dummy controller as follows:

    max_per_block(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    c                   $    e Zd Zd Zd Zd Zd Zy)&max_per_block.<locals>.MaxPerBlockImplc                     t         S r    )r   r   s    r   r_   z)max_per_block.<locals>.MaxPerBlockImpl.opu  r   r   c                 "    t         j                  S r    r   r   s    r   r   z*max_per_block.<locals>.MaxPerBlockImpl.op2x  r   r   c                 &    ddl m} |j                  S r   r   r   s     r   rm   z-max_per_block.<locals>.MaxPerBlockImpl.mpi_op{  r   r   c                 \    t        j                  t         j                        j                  S r    r   r   s    r   rl   z.max_per_block.<locals>.MaxPerBlockImpl.default  r   r   Nr   r#   r   r   MaxPerBlockImplr  t      				2r   r  r   )r   ro   rp   r  s       r   max_per_blockr  c  "    "2 2 _.tZHHr   c                 <     G d d      }t         |       | ||      S )a  Returns the min of all values along a particular axis (dimension)
    for each block of a VTKCompositeDataArray.
    Given an array of m tuples and n components:
    * Default is to return the min of all values in an array.
    * axis=0: Return the min values of all components and return a one
      tuple, n-component array.
    * axis=1: Return the min values of all components of each tuple and
      return an m-tuple, 1-component array.

    When called in parallel, this function will compute the min across
    processes when a controller argument is passed or the global controller
    is defined. To disable parallel summing when running in parallel, pass a
    dummy controller as follows:

    min_per_block(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    c                   $    e Zd Zd Zd Zd Zd Zy)&min_per_block.<locals>.MinPerBlockImplc                     t         S r    )r   r   s    r   r_   z)min_per_block.<locals>.MinPerBlockImpl.op  r   r   c                 "    t         j                  S r    r   r   s    r   r   z*min_per_block.<locals>.MinPerBlockImpl.op2  r   r   c                 &    ddl m} |j                  S r   r   r   s     r   rm   z-min_per_block.<locals>.MinPerBlockImpl.mpi_op  r   r   c                 \    t        j                  t         j                        j                  S r    r   r   s    r   rl   z.min_per_block.<locals>.MinPerBlockImpl.default  r   r   Nr   r#   r   r   MinPerBlockImplr    r  r   r  r   )r   ro   rp   r  s       r   min_per_blockr    r  r   c                 <     G d d      }t         |       | ||      S )zReturns True if all values of an array evaluate to True, returns
    False otherwise.
    This is useful to check if all values of an array match a certain
    condition such as:

    algorithms.all(array > 5)
    c                   $    e Zd Zd Zd Zd Zd Zy)all.<locals>.MinImplc                 "    t         j                  S r    )r   r   r   s    r   r_   zall.<locals>.MinImpl.op  r   r   c                 &    ddl m} |j                  S r   )rC   rA   LANDr   s     r   rm   zall.<locals>.MinImpl.mpi_op  s    "88Or   c                     t        t        j                  ||f      }g }|D ]&  }|t        j                  us|j                  |       ( |g u ry t        j                  |d      S r   )r   r   r   r   r   r   r   s         r   r^   z%all.<locals>.MinImpl.serial_composite  s]    txx8CJCMM)%%a(  R88JQ//r   c                 8    t        j                  |t              S )NrO   )rE   onesrD   )r   	max_compss     r   rl   zall.<locals>.MinImpl.default  s    ::it44r   Nr   r#   r   r   r   r!    s    			0	5r   r   r   r   s       r   r   r     s!    5 5* 	5$
;;r   c                     | t         j                  u rt        j                  d      S |t        j                  | j                        S t        j                  t        |       d         S r   )r   r   rE   int64rZ   rQ   )r   ro   s     r   r  r    sK    {{1~	{{5::&&{{5<?++r   c                 N   | t         j                  u rt        j                  d      }nC| t        j                  | j                        }n!t        j                  t        |       d         }|t        t        j                         }|r|j                  d      rddl	m
} t        j                  |j                               }t        j                  |t        j                        }t        t        j                        }|j!                  ||g||g|j"                         |}|S )Nr   r]   r@   rO   )r   r   rE   r*  rZ   rQ   r   rb   rc   rC   rA   r	   rg   rh   r   rM   rS   r   )r   ro   rp   rZ   rA   rU   
total_sizerY   s           r   r  r    s    {{1~	{{5::&{{5<?+7C.BBD
jnn%78$44Z5O5O5QR[[U[[9
"5;;/gW(=swwGKr   c                     ||dk(  r|t        | ||      }t        | |      |z  S t        |       t        j                  k(  rt        t        j                  | |f      S t        j                  | |      S )a  Returns the mean of all values along a particular axis (dimension).
    Given an array of m tuples and n components:
    * Default is to return the mean of all values in an array.
    * axis=0: Return the mean values of all components and return a one
      tuple, n-component array.
    * axis=1: Return the mean values of all components of each tuple and
      return an m-tuple, 1-component array.

    When called in parallel, this function will compute the mean across
    processes when a controller argument is passed or the global controller
    is defined. To disable parallel summing when running in parallel, pass a
    dummy controller as follows:

    mean(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    r   )r  r   r   r   r   r   r   mean)r   ro   rp   rZ   s       r   r.  r.    sl    " |tqy<tZ8D5$$&&;#333tyy%$9999UD))r   c                    ||dk(  r1t        | ||      }| t        | |||      z
  }t        ||z  ||      |z  S t        |       t        j
                  k(  rt        t        j                  | |f      S t        j                  | |      S )a  Returns the variance of all values along a particular axis (dimension).
    Given an array of m tuples and n components:
    * Default is to return the variance of all values in an array.
    * axis=0: Return the variance values of all components and return a one
      tuple, n-component array.
    * axis=1: Return the variance values of all components of each tuple and
      return an m-tuple, 1-component array.

    When called in parallel, this function will compute the variance across
    processes when a controller argument is passed or the global controller
    is defined. To disable parallel summing when running in parallel, pass a
    dummy controller as follows:

    var(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    r   )	r  r.  r   r   r   r   r   r   var)r   ro   rp   rZ   tmps        r   r0  r0    s    " |tqyE44d5$
D993s7D*-44;#333txx8888E4((r   c                 .    t        t        | ||            S )a/  Returns the standard deviation of all values along a particular
    axis (dimension).
    Given an array of m tuples and n components:
    * Default is to return the standard deviation of all values in an array.
    * axis=0: Return the standard deviation values of all components and
      return a one tuple, n-component array.
    * axis=1: Return the standard deviation values of all components of
      each tuple and return an m-tuple, 1-component array.

    When called in parallel, this function will compute the standard deviation
    across processes when a controller argument is passed or the global controller
    is defined. To disable parallel summing when running in parallel, pass a dummy
    controller as follows:

    std(array, controller=vtkmodules.vtkParallelCore.vtkDummyController()).
    )sqrtr0  r	  s      r   stdr4    s    " E4,--r   c                    t        |       t        j                  k(  rd}| j                  D ]  }|t        j                  us|t        |j                        }.|j                  }t        |      t        |      k7  rt        d      |dxx   |d   z  cc<   t        dt        |            D ]  }||   ||   k7  st        d        t        |      S | t        j                  u ryt        j                  |       S )z+Returns the shape (dimensions) of an array.NzExpected arrays of same shaper   r   r#   )r   r   r   r   r   listrQ   rR   r   r   tuplerE   )r   rW   r   r1  idxs        r   rQ   rQ   -  s    E{c///A%;qww-C''CCCH,()HIIFc!f$F$Qs3x0s8s3x/",-L"MM  1  Sz	#--	{{5!!r   c                    t        |       t        j                  k(  rt        |      t        j                  k(  rt        |      t        j                  k(  s|gg }|t        | j                  |j                        D ]n  \  }}|t        j
                  ur8|t        j
                  ur&|j                  t        j                  ||             P|j                  t        j
                         p nt        | j                  |j                  |j                        D ]  \  }}}|t        j
                  urK|t        j
                  ur9|t        j
                  ur'|j                  t        j                  |||             d|j                  t        j
                          t        j                  || j                        S t        j                  | ||      S )z}Given 2 or 3 scalar arrays, returns a vector array. If only
    2 scalars are provided, the third component will be set to 0.r   )
r   r   r   r&   r   r   r   r   make_vectorr   )arrayxarrayyarrayzr   axayazs          r   r:  r:  C  sc    F|s000T&\SE^E^5^dhiodptw  uN  uN  eN  RX  R`>fmmV]];BS]]*r/FJJt//B78JJs}}-	 < "&--N
BS]]*r/F2UXUbUbKbJJt//B;<JJs}}-	 O
 ((GG77r   c                 X   	 | j                   }|| t        j                  urt	        d      | t        j                  u rg }n| j
                  }t        j                  t        |      t        j                        }d}|g }n|j                         }|j                  d       |j                         }	t        j                  t        |      t        j                        }d}
|j                         s;|j                         }|||
<   |
dz  }
|j!                          |j                         s;|t"        t#        j$                         }|r|j'                  d      rddlm} t-        j.                  |j1                               }|j3                         }t        j                  d      }||j                         }|j                  d       |j5                  d       |j                         se|j                         }t        j6                  ||f      j9                  t        j                        }|j!                          |j                         set        j:                  dt        j                        }t=        t        j                        }|j?                  ||g||g|j@                         t        j                  |t        j                        }|jC                  t        jD                  t        j                        j6                         t        j                  |t        j                        }||j                         }|j                  d       |jG                          |j                         }	|j                         sQ|j                         }tI        |	      t        j                  ur|||<   |j!                          |j                         sQt=        t        j                        }|j?                  ||g||g|jJ                         d	d
l&m'} d	dl(m)}m*}  |       } |       }|jW                  d        |       }|jY                  |       d}
|D ],  }|||
      |k(  r|j[                  t]        |             |
dz  }
. |j_                  |       |D ]  \  }}|t        j                  us |       }|j
                  d   ja                         jb                  d   }|jW                  |       d}
|j
                  D ]F  }|||
      |k(  r4|ja                         }te        |      D ]  }|jg                  ||           |
dz  }
H t              dkD  s|ji                  |       |jk                         jm                  |        |S # t        $ r d}Y w xY w)a  Given a set of VTKCompositeDataArrays, creates a vtkUnstructuredGrid.
    The main goal of this function is to transform the output of XXX_per_block()
    methods to a single dataset that can be visualized and further processed.
    Here arrays is an iterable (e.g. list) of (array, name) pairs. Here is
    an example:

    centroid = mean_per_block(composite_data.Points)
    T = mean_per_block(composite_data.PointData['Temperature'])
    ug = unstructured_from_composite_arrays(centroid, (T, 'Temperature'))

    When called in parallel, this function makes sure that each array in
    the input dataset is represented only on 1 process. This is important
    because methods like mean_per_block() return the same value for blocks
    that are partitioned on all of the participating processes. If the
    same point were to be created across multiple processes in the output,
    filters like histogram would report duplicate values erroneously.
    Nz5Expecting a points arrays with an associated dataset.rO   r   r   r]   r@   Fr   )vtkUnstructuredGrid)vtkDoubleArray	vtkPoints   )7r   rI   r   r   r   r   rE   r   rR   rP   r   r   __iter__rj   r   r   r   r   rb   rc   rC   rA   r	   rg   rh   Get_rankr   r   r`   r   rM   rS   rT   rk   iinfoInitTraversalnextr   rz   rB  vtkCommonCorerC  rD  SetNumberOfComponentsSetDataInsertNextPointr7  	SetPointsr   rQ   r   InsertNextValueSetNameGetPointDataAddArray)pointsr   rp   r   cpts	ownershiprankr   r   itrcounterr   rA   rU   r   r   rY   
lownershiprB  rC  rD  ugriddaptsptcanamencompsr   r   s                                 r   "unstructured_from_composite_arraysrb  X  s   &.. 66PQQ}}CIU[[9ID   "
dmmokk#d)5;;7((*((*CCLqLGOO	 ((* 7C.BBD
jnn%78$44Z5O5O5QR}} ++a.$$&BMM$  ',,.,,.))WcN3::5;;G! ,,. Qekk2"5;;/)FG+<cggF [[=
EKK0445KK<	$$&BMM$--/C,,.,,.9CMM1&*JsO!	 ,,.
 #5;;/ 	
G,y'.BCGGL 99!E		BQ
+CKKOGS\"d*b	*1  
OOCDS]]"!BYYq\))+11!4F$$V,GYYS\*d2		A"6]**1Q40 +1  1vz

4 ""$--b1  LO  s   V V)(V)zKTest whether each element of a 1-D array is also present in a second array.z,Returns a bool array, true if values is nan.zComputes square root.z!Numerical negative, element-wise.z:Return the reciprocal (1/x) of the argument, element-wise.z,Return the element-wise square of the input.zThe exponential function.z+Returns the floor of floating point values.z-Returns the ceiling of floating point values.z3Round elements of the array to the nearest integer.z#Computes sine of values in radians.z%Computes cosine of values in radians.z&Computes tangent of values in radians.zComputes inverse sine.zComputes inverse cosine.zComputes inverse tangent.z-Computes inverse tangent using two arguments.zComputes hyperbolic sine.zComputes hyperbolic cosine.zComputes hyperbolic tangent.z!Computes inverse hyperbolic sine.z#Computes inverse hyperbolic cosine.z$Computes inverse hyperbolic tangent.a`  Returns the location (indices) of an array where the given
expression is true. For scalars, it returns a single array of indices.
For vectors and matrices, it returns two arrays: first with tuple indices,
second with component indices. The output of this method can be used to
extract the values from the array also by using it as the index of the [] operator.

For example:

>>> algs.where(algs.array([1,2,3]) == 2)
(array([1]),)

>>> algs.where(algs.array([[1,2,3], [2,1,1]]) == 2)
(array([0, 1]), array([1, 0]))

>>> a = array([[1,2,3], [2,1,1]])
>>> indices = algs.where(a > 2)
>>> a[indices]
array([3])
zMReturn indices that are non-zero in the flattened version of the input array.z?Return the indices of the non-zero elements of the input array.a  Insert a new dimension, corresponding to a given
position in the array shape. In VTK, this function's main use is to
enable an operator to work on a vector and a scalar field. For example,
say you want to divide each component of a vector by the magnitude of
that vector. You might try this:

>>> v
VTKArray([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
>>> algs.mag(v)
VTKArray([ 1.73205081,  1.73205081,  1.73205081,  1.73205081,  1.73205081])
>>> v / algs.mag(v)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (5,3) (5)

The division operator does not know how to map a scalar to a vector
due to a mismatch in dimensions. This can be solved by making the
scalar a vector of 1 component (increasing its dimension to 2) as follows:

>>> v / algs.expand_dims(algs.mag(v), 1)
VTKArray([[ 0.57735027,  0.57735027,  0.57735027],
       [ 0.57735027,  0.57735027,  0.57735027],
       [ 0.57735027,  0.57735027,  0.57735027],
       [ 0.57735027,  0.57735027,  0.57735027],
       [ 0.57735027,  0.57735027,  0.57735027]])zCReturns the absolute values of an array of scalars/vectors/tensors.z3Returns the surface area of each 2D cell in a mesh.zWReturns the aspect ratio of each cell in a mesh. See Verdict documentation for details.zReturns the aspect gamma of each cell in a mesh. This metric compares root-mean-square edge length to volume. See Verdict documentation for details.z[Returns the condition number of each cell in a mesh. See Verdict documentation for details.z(Return the cross product of two vectors.z Returns the curl a vector field.z)Returns the divergence of a vector field.z'Returns the determinant of 2D matrices.z\Returns the diagonal length of each cell in a dataset. See Verdict documentation for detailsz'Returns the dot product of two vectors.zVReturns the eigenvalues of 3x3 matrices. Currently only works with symmetric matrices.zWReturns the eigenvectors of 3x3 matrices. Currently only works with symmetric matrices.z+Returns the gradient of scalars or vectors.z$Returns the inverse of 3x3 matrices.z"Returns the Jacobian of a dataset.z(Returns the Laplacian of a scalar field.z+Returns the natural logarithm of its input.z+Returns the base 10 logarithm of its input.zZReturns the maximum angle of each cell in a dataset. See Verdict documentation for detailsz!Returns the magnitude of vectors.z4Returns the minimum angle of each cell in a dataset.z*Computes the normalized values of vectors.zSReturns the shear of each cell in a dataset. See Verdict documentation for details.zRReturns the skew of each cell in a dataset. See Verdict documentation for details.zGiven a deformation vector, this function computes the infinitesimal (Cauchy) strain tensor. It can also be used to compute strain rate if the input is velocity.z5Returns the surface normal of each cell in a dataset.z%Returns the trace of square matrices.z]Returns the volume of each cell in a dataset. Use sum to calculate total volume of a dataset.z-Given a velocity field, calculates vorticity.zReturns the normal at each vertex of a dataset, which is defined as the average of the cell normals of all cells containing that vertex.z/Computes the truth value of NOT x element-wise.zUElement by element division. Both elements can be single values or arrays. Same as /.z[Element by element multiplication. Both elements can be single values or arrays. Same as *.zUElement by element addition. Both elements can be single values or arrays. Same as +.zAReturns the difference of two values element-wise. Same as x - y.zComputes x1 - floor(x1 / x2) * x2, the result has the same sign as the divisor x2. It is equivalent to the Python modulus operator x1 % x2. Same as remainder.zComputes x1 - floor(x1 / x2) * x2, the result has the same sign as the divisor x2. It is equivalent to the Python modulus operator x1 % x2. Same as mod.zFFirst array elements raised to powers from second array, element-wise.z<Given the 'legs' of a right triangle, return its hypotenuse.)r#   )NN)NNNr    )x__doc__
__future__r   sysrE   re   rf    r   r   r   r   	itertoolsvtkParallelCorer   vtkParallelMPI4Pyr	   r   r   r$   r.   r2   r9   r>   rM   r[   rs   ru   r   r   r   r}   r   r   r   r   r   r  r
  r  r  r   r  r  r.  r0  r4  rQ   r:  rb  in1dr   r3  negative
reciprocalsquareexpfloorceilrintsincostanarcsinarccosarctanarctan2sinhcoshtanharcsinharccosharctanhwhereflatnonzerononzeroexpand_dimsabsareaaspectaspect_gamma	conditioncrosscurl
divergencedetdeterminantdiagonaldot
eigenvalueeigenvectorgradientinvinversejacobian	laplacianlnloglog10	max_anglemag	min_anglenormshearskewstrainsurface_normaltracevolume	vorticityvertex_normallogical_notdividemultiplyaddsubtractmod	remainderpowerhypotr#   r   r   <module>r     s*   ' 
&
 % ) !;9
"!<&
'4)V,0.4 ,/==%  &<P%<N$<LB?H IDK8,.IBIB<>,.*6)6.&",8*|| 5::\EKK >5::&u~~&6 ))*
Q
 	U\\	"?%)))EKK =5::>5::D%))3%))5%))6	U\\	")	U\\	"+	U\\	",
emm
$A5::*5::,5::-
emm
$5
emm
$7
emm
$8EKK ( %++,e 
emm
$S%++,4 < $((STYYD	t{{	#jT../ n $..)	q	 DJJ:DII1$//*
@
 $((7$**+? 'q $((7)
m
 $**+o &@ $((4
dll
#8'7 (	>	 :
$((;DJJ=$..)	p	 $((1$..)	J	 499;djj!eTYYc	dkk	" ut223P DJJ7	t{{	#p(	C	 d001 c %++,G 	U\\	"hu~~&p %))eu~~&V %)) o(	 o	 EKK XEKK NK&  &
 % & &&  ! $ !s   ^ ^ ^	^! ^!