Browse Source

Add multipole example to Python. Move more constants to config.ts

Konstantin Ladutenko 3 years ago
parent
commit
9d4dbee936

+ 76 - 0
examples/calc-multipole-spectra.py

@@ -0,0 +1,76 @@
+#!/usr/bin/env python3
+# -*- coding: UTF-8 -*-
+#
+#    Copyright (C) 2019-2021  Konstantin Ladutenko <kostyfisik@gmail.com>
+#
+#    This file is part of python-scattnlay
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    The only additional remark is that we expect that all publications
+#    describing work using this software, or all commercial products
+#    using it, cite the following reference:
+#    [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by
+#        a multilayered sphere," Computer Physics Communications,
+#        vol. 180, Nov. 2009, pp. 2348-2354.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+import sys
+sys.path.insert(0,'..')  # to be able to import scattnlay from the upper dir
+
+from scattnlay import scattnlay,scattcoeffs,fieldnlay, mie
+
+import matplotlib.pyplot as plt
+import numpy as np
+import cmath
+
+from_WL = 300
+to_WL = 1000
+WL_points= 1000
+WLs = np.linspace(from_WL, to_WL, WL_points)
+index_NP = 4+0.01j
+
+x = np.ones((1), dtype = np.float64)
+m = np.ones((1), dtype = np.complex128)
+
+core_r = 100
+
+Qsca_vec = []
+Qsca_mode_vec = []
+
+for WL in WLs:
+    x[0] = 2.0*np.pi*core_r/WL
+    m[0] = index_NP
+    mie.SetLayersSize(x)
+    mie.SetLayersIndex(m)
+
+    mie.SetModeNmaxAndType(-1,-1)
+    mie.RunMieCalculation()
+    Qsca =  mie.GetQsca()
+
+    mie.SetModeNmaxAndType(1,0)
+    mie.RunMieCalculation()
+    Qsca_mode =  mie.GetQsca()
+
+    print(np.array([Qsca]))
+    Qsca_vec.append(Qsca)
+    Qsca_mode_vec.append(Qsca_mode)
+
+fig, axs2 = plt.subplots(1,1)#, sharey=True, sharex=True)
+axs2.plot(WLs, Qsca_vec, color="blue")
+axs2.plot(WLs, Qsca_mode_vec, color="red")
+plt.savefig("spectra.pdf",pad_inches=0.02, bbox_inches='tight')
+plt.show()
+plt.clf()
+plt.close()

+ 1 - 1
examples/calc-spectra.py

@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 # -*- coding: UTF-8 -*-
 #
-#    Copyright (C) 2019  Konstantin Ladutenko <kostyfisik@gmail.com>
+#    Copyright (C) 2019-2021  Konstantin Ladutenko <kostyfisik@gmail.com>
 #
 #    This file is part of python-scattnlay
 #

+ 1 - 1
guiapp/src/components/GetParticleParameters.vue

@@ -167,7 +167,7 @@ export default defineComponent({
 
     watch(numberOfLayers, ()=>{
       numberOfLayers.value = parseInt(numberOfLayers.value.toString())
-      if (isNaN(numberOfLayers.value)) numberOfLayers.value = 3
+      if (isNaN(numberOfLayers.value)) return
       if (numberOfLayers.value < 1) numberOfLayers.value = 1
       if (numberOfLayers.value > maxNumberOfLayers) numberOfLayers.value = maxNumberOfLayers
 

+ 16 - 10
guiapp/src/components/InputWithUnits.vue

@@ -5,7 +5,7 @@
         anchor="top middle"
         self="center middle"
     >
-      {{formatNumber(localTooltipText,digits+1)}}
+      {{formatNumber(localTooltipText,inputWithUnitsTooltipDigits)}}
     </q-tooltip>
     <q-tooltip v-if="isShowingHelp"
                v-model = "isShowingHelpLocal"
@@ -22,7 +22,7 @@
           class="items-center bg-grey-2"
           horizontal>
         <div class="side_note text-grey-9 q-px-xs text-center"
-             style="width: 4em"
+             :style="'width: '+inputWithUnitsTitleWidthStyle"
         >
           {{title}}
         </div>
@@ -36,7 +36,7 @@
               hide-selected
               input-debounce="0"
               options-dense
-              style="width: 10em"
+              :style="'width: '+inputWithUnitsBodyWidthStyle"
               use-input
               behavior="menu"
               @filter="filterQSelectOptions"
@@ -56,7 +56,7 @@
                   style="font-size: 12px"
                   class="q-py-sm"
               >
-                {{formatNumber(localTooltipText,digits)}}
+                {{formatNumber(localTooltipText,inputWithUnitsInlineDigits)}}
               </div>
             </template>
             <template #option="scope">
@@ -65,7 +65,7 @@
                   {{scope.opt}}
                 </q-item-section>
                 <q-item-section side>
-                  {{formatNumber(evalString(scope.opt),digits)}}
+                  {{formatNumber(evalString(scope.opt),inputWithUnitsInlineDigits)}}
                 </q-item-section>
               </q-item>
             </template>
@@ -73,7 +73,7 @@
         </div>
         <div
             class="side_note text-grey-9 q-px-xs text-center"
-            style="width: 3em"
+            :style="'width: '+inputWithUnitsUnitsWidthStyle"
         >
           {{units}}
         </div>
@@ -89,6 +89,13 @@ import {
   ref,
   watch,
   } from 'vue'
+import {inputWithUnitsTitleWidthStyle,
+  inputWithUnitsBodyWidthStyle,
+  inputWithUnitsUnitsWidthStyle,
+    inputWithUnitsHistoryLength,
+    inputWithUnitsInlineDigits,
+    inputWithUnitsTooltipDigits
+} from 'components/config'
 
 export default defineComponent({
   name: 'InputWithUnits',
@@ -135,7 +142,6 @@ export default defineComponent({
 
     let evaluated = ref(0)
     let count_updates = 0
-    const digits = 1
 
     // Set some random values to get correct typing with
     // TypeScript and remove them after initialization.
@@ -225,6 +231,8 @@ export default defineComponent({
     isShowingTooltip.value = false
 
     return {
+      inputWithUnitsTitleWidthStyle, inputWithUnitsBodyWidthStyle, inputWithUnitsUnitsWidthStyle,
+      inputWithUnitsHistoryLength, inputWithUnitsInlineDigits, inputWithUnitsTooltipDigits,
       localTooltipText, isShowingTooltip,
       isShowingTooltipAppend, isShowingHelpLocal,
       helpExpr,
@@ -234,7 +242,7 @@ export default defineComponent({
         isShowingTooltip.value = false
         const expr = localQSelectModel.value
         if (!qSelectOptionsHistory.value.includes(expr)) qSelectOptionsHistory.value.unshift(expr)
-        if (qSelectOptionsHistory.value.length > 5) qSelectOptionsHistory.value.pop()
+        if (qSelectOptionsHistory.value.length > inputWithUnitsHistoryLength) qSelectOptionsHistory.value.pop()
       },
 
       filterQSelectOptions (val:string,
@@ -246,8 +254,6 @@ export default defineComponent({
         })
       },
 
-      digits,
-
       formatNumber (value:string, digits:number):string {
         if (value==='') return ''
         const num = parseFloat(value)

+ 33 - 0
guiapp/src/components/PlotSpectra.vue

@@ -0,0 +1,33 @@
+<template>
+  <div>
+    <ReactiveChart :chart="$store.state.plotRuntime.spectraPlot"/>
+  </div>
+</template>
+
+<script>
+import ReactiveChart from 'components/ReactiveChart.vue'
+import { useStore } from 'src/store'
+import {
+  defineComponent,
+  // ref,
+  // reactive,
+  computed,
+  watch
+} from 'vue'
+
+export default defineComponent({
+  name: 'PlotSpectra',
+  components: {
+    ReactiveChart,
+  },
+  setup () {
+    const $store = useStore()
+    const isPlotQabs = computed( ()=>$store.state.plotRuntime.isPlotQabs)
+    watch(isPlotQabs, ()=>{
+      console.log('isPlotQabs = ', isPlotQabs.value)
+      $store.commit('plotRuntime/updateSpectraPlot')
+    })
+    return {}
+  }
+})
+</script>

+ 16 - 1
guiapp/src/components/config.ts

@@ -1,6 +1,21 @@
 export const flexRowTitleStyle = 'width:10em; margin: auto;'
 export const basicSelectorWidthStyle = '6.5em'
-export const basicWidthStyle = '17.7em'
+
+export const inputWithUnitsHistoryLength = 5
+export const inputWithUnitsTooltipDigits = 2
+export const inputWithUnitsInlineDigits = 1
+
+export const inputWithUnitsTitleWidthStyle = '4em'
+export const inputWithUnitsBodyWidthStyle = '10em'
+export const inputWithUnitsUnitsWidthStyle = '3em'
+function getInt(val:string) {return parseInt(val.slice(0,-2))}
+export const basicWidthStyle = (
+    getInt(inputWithUnitsBodyWidthStyle)
+    + getInt(inputWithUnitsTitleWidthStyle)
+    + getInt(inputWithUnitsUnitsWidthStyle)
+    + 0.71 // To get the same width in GUI as all three parts above joined in inputWithUnits component
+).toString()+'em'
+
 
 export const maxNumberOfModesToPlot = 10
 export const maxNumberOfLayers = 10

+ 4 - 3
guiapp/src/pages/Spectrum.vue

@@ -18,7 +18,7 @@
     <div class="q-ma-sm"/>
     <PlotSelector/>
     <div class="q-ma-xs"/>
-    <ReactiveChart :chart="$store.state.plotRuntime.spectraPlot"/>
+    <PlotSpectra/>
   </q-page>
 </template>
 
@@ -33,8 +33,8 @@ import GetSourceParameters from 'components/GetSourceParameters.vue'
 import GetParticleParameters from 'components/GetParticleParameters.vue'
 import GetPlotSettings from 'components/GetPlotSettings.vue'
 import RunSimulationSpectrum from 'components/RunSimulationSpectrum.vue'
-import ReactiveChart from 'components/ReactiveChart.vue'
 import PlotSelector from 'components/PlotSelector.vue'
+import PlotSpectra from 'components/PlotSpectra.vue'
 // import { useStore } from 'src/store'
 
 
@@ -43,7 +43,8 @@ export default defineComponent({
   components: {
     RunSimulationSpectrum, GetUnits, GetHostIndex,
     GetSourceParameters, GetParticleParameters,
-    GetPlotSettings, ReactiveChart, PlotSelector
+    GetPlotSettings, PlotSelector,
+    PlotSpectra
   },
   // setup() {
   //   const $store = useStore()

+ 1 - 0
guiapp/src/store/plot-runtime/mutations.ts

@@ -15,6 +15,7 @@ const mutation: MutationTree<prsi> = {
   },
 
   updateSpectraPlot (state: prsi) {
+    console.log('updating spectra plot...')
     const traceQsca:Partial<Data> = {
       x: state.WLs,
       y: state.Qsca,

+ 0 - 1
src/pb11_wrapper.cc

@@ -102,7 +102,6 @@ class PyMultiLayerMie : public MultiLayerMie<FloatType> {
   void SetFieldCoords(const py::array_t<double, py::array::c_style | py::array::forcecast> &py_Xp,
                       const py::array_t<double, py::array::c_style | py::array::forcecast> &py_Yp,
                       const py::array_t<double, py::array::c_style | py::array::forcecast> &py_Zp);
-
 };
 
 // Python interface