Bladeren bron

take care of roundoff errors during conversioninto source units

Konstantin Ladutenko 3 jaren geleden
bovenliggende
commit
788796c878
2 gewijzigde bestanden met toevoegingen van 21 en 4 verwijderingen
  1. 15 2
      guiapp/src/components/GetSourceParameters.vue
  2. 6 2
      guiapp/src/components/GetUnits.vue

+ 15 - 2
guiapp/src/components/GetSourceParameters.vue

@@ -59,14 +59,27 @@ export default defineComponent({
       return $store.state.guiRuntime.sourceUnits
     })
 
+    function isAlmostSame(a:number,b:number) {
+      if ( Math.abs((a-b)/(a+b)) < 1e-15) return true
+      return false
+    }
+
     const fromWavelengthInSourceUnits = computed({
       get: () => toUnits($store.state.simulationSetup.gui.fromWL, sourceUnits.value),
-      set: val => $store.commit('simulationSetup/setFromWL', fromUnits(sourceUnits.value, val))
+      set: val => {
+        if (!isAlmostSame($store.state.simulationSetup.gui.fromWL, fromUnits(sourceUnits.value, val))) {
+          $store.commit('simulationSetup/setFromWL', fromUnits(sourceUnits.value, val))
+        }
+      }
     })
 
     const toWavelengthInSourceUnits = computed({
       get: () => toUnits($store.state.simulationSetup.gui.toWL, sourceUnits.value),
-      set: val => $store.commit('simulationSetup/setToWL', fromUnits(sourceUnits.value, val))
+      set: val => {
+        if (!isAlmostSame($store.state.simulationSetup.gui.toWL, fromUnits(sourceUnits.value, val))) {
+          $store.commit('simulationSetup/setToWL', fromUnits(sourceUnits.value, val))
+        }
+      }
     })
 
     const pointsSource = computed({

+ 6 - 2
guiapp/src/components/GetUnits.vue

@@ -116,9 +116,13 @@ export default defineComponent({
       set: val => $store.commit('guiRuntime/setSourceUnits', val.label)
     })
 
+    let prevCustomSourceUnits = 'THz'
     watch(isSourceSameUnits, ()=>{
-      if (isSourceSameUnits.value) $store.commit('guiRuntime/setSourceUnits',units.value)
-      else $store.commit('guiRuntime/setSourceUnits','THz')
+      if (isSourceSameUnits.value) {
+        prevCustomSourceUnits = $store.state.guiRuntime.sourceUnits
+        $store.commit('guiRuntime/setSourceUnits',units.value)
+      }
+      else $store.commit('guiRuntime/setSourceUnits',prevCustomSourceUnits)
     })
 
     watch(units, ()=>{