Reuse And Filter Resultbag Results

This tutorial uses Resultbag.filter to store a batch of results, repeat the batch to reuse matching entries, and filters the stored project keys.

The tutorial uses an exact filter and an inclusive between range filter. It then calls get_results on the range query to load the selected entries as a standard solve result.

Run the tutorial from the tutorial root:

python resultbag/basic_operations/run.py

Python File

import jcmsuite
from jcmsuite.results.resultbag import between

batched_keys = [
    {"radius": 0.3},
    {"radius": 0.4},
    {"radius": 0.5},
]

resultbag = jcmsuite.results.Resultbag("results.jcmrb")

# The first solve stores one entry for each set of Project Keys.
first_batch = jcmsuite.solve(
    "mie2D.jcmp",
    keys=batched_keys,
    resultbag=resultbag,
)

# Matching entries are reused during the second solve.
print("Using results from the Resultbag for the second solve")
second_batch = jcmsuite.solve(
    "mie2D.jcmp",
    keys=batched_keys,
    resultbag=resultbag,
)


# Access the keys of the Resultbag
rb_keys = resultbag.keys
print(resultbag.get_results(rb_keys))
print(resultbag.get_results(rb_keys[0]))

# Filter the Resultbag for entries. The filter method returns a lazy, read-only query view
# with only matching entries. The get_results method returns one standard solve result shape
# for the matching entries, not a list.
exact = resultbag.filter(radius=0.3)
print(exact.keys)
print(exact.get_results())

bounded = resultbag.filter(radius=between(0.35, 0.55))
print(bounded.keys)
print(bounded.get_results())

Project Files

layout.jcmt
Layout2D {
  UnitOfLength = 1e-6

  MeshOptions {
    MaximumSideLength = 0.1
  }

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

      Boundary {
        Class = Transparent
      }
    }

    Circle {
      Name = "Rod"
      DomainId = 2
      Priority = 1
      Radius = %(radius)e
      RefineAll = 4
    }
  }
}
materials.jcm
Material {
  Name = "Vacuum"
  DomainId = 1
  RelPermittivity = 1.0
}

Material {
  Name = "Sphere"
  DomainId = 2
  RelPermittivity = 2.25
}
mie2D.jcmp
Project {
  InfoLevel = 3
  Electromagnetics {
    TimeHarmonic {
      Scattering {
        FieldComponents = Electric
        Accuracy {
          FiniteElementDegree = 3
          Refinement {
            MaxNumberSteps = 0
            PreRefinements = 0
          }
        }
      }
    }
  }
}

PostProcess {
  FluxIntegration {
    FieldBagPath = "./mie2D_results/fieldbag.jcm"
    OutputFileName = "./mie2D_results/energyflux_scattered.jcm"
    OutputQuantity = ElectromagneticFieldEnergyFlux
    InterfaceType = ExteriorDomain
  }
}
sources.jcm
SourceBag {
  Source {
    ElectricFieldStrength {
      PlaneWave {
        K = [1.14239732857811e7 0.0 0.0]
        Amplitude = [0 0 1]
      }
    }
  }
}