Skip to content

Contour Lines

Contour lines connect points of equal value in a field, providing an intuitive way to visualize the shape and structure of a dataset. They are particularly useful in MICRESS simulations for analyzing interfaces between different phases or concentration gradients. In this section, we demonstrate how to generate contour plots using MicPy, focusing on phase fractions and element concentrations.

We begin with an example showing the distribution of graphite (A9) phase fraction. Then, we extend the concept to visualize the carbon concentration field, using contour levels to highlight different concentration thresholds.

Example 1: Contour Lines for Graphite Fraction

In this example, we visualize the fraction of graphite (A9) phase in a MICRESS simulation. The contour lines help identify regions where the phase fraction reaches a specific threshold, offering insight into the phase distribution.

Step 1: Import Required Libraries

We start by importing the necessary library for reading MICRESS binary files.

from micpy import bin

Step 2: Load the Phase Fraction Data

The frac2 file contains phase fraction data for the graphite (A9) phase in the simulation. We load the data using MicPy's bin.File interface.

with bin.File("A003_CastIronDendriteNodules.frac2") as file:
    series = file.read()

Step 3: Extract the Final Time Step

We extract the phase fraction field at the 30th recorded time step to analyze the final phase distribution.

field = series.get_field(30)

Step 4: Create the Initial Plot

We use the bin.plot() function to visualize the graphite fraction. The transparency (alpha=0.66) allows better visibility of the overlaid contour lines.

args = bin.PlotArgs(title="Graphite (A9) Fraction", alpha=0.66)
fig, ax, cbar = bin.plot(field, axis="z", args=args)

Step 5: Access Image Data

To overlay contour lines, we access the underlying image array from the plot.

image = ax.images[0].get_array()

Step 6: Add Contour Lines

We draw contour lines at a phase fraction level of 0.5, which helps highlight regions where the graphite phase reaches 50%.

contours = ax.contour(
    image, levels=[0.5], colors="black", linewidths=1, linestyles="solid"
)

Step 7: Annotate the Colorbar

A horizontal line is added to the colorbar to indicate the contour level.

cbar.ax.hlines([0.5], 0, 1, colors="black", linewidth=1)

The resulting plot provides an intuitive representation of graphite fraction distribution in the simulation and the location of the sharp interface.


Example 2: Contour Lines for Carbon Concentration

In this example, we analyze the carbon concentration field in a cast iron solidification simulation. Contour lines are used to highlight specific concentration levels, revealing spatial variations in composition.

Step 1: Import Required Libraries

We begin by importing the MicPy library for handling MICRESS binary files.

from micpy import bin

Step 2: Load the Concentration Data

We load the carbon concentration data from the conc1 file, which stores the simulation output for solute concentrations.

with bin.File("A003_CastIronDendriteNodules.conc1") as file:
    series = file.read()

Step 3: Extract the Final Time Step

To analyze the 30th distribution of carbon concentration, we extract the data from the last time step.

field = series.get_field(30)

Step 4: Create the Initial Plot

The concentration field is visualized using bin.plot(), with transparency set for better readability.

args = bin.PlotArgs(title="Carbon Concentration [wt%]", alpha=0.66)
fig, ax, cbar = bin.plot(field, axis="z", args=args)

Step 5: Access Image Data

We retrieve the image array from the plot to overlay contour lines.

image = ax.images[0].get_array()

Step 6: Add Contour Lines

Contour lines are drawn at carbon concentration levels of 25%, 50%, and 75%. These levels highlight the concentration gradient within the material.

contours = ax.contour(
    image, levels=[25, 50, 75], colors="black", linewidths=1, linestyles="solid"
)

Step 7: Annotate the Colorbar

To improve readability, horizontal lines are added to the colorbar at the same contour levels.

cbar.ax.hlines([25, 50, 75], 0, 1, colors="black", linewidth=1)

The resulting plot visually separates regions with different carbon concentrations, making it easier to identify areas with specific composition levels.