Inspect Batched Results

This tutorial collects a deliberate failure, then inspects successful and failed batch entries by their original indices.

Run it from demos/documentation_tutorials:

python result_access/03_batched_solves/run.py
import jcmsuite

batched_keys = [
    {"radius": 0.3},
    {"radius": -0.4},  # Deliberate failure for this batch entry.
    {"radius": 0.5},
]
results = jcmsuite.solve(
    "mie2D.jcmp",
    keys=batched_keys,
    on_error="collect",  # Keep all entries, even when one solve fails.
    resultbag=jcmsuite.results.Resultbag("results.jcmrb"),
    quiet=True,
)

# Show the path to the project file
print(results.project_file)

# Show a preview of the project keys used for solving, when available
print(results.keys)

# Show a preview of the computational costs associated with the solve call
print(results.costs)

# Show a preview of the eigenvalues of the resonance problem, when available
print(results.eigenvalues)

# Show a preview of the postprocesses available in the results
print(results.postprocesses)

# Give a list of the titles of the postprocesses available in the results
print(results.postprocesses.titles())

# Show a preview of the ElectromagneticFieldEnergyFlux postprocess
print(results.postprocesses[0])

print(f"Number of entries: {len(results)}")
print(f"Successful indices: {results.success_indices}")
print(f"Failed indices: {results.failure_indices}")

# Integer indexing selects one entry. Slicing creates another batch.
print(f"First entry: {results[0].index}")


print(f"Every other entry: {[entry.index for entry in results[::2]]}")

for entry in results:
    # The batch index is the original position in batched_keys. It stays stable
    # when another entry fails.
    print(f"Batch index {entry.index}: {entry.keys}")

    if entry.failure is not None:
        print(f"Failure: {entry.failure.message}")

Project Files

layout.jcm
Layout2D {
  UnitOfLength = 1e-6

  MeshOptions {
    MaximumSideLength = 0.1
  }

  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 = -0.4
      RefineAll = 4
    }
  }
}
layout.jcmt
Layout2D {
  UnitOfLength = 1e-6

  MeshOptions {
    MaximumSideLength = 0.1
  }

  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
      RefineAll = 4
    }
  }
}
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
  }
}

PostProcess {
  FourierTransform {
    FieldBagPath = "./mie2D_results/fieldbag.jcm"
    OutputFileName = "./mie2D_results/fc.jcm"
    NormalDirection = Y
    Rotation = X:Z:-Y
    Format = JCM
  }
}

# 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]
      }
    }
  }
}