RunSimulationSpectrum.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div class="row items-baseline">
  3. <div class="col-xs-12 col-sm-auto text-weight-bold text-center q-px-md q-py-sm">
  4. <q-btn :loading="isRunning"
  5. :disable="isRunning||!isNmieLoaded"
  6. color="primary"
  7. no-caps
  8. :label="isNmieLoaded ? 'Run simulation' : 'Loading...'"
  9. @click="runSpectrumSimulation">
  10. <template #loading>
  11. <q-spinner-gears />
  12. </template>
  13. </q-btn>
  14. </div>
  15. <div class="col-xs-grow col-sm q-px-xs">
  16. <div class="row justify-xs-center justify-sm-start items-baseline">
  17. <div class="col-auto">
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </template>
  23. <script lang="ts">
  24. import {
  25. defineComponent,
  26. computed, watch,
  27. } from 'vue'
  28. import { useStore } from 'src/store'
  29. import { range, rangeInt } from 'components/utils'
  30. import { cloneDeep } from 'lodash'
  31. export default defineComponent({
  32. name: 'RunSimulationSpectrum',
  33. setup() {
  34. const $store = useStore()
  35. const isRunning = computed({
  36. get: ()=> $store.state.simulationSetup.isNmieRunning,
  37. set: val => {
  38. val ? $store.commit('simulationSetup/markNmieAsStarted') : $store.commit('simulationSetup/markNmieAsFinished')
  39. }
  40. })
  41. const isNmieLoaded = computed(()=>{ return $store.state.simulationSetup.isNmieLoaded })
  42. function getWLs(){
  43. const fromWL = $store.state.simulationSetup.current.fromWL
  44. const toWL = $store.state.simulationSetup.current.toWL
  45. const pointsWL = $store.state.simulationSetup.current.pointsWL
  46. const stepWL = (toWL-fromWL)/(pointsWL-1)
  47. const WLs = range(fromWL, toWL, stepWL);
  48. return WLs
  49. }
  50. function initQ(mode_n:number[], mode_types:number[]) {
  51. let Qsca:number[] = [], Qabs:number[] = [], Qext:number[] = []
  52. let Qsca_n:number[][][] = [[], []]
  53. let Qabs_n:number[][][] = [[], []]
  54. let Qext_n:number[][][] = [[], []]
  55. mode_types.forEach(function (mode_type) {
  56. mode_n.forEach(function () {
  57. Qsca_n[mode_type].push([])
  58. Qabs_n[mode_type].push([])
  59. Qext_n[mode_type].push([])
  60. })
  61. })
  62. return {Qsca, Qabs, Qext, Qsca_n, Qabs_n, Qext_n}
  63. }
  64. //-------------------------------------------------------------------------//
  65. //--------------------- Main --------------------------------------------//
  66. //-------------------------------------------------------------------------//
  67. function runSpectrumSimulation() {
  68. if (isRunning.value) {
  69. console.log('Some Nmie is already running!')
  70. return
  71. }
  72. isRunning.value = true
  73. setTimeout(()=> {
  74. $store.commit('simulationSetup/copySetupFromGuiToCurrent')
  75. const host = $store.state.simulationSetup.current.hostIndex
  76. const WLs = getWLs()
  77. const mode_n = rangeInt($store.state.simulationSetup.current.numberOfModesToPlot, 1);
  78. const mode_types = range(0, 1);
  79. let {Qsca, Qabs, Qext, Qsca_n, Qabs_n, Qext_n} = initQ(mode_n, mode_types)
  80. try {
  81. if (!$store.state.simulationSetup.nmie) throw 'ERROR! Scattnlay module was not loaded'
  82. const nmie = $store.state.simulationSetup.nmie
  83. const layers = cloneDeep($store.state.simulationSetup.current.layers)
  84. const nmieStartedTime = performance.now()
  85. for (const WL of WLs) {
  86. nmie.SetWavelength(WL)
  87. nmie.ClearTarget()
  88. for (const layer of layers) {
  89. if (layer.nSpline) layer.n = layer.nSpline.at(WL)
  90. if (layer.kSpline) layer.k = layer.kSpline.at(WL)
  91. nmie.AddTargetLayerReIm(layer.layerWidth * host, layer.n / host, layer.k / host)
  92. }
  93. nmie.SetModeNmaxAndType(-1, -1)
  94. nmie.RunMieCalculation()
  95. Qsca.push(nmie.GetQsca())
  96. Qabs.push(nmie.GetQabs())
  97. Qext.push(nmie.GetQext())
  98. for (const mode_type of mode_types) {
  99. for (const n of mode_n) {
  100. nmie.SetModeNmaxAndType(n, mode_type)
  101. nmie.RunMieCalculation()
  102. Qsca_n[mode_type][n - 1].push(nmie.GetQsca())
  103. Qabs_n[mode_type][n - 1].push(nmie.GetQabs())
  104. Qext_n[mode_type][n - 1].push(nmie.GetQext())
  105. }
  106. }
  107. }
  108. const nmieTotalRunTime = (performance.now()-nmieStartedTime)/1000
  109. // console.log('Total simulation time:', nmieTotalRunTime, 's')
  110. $store.commit('simulationSetup/setNmieTotalRunTime', nmieTotalRunTime)
  111. $store.commit('plotRuntime/setQ', {WLs, Qsca, Qabs, Qext, Qsca_n, Qabs_n, Qext_n})
  112. $store.commit('plotRuntime/updateNumberOfPlotsFromPreviousSimulations')
  113. $store.commit('plotRuntime/setCommonLabel', $store.state.simulationSetup.current.plotLabel)
  114. $store.commit('simulationSetup/setPlotLabel', '')
  115. $store.commit('plotRuntime/updateSpectraPlot')
  116. } catch (e) {
  117. console.log(e)
  118. }
  119. isRunning.value = false
  120. },50)
  121. }
  122. watch(isNmieLoaded, ()=>{
  123. if (isNmieLoaded.value) runSpectrumSimulation()
  124. })
  125. return { isRunning, isNmieLoaded,
  126. runSpectrumSimulation
  127. }
  128. },
  129. })
  130. </script>