PageRenderTime 274ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/postpic/datareader/dummy.py

https://gitlab.com/planto/postpic
Python | 238 lines | 135 code | 53 blank | 50 comment | 23 complexity | b3059d7206668675a2601eca02fb0b41 MD5 | raw file
  1. #
  2. # This file is part of postpic.
  3. #
  4. # postpic is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # postpic is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with postpic. If not, see <http://www.gnu.org/licenses/>.
  16. #
  17. # Stephan Kuschel 2014
  18. '''
  19. Dummy reader for creating fake simulation Data for testing purposes.
  20. Stephan Kuschel 2014
  21. '''
  22. from __future__ import absolute_import, division, print_function, unicode_literals
  23. from . import Dumpreader_ifc
  24. from . import Simulationreader_ifc
  25. import numpy as np
  26. from .. import helper
  27. from ..helper import PhysicalConstants
  28. class Dummyreader(Dumpreader_ifc):
  29. '''
  30. Dummyreader creates fake Data for testing purposes.
  31. Args:
  32. dumpid : int
  33. the dumpidentifier is the dumpid in this case. It is a float variable,
  34. that will also change the dummyreaders output (for example it
  35. will pretend to have dumpid many particles).
  36. '''
  37. def __init__(self, dumpid, dimensions=2, randfunc=np.random.normal, seed=0, **kwargs):
  38. super(self.__class__, self).__init__(dumpid, **kwargs)
  39. self._dimensions = dimensions
  40. self._seed = seed
  41. self._randfunc = randfunc
  42. # initialize fake data
  43. if seed is not None:
  44. np.random.seed(seed)
  45. self._xdata = randfunc(size=dumpid)
  46. if dimensions > 1:
  47. self._ydata = randfunc(size=dumpid)
  48. if dimensions > 2:
  49. self._zdata = randfunc(size=dumpid)
  50. def keys(self):
  51. pass
  52. def __getitem__(self, key):
  53. pass
  54. def __eq__(self, other):
  55. ret = super(self.__class__, self).__eq__(other)
  56. return ret \
  57. and self._randfunc == other._randfunc \
  58. and self._seed == other._seed \
  59. and self._dimensions == other._dimensions
  60. def timestep(self):
  61. return self.dumpidentifier
  62. def time(self):
  63. return self.timestep() * 1e-10
  64. def simdimensions(self):
  65. return self._dimensions
  66. def gridoffset(self, key, axis):
  67. raise Exception('Not Implemented')
  68. def gridspacing(self, key, axis):
  69. raise Exception('Not Implemented')
  70. def data(self, axis):
  71. axid = helper.axesidentify[axis]
  72. def _Ex(x, y, z):
  73. ret = np.sin(np.pi * self.timestep() *
  74. np.sqrt(x**2 + y**2 + z**2)) / \
  75. np.sqrt(x**2 + y**2 + z**2 + 1e-9)
  76. return ret
  77. def _Ey(x, y, z):
  78. ret = np.sin(np.pi * self.timestep() * x) + \
  79. np.sin(np.pi * (self.timestep() - 1) * y) + \
  80. np.sin(np.pi * (self.timestep() - 2) * z)
  81. return ret
  82. def _Ez(x, y, z):
  83. ret = x**2 + y**2 + z**2
  84. return ret
  85. fkts = {0: _Ex,
  86. 1: _Ey,
  87. 2: _Ez}
  88. if self.simdimensions() == 1:
  89. ret = fkts[axid](self.grid(None, 'x'), 0, 0)
  90. elif self.simdimensions() == 2:
  91. xx, yy = np.meshgrid(self.grid(None, 'x'), self.grid(None, 'y'), indexing='ij')
  92. ret = fkts[axid](xx, yy, 0)
  93. elif self.simdimensions() == 3:
  94. xx, yy, zz = np.meshgrid(self.grid(None, 'x'),
  95. self.grid(None, 'y'),
  96. self.grid(None, 'z'), indexing='ij')
  97. ret = fkts[axid](xx, yy, zz)
  98. return ret
  99. def _keyE(self, component):
  100. return component
  101. def _keyB(self, component):
  102. return component
  103. def gridnode(self, key, axis):
  104. '''
  105. Args:
  106. axis : string or int
  107. the axisidentifier
  108. Returns: list of grid points of the axis specified.
  109. Thus only regular grids are supported currently.
  110. '''
  111. axid = helper.axesidentify[axis]
  112. grids = {1: [(-2, 10, 601)],
  113. 2: [(-2, 10, 301), (-5, 5, 401)],
  114. 3: [(-2, 10, 101), (-5, 5, 81), (-4, 4, 61)]}
  115. if axid >= self.simdimensions():
  116. raise KeyError('axis ' + str(axis) + ' not present.')
  117. args = grids[self.simdimensions()][axid]
  118. ret = np.linspace(*args)
  119. return ret
  120. def grid(self, key, axis):
  121. '''
  122. Args:
  123. axis : string or int
  124. the axisidentifier
  125. Returns: list of grid points of the axis specified.
  126. Thus only regular grids are supported currently.
  127. '''
  128. axid = helper.axesidentify[axis]
  129. grids = {1: [(-2, 10, 600)],
  130. 2: [(-2, 10, 300), (-5, 5, 400)],
  131. 3: [(-2, 10, 100), (-5, 5, 80), (-4, 4, 60)]}
  132. if axid >= self.simdimensions():
  133. raise KeyError('axis ' + str(axis) + ' not present.')
  134. args = grids[self.simdimensions()][axid]
  135. ret = np.linspace(*args)
  136. return ret
  137. def listSpecies(self):
  138. return ['electron']
  139. def getSpecies(self, species, attrib):
  140. attribid = helper.attribidentify[attrib]
  141. if attribid == 0: # x
  142. ret = self._xdata
  143. elif attribid == 1 and self.simdimensions() > 1: # y
  144. ret = self._ydata
  145. elif attribid == 2 and self.simdimensions() > 2: # z
  146. ret = self._zdata
  147. elif attribid == 3: # px
  148. ret = np.roll(self._xdata, 1) ** 2 * (PhysicalConstants.me * PhysicalConstants.c)
  149. elif attribid == 4: # py
  150. ret = np.roll(self._ydata, 1) * (PhysicalConstants.me * PhysicalConstants.c)
  151. elif attribid == 5: # pz
  152. ret = np.roll(self._xdata, 3) * (PhysicalConstants.me * PhysicalConstants.c)
  153. elif attribid == 9: # weights
  154. ret = np.repeat(1, len(self._xdata))
  155. else:
  156. raise KeyError('Attrib "' + str(attrib) + '" of species "' +
  157. str(species) + '" not present')
  158. return ret
  159. def __str__(self):
  160. ret = '<Dummyreader ({:d}d) initialized with "' \
  161. + str(self.dumpidentifier) + '">'
  162. ret = ret.format(self._dimensions)
  163. return ret
  164. class Dummysim(Simulationreader_ifc):
  165. def __init__(self, simidentifier, dimensions=2, **kwargs):
  166. super(self.__class__, self).__init__(simidentifier, **kwargs)
  167. self._dimensions = dimensions
  168. def __len__(self):
  169. return self.simidentifier
  170. def _getDumpreader(self, index):
  171. if index < len(self):
  172. return Dummyreader(index, dimensions=self._dimensions)
  173. else:
  174. raise IndexError()
  175. def __str__(self):
  176. ret = '<Dummysimulation ({:d}d) initialized with "' \
  177. + str(self.dumpidentifier) + '">'
  178. ret = ret.format(self._dimensions)
  179. return ret