SaveSimulationNearField.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <q-btn
  3. color="primary"
  4. no-caps
  5. :disable="data ? false : true"
  6. @click="saveSpectrumSimulation"
  7. >Save</q-btn
  8. >
  9. <q-checkbox v-model="isSaveWithPythonScript" size="sm"
  10. >Include *.py plotting script</q-checkbox
  11. >
  12. </template>
  13. <script lang="ts">
  14. import {
  15. computed,
  16. defineComponent,
  17. // ref,
  18. // watch
  19. // nextTick
  20. } from 'vue';
  21. import { useStore } from 'src/store';
  22. import { saveAs } from 'file-saver';
  23. export default defineComponent({
  24. name: 'SaveSimulationNearField',
  25. setup() {
  26. const $store = useStore();
  27. const isSaveWithPythonScript = computed({
  28. get: () => $store.state.guiRuntime.isSaveWithPythonScript,
  29. set: (val) => $store.commit('guiRuntime/setIsSaveWithPythonScript', val),
  30. });
  31. const coordX = computed(() => $store.state.plotRuntime.nearFieldCoordX);
  32. const coordY = computed(() => $store.state.plotRuntime.nearFieldCoordY);
  33. const data = computed(() => $store.state.plotRuntime.nearFieldEabs);
  34. const layerWidths = computed(() =>
  35. $store.state.simulationSetup.current.layers.map((x) => x.layerWidth)
  36. );
  37. const crossSection = computed(() => {
  38. let val =
  39. $store.state.simulationSetup.current.nearFieldSetup.crossSection;
  40. if (val == 0) return 'Ek';
  41. if (val == 1) return 'Hk';
  42. return 'EH';
  43. });
  44. const radii = computed(() => {
  45. let r = 0;
  46. let radiiLocal = '';
  47. for (let widthLayer of layerWidths.value) {
  48. r += widthLayer;
  49. radiiLocal += r.toString() + ',';
  50. }
  51. return radiiLocal;
  52. });
  53. const lengthUnits = computed(() => $store.state.guiRuntime.units);
  54. return {
  55. data,
  56. isSaveWithPythonScript,
  57. saveSpectrumSimulation() {
  58. if (!data.value) return;
  59. const pythonScript =
  60. '# You can open and plot this file using Python\n' +
  61. '# (without manually removing this header, it will be skipped), see example below.\n' +
  62. 'import numpy as np\n' +
  63. 'from matplotlib import pyplot as plt\n' +
  64. "data = np.genfromtxt('scattnlay-near-field.txt', names=True, delimiter=', ')\n" +
  65. 'x = data[data.dtype.names[0]] # x-axis has units\n' +
  66. 'y = data[data.dtype.names[1]] # x-axis has units\n' +
  67. 'x_size = len(np.unique(x))\n' +
  68. 'y_size = len(np.unique(y))\n' +
  69. 'x_min = np.min(x)\n' +
  70. 'y_min = np.min(y)\n' +
  71. 'x_max = np.max(x)\n' +
  72. 'y_max = np.max(y)\n' +
  73. 'dx = (x_max-x_min)/(x_size-1)\n' +
  74. 'dy = (y_max-y_min)/(y_size-1)\n' +
  75. "Eabs = data['Eabs'].reshape((y_size,x_size))\n" +
  76. '\n' +
  77. 'plt.figure()\n' +
  78. "plt.imshow(Eabs, cmap='jet', origin='lower',\n" +
  79. ' extent=(x_min-dx/2, x_max+dx/2, y_min-dy/2, y_max+dy/2))\n' +
  80. 'cbar = plt.colorbar(shrink=0.7)\n' +
  81. "cbar.ax.set_title(r'$|E|/|E_0|$')\n" +
  82. "plt.xlabel(data.dtype.names[0].replace('_',', '))\n" +
  83. "plt.ylabel(data.dtype.names[1].replace('_',', '))\n" +
  84. 'fig = plt.gcf()\n' +
  85. 'ax = fig.gca()\n' +
  86. 'radii = [' +
  87. radii.value +
  88. ']\n' +
  89. 'for r in radii:\n' +
  90. " ax.add_patch(plt.Circle((0, 0), r, color='w', alpha=0.7, lw=3, fill=False))\n" +
  91. "plt.title('" +
  92. crossSection.value +
  93. " cross-section')\n" +
  94. 'plt.show()\n\n';
  95. let columnNames =
  96. '# X [' +
  97. lengthUnits.value.toString() +
  98. '] , Y [' +
  99. lengthUnits.value.toString() +
  100. '], Eabs\n';
  101. let body = '';
  102. for (let i = 0; i < data.value.length; ++i) {
  103. let row =
  104. coordX.value[i].toString() +
  105. ', ' +
  106. coordY.value[i].toString() +
  107. ', ' +
  108. data.value[i].toString();
  109. row += '\n';
  110. body += row;
  111. }
  112. const scattnlayNearField = new Blob(
  113. [columnNames + body],
  114. { type: 'text/plain;charset=utf-8', endings: 'native' } //TODO test if newline is correctly written in Windows, MacOS
  115. );
  116. saveAs(scattnlayNearField, 'scattnlay-near-field.txt');
  117. const scattnlayNearFieldScript = new Blob(
  118. [pythonScript],
  119. { type: 'text/plain;charset=utf-8', endings: 'native' } //TODO test if newline is correctly written in Windows, MacOS
  120. );
  121. saveAs(scattnlayNearFieldScript, 'scattnlay-near-field-plot.py');
  122. },
  123. };
  124. },
  125. });
  126. </script>