triinterpolate.py 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  1. """
  2. Interpolation inside triangular grids.
  3. """
  4. import numpy as np
  5. from matplotlib import cbook
  6. from matplotlib.tri import Triangulation
  7. from matplotlib.tri.trifinder import TriFinder
  8. from matplotlib.tri.tritools import TriAnalyzer
  9. __all__ = ('TriInterpolator', 'LinearTriInterpolator', 'CubicTriInterpolator')
  10. class TriInterpolator:
  11. """
  12. Abstract base class for classes used to interpolate on a triangular grid.
  13. Derived classes implement the following methods:
  14. - ``__call__(x, y)``,
  15. where x, y are array-like point coordinates of the same shape, and
  16. that returns a masked array of the same shape containing the
  17. interpolated z-values.
  18. - ``gradient(x, y)``,
  19. where x, y are array-like point coordinates of the same
  20. shape, and that returns a list of 2 masked arrays of the same shape
  21. containing the 2 derivatives of the interpolator (derivatives of
  22. interpolated z values with respect to x and y).
  23. """
  24. def __init__(self, triangulation, z, trifinder=None):
  25. cbook._check_isinstance(Triangulation, triangulation=triangulation)
  26. self._triangulation = triangulation
  27. self._z = np.asarray(z)
  28. if self._z.shape != self._triangulation.x.shape:
  29. raise ValueError("z array must have same length as triangulation x"
  30. " and y arrays")
  31. cbook._check_isinstance((TriFinder, None), trifinder=trifinder)
  32. self._trifinder = trifinder or self._triangulation.get_trifinder()
  33. # Default scaling factors : 1.0 (= no scaling)
  34. # Scaling may be used for interpolations for which the order of
  35. # magnitude of x, y has an impact on the interpolant definition.
  36. # Please refer to :meth:`_interpolate_multikeys` for details.
  37. self._unit_x = 1.0
  38. self._unit_y = 1.0
  39. # Default triangle renumbering: None (= no renumbering)
  40. # Renumbering may be used to avoid unnecessary computations
  41. # if complex calculations are done inside the Interpolator.
  42. # Please refer to :meth:`_interpolate_multikeys` for details.
  43. self._tri_renum = None
  44. # __call__ and gradient docstrings are shared by all subclasses
  45. # (except, if needed, relevant additions).
  46. # However these methods are only implemented in subclasses to avoid
  47. # confusion in the documentation.
  48. _docstring__call__ = """
  49. Returns a masked array containing interpolated values at the specified
  50. (x, y) points.
  51. Parameters
  52. ----------
  53. x, y : array-like
  54. x and y coordinates of the same shape and any number of
  55. dimensions.
  56. Returns
  57. -------
  58. np.ma.array
  59. Masked array of the same shape as *x* and *y*; values corresponding
  60. to (*x*, *y*) points outside of the triangulation are masked out.
  61. """
  62. _docstringgradient = r"""
  63. Returns a list of 2 masked arrays containing interpolated derivatives
  64. at the specified (x, y) points.
  65. Parameters
  66. ----------
  67. x, y : array-like
  68. x and y coordinates of the same shape and any number of
  69. dimensions.
  70. Returns
  71. -------
  72. dzdx, dzdy : np.ma.array
  73. 2 masked arrays of the same shape as *x* and *y*; values
  74. corresponding to (x, y) points outside of the triangulation
  75. are masked out.
  76. The first returned array contains the values of
  77. :math:`\frac{\partial z}{\partial x}` and the second those of
  78. :math:`\frac{\partial z}{\partial y}`.
  79. """
  80. def _interpolate_multikeys(self, x, y, tri_index=None,
  81. return_keys=('z',)):
  82. """
  83. Versatile (private) method defined for all TriInterpolators.
  84. :meth:`_interpolate_multikeys` is a wrapper around method
  85. :meth:`_interpolate_single_key` (to be defined in the child
  86. subclasses).
  87. :meth:`_interpolate_single_key actually performs the interpolation,
  88. but only for 1-dimensional inputs and at valid locations (inside
  89. unmasked triangles of the triangulation).
  90. The purpose of :meth:`_interpolate_multikeys` is to implement the
  91. following common tasks needed in all subclasses implementations:
  92. - calculation of containing triangles
  93. - dealing with more than one interpolation request at the same
  94. location (e.g., if the 2 derivatives are requested, it is
  95. unnecessary to compute the containing triangles twice)
  96. - scaling according to self._unit_x, self._unit_y
  97. - dealing with points outside of the grid (with fill value np.nan)
  98. - dealing with multi-dimensional *x*, *y* arrays: flattening for
  99. :meth:`_interpolate_params` call and final reshaping.
  100. (Note that np.vectorize could do most of those things very well for
  101. you, but it does it by function evaluations over successive tuples of
  102. the input arrays. Therefore, this tends to be more time consuming than
  103. using optimized numpy functions - e.g., np.dot - which can be used
  104. easily on the flattened inputs, in the child-subclass methods
  105. :meth:`_interpolate_single_key`.)
  106. It is guaranteed that the calls to :meth:`_interpolate_single_key`
  107. will be done with flattened (1-d) array-like input parameters *x*, *y*
  108. and with flattened, valid `tri_index` arrays (no -1 index allowed).
  109. Parameters
  110. ----------
  111. x, y : array-like
  112. x and y coordinates where interpolated values are requested.
  113. tri_index : array-like of int, optional
  114. Array of the containing triangle indices, same shape as
  115. *x* and *y*. Defaults to None. If None, these indices
  116. will be computed by a TriFinder instance.
  117. (Note: For point outside the grid, tri_index[ipt] shall be -1).
  118. return_keys : tuple of keys from {'z', 'dzdx', 'dzdy'}
  119. Defines the interpolation arrays to return, and in which order.
  120. Returns
  121. -------
  122. list of arrays
  123. Each array-like contains the expected interpolated values in the
  124. order defined by *return_keys* parameter.
  125. """
  126. # Flattening and rescaling inputs arrays x, y
  127. # (initial shape is stored for output)
  128. x = np.asarray(x, dtype=np.float64)
  129. y = np.asarray(y, dtype=np.float64)
  130. sh_ret = x.shape
  131. if x.shape != y.shape:
  132. raise ValueError("x and y shall have same shapes."
  133. " Given: {0} and {1}".format(x.shape, y.shape))
  134. x = np.ravel(x)
  135. y = np.ravel(y)
  136. x_scaled = x/self._unit_x
  137. y_scaled = y/self._unit_y
  138. size_ret = np.size(x_scaled)
  139. # Computes & ravels the element indexes, extract the valid ones.
  140. if tri_index is None:
  141. tri_index = self._trifinder(x, y)
  142. else:
  143. if tri_index.shape != sh_ret:
  144. raise ValueError(
  145. "tri_index array is provided and shall"
  146. " have same shape as x and y. Given: "
  147. "{0} and {1}".format(tri_index.shape, sh_ret))
  148. tri_index = np.ravel(tri_index)
  149. mask_in = (tri_index != -1)
  150. if self._tri_renum is None:
  151. valid_tri_index = tri_index[mask_in]
  152. else:
  153. valid_tri_index = self._tri_renum[tri_index[mask_in]]
  154. valid_x = x_scaled[mask_in]
  155. valid_y = y_scaled[mask_in]
  156. ret = []
  157. for return_key in return_keys:
  158. # Find the return index associated with the key.
  159. try:
  160. return_index = {'z': 0, 'dzdx': 1, 'dzdy': 2}[return_key]
  161. except KeyError as err:
  162. raise ValueError("return_keys items shall take values in"
  163. " {'z', 'dzdx', 'dzdy'}") from err
  164. # Sets the scale factor for f & df components
  165. scale = [1., 1./self._unit_x, 1./self._unit_y][return_index]
  166. # Computes the interpolation
  167. ret_loc = np.empty(size_ret, dtype=np.float64)
  168. ret_loc[~mask_in] = np.nan
  169. ret_loc[mask_in] = self._interpolate_single_key(
  170. return_key, valid_tri_index, valid_x, valid_y) * scale
  171. ret += [np.ma.masked_invalid(ret_loc.reshape(sh_ret), copy=False)]
  172. return ret
  173. def _interpolate_single_key(self, return_key, tri_index, x, y):
  174. """
  175. Interpolate at points belonging to the triangulation
  176. (inside an unmasked triangles).
  177. Parameters
  178. ----------
  179. return_index : {'z', 'dzdx', 'dzdy'}
  180. Identifies the requested values (z or its derivatives)
  181. tri_index : 1d int array
  182. Valid triangle index (-1 prohibited)
  183. x, y : 1d arrays, same shape as `tri_index`
  184. Valid locations where interpolation is requested.
  185. Returns
  186. -------
  187. 1-d array
  188. Returned array of the same size as *tri_index*
  189. """
  190. raise NotImplementedError("TriInterpolator subclasses" +
  191. "should implement _interpolate_single_key!")
  192. class LinearTriInterpolator(TriInterpolator):
  193. """
  194. Linear interpolator on a triangular grid.
  195. Each triangle is represented by a plane so that an interpolated value at
  196. point (x, y) lies on the plane of the triangle containing (x, y).
  197. Interpolated values are therefore continuous across the triangulation, but
  198. their first derivatives are discontinuous at edges between triangles.
  199. Parameters
  200. ----------
  201. triangulation : `~matplotlib.tri.Triangulation`
  202. The triangulation to interpolate over.
  203. z : array-like of shape (npoints,)
  204. Array of values, defined at grid points, to interpolate between.
  205. trifinder : `~matplotlib.tri.TriFinder`, optional
  206. If this is not specified, the Triangulation's default TriFinder will
  207. be used by calling `.Triangulation.get_trifinder`.
  208. Methods
  209. -------
  210. `__call__` (x, y) : Returns interpolated values at (x, y) points.
  211. `gradient` (x, y) : Returns interpolated derivatives at (x, y) points.
  212. """
  213. def __init__(self, triangulation, z, trifinder=None):
  214. TriInterpolator.__init__(self, triangulation, z, trifinder)
  215. # Store plane coefficients for fast interpolation calculations.
  216. self._plane_coefficients = \
  217. self._triangulation.calculate_plane_coefficients(self._z)
  218. def __call__(self, x, y):
  219. return self._interpolate_multikeys(x, y, tri_index=None,
  220. return_keys=('z',))[0]
  221. __call__.__doc__ = TriInterpolator._docstring__call__
  222. def gradient(self, x, y):
  223. return self._interpolate_multikeys(x, y, tri_index=None,
  224. return_keys=('dzdx', 'dzdy'))
  225. gradient.__doc__ = TriInterpolator._docstringgradient
  226. def _interpolate_single_key(self, return_key, tri_index, x, y):
  227. if return_key == 'z':
  228. return (self._plane_coefficients[tri_index, 0]*x +
  229. self._plane_coefficients[tri_index, 1]*y +
  230. self._plane_coefficients[tri_index, 2])
  231. elif return_key == 'dzdx':
  232. return self._plane_coefficients[tri_index, 0]
  233. elif return_key == 'dzdy':
  234. return self._plane_coefficients[tri_index, 1]
  235. else:
  236. raise ValueError("Invalid return_key: " + return_key)
  237. class CubicTriInterpolator(TriInterpolator):
  238. r"""
  239. Cubic interpolator on a triangular grid.
  240. In one-dimension - on a segment - a cubic interpolating function is
  241. defined by the values of the function and its derivative at both ends.
  242. This is almost the same in 2-d inside a triangle, except that the values
  243. of the function and its 2 derivatives have to be defined at each triangle
  244. node.
  245. The CubicTriInterpolator takes the value of the function at each node -
  246. provided by the user - and internally computes the value of the
  247. derivatives, resulting in a smooth interpolation.
  248. (As a special feature, the user can also impose the value of the
  249. derivatives at each node, but this is not supposed to be the common
  250. usage.)
  251. Parameters
  252. ----------
  253. triangulation : `~matplotlib.tri.Triangulation`
  254. The triangulation to interpolate over.
  255. z : array-like of shape (npoints,)
  256. Array of values, defined at grid points, to interpolate between.
  257. kind : {'min_E', 'geom', 'user'}, optional
  258. Choice of the smoothing algorithm, in order to compute
  259. the interpolant derivatives (defaults to 'min_E'):
  260. - if 'min_E': (default) The derivatives at each node is computed
  261. to minimize a bending energy.
  262. - if 'geom': The derivatives at each node is computed as a
  263. weighted average of relevant triangle normals. To be used for
  264. speed optimization (large grids).
  265. - if 'user': The user provides the argument *dz*, no computation
  266. is hence needed.
  267. trifinder : `~matplotlib.tri.TriFinder`, optional
  268. If not specified, the Triangulation's default TriFinder will
  269. be used by calling `.Triangulation.get_trifinder`.
  270. dz : tuple of array-likes (dzdx, dzdy), optional
  271. Used only if *kind* ='user'. In this case *dz* must be provided as
  272. (dzdx, dzdy) where dzdx, dzdy are arrays of the same shape as *z* and
  273. are the interpolant first derivatives at the *triangulation* points.
  274. Methods
  275. -------
  276. `__call__` (x, y) : Returns interpolated values at (x, y) points.
  277. `gradient` (x, y) : Returns interpolated derivatives at (x, y) points.
  278. Notes
  279. -----
  280. This note is a bit technical and details how the cubic interpolation is
  281. computed.
  282. The interpolation is based on a Clough-Tocher subdivision scheme of
  283. the *triangulation* mesh (to make it clearer, each triangle of the
  284. grid will be divided in 3 child-triangles, and on each child triangle
  285. the interpolated function is a cubic polynomial of the 2 coordinates).
  286. This technique originates from FEM (Finite Element Method) analysis;
  287. the element used is a reduced Hsieh-Clough-Tocher (HCT)
  288. element. Its shape functions are described in [1]_.
  289. The assembled function is guaranteed to be C1-smooth, i.e. it is
  290. continuous and its first derivatives are also continuous (this
  291. is easy to show inside the triangles but is also true when crossing the
  292. edges).
  293. In the default case (*kind* ='min_E'), the interpolant minimizes a
  294. curvature energy on the functional space generated by the HCT element
  295. shape functions - with imposed values but arbitrary derivatives at each
  296. node. The minimized functional is the integral of the so-called total
  297. curvature (implementation based on an algorithm from [2]_ - PCG sparse
  298. solver):
  299. .. math::
  300. E(z) = \frac{1}{2} \int_{\Omega} \left(
  301. \left( \frac{\partial^2{z}}{\partial{x}^2} \right)^2 +
  302. \left( \frac{\partial^2{z}}{\partial{y}^2} \right)^2 +
  303. 2\left( \frac{\partial^2{z}}{\partial{y}\partial{x}} \right)^2
  304. \right) dx\,dy
  305. If the case *kind* ='geom' is chosen by the user, a simple geometric
  306. approximation is used (weighted average of the triangle normal
  307. vectors), which could improve speed on very large grids.
  308. References
  309. ----------
  310. .. [1] Michel Bernadou, Kamal Hassan, "Basis functions for general
  311. Hsieh-Clough-Tocher triangles, complete or reduced.",
  312. International Journal for Numerical Methods in Engineering,
  313. 17(5):784 - 789. 2.01.
  314. .. [2] C.T. Kelley, "Iterative Methods for Optimization".
  315. """
  316. def __init__(self, triangulation, z, kind='min_E', trifinder=None,
  317. dz=None):
  318. TriInterpolator.__init__(self, triangulation, z, trifinder)
  319. # Loads the underlying c++ _triangulation.
  320. # (During loading, reordering of triangulation._triangles may occur so
  321. # that all final triangles are now anti-clockwise)
  322. self._triangulation.get_cpp_triangulation()
  323. # To build the stiffness matrix and avoid zero-energy spurious modes
  324. # we will only store internally the valid (unmasked) triangles and
  325. # the necessary (used) points coordinates.
  326. # 2 renumbering tables need to be computed and stored:
  327. # - a triangle renum table in order to translate the result from a
  328. # TriFinder instance into the internal stored triangle number.
  329. # - a node renum table to overwrite the self._z values into the new
  330. # (used) node numbering.
  331. tri_analyzer = TriAnalyzer(self._triangulation)
  332. (compressed_triangles, compressed_x, compressed_y, tri_renum,
  333. node_renum) = tri_analyzer._get_compressed_triangulation()
  334. self._triangles = compressed_triangles
  335. self._tri_renum = tri_renum
  336. # Taking into account the node renumbering in self._z:
  337. valid_node = (node_renum != -1)
  338. self._z[node_renum[valid_node]] = self._z[valid_node]
  339. # Computing scale factors
  340. self._unit_x = np.ptp(compressed_x)
  341. self._unit_y = np.ptp(compressed_y)
  342. self._pts = np.column_stack([compressed_x / self._unit_x,
  343. compressed_y / self._unit_y])
  344. # Computing triangle points
  345. self._tris_pts = self._pts[self._triangles]
  346. # Computing eccentricities
  347. self._eccs = self._compute_tri_eccentricities(self._tris_pts)
  348. # Computing dof estimations for HCT triangle shape function
  349. self._dof = self._compute_dof(kind, dz=dz)
  350. # Loading HCT element
  351. self._ReferenceElement = _ReducedHCT_Element()
  352. def __call__(self, x, y):
  353. return self._interpolate_multikeys(x, y, tri_index=None,
  354. return_keys=('z',))[0]
  355. __call__.__doc__ = TriInterpolator._docstring__call__
  356. def gradient(self, x, y):
  357. return self._interpolate_multikeys(x, y, tri_index=None,
  358. return_keys=('dzdx', 'dzdy'))
  359. gradient.__doc__ = TriInterpolator._docstringgradient
  360. def _interpolate_single_key(self, return_key, tri_index, x, y):
  361. tris_pts = self._tris_pts[tri_index]
  362. alpha = self._get_alpha_vec(x, y, tris_pts)
  363. ecc = self._eccs[tri_index]
  364. dof = np.expand_dims(self._dof[tri_index], axis=1)
  365. if return_key == 'z':
  366. return self._ReferenceElement.get_function_values(
  367. alpha, ecc, dof)
  368. elif return_key in ['dzdx', 'dzdy']:
  369. J = self._get_jacobian(tris_pts)
  370. dzdx = self._ReferenceElement.get_function_derivatives(
  371. alpha, J, ecc, dof)
  372. if return_key == 'dzdx':
  373. return dzdx[:, 0, 0]
  374. else:
  375. return dzdx[:, 1, 0]
  376. else:
  377. raise ValueError("Invalid return_key: " + return_key)
  378. def _compute_dof(self, kind, dz=None):
  379. """
  380. Compute and return nodal dofs according to kind.
  381. Parameters
  382. ----------
  383. kind : {'min_E', 'geom', 'user'}
  384. Choice of the _DOF_estimator subclass to estimate the gradient.
  385. dz : tuple of array-likes (dzdx, dzdy), optional
  386. Used only if *kind*=user; in this case passed to the
  387. :class:`_DOF_estimator_user`.
  388. Returns
  389. -------
  390. array-like, shape (npts, 2)
  391. Estimation of the gradient at triangulation nodes (stored as
  392. degree of freedoms of reduced-HCT triangle elements).
  393. """
  394. if kind == 'user':
  395. if dz is None:
  396. raise ValueError("For a CubicTriInterpolator with "
  397. "*kind*='user', a valid *dz* "
  398. "argument is expected.")
  399. TE = _DOF_estimator_user(self, dz=dz)
  400. elif kind == 'geom':
  401. TE = _DOF_estimator_geom(self)
  402. elif kind == 'min_E':
  403. TE = _DOF_estimator_min_E(self)
  404. else:
  405. cbook._check_in_list(['user', 'geom', 'min_E'], kind=kind)
  406. return TE.compute_dof_from_df()
  407. @staticmethod
  408. def _get_alpha_vec(x, y, tris_pts):
  409. """
  410. Fast (vectorized) function to compute barycentric coordinates alpha.
  411. Parameters
  412. ----------
  413. x, y : array-like of dim 1 (shape (nx,))
  414. Coordinates of the points whose points barycentric coordinates are
  415. requested.
  416. tris_pts : array like of dim 3 (shape: (nx, 3, 2))
  417. Coordinates of the containing triangles apexes.
  418. Returns
  419. -------
  420. array of dim 2 (shape (nx, 3))
  421. Barycentric coordinates of the points inside the containing
  422. triangles.
  423. """
  424. ndim = tris_pts.ndim-2
  425. a = tris_pts[:, 1, :] - tris_pts[:, 0, :]
  426. b = tris_pts[:, 2, :] - tris_pts[:, 0, :]
  427. abT = np.stack([a, b], axis=-1)
  428. ab = _transpose_vectorized(abT)
  429. OM = np.stack([x, y], axis=1) - tris_pts[:, 0, :]
  430. metric = _prod_vectorized(ab, abT)
  431. # Here we try to deal with the colinear cases.
  432. # metric_inv is in this case set to the Moore-Penrose pseudo-inverse
  433. # meaning that we will still return a set of valid barycentric
  434. # coordinates.
  435. metric_inv = _pseudo_inv22sym_vectorized(metric)
  436. Covar = _prod_vectorized(ab, _transpose_vectorized(
  437. np.expand_dims(OM, ndim)))
  438. ksi = _prod_vectorized(metric_inv, Covar)
  439. alpha = _to_matrix_vectorized([
  440. [1-ksi[:, 0, 0]-ksi[:, 1, 0]], [ksi[:, 0, 0]], [ksi[:, 1, 0]]])
  441. return alpha
  442. @staticmethod
  443. def _get_jacobian(tris_pts):
  444. """
  445. Fast (vectorized) function to compute triangle jacobian matrix.
  446. Parameters
  447. ----------
  448. tris_pts : array like of dim 3 (shape: (nx, 3, 2))
  449. Coordinates of the containing triangles apexes.
  450. Returns
  451. -------
  452. array of dim 3 (shape (nx, 2, 2))
  453. Barycentric coordinates of the points inside the containing
  454. triangles.
  455. J[itri, :, :] is the jacobian matrix at apex 0 of the triangle
  456. itri, so that the following (matrix) relationship holds:
  457. [dz/dksi] = [J] x [dz/dx]
  458. with x: global coordinates
  459. ksi: element parametric coordinates in triangle first apex
  460. local basis.
  461. """
  462. a = np.array(tris_pts[:, 1, :] - tris_pts[:, 0, :])
  463. b = np.array(tris_pts[:, 2, :] - tris_pts[:, 0, :])
  464. J = _to_matrix_vectorized([[a[:, 0], a[:, 1]],
  465. [b[:, 0], b[:, 1]]])
  466. return J
  467. @staticmethod
  468. def _compute_tri_eccentricities(tris_pts):
  469. """
  470. Compute triangle eccentricities.
  471. Parameters
  472. ----------
  473. tris_pts : array like of dim 3 (shape: (nx, 3, 2))
  474. Coordinates of the triangles apexes.
  475. Returns
  476. -------
  477. array like of dim 2 (shape: (nx, 3))
  478. The so-called eccentricity parameters [1] needed for HCT triangular
  479. element.
  480. """
  481. a = np.expand_dims(tris_pts[:, 2, :] - tris_pts[:, 1, :], axis=2)
  482. b = np.expand_dims(tris_pts[:, 0, :] - tris_pts[:, 2, :], axis=2)
  483. c = np.expand_dims(tris_pts[:, 1, :] - tris_pts[:, 0, :], axis=2)
  484. # Do not use np.squeeze, this is dangerous if only one triangle
  485. # in the triangulation...
  486. dot_a = _prod_vectorized(_transpose_vectorized(a), a)[:, 0, 0]
  487. dot_b = _prod_vectorized(_transpose_vectorized(b), b)[:, 0, 0]
  488. dot_c = _prod_vectorized(_transpose_vectorized(c), c)[:, 0, 0]
  489. # Note that this line will raise a warning for dot_a, dot_b or dot_c
  490. # zeros, but we choose not to support triangles with duplicate points.
  491. return _to_matrix_vectorized([[(dot_c-dot_b) / dot_a],
  492. [(dot_a-dot_c) / dot_b],
  493. [(dot_b-dot_a) / dot_c]])
  494. # FEM element used for interpolation and for solving minimisation
  495. # problem (Reduced HCT element)
  496. class _ReducedHCT_Element:
  497. """
  498. Implementation of reduced HCT triangular element with explicit shape
  499. functions.
  500. Computes z, dz, d2z and the element stiffness matrix for bending energy:
  501. E(f) = integral( (d2z/dx2 + d2z/dy2)**2 dA)
  502. *** Reference for the shape functions: ***
  503. [1] Basis functions for general Hsieh-Clough-Tocher _triangles, complete or
  504. reduced.
  505. Michel Bernadou, Kamal Hassan
  506. International Journal for Numerical Methods in Engineering.
  507. 17(5):784 - 789. 2.01
  508. *** Element description: ***
  509. 9 dofs: z and dz given at 3 apex
  510. C1 (conform)
  511. """
  512. # 1) Loads matrices to generate shape functions as a function of
  513. # triangle eccentricities - based on [1] p.11 '''
  514. M = np.array([
  515. [ 0.00, 0.00, 0.00, 4.50, 4.50, 0.00, 0.00, 0.00, 0.00, 0.00],
  516. [-0.25, 0.00, 0.00, 0.50, 1.25, 0.00, 0.00, 0.00, 0.00, 0.00],
  517. [-0.25, 0.00, 0.00, 1.25, 0.50, 0.00, 0.00, 0.00, 0.00, 0.00],
  518. [ 0.50, 1.00, 0.00, -1.50, 0.00, 3.00, 3.00, 0.00, 0.00, 3.00],
  519. [ 0.00, 0.00, 0.00, -0.25, 0.25, 0.00, 1.00, 0.00, 0.00, 0.50],
  520. [ 0.25, 0.00, 0.00, -0.50, -0.25, 1.00, 0.00, 0.00, 0.00, 1.00],
  521. [ 0.50, 0.00, 1.00, 0.00, -1.50, 0.00, 0.00, 3.00, 3.00, 3.00],
  522. [ 0.25, 0.00, 0.00, -0.25, -0.50, 0.00, 0.00, 0.00, 1.00, 1.00],
  523. [ 0.00, 0.00, 0.00, 0.25, -0.25, 0.00, 0.00, 1.00, 0.00, 0.50]])
  524. M0 = np.array([
  525. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  526. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  527. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  528. [-1.00, 0.00, 0.00, 1.50, 1.50, 0.00, 0.00, 0.00, 0.00, -3.00],
  529. [-0.50, 0.00, 0.00, 0.75, 0.75, 0.00, 0.00, 0.00, 0.00, -1.50],
  530. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  531. [ 1.00, 0.00, 0.00, -1.50, -1.50, 0.00, 0.00, 0.00, 0.00, 3.00],
  532. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  533. [ 0.50, 0.00, 0.00, -0.75, -0.75, 0.00, 0.00, 0.00, 0.00, 1.50]])
  534. M1 = np.array([
  535. [-0.50, 0.00, 0.00, 1.50, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  536. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  537. [-0.25, 0.00, 0.00, 0.75, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  538. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  539. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  540. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  541. [ 0.50, 0.00, 0.00, -1.50, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  542. [ 0.25, 0.00, 0.00, -0.75, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  543. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00]])
  544. M2 = np.array([
  545. [ 0.50, 0.00, 0.00, 0.00, -1.50, 0.00, 0.00, 0.00, 0.00, 0.00],
  546. [ 0.25, 0.00, 0.00, 0.00, -0.75, 0.00, 0.00, 0.00, 0.00, 0.00],
  547. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  548. [-0.50, 0.00, 0.00, 0.00, 1.50, 0.00, 0.00, 0.00, 0.00, 0.00],
  549. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  550. [-0.25, 0.00, 0.00, 0.00, 0.75, 0.00, 0.00, 0.00, 0.00, 0.00],
  551. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  552. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
  553. [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00]])
  554. # 2) Loads matrices to rotate components of gradient & Hessian
  555. # vectors in the reference basis of triangle first apex (a0)
  556. rotate_dV = np.array([[ 1., 0.], [ 0., 1.],
  557. [ 0., 1.], [-1., -1.],
  558. [-1., -1.], [ 1., 0.]])
  559. rotate_d2V = np.array([[1., 0., 0.], [0., 1., 0.], [ 0., 0., 1.],
  560. [0., 1., 0.], [1., 1., 1.], [ 0., -2., -1.],
  561. [1., 1., 1.], [1., 0., 0.], [-2., 0., -1.]])
  562. # 3) Loads Gauss points & weights on the 3 sub-_triangles for P2
  563. # exact integral - 3 points on each subtriangles.
  564. # NOTE: as the 2nd derivative is discontinuous , we really need those 9
  565. # points!
  566. n_gauss = 9
  567. gauss_pts = np.array([[13./18., 4./18., 1./18.],
  568. [ 4./18., 13./18., 1./18.],
  569. [ 7./18., 7./18., 4./18.],
  570. [ 1./18., 13./18., 4./18.],
  571. [ 1./18., 4./18., 13./18.],
  572. [ 4./18., 7./18., 7./18.],
  573. [ 4./18., 1./18., 13./18.],
  574. [13./18., 1./18., 4./18.],
  575. [ 7./18., 4./18., 7./18.]], dtype=np.float64)
  576. gauss_w = np.ones([9], dtype=np.float64) / 9.
  577. # 4) Stiffness matrix for curvature energy
  578. E = np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 2.]])
  579. # 5) Loads the matrix to compute DOF_rot from tri_J at apex 0
  580. J0_to_J1 = np.array([[-1., 1.], [-1., 0.]])
  581. J0_to_J2 = np.array([[ 0., -1.], [ 1., -1.]])
  582. def get_function_values(self, alpha, ecc, dofs):
  583. """
  584. Parameters
  585. ----------
  586. alpha : is a (N x 3 x 1) array (array of column-matrices) of
  587. barycentric coordinates,
  588. ecc : is a (N x 3 x 1) array (array of column-matrices) of triangle
  589. eccentricities,
  590. dofs : is a (N x 1 x 9) arrays (arrays of row-matrices) of computed
  591. degrees of freedom.
  592. Returns
  593. -------
  594. Returns the N-array of interpolated function values.
  595. """
  596. subtri = np.argmin(alpha, axis=1)[:, 0]
  597. ksi = _roll_vectorized(alpha, -subtri, axis=0)
  598. E = _roll_vectorized(ecc, -subtri, axis=0)
  599. x = ksi[:, 0, 0]
  600. y = ksi[:, 1, 0]
  601. z = ksi[:, 2, 0]
  602. x_sq = x*x
  603. y_sq = y*y
  604. z_sq = z*z
  605. V = _to_matrix_vectorized([
  606. [x_sq*x], [y_sq*y], [z_sq*z], [x_sq*z], [x_sq*y], [y_sq*x],
  607. [y_sq*z], [z_sq*y], [z_sq*x], [x*y*z]])
  608. prod = _prod_vectorized(self.M, V)
  609. prod += _scalar_vectorized(E[:, 0, 0],
  610. _prod_vectorized(self.M0, V))
  611. prod += _scalar_vectorized(E[:, 1, 0],
  612. _prod_vectorized(self.M1, V))
  613. prod += _scalar_vectorized(E[:, 2, 0],
  614. _prod_vectorized(self.M2, V))
  615. s = _roll_vectorized(prod, 3*subtri, axis=0)
  616. return _prod_vectorized(dofs, s)[:, 0, 0]
  617. def get_function_derivatives(self, alpha, J, ecc, dofs):
  618. """
  619. Parameters
  620. ----------
  621. *alpha* is a (N x 3 x 1) array (array of column-matrices of
  622. barycentric coordinates)
  623. *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
  624. triangle first apex)
  625. *ecc* is a (N x 3 x 1) array (array of column-matrices of triangle
  626. eccentricities)
  627. *dofs* is a (N x 1 x 9) arrays (arrays of row-matrices) of computed
  628. degrees of freedom.
  629. Returns
  630. -------
  631. Returns the values of interpolated function derivatives [dz/dx, dz/dy]
  632. in global coordinates at locations alpha, as a column-matrices of
  633. shape (N x 2 x 1).
  634. """
  635. subtri = np.argmin(alpha, axis=1)[:, 0]
  636. ksi = _roll_vectorized(alpha, -subtri, axis=0)
  637. E = _roll_vectorized(ecc, -subtri, axis=0)
  638. x = ksi[:, 0, 0]
  639. y = ksi[:, 1, 0]
  640. z = ksi[:, 2, 0]
  641. x_sq = x*x
  642. y_sq = y*y
  643. z_sq = z*z
  644. dV = _to_matrix_vectorized([
  645. [ -3.*x_sq, -3.*x_sq],
  646. [ 3.*y_sq, 0.],
  647. [ 0., 3.*z_sq],
  648. [ -2.*x*z, -2.*x*z+x_sq],
  649. [-2.*x*y+x_sq, -2.*x*y],
  650. [ 2.*x*y-y_sq, -y_sq],
  651. [ 2.*y*z, y_sq],
  652. [ z_sq, 2.*y*z],
  653. [ -z_sq, 2.*x*z-z_sq],
  654. [ x*z-y*z, x*y-y*z]])
  655. # Puts back dV in first apex basis
  656. dV = _prod_vectorized(dV, _extract_submatrices(
  657. self.rotate_dV, subtri, block_size=2, axis=0))
  658. prod = _prod_vectorized(self.M, dV)
  659. prod += _scalar_vectorized(E[:, 0, 0],
  660. _prod_vectorized(self.M0, dV))
  661. prod += _scalar_vectorized(E[:, 1, 0],
  662. _prod_vectorized(self.M1, dV))
  663. prod += _scalar_vectorized(E[:, 2, 0],
  664. _prod_vectorized(self.M2, dV))
  665. dsdksi = _roll_vectorized(prod, 3*subtri, axis=0)
  666. dfdksi = _prod_vectorized(dofs, dsdksi)
  667. # In global coordinates:
  668. # Here we try to deal with the simplest colinear cases, returning a
  669. # null matrix.
  670. J_inv = _safe_inv22_vectorized(J)
  671. dfdx = _prod_vectorized(J_inv, _transpose_vectorized(dfdksi))
  672. return dfdx
  673. def get_function_hessians(self, alpha, J, ecc, dofs):
  674. """
  675. Parameters
  676. ----------
  677. *alpha* is a (N x 3 x 1) array (array of column-matrices) of
  678. barycentric coordinates
  679. *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
  680. triangle first apex)
  681. *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle
  682. eccentricities
  683. *dofs* is a (N x 1 x 9) arrays (arrays of row-matrices) of computed
  684. degrees of freedom.
  685. Returns
  686. -------
  687. Returns the values of interpolated function 2nd-derivatives
  688. [d2z/dx2, d2z/dy2, d2z/dxdy] in global coordinates at locations alpha,
  689. as a column-matrices of shape (N x 3 x 1).
  690. """
  691. d2sdksi2 = self.get_d2Sidksij2(alpha, ecc)
  692. d2fdksi2 = _prod_vectorized(dofs, d2sdksi2)
  693. H_rot = self.get_Hrot_from_J(J)
  694. d2fdx2 = _prod_vectorized(d2fdksi2, H_rot)
  695. return _transpose_vectorized(d2fdx2)
  696. def get_d2Sidksij2(self, alpha, ecc):
  697. """
  698. Parameters
  699. ----------
  700. *alpha* is a (N x 3 x 1) array (array of column-matrices) of
  701. barycentric coordinates
  702. *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle
  703. eccentricities
  704. Returns
  705. -------
  706. Returns the arrays d2sdksi2 (N x 3 x 1) Hessian of shape functions
  707. expressed in covariant coordinates in first apex basis.
  708. """
  709. subtri = np.argmin(alpha, axis=1)[:, 0]
  710. ksi = _roll_vectorized(alpha, -subtri, axis=0)
  711. E = _roll_vectorized(ecc, -subtri, axis=0)
  712. x = ksi[:, 0, 0]
  713. y = ksi[:, 1, 0]
  714. z = ksi[:, 2, 0]
  715. d2V = _to_matrix_vectorized([
  716. [ 6.*x, 6.*x, 6.*x],
  717. [ 6.*y, 0., 0.],
  718. [ 0., 6.*z, 0.],
  719. [ 2.*z, 2.*z-4.*x, 2.*z-2.*x],
  720. [2.*y-4.*x, 2.*y, 2.*y-2.*x],
  721. [2.*x-4.*y, 0., -2.*y],
  722. [ 2.*z, 0., 2.*y],
  723. [ 0., 2.*y, 2.*z],
  724. [ 0., 2.*x-4.*z, -2.*z],
  725. [ -2.*z, -2.*y, x-y-z]])
  726. # Puts back d2V in first apex basis
  727. d2V = _prod_vectorized(d2V, _extract_submatrices(
  728. self.rotate_d2V, subtri, block_size=3, axis=0))
  729. prod = _prod_vectorized(self.M, d2V)
  730. prod += _scalar_vectorized(E[:, 0, 0],
  731. _prod_vectorized(self.M0, d2V))
  732. prod += _scalar_vectorized(E[:, 1, 0],
  733. _prod_vectorized(self.M1, d2V))
  734. prod += _scalar_vectorized(E[:, 2, 0],
  735. _prod_vectorized(self.M2, d2V))
  736. d2sdksi2 = _roll_vectorized(prod, 3*subtri, axis=0)
  737. return d2sdksi2
  738. def get_bending_matrices(self, J, ecc):
  739. """
  740. Parameters
  741. ----------
  742. *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
  743. triangle first apex)
  744. *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle
  745. eccentricities
  746. Returns
  747. -------
  748. Returns the element K matrices for bending energy expressed in
  749. GLOBAL nodal coordinates.
  750. K_ij = integral [ (d2zi/dx2 + d2zi/dy2) * (d2zj/dx2 + d2zj/dy2) dA]
  751. tri_J is needed to rotate dofs from local basis to global basis
  752. """
  753. n = np.size(ecc, 0)
  754. # 1) matrix to rotate dofs in global coordinates
  755. J1 = _prod_vectorized(self.J0_to_J1, J)
  756. J2 = _prod_vectorized(self.J0_to_J2, J)
  757. DOF_rot = np.zeros([n, 9, 9], dtype=np.float64)
  758. DOF_rot[:, 0, 0] = 1
  759. DOF_rot[:, 3, 3] = 1
  760. DOF_rot[:, 6, 6] = 1
  761. DOF_rot[:, 1:3, 1:3] = J
  762. DOF_rot[:, 4:6, 4:6] = J1
  763. DOF_rot[:, 7:9, 7:9] = J2
  764. # 2) matrix to rotate Hessian in global coordinates.
  765. H_rot, area = self.get_Hrot_from_J(J, return_area=True)
  766. # 3) Computes stiffness matrix
  767. # Gauss quadrature.
  768. K = np.zeros([n, 9, 9], dtype=np.float64)
  769. weights = self.gauss_w
  770. pts = self.gauss_pts
  771. for igauss in range(self.n_gauss):
  772. alpha = np.tile(pts[igauss, :], n).reshape(n, 3)
  773. alpha = np.expand_dims(alpha, 2)
  774. weight = weights[igauss]
  775. d2Skdksi2 = self.get_d2Sidksij2(alpha, ecc)
  776. d2Skdx2 = _prod_vectorized(d2Skdksi2, H_rot)
  777. K += weight * _prod_vectorized(_prod_vectorized(d2Skdx2, self.E),
  778. _transpose_vectorized(d2Skdx2))
  779. # 4) With nodal (not elem) dofs
  780. K = _prod_vectorized(_prod_vectorized(_transpose_vectorized(DOF_rot),
  781. K), DOF_rot)
  782. # 5) Need the area to compute total element energy
  783. return _scalar_vectorized(area, K)
  784. def get_Hrot_from_J(self, J, return_area=False):
  785. """
  786. Parameters
  787. ----------
  788. *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
  789. triangle first apex)
  790. Returns
  791. -------
  792. Returns H_rot used to rotate Hessian from local basis of first apex,
  793. to global coordinates.
  794. if *return_area* is True, returns also the triangle area (0.5*det(J))
  795. """
  796. # Here we try to deal with the simplest colinear cases; a null
  797. # energy and area is imposed.
  798. J_inv = _safe_inv22_vectorized(J)
  799. Ji00 = J_inv[:, 0, 0]
  800. Ji11 = J_inv[:, 1, 1]
  801. Ji10 = J_inv[:, 1, 0]
  802. Ji01 = J_inv[:, 0, 1]
  803. H_rot = _to_matrix_vectorized([
  804. [Ji00*Ji00, Ji10*Ji10, Ji00*Ji10],
  805. [Ji01*Ji01, Ji11*Ji11, Ji01*Ji11],
  806. [2*Ji00*Ji01, 2*Ji11*Ji10, Ji00*Ji11+Ji10*Ji01]])
  807. if not return_area:
  808. return H_rot
  809. else:
  810. area = 0.5 * (J[:, 0, 0]*J[:, 1, 1] - J[:, 0, 1]*J[:, 1, 0])
  811. return H_rot, area
  812. def get_Kff_and_Ff(self, J, ecc, triangles, Uc):
  813. """
  814. Build K and F for the following elliptic formulation:
  815. minimization of curvature energy with value of function at node
  816. imposed and derivatives 'free'.
  817. Build the global Kff matrix in cco format.
  818. Build the full Ff vec Ff = - Kfc x Uc.
  819. Parameters
  820. ----------
  821. *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
  822. triangle first apex)
  823. *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle
  824. eccentricities
  825. *triangles* is a (N x 3) array of nodes indexes.
  826. *Uc* is (N x 3) array of imposed displacements at nodes
  827. Returns
  828. -------
  829. (Kff_rows, Kff_cols, Kff_vals) Kff matrix in coo format - Duplicate
  830. (row, col) entries must be summed.
  831. Ff: force vector - dim npts * 3
  832. """
  833. ntri = np.size(ecc, 0)
  834. vec_range = np.arange(ntri, dtype=np.int32)
  835. c_indices = np.full(ntri, -1, dtype=np.int32) # for unused dofs, -1
  836. f_dof = [1, 2, 4, 5, 7, 8]
  837. c_dof = [0, 3, 6]
  838. # vals, rows and cols indices in global dof numbering
  839. f_dof_indices = _to_matrix_vectorized([[
  840. c_indices, triangles[:, 0]*2, triangles[:, 0]*2+1,
  841. c_indices, triangles[:, 1]*2, triangles[:, 1]*2+1,
  842. c_indices, triangles[:, 2]*2, triangles[:, 2]*2+1]])
  843. expand_indices = np.ones([ntri, 9, 1], dtype=np.int32)
  844. f_row_indices = _prod_vectorized(_transpose_vectorized(f_dof_indices),
  845. _transpose_vectorized(expand_indices))
  846. f_col_indices = _prod_vectorized(expand_indices, f_dof_indices)
  847. K_elem = self.get_bending_matrices(J, ecc)
  848. # Extracting sub-matrices
  849. # Explanation & notations:
  850. # * Subscript f denotes 'free' degrees of freedom (i.e. dz/dx, dz/dx)
  851. # * Subscript c denotes 'condensated' (imposed) degrees of freedom
  852. # (i.e. z at all nodes)
  853. # * F = [Ff, Fc] is the force vector
  854. # * U = [Uf, Uc] is the imposed dof vector
  855. # [ Kff Kfc ]
  856. # * K = [ ] is the laplacian stiffness matrix
  857. # [ Kcf Kff ]
  858. # * As F = K x U one gets straightforwardly: Ff = - Kfc x Uc
  859. # Computing Kff stiffness matrix in sparse coo format
  860. Kff_vals = np.ravel(K_elem[np.ix_(vec_range, f_dof, f_dof)])
  861. Kff_rows = np.ravel(f_row_indices[np.ix_(vec_range, f_dof, f_dof)])
  862. Kff_cols = np.ravel(f_col_indices[np.ix_(vec_range, f_dof, f_dof)])
  863. # Computing Ff force vector in sparse coo format
  864. Kfc_elem = K_elem[np.ix_(vec_range, f_dof, c_dof)]
  865. Uc_elem = np.expand_dims(Uc, axis=2)
  866. Ff_elem = - _prod_vectorized(Kfc_elem, Uc_elem)[:, :, 0]
  867. Ff_indices = f_dof_indices[np.ix_(vec_range, [0], f_dof)][:, 0, :]
  868. # Extracting Ff force vector in dense format
  869. # We have to sum duplicate indices - using bincount
  870. Ff = np.bincount(np.ravel(Ff_indices), weights=np.ravel(Ff_elem))
  871. return Kff_rows, Kff_cols, Kff_vals, Ff
  872. # :class:_DOF_estimator, _DOF_estimator_user, _DOF_estimator_geom,
  873. # _DOF_estimator_min_E
  874. # Private classes used to compute the degree of freedom of each triangular
  875. # element for the TriCubicInterpolator.
  876. class _DOF_estimator:
  877. """
  878. Abstract base class for classes used to estimate a function's first
  879. derivatives, and deduce the dofs for a CubicTriInterpolator using a
  880. reduced HCT element formulation.
  881. Derived classes implement ``compute_df(self, **kwargs)``, returning
  882. ``np.vstack([dfx, dfy]).T`` where ``dfx, dfy`` are the estimation of the 2
  883. gradient coordinates.
  884. """
  885. def __init__(self, interpolator, **kwargs):
  886. cbook._check_isinstance(
  887. CubicTriInterpolator, interpolator=interpolator)
  888. self._pts = interpolator._pts
  889. self._tris_pts = interpolator._tris_pts
  890. self.z = interpolator._z
  891. self._triangles = interpolator._triangles
  892. (self._unit_x, self._unit_y) = (interpolator._unit_x,
  893. interpolator._unit_y)
  894. self.dz = self.compute_dz(**kwargs)
  895. self.compute_dof_from_df()
  896. def compute_dz(self, **kwargs):
  897. raise NotImplementedError
  898. def compute_dof_from_df(self):
  899. """
  900. Compute reduced-HCT elements degrees of freedom, from the gradient.
  901. """
  902. J = CubicTriInterpolator._get_jacobian(self._tris_pts)
  903. tri_z = self.z[self._triangles]
  904. tri_dz = self.dz[self._triangles]
  905. tri_dof = self.get_dof_vec(tri_z, tri_dz, J)
  906. return tri_dof
  907. @staticmethod
  908. def get_dof_vec(tri_z, tri_dz, J):
  909. """
  910. Compute the dof vector of a triangle, from the value of f, df and
  911. of the local Jacobian at each node.
  912. Parameters
  913. ----------
  914. tri_z : shape (3,) array
  915. f nodal values.
  916. tri_dz : shape (3, 2) array
  917. df/dx, df/dy nodal values.
  918. J
  919. Jacobian matrix in local basis of apex 0.
  920. Returns
  921. -------
  922. dof : shape (9,) array
  923. For each apex ``iapex``::
  924. dof[iapex*3+0] = f(Ai)
  925. dof[iapex*3+1] = df(Ai).(AiAi+)
  926. dof[iapex*3+2] = df(Ai).(AiAi-)
  927. """
  928. npt = tri_z.shape[0]
  929. dof = np.zeros([npt, 9], dtype=np.float64)
  930. J1 = _prod_vectorized(_ReducedHCT_Element.J0_to_J1, J)
  931. J2 = _prod_vectorized(_ReducedHCT_Element.J0_to_J2, J)
  932. col0 = _prod_vectorized(J, np.expand_dims(tri_dz[:, 0, :], axis=2))
  933. col1 = _prod_vectorized(J1, np.expand_dims(tri_dz[:, 1, :], axis=2))
  934. col2 = _prod_vectorized(J2, np.expand_dims(tri_dz[:, 2, :], axis=2))
  935. dfdksi = _to_matrix_vectorized([
  936. [col0[:, 0, 0], col1[:, 0, 0], col2[:, 0, 0]],
  937. [col0[:, 1, 0], col1[:, 1, 0], col2[:, 1, 0]]])
  938. dof[:, 0:7:3] = tri_z
  939. dof[:, 1:8:3] = dfdksi[:, 0]
  940. dof[:, 2:9:3] = dfdksi[:, 1]
  941. return dof
  942. class _DOF_estimator_user(_DOF_estimator):
  943. """dz is imposed by user; accounts for scaling if any."""
  944. def compute_dz(self, dz):
  945. (dzdx, dzdy) = dz
  946. dzdx = dzdx * self._unit_x
  947. dzdy = dzdy * self._unit_y
  948. return np.vstack([dzdx, dzdy]).T
  949. class _DOF_estimator_geom(_DOF_estimator):
  950. """Fast 'geometric' approximation, recommended for large arrays."""
  951. def compute_dz(self):
  952. """
  953. self.df is computed as weighted average of _triangles sharing a common
  954. node. On each triangle itri f is first assumed linear (= ~f), which
  955. allows to compute d~f[itri]
  956. Then the following approximation of df nodal values is then proposed:
  957. f[ipt] = SUM ( w[itri] x d~f[itri] , for itri sharing apex ipt)
  958. The weighted coeff. w[itri] are proportional to the angle of the
  959. triangle itri at apex ipt
  960. """
  961. el_geom_w = self.compute_geom_weights()
  962. el_geom_grad = self.compute_geom_grads()
  963. # Sum of weights coeffs
  964. w_node_sum = np.bincount(np.ravel(self._triangles),
  965. weights=np.ravel(el_geom_w))
  966. # Sum of weighted df = (dfx, dfy)
  967. dfx_el_w = np.empty_like(el_geom_w)
  968. dfy_el_w = np.empty_like(el_geom_w)
  969. for iapex in range(3):
  970. dfx_el_w[:, iapex] = el_geom_w[:, iapex]*el_geom_grad[:, 0]
  971. dfy_el_w[:, iapex] = el_geom_w[:, iapex]*el_geom_grad[:, 1]
  972. dfx_node_sum = np.bincount(np.ravel(self._triangles),
  973. weights=np.ravel(dfx_el_w))
  974. dfy_node_sum = np.bincount(np.ravel(self._triangles),
  975. weights=np.ravel(dfy_el_w))
  976. # Estimation of df
  977. dfx_estim = dfx_node_sum/w_node_sum
  978. dfy_estim = dfy_node_sum/w_node_sum
  979. return np.vstack([dfx_estim, dfy_estim]).T
  980. def compute_geom_weights(self):
  981. """
  982. Build the (nelems, 3) weights coeffs of _triangles angles,
  983. renormalized so that np.sum(weights, axis=1) == np.ones(nelems)
  984. """
  985. weights = np.zeros([np.size(self._triangles, 0), 3])
  986. tris_pts = self._tris_pts
  987. for ipt in range(3):
  988. p0 = tris_pts[:, ipt % 3, :]
  989. p1 = tris_pts[:, (ipt+1) % 3, :]
  990. p2 = tris_pts[:, (ipt-1) % 3, :]
  991. alpha1 = np.arctan2(p1[:, 1]-p0[:, 1], p1[:, 0]-p0[:, 0])
  992. alpha2 = np.arctan2(p2[:, 1]-p0[:, 1], p2[:, 0]-p0[:, 0])
  993. # In the below formula we could take modulo 2. but
  994. # modulo 1. is safer regarding round-off errors (flat triangles).
  995. angle = np.abs(((alpha2-alpha1) / np.pi) % 1)
  996. # Weight proportional to angle up np.pi/2; null weight for
  997. # degenerated cases 0 and np.pi (note that *angle* is normalized
  998. # by np.pi).
  999. weights[:, ipt] = 0.5 - np.abs(angle-0.5)
  1000. return weights
  1001. def compute_geom_grads(self):
  1002. """
  1003. Compute the (global) gradient component of f assumed linear (~f).
  1004. returns array df of shape (nelems, 2)
  1005. df[ielem].dM[ielem] = dz[ielem] i.e. df = dz x dM = dM.T^-1 x dz
  1006. """
  1007. tris_pts = self._tris_pts
  1008. tris_f = self.z[self._triangles]
  1009. dM1 = tris_pts[:, 1, :] - tris_pts[:, 0, :]
  1010. dM2 = tris_pts[:, 2, :] - tris_pts[:, 0, :]
  1011. dM = np.dstack([dM1, dM2])
  1012. # Here we try to deal with the simplest colinear cases: a null
  1013. # gradient is assumed in this case.
  1014. dM_inv = _safe_inv22_vectorized(dM)
  1015. dZ1 = tris_f[:, 1] - tris_f[:, 0]
  1016. dZ2 = tris_f[:, 2] - tris_f[:, 0]
  1017. dZ = np.vstack([dZ1, dZ2]).T
  1018. df = np.empty_like(dZ)
  1019. # With np.einsum: could be ej,eji -> ej
  1020. df[:, 0] = dZ[:, 0]*dM_inv[:, 0, 0] + dZ[:, 1]*dM_inv[:, 1, 0]
  1021. df[:, 1] = dZ[:, 0]*dM_inv[:, 0, 1] + dZ[:, 1]*dM_inv[:, 1, 1]
  1022. return df
  1023. class _DOF_estimator_min_E(_DOF_estimator_geom):
  1024. """
  1025. The 'smoothest' approximation, df is computed through global minimization
  1026. of the bending energy:
  1027. E(f) = integral[(d2z/dx2 + d2z/dy2 + 2 d2z/dxdy)**2 dA]
  1028. """
  1029. def __init__(self, Interpolator):
  1030. self._eccs = Interpolator._eccs
  1031. _DOF_estimator_geom.__init__(self, Interpolator)
  1032. def compute_dz(self):
  1033. """
  1034. Elliptic solver for bending energy minimization.
  1035. Uses a dedicated 'toy' sparse Jacobi PCG solver.
  1036. """
  1037. # Initial guess for iterative PCG solver.
  1038. dz_init = _DOF_estimator_geom.compute_dz(self)
  1039. Uf0 = np.ravel(dz_init)
  1040. reference_element = _ReducedHCT_Element()
  1041. J = CubicTriInterpolator._get_jacobian(self._tris_pts)
  1042. eccs = self._eccs
  1043. triangles = self._triangles
  1044. Uc = self.z[self._triangles]
  1045. # Building stiffness matrix and force vector in coo format
  1046. Kff_rows, Kff_cols, Kff_vals, Ff = reference_element.get_Kff_and_Ff(
  1047. J, eccs, triangles, Uc)
  1048. # Building sparse matrix and solving minimization problem
  1049. # We could use scipy.sparse direct solver; however to avoid this
  1050. # external dependency an implementation of a simple PCG solver with
  1051. # a simple diagonal Jacobi preconditioner is implemented.
  1052. tol = 1.e-10
  1053. n_dof = Ff.shape[0]
  1054. Kff_coo = _Sparse_Matrix_coo(Kff_vals, Kff_rows, Kff_cols,
  1055. shape=(n_dof, n_dof))
  1056. Kff_coo.compress_csc()
  1057. Uf, err = _cg(A=Kff_coo, b=Ff, x0=Uf0, tol=tol)
  1058. # If the PCG did not converge, we return the best guess between Uf0
  1059. # and Uf.
  1060. err0 = np.linalg.norm(Kff_coo.dot(Uf0) - Ff)
  1061. if err0 < err:
  1062. # Maybe a good occasion to raise a warning here ?
  1063. cbook._warn_external("In TriCubicInterpolator initialization, "
  1064. "PCG sparse solver did not converge after "
  1065. "1000 iterations. `geom` approximation is "
  1066. "used instead of `min_E`")
  1067. Uf = Uf0
  1068. # Building dz from Uf
  1069. dz = np.empty([self._pts.shape[0], 2], dtype=np.float64)
  1070. dz[:, 0] = Uf[::2]
  1071. dz[:, 1] = Uf[1::2]
  1072. return dz
  1073. # The following private :class:_Sparse_Matrix_coo and :func:_cg provide
  1074. # a PCG sparse solver for (symmetric) elliptic problems.
  1075. class _Sparse_Matrix_coo:
  1076. def __init__(self, vals, rows, cols, shape):
  1077. """
  1078. Create a sparse matrix in coo format.
  1079. *vals*: arrays of values of non-null entries of the matrix
  1080. *rows*: int arrays of rows of non-null entries of the matrix
  1081. *cols*: int arrays of cols of non-null entries of the matrix
  1082. *shape*: 2-tuple (n, m) of matrix shape
  1083. """
  1084. self.n, self.m = shape
  1085. self.vals = np.asarray(vals, dtype=np.float64)
  1086. self.rows = np.asarray(rows, dtype=np.int32)
  1087. self.cols = np.asarray(cols, dtype=np.int32)
  1088. def dot(self, V):
  1089. """
  1090. Dot product of self by a vector *V* in sparse-dense to dense format
  1091. *V* dense vector of shape (self.m,).
  1092. """
  1093. assert V.shape == (self.m,)
  1094. return np.bincount(self.rows,
  1095. weights=self.vals*V[self.cols],
  1096. minlength=self.m)
  1097. def compress_csc(self):
  1098. """
  1099. Compress rows, cols, vals / summing duplicates. Sort for csc format.
  1100. """
  1101. _, unique, indices = np.unique(
  1102. self.rows + self.n*self.cols,
  1103. return_index=True, return_inverse=True)
  1104. self.rows = self.rows[unique]
  1105. self.cols = self.cols[unique]
  1106. self.vals = np.bincount(indices, weights=self.vals)
  1107. def compress_csr(self):
  1108. """
  1109. Compress rows, cols, vals / summing duplicates. Sort for csr format.
  1110. """
  1111. _, unique, indices = np.unique(
  1112. self.m*self.rows + self.cols,
  1113. return_index=True, return_inverse=True)
  1114. self.rows = self.rows[unique]
  1115. self.cols = self.cols[unique]
  1116. self.vals = np.bincount(indices, weights=self.vals)
  1117. def to_dense(self):
  1118. """
  1119. Return a dense matrix representing self, mainly for debugging purposes.
  1120. """
  1121. ret = np.zeros([self.n, self.m], dtype=np.float64)
  1122. nvals = self.vals.size
  1123. for i in range(nvals):
  1124. ret[self.rows[i], self.cols[i]] += self.vals[i]
  1125. return ret
  1126. def __str__(self):
  1127. return self.to_dense().__str__()
  1128. @property
  1129. def diag(self):
  1130. """Return the (dense) vector of the diagonal elements."""
  1131. in_diag = (self.rows == self.cols)
  1132. diag = np.zeros(min(self.n, self.n), dtype=np.float64) # default 0.
  1133. diag[self.rows[in_diag]] = self.vals[in_diag]
  1134. return diag
  1135. def _cg(A, b, x0=None, tol=1.e-10, maxiter=1000):
  1136. """
  1137. Use Preconditioned Conjugate Gradient iteration to solve A x = b
  1138. A simple Jacobi (diagonal) preconditionner is used.
  1139. Parameters
  1140. ----------
  1141. A : _Sparse_Matrix_coo
  1142. *A* must have been compressed before by compress_csc or
  1143. compress_csr method.
  1144. b : array
  1145. Right hand side of the linear system.
  1146. x0 : array, optional
  1147. Starting guess for the solution. Defaults to the zero vector.
  1148. tol : float, optional
  1149. Tolerance to achieve. The algorithm terminates when the relative
  1150. residual is below tol. Default is 1e-10.
  1151. maxiter : int, optional
  1152. Maximum number of iterations. Iteration will stop after *maxiter*
  1153. steps even if the specified tolerance has not been achieved. Defaults
  1154. to 1000.
  1155. Returns
  1156. -------
  1157. x : array
  1158. The converged solution.
  1159. err : float
  1160. The absolute error np.linalg.norm(A.dot(x) - b)
  1161. """
  1162. n = b.size
  1163. assert A.n == n
  1164. assert A.m == n
  1165. b_norm = np.linalg.norm(b)
  1166. # Jacobi pre-conditioner
  1167. kvec = A.diag
  1168. # For diag elem < 1e-6 we keep 1e-6.
  1169. kvec = np.maximum(kvec, 1e-6)
  1170. # Initial guess
  1171. if x0 is None:
  1172. x = np.zeros(n)
  1173. else:
  1174. x = x0
  1175. r = b - A.dot(x)
  1176. w = r/kvec
  1177. p = np.zeros(n)
  1178. beta = 0.0
  1179. rho = np.dot(r, w)
  1180. k = 0
  1181. # Following C. T. Kelley
  1182. while (np.sqrt(abs(rho)) > tol*b_norm) and (k < maxiter):
  1183. p = w + beta*p
  1184. z = A.dot(p)
  1185. alpha = rho/np.dot(p, z)
  1186. r = r - alpha*z
  1187. w = r/kvec
  1188. rhoold = rho
  1189. rho = np.dot(r, w)
  1190. x = x + alpha*p
  1191. beta = rho/rhoold
  1192. #err = np.linalg.norm(A.dot(x) - b) # absolute accuracy - not used
  1193. k += 1
  1194. err = np.linalg.norm(A.dot(x) - b)
  1195. return x, err
  1196. # The following private functions:
  1197. # :func:`_safe_inv22_vectorized`
  1198. # :func:`_pseudo_inv22sym_vectorized`
  1199. # :func:`_prod_vectorized`
  1200. # :func:`_scalar_vectorized`
  1201. # :func:`_transpose_vectorized`
  1202. # :func:`_roll_vectorized`
  1203. # :func:`_to_matrix_vectorized`
  1204. # :func:`_extract_submatrices`
  1205. # provide fast numpy implementation of some standard operations on arrays of
  1206. # matrices - stored as (:, n_rows, n_cols)-shaped np.arrays.
  1207. # Development note: Dealing with pathologic 'flat' triangles in the
  1208. # CubicTriInterpolator code and impact on (2, 2)-matrix inversion functions
  1209. # :func:`_safe_inv22_vectorized` and :func:`_pseudo_inv22sym_vectorized`.
  1210. #
  1211. # Goals:
  1212. # 1) The CubicTriInterpolator should be able to handle flat or almost flat
  1213. # triangles without raising an error,
  1214. # 2) These degenerated triangles should have no impact on the automatic dof
  1215. # calculation (associated with null weight for the _DOF_estimator_geom and
  1216. # with null energy for the _DOF_estimator_min_E),
  1217. # 3) Linear patch test should be passed exactly on degenerated meshes,
  1218. # 4) Interpolation (with :meth:`_interpolate_single_key` or
  1219. # :meth:`_interpolate_multi_key`) shall be correctly handled even *inside*
  1220. # the pathologic triangles, to interact correctly with a TriRefiner class.
  1221. #
  1222. # Difficulties:
  1223. # Flat triangles have rank-deficient *J* (so-called jacobian matrix) and
  1224. # *metric* (the metric tensor = J x J.T). Computation of the local
  1225. # tangent plane is also problematic.
  1226. #
  1227. # Implementation:
  1228. # Most of the time, when computing the inverse of a rank-deficient matrix it
  1229. # is safe to simply return the null matrix (which is the implementation in
  1230. # :func:`_safe_inv22_vectorized`). This is because of point 2), itself
  1231. # enforced by:
  1232. # - null area hence null energy in :class:`_DOF_estimator_min_E`
  1233. # - angles close or equal to 0 or np.pi hence null weight in
  1234. # :class:`_DOF_estimator_geom`.
  1235. # Note that the function angle -> weight is continuous and maximum for an
  1236. # angle np.pi/2 (refer to :meth:`compute_geom_weights`)
  1237. # The exception is the computation of barycentric coordinates, which is done
  1238. # by inversion of the *metric* matrix. In this case, we need to compute a set
  1239. # of valid coordinates (1 among numerous possibilities), to ensure point 4).
  1240. # We benefit here from the symmetry of metric = J x J.T, which makes it easier
  1241. # to compute a pseudo-inverse in :func:`_pseudo_inv22sym_vectorized`
  1242. def _safe_inv22_vectorized(M):
  1243. """
  1244. Inversion of arrays of (2, 2) matrices, returns 0 for rank-deficient
  1245. matrices.
  1246. *M* : array of (2, 2) matrices to inverse, shape (n, 2, 2)
  1247. """
  1248. assert M.ndim == 3
  1249. assert M.shape[-2:] == (2, 2)
  1250. M_inv = np.empty_like(M)
  1251. prod1 = M[:, 0, 0]*M[:, 1, 1]
  1252. delta = prod1 - M[:, 0, 1]*M[:, 1, 0]
  1253. # We set delta_inv to 0. in case of a rank deficient matrix; a
  1254. # rank-deficient input matrix *M* will lead to a null matrix in output
  1255. rank2 = (np.abs(delta) > 1e-8*np.abs(prod1))
  1256. if np.all(rank2):
  1257. # Normal 'optimized' flow.
  1258. delta_inv = 1./delta
  1259. else:
  1260. # 'Pathologic' flow.
  1261. delta_inv = np.zeros(M.shape[0])
  1262. delta_inv[rank2] = 1./delta[rank2]
  1263. M_inv[:, 0, 0] = M[:, 1, 1]*delta_inv
  1264. M_inv[:, 0, 1] = -M[:, 0, 1]*delta_inv
  1265. M_inv[:, 1, 0] = -M[:, 1, 0]*delta_inv
  1266. M_inv[:, 1, 1] = M[:, 0, 0]*delta_inv
  1267. return M_inv
  1268. def _pseudo_inv22sym_vectorized(M):
  1269. """
  1270. Inversion of arrays of (2, 2) SYMMETRIC matrices; returns the
  1271. (Moore-Penrose) pseudo-inverse for rank-deficient matrices.
  1272. In case M is of rank 1, we have M = trace(M) x P where P is the orthogonal
  1273. projection on Im(M), and we return trace(M)^-1 x P == M / trace(M)**2
  1274. In case M is of rank 0, we return the null matrix.
  1275. *M* : array of (2, 2) matrices to inverse, shape (n, 2, 2)
  1276. """
  1277. assert M.ndim == 3
  1278. assert M.shape[-2:] == (2, 2)
  1279. M_inv = np.empty_like(M)
  1280. prod1 = M[:, 0, 0]*M[:, 1, 1]
  1281. delta = prod1 - M[:, 0, 1]*M[:, 1, 0]
  1282. rank2 = (np.abs(delta) > 1e-8*np.abs(prod1))
  1283. if np.all(rank2):
  1284. # Normal 'optimized' flow.
  1285. M_inv[:, 0, 0] = M[:, 1, 1] / delta
  1286. M_inv[:, 0, 1] = -M[:, 0, 1] / delta
  1287. M_inv[:, 1, 0] = -M[:, 1, 0] / delta
  1288. M_inv[:, 1, 1] = M[:, 0, 0] / delta
  1289. else:
  1290. # 'Pathologic' flow.
  1291. # Here we have to deal with 2 sub-cases
  1292. # 1) First sub-case: matrices of rank 2:
  1293. delta = delta[rank2]
  1294. M_inv[rank2, 0, 0] = M[rank2, 1, 1] / delta
  1295. M_inv[rank2, 0, 1] = -M[rank2, 0, 1] / delta
  1296. M_inv[rank2, 1, 0] = -M[rank2, 1, 0] / delta
  1297. M_inv[rank2, 1, 1] = M[rank2, 0, 0] / delta
  1298. # 2) Second sub-case: rank-deficient matrices of rank 0 and 1:
  1299. rank01 = ~rank2
  1300. tr = M[rank01, 0, 0] + M[rank01, 1, 1]
  1301. tr_zeros = (np.abs(tr) < 1.e-8)
  1302. sq_tr_inv = (1.-tr_zeros) / (tr**2+tr_zeros)
  1303. #sq_tr_inv = 1. / tr**2
  1304. M_inv[rank01, 0, 0] = M[rank01, 0, 0] * sq_tr_inv
  1305. M_inv[rank01, 0, 1] = M[rank01, 0, 1] * sq_tr_inv
  1306. M_inv[rank01, 1, 0] = M[rank01, 1, 0] * sq_tr_inv
  1307. M_inv[rank01, 1, 1] = M[rank01, 1, 1] * sq_tr_inv
  1308. return M_inv
  1309. def _prod_vectorized(M1, M2):
  1310. """
  1311. Matrix product between arrays of matrices, or a matrix and an array of
  1312. matrices (*M1* and *M2*)
  1313. """
  1314. sh1 = M1.shape
  1315. sh2 = M2.shape
  1316. assert len(sh1) >= 2
  1317. assert len(sh2) >= 2
  1318. assert sh1[-1] == sh2[-2]
  1319. ndim1 = len(sh1)
  1320. t1_index = [*range(ndim1-2), ndim1-1, ndim1-2]
  1321. return np.sum(np.transpose(M1, t1_index)[..., np.newaxis] *
  1322. M2[..., np.newaxis, :], -3)
  1323. def _scalar_vectorized(scalar, M):
  1324. """
  1325. Scalar product between scalars and matrices.
  1326. """
  1327. return scalar[:, np.newaxis, np.newaxis]*M
  1328. def _transpose_vectorized(M):
  1329. """
  1330. Transposition of an array of matrices *M*.
  1331. """
  1332. return np.transpose(M, [0, 2, 1])
  1333. def _roll_vectorized(M, roll_indices, axis):
  1334. """
  1335. Roll an array of matrices along *axis* (0: rows, 1: columns) according to
  1336. an array of indices *roll_indices*.
  1337. """
  1338. assert axis in [0, 1]
  1339. ndim = M.ndim
  1340. assert ndim == 3
  1341. ndim_roll = roll_indices.ndim
  1342. assert ndim_roll == 1
  1343. sh = M.shape
  1344. r, c = sh[-2:]
  1345. assert sh[0] == roll_indices.shape[0]
  1346. vec_indices = np.arange(sh[0], dtype=np.int32)
  1347. # Builds the rolled matrix
  1348. M_roll = np.empty_like(M)
  1349. if axis == 0:
  1350. for ir in range(r):
  1351. for ic in range(c):
  1352. M_roll[:, ir, ic] = M[vec_indices, (-roll_indices+ir) % r, ic]
  1353. elif axis == 1:
  1354. for ir in range(r):
  1355. for ic in range(c):
  1356. M_roll[:, ir, ic] = M[vec_indices, ir, (-roll_indices+ic) % c]
  1357. return M_roll
  1358. def _to_matrix_vectorized(M):
  1359. """
  1360. Build an array of matrices from individuals np.arrays of identical shapes.
  1361. Parameters
  1362. ----------
  1363. M
  1364. ncols-list of nrows-lists of shape sh.
  1365. Returns
  1366. -------
  1367. M_res : np.array of shape (sh, nrow, ncols)
  1368. *M_res* satisfies ``M_res[..., i, j] = M[i][j]``.
  1369. """
  1370. assert isinstance(M, (tuple, list))
  1371. assert all(isinstance(item, (tuple, list)) for item in M)
  1372. c_vec = np.asarray([len(item) for item in M])
  1373. assert np.all(c_vec-c_vec[0] == 0)
  1374. r = len(M)
  1375. c = c_vec[0]
  1376. M00 = np.asarray(M[0][0])
  1377. dt = M00.dtype
  1378. sh = [M00.shape[0], r, c]
  1379. M_ret = np.empty(sh, dtype=dt)
  1380. for irow in range(r):
  1381. for icol in range(c):
  1382. M_ret[:, irow, icol] = np.asarray(M[irow][icol])
  1383. return M_ret
  1384. def _extract_submatrices(M, block_indices, block_size, axis):
  1385. """
  1386. Extract selected blocks of a matrices *M* depending on parameters
  1387. *block_indices* and *block_size*.
  1388. Returns the array of extracted matrices *Mres* so that ::
  1389. M_res[..., ir, :] = M[(block_indices*block_size+ir), :]
  1390. """
  1391. assert block_indices.ndim == 1
  1392. assert axis in [0, 1]
  1393. r, c = M.shape
  1394. if axis == 0:
  1395. sh = [block_indices.shape[0], block_size, c]
  1396. elif axis == 1:
  1397. sh = [block_indices.shape[0], r, block_size]
  1398. dt = M.dtype
  1399. M_res = np.empty(sh, dtype=dt)
  1400. if axis == 0:
  1401. for ir in range(block_size):
  1402. M_res[:, ir, :] = M[(block_indices*block_size+ir), :]
  1403. elif axis == 1:
  1404. for ic in range(block_size):
  1405. M_res[:, :, ic] = M[:, (block_indices*block_size+ic)]
  1406. return M_res