{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# PyMKS (Dask) Introduction\n", "\n", "This notebook demonstrates homogenization and localization in PyMKS . 2-point statistics are used to predict effective properties (homogenization) and local properties are predicted using a regression technique (localization). See the [theory section](../../THEORY.html) for more details." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "import os\n", "os.environ[\"OMP_NUM_THREADS\"] = \"1\"\n", "\n", "import numpy as np\n", "import dask.array as da\n", "import matplotlib.pyplot as plt\n", "from sklearn.pipeline import Pipeline\n", "from dask_ml.decomposition import PCA\n", "from dask_ml.preprocessing import PolynomialFeatures\n", "from sklearn.linear_model import LinearRegression\n", "\n", "from pymks import (\n", " generate_multiphase,\n", " generate_delta,\n", " plot_microstructures,\n", " PrimitiveTransformer,\n", " FlattenTransformer,\n", " solve_fe,\n", " TwoPointCorrelation,\n", " FlattenTransformer,\n", " LocalizationRegressor\n", ")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [], "source": [ "#PYTEST_VALIDATE_IGNORE_OUTPUT\n", "\n", "%matplotlib inline\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quantify Microstructures using 2-Point Statistics\n", "\n", "The following generates two dual-phase microstructures with different morphologies. The `generate` function generates microstructures with different grains sizes and volume fractions." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [], "source": [ "da.random.seed(10)\n", "np.random.seed(10)\n", "\n", "x_data = da.concatenate([\n", " generate_multiphase(shape=(1, 101, 101), grain_size=(25, 25), volume_fraction=(0.5, 0.5)),\n", " generate_multiphase(shape=(1, 101, 101), grain_size=(95, 15), volume_fraction=(0.5, 0.5))\n", "]).persist()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`x_data` is a Dask array. The `persist` method ensures that the data is actually calculated. The array has two chunks and two samples. The spatial shape is `(101, 101)`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n",
"
| \n",
"\n", "\n", " | \n", "
\n",
"
| \n",
"\n", "\n", " | \n", "
\n",
"
| \n",
"\n", "\n", " | \n", "
\n",
"
| \n",
"\n", "\n", " | \n", "
\n",
"
| \n",
"\n", "\n", " | \n", "
\n",
"
| \n",
"\n", "\n", " | \n", "
\n",
"
| \n",
"\n", "\n", " | \n", "