/script/CTCommon/DTField.py

https://gitlab.com/liuxin429go/H2D2-tools
Python | 268 lines | 189 code | 50 blank | 29 comment | 34 complexity | 818e5ecd009367001c86abec168cd24c MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #************************************************************************
  3. # --- Copyright (c) INRS 2016
  4. # --- Institut National de la Recherche Scientifique (INRS)
  5. # ---
  6. # --- Distributed under the GNU Lesser General Public License, Version 3.0.
  7. # --- See accompanying file LICENSE.txt.
  8. #************************************************************************
  9. from . import FEMesh
  10. from . import DTData
  11. try:
  12. from .DTReducOperation import REDUCTION_OPERATION as Operation
  13. except ImportError:
  14. from .DTReducOperation_pp import REDUCTION_OPERATION as Operation
  15. import numpy as np
  16. import logging
  17. LOGGER = logging.getLogger("INRS.H2D2.Tools.Data.Field")
  18. class DTField:
  19. def __init__(self):
  20. pass
  21. def getDataAtTime(self, reducOp = Operation.op_noop, tsteps = None, cols = []):
  22. raise NotImplementedError
  23. def getDataAtStep(self, reducOp = Operation.op_noop, tsteps = None, cols = []):
  24. raise NotImplementedError
  25. def getDataActual(self):
  26. raise NotImplementedError
  27. def getData(self):
  28. raise NotImplementedError
  29. def doInterpolate(self, X, Y):
  30. raise NotImplementedError
  31. def doProbe(self, X, Y):
  32. raise NotImplementedError
  33. def getGrid(self):
  34. raise NotImplementedError
  35. def getDataMin(self):
  36. raise NotImplementedError
  37. def getDataMax(self):
  38. raise NotImplementedError
  39. class DT2DFiniteElementField(DTField):
  40. def __init__(self, mesh, data=None):
  41. super(DT2DFiniteElementField, self).__init__()
  42. self.mesh = mesh # FEMesh or subgrid
  43. self.data = data # DTData
  44. self.tstp = None # (Operation, Time steps)
  45. self.cols = None # Columns in data
  46. self.actu = None # Activ data
  47. self.itrp = FEMesh.FEInterpolatedPoints(self.mesh) if self.mesh else None
  48. self.prbe = FEMesh.FEProbeOnPoints(self.mesh) if self.mesh else None
  49. self.ndgb = self.mesh.getNodeNumbersGlobal() if self.mesh and self.mesh.isSubMesh() else None
  50. def __copy__(self):
  51. """
  52. Shallow copy, while reseting the activ data
  53. """
  54. newone = type(self)(None)
  55. newone.mesh = self.mesh
  56. newone.ndgb = self.ndgb
  57. newone.itrp = self.itrp
  58. newone.prbe = self.prbe
  59. newone.data = self.data
  60. newone.tstp = None
  61. newone.cols = None
  62. newone.actu = None
  63. return newone
  64. def __str__(self):
  65. if self.tstp:
  66. o, t = self.tstp
  67. if o == Operation.op_noop:
  68. s = ''
  69. else:
  70. s = '%s(' % o.name
  71. if len(t) == 1:
  72. s += '%f' % t[0]
  73. else:
  74. s += '[%f,%f]' % (t[0], t[-1])
  75. if o != Operation.op_noop:
  76. s += ')'
  77. else:
  78. s = ''
  79. return s
  80. def getDataAtTime(self, reducOp=Operation.op_noop, tsteps=None, cols=[]):
  81. LOGGER.debug('DT2DFiniteElementField: Get data with op %s at times (%s)', reducOp, tsteps)
  82. if (reducOp, tsteps) != self.tstp or cols != self.cols:
  83. op = DTData.DTDataReduc()
  84. op.compute(self.data, reducOp=reducOp, tsteps=tsteps, cols=cols)
  85. self.tstp = (reducOp, tsteps)
  86. self.cols = cols
  87. self.actu = op.getVal()[self.ndgb] if self.ndgb else op.getVal()
  88. return self.actu
  89. def getDataAtStep(self, reducOp=Operation.op_noop, tsteps=None, cols=[]):
  90. LOGGER.debug('DT2DFiniteElementField: Get data with op %s at steps (%s)', reducOp, tsteps)
  91. if (reducOp, tsteps) != self.tstp or cols != self.cols:
  92. op = DTData.DTDataReduc()
  93. op.compute(self.data, reducOp=reducOp, tsteps=tsteps, cols=cols)
  94. self.tstp = (reducOp, tsteps)
  95. self.cols = cols
  96. self.actu = op.getVal()[self.ndgb] if self.ndgb else op.getVal()
  97. return self.actu
  98. def getDataActual(self):
  99. return self.actu
  100. def getData(self):
  101. return self.data
  102. def doInterpolate(self, X, Y):
  103. self.itrp.setCoord(X, Y)
  104. i = self.itrp.interpolate(self.actu)
  105. return i
  106. def doProbe(self, X, Y):
  107. return self.prbe.interpolate(X, Y, self.actu)
  108. def getGrid(self):
  109. return self.mesh
  110. def getDataMin(self):
  111. if self.actu.ndim == 1:
  112. return np.min(self.actu)
  113. else:
  114. return np.min(np.linalg.norm(self.actu, axis=self.actu.ndim-1))
  115. def getDataMax(self):
  116. if self.actu.ndim == 1:
  117. return np.max(self.actu)
  118. else:
  119. return np.max(np.linalg.norm(self.actu, axis=self.actu.ndim-1))
  120. class DT2DRegularGridField(DTField):
  121. def __init__(self, rgrid, source):
  122. super(DT2DRegularGridField, self).__init__()
  123. self.rgrid = rgrid
  124. self.source = source # DTField
  125. self.actu = None # Activ data
  126. def getDataAtTime(self, reducOp = Operation.op_noop, tsteps = None, cols = []):
  127. LOGGER.debug('DT2DRegularGridField: Get data with op %s at times (%s)', reducOp, tsteps)
  128. self.source.getDataAtTime(reducOp, tsteps, cols)
  129. self.actu = self.source.doInterpolate(*self.rgrid.getCoordinates())
  130. return self.actu
  131. def getDataAtStep(self, reducOp = Operation.op_noop, tsteps = None, cols = []):
  132. LOGGER.debug('DT2DRegularGridField: Get data with op %s at steps (%s)', reducOp, tsteps)
  133. self.source.getDataAtStep(reducOp, tsteps, cols)
  134. self.actu = self.source.doInterpolate(*self.rgrid.getCoordinates())
  135. return self.actu
  136. def getData(self):
  137. return self.source.getData()
  138. def getDataActual(self):
  139. #nx, ny = self.rgrid.getGridSize()
  140. #nd = np.size(self.actu[0])
  141. #shp = (nx, ny, nd)
  142. #if nd <= 1: shp = (nx, ny)
  143. #return np.array(self.actu).reshape(shp)
  144. return self.actu
  145. def doInterpolate(self, X, Y):
  146. raise NotImplementedError
  147. def doProbe(self, X, Y):
  148. raise NotImplementedError
  149. def getGrid(self):
  150. return self.rgrid
  151. def getDataMin(self):
  152. """
  153. return min ignoring nan
  154. """
  155. if self.actu.ndim == 1:
  156. return np.nanmin(self.actu)
  157. else:
  158. return np.nanmin(np.linalg.norm(self.actu, axis=self.actu.ndim-1))
  159. def getDataMax(self):
  160. """
  161. return max ignoring nan
  162. """
  163. if self.actu.ndim == 1:
  164. return np.nanmax(self.actu)
  165. else:
  166. return np.nanmax(np.linalg.norm(self.actu, axis=self.actu.ndim-1))
  167. class DT1DRegularGridField(DTField):
  168. def __init__(self, rgrid, source):
  169. super(DT1DRegularGridField, self).__init__()
  170. self.rgrid = rgrid
  171. self.source = source # DTField
  172. self.actu = None # Activ data
  173. def getDataAtTime(self, reducOp = Operation.op_noop, tsteps = None, cols = []):
  174. LOGGER.debug('DT1DRegularGridField: Get data with op %s at times (%s)', reducOp, tsteps)
  175. self.source.getDataAtTime(reducOp, tsteps, cols)
  176. self.actu = self.source.doInterpolate(*self.rgrid.getCoordinates())
  177. return self.actu
  178. def getDataAtStep(self, reducOp = Operation.op_noop, tsteps = None, cols = []):
  179. LOGGER.debug('DT1DRegularGridField: Get data with op %s at steps (%s)', reducOp, tsteps)
  180. self.source.getDataAtStep(reducOp, tsteps, cols)
  181. self.actu = self.source.doInterpolate(*self.rgrid.getCoordinates())
  182. return self.actu
  183. def getData(self):
  184. return self.source.getData()
  185. def getDataActual(self):
  186. nx, ny = self.rgrid.getGridSize()
  187. nd = np.size(self.actu[0])
  188. shp = (nx, ny, nd)
  189. if nd <= 1: shp = (nx, ny)
  190. return np.array(self.actu).reshape(shp)
  191. def doInterpolate(self, X, Y):
  192. raise NotImplementedError
  193. def doProbe(self, X, Y):
  194. raise NotImplementedError
  195. def getGrid(self):
  196. return self.rgrid
  197. def getDataMin(self):
  198. """
  199. return min ignoring nan
  200. """
  201. return np.nanmin(self.actu)
  202. def getDataMax(self):
  203. """
  204. return max ignoring nan
  205. """
  206. return np.nanmax(self.actu)
  207. if __name__ == "__main__":
  208. # pylint: disable=all
  209. def main():
  210. import os
  211. p = 'E:/Projets_simulation/EQ/Dry-Wet/Simulations/GLOBAL_01/Simulation/global01_0036'
  212. f = os.path.join(p, 'simul000.pst.sim')
  213. reader = DTData.constructReaderFromFile(f) #, cols = (3,))
  214. blocs = reader.readStructNoStats()
  215. streamHandler = logging.StreamHandler()
  216. LOGGER.addHandler(streamHandler)
  217. LOGGER.setLevel(logging.DEBUG)
  218. main()