Persistent Parameter Scan

Persistent Parameter Scan repeats the radius scan from Parameter Scan and stores the results in a Resultbag. On a later run, JCMsuite reuses resultbag entries that match the project, project keys, JCMT pattern, and source-file state.

The same workflow supports multi-dimensional parameter scans. Each complete set of project keys is one Resultbag entry.

Define The Scan

The script uses numpy.linspace(...) to create 41 equally spaced radii from 0.3\,\mathrm{\mu m} through 0.5\,\mathrm{\mu m}. The script then creates one set of project keys for each radius and opens results.jcmrb in the current working directory:

# Set geometry parameter
radii = np.linspace(0.3, 0.5, 41)
keys = [{"radius": radius} for radius in radii]

# Create a Resultbag handle
resultbag = jcmsuite.results.Resultbag("results.jcmrb")

Run The Scan

Warning

Open Resultbag files only from trusted sources. Loading stored results uses Python pickle and can execute code from the Resultbag file.

import numpy as np
from matplotlib import pyplot as plt

import jcmsuite

# Set geometry parameter
radii = np.linspace(0.3, 0.5, 41)
keys = [{"radius": radius} for radius in radii]

# Create a Resultbag handle
resultbag = jcmsuite.results.Resultbag("results.jcmrb")

# Run the project and store the results in the Resultbag. A second run will reuse the
# results from the Resultbag.
results = jcmsuite.solve(
    "mie2D.jcmp", keys=keys, temporary=True, quiet=True, resultbag=resultbag
)

# Access the results of the first postprocess and export it to a dictionary
scattering_postprocess = results.postprocesses[0]
scattering_export = scattering_postprocess.to_xarray()

cross_sections = scattering_export["ElectromagneticFieldEnergyFlux"].squeeze().real

plt.plot(radii, cross_sections, "-+")
plt.xlabel(r"radius [$\mu$ m]")
plt.ylabel("integral scattering cross section")
plt.axis("tight")
plt.show()

Pass the Resultbag to solve with resultbag=. On the first run, the call solves and stores all radius values. On a later run, the call solves and stores only project keys without matching resultbag entries. Matching entries are loaded from the resultbag. Set recompute=True to run a project solve again instead of reusing a matching resultbag entry.

The solve call returns a BatchSolveResult. The script selects the first postprocess, exports the data to xarray, and plots the real scattering cross section.

Load Stored Results

run_load_results.py opens the existing resultbag without running a project solve. The script reads the stored project keys and reconstructs the radii from the radius value in each set of project keys:

# Create a Resultbag handle
resultbag = jcmsuite.results.Resultbag("results.jcmrb")

# We can reconstruct the keys and the radii
keys = resultbag.keys
radii = [key["radius"] for key in keys]

# Actively load the results from the Resultbag.
results = resultbag.get_results(keys=keys)

resultbag.get_results(keys=keys) loads the resultbag entries in the order of the stored project keys. The script selects the first postprocess, exports the data to xarray, and plots the real scattering cross section:

from matplotlib import pyplot as plt

import jcmsuite

# Create a Resultbag handle
resultbag = jcmsuite.results.Resultbag("results.jcmrb")

# We can reconstruct the keys and the radii
keys = resultbag.keys
radii = [key["radius"] for key in keys]

# Actively load the results from the Resultbag.
results = resultbag.get_results(keys=keys)

# Access the results of the first postprocess and export it to a dictionary
scattering_postprocess = results.postprocesses[0]
scattering_export = scattering_postprocess.to_xarray()

cross_sections = scattering_export["ElectromagneticFieldEnergyFlux"].squeeze().real

plt.plot(radii, cross_sections, "-+")
plt.xlabel(r"radius [$\mu$ m]")
plt.ylabel("integral scattering cross section")
plt.axis("tight")
plt.show()

The script requires JCMsuite, an available license, and matplotlib.

Project Files

run.py
import numpy as np
from matplotlib import pyplot as plt

import jcmsuite

# Set geometry parameter
radii = np.linspace(0.3, 0.5, 41)
keys = [{"radius": radius} for radius in radii]

# Create a Resultbag handle
resultbag = jcmsuite.results.Resultbag("results.jcmrb")

# Run the project and store the results in the Resultbag. A second run will reuse the
# results from the Resultbag.
results = jcmsuite.solve(
    "mie2D.jcmp", keys=keys, temporary=True, quiet=True, resultbag=resultbag
)

# Access the results of the first postprocess and export it to a dictionary
scattering_postprocess = results.postprocesses[0]
scattering_export = scattering_postprocess.to_xarray()

cross_sections = scattering_export["ElectromagneticFieldEnergyFlux"].squeeze().real

plt.plot(radii, cross_sections, "-+")
plt.xlabel(r"radius [$\mu$ m]")
plt.ylabel("integral scattering cross section")
plt.axis("tight")
plt.show()
run_load_results.py
from matplotlib import pyplot as plt

import jcmsuite

# Create a Resultbag handle
resultbag = jcmsuite.results.Resultbag("results.jcmrb")

# We can reconstruct the keys and the radii
keys = resultbag.keys
radii = [key["radius"] for key in keys]

# Actively load the results from the Resultbag.
results = resultbag.get_results(keys=keys)

# Access the results of the first postprocess and export it to a dictionary
scattering_postprocess = results.postprocesses[0]
scattering_export = scattering_postprocess.to_xarray()

cross_sections = scattering_export["ElectromagneticFieldEnergyFlux"].squeeze().real

plt.plot(radii, cross_sections, "-+")
plt.xlabel(r"radius [$\mu$ m]")
plt.ylabel("integral scattering cross section")
plt.axis("tight")
plt.show()
layout.jcmt
Layout2D {
  UnitOfLength = 1e-6

  MeshOptions {
    MaximumSideLength = 0.1
    CurvilinearDegree = 2
  }

  Objects {
    # Computational domain
    Parallelogram {
      Name = "ComputationalDomain"
      DomainId = 1
      Priority = ComputationalDomain
      Width = 4
      Height = 4

      Boundary {
        Class = Transparent
      }
    }

    # Scatterer (rod)
    Circle {
      Name = "Rod"
      DomainId = 2
      Priority = 1
      Radius = %(radius)e
    }
  }
}
materials.jcm
# material properties for the surroundings (air)
Material {
  DomainId = 1
  RelPermittivity = 1.0
  RelPermeability = 1.0
}

# material properties the glass rod
Material {
  DomainId = 2
  RelPermittivity = 2.3104 # 1.52^2
  RelPermeability = 1.0
}
mie2D.jcmp
Project {
  InfoLevel = 3
  Electromagnetics {
    TimeHarmonic {
      Scattering {
        FieldComponents = Electric
        Accuracy {
          FiniteElementDegree = 3
          Refinement {
            MaxNumberSteps = 0
            PreRefinements = 0
          }
        }
      }
    }
  }
}
# computes the energy flux of the scattered field into the exterior domain
PostProcess {
  FluxIntegration {
    FieldBagPath = "./mie2D_results/fieldbag.jcm"
    OutputFileName = "./mie2D_results/energyflux_scattered.jcm"
    OutputQuantity = ElectromagneticFieldEnergyFlux
    InterfaceType = ExteriorDomain
  }
}

# exports computed field on cartesian grid
PostProcess {
  ExportFields {
    FieldBagPath = "./mie2D_results/fieldbag.jcm"
    OutputFileName = "./mie2D_results/fieldbag_cartesian.jcm"
    Cartesian {
      NGridPointsX = 200
      NGridPointsY = 200
    }
  }
}
sources.jcm
SourceBag {
  Source {
    ElectricFieldStrength {
      PlaneWave {
        K = [1.14239732857811e7 0.0 0.0]
        Amplitude = [0 0 1]
      }
    }
  }
}

Next Step

Continue to Resultbag to query and manage stored results.