From 2edd9a65b61334d8c4866cd3b1f04d06ce264b5b Mon Sep 17 00:00:00 2001 From: Daniel Garcia-Sanchez Date: Wed, 21 Apr 2021 21:41:05 +0200 Subject: [PATCH] Use the Jupyter Notebook only the analyze the results --- examples/step-62/doc/results.dox | 103 +++++++++++ examples/step-62/step-62.cc | 95 +++++++++- examples/step-62/step-62.ipynb | 306 +++++++++---------------------- 3 files changed, 282 insertions(+), 222 deletions(-) diff --git a/examples/step-62/doc/results.dox b/examples/step-62/doc/results.dox index bbe4d34152..888dbecb5f 100644 --- a/examples/step-62/doc/results.dox +++ b/examples/step-62/doc/results.dox @@ -132,3 +132,106 @@ which are promising candidates to study macroscopic quantum phenomena. The 20GHz mode of a micropillar superlattice cavity is essentially a mechanical harmonic oscillator that is very well isolated from the environment. If the device is cooled down to 20mK in a dilution fridge, the mode would then become a macroscopic quantum harmonic oscillator. + + +

Possibilities for extensions

+ +Instead of setting the parameters in the C++ file we could set the parameters +using a python script and save them in the HDF5 file that we will use for +the simulations. Then the deal.II program will read the parameters from the +HDF5 file. + +@code{.py} +import numpy as np +import h5py +import matplotlib.pyplot as plt +import subprocess +import scipy.constants as constants +import scipy.optimize + +# This considerably reduces the size of the svg data +plt.rcParams['svg.fonttype'] = 'none' + +h5_file = h5py.File('results.h5', 'w') +data = h5_file.create_group('data') +displacement = data.create_group('displacement') +calibration = data.create_group('calibration') + +# Set the parameters +for group in [displacement, calibration]: + # Dimensions of the domain + # The waveguide length is equal to dimension_x + group.attrs['dimension_x'] = 2e-5 + # The waveguide width is equal to dimension_y + group.attrs['dimension_y'] = 2e-8 + + # Position of the probe that we use to measure the flux + group.attrs['probe_pos_x'] = 8e-6 + group.attrs['probe_pos_y'] = 0 + group.attrs['probe_width_y'] = 2e-08 + + # Number of points in the probe + group.attrs['nb_probe_points'] = 5 + + # Global refinement + group.attrs['grid_level'] = 1 + + # Cavity + group.attrs['cavity_resonance_frequency'] = 20e9 + group.attrs['nb_mirror_pairs'] = 15 + + # Material + group.attrs['poissons_ratio'] = 0.27 + group.attrs['youngs_modulus'] = 270000000000.0 + group.attrs['material_a_rho'] = 3200 + if group == displacement: + group.attrs['material_b_rho'] = 2000 + else: + group.attrs['material_b_rho'] = 3200 + group.attrs['lambda'] = (group.attrs['youngs_modulus'] * group.attrs['poissons_ratio'] / + ((1 + group.attrs['poissons_ratio']) * + (1 - 2 * group.attrs['poissons_ratio']))) + group.attrs['mu']= (group.attrs['youngs_modulus'] / (2 * (1 + group.attrs['poissons_ratio']))) + + # Force + group.attrs['max_force_amplitude'] = 1e26 + group.attrs['force_sigma_x'] = 1e-7 + group.attrs['force_sigma_y'] = 1 + group.attrs['max_force_width_x'] = 3e-7 + group.attrs['max_force_width_y'] = 2e-8 + group.attrs['force_x_pos'] = -8e-6 + group.attrs['force_y_pos'] = 0 + + # PML + group.attrs['pml_x'] = True + group.attrs['pml_y'] = False + group.attrs['pml_width_x'] = 1.8e-6 + group.attrs['pml_width_y'] = 5e-7 + group.attrs['pml_coeff'] = 1.6 + group.attrs['pml_coeff_degree'] = 2 + + # Frequency sweep + group.attrs['center_frequency'] = 20e9 + group.attrs['frequency_range'] = 0.5e9 + group.attrs['start_frequency'] = group.attrs['center_frequency'] - group.attrs['frequency_range'] / 2 + group.attrs['stop_frequency'] = group.attrs['center_frequency'] + group.attrs['frequency_range'] / 2 + group.attrs['nb_frequency_points'] = 400 + + # Other parameters + if group == displacement: + group.attrs['simulation_name'] = 'phononic_cavity_displacement' + else: + group.attrs['simulation_name'] = 'phononic_cavity_calibration' + group.attrs['save_vtu_files'] = False + +h5_file.close() +@endcode + +In order to read the HDF5 parameters we have to use the +HDF5::File::FileAccessMode::open flag. +@code{.py} + HDF5::File data_file("results.h5", + HDF5::File::FileAccessMode::open, + MPI_COMM_WORLD); + auto data = data_file.open_group("data"); +@endcode diff --git a/examples/step-62/step-62.cc b/examples/step-62/step-62.cc index f321f88b38..3088dc317e 100644 --- a/examples/step-62/step-62.cc +++ b/examples/step-62/step-62.cc @@ -1370,10 +1370,99 @@ int main(int argc, char *argv[]) Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1); - HDF5::File data_file("results.h5", - HDF5::File::FileAccessMode::open, + HDF5::File data_file("results.h5", + HDF5::File::FileAccessMode::create, MPI_COMM_WORLD); - HDF5::Group data = data_file.open_group("data"); + auto data = data_file.create_group("data"); + + // Each of the simulations (displacement and calibration) is stored in a + // separate HDF5 group: + const std::vector group_names = {"displacement", + "calibration"}; + for (auto group_name : group_names) + { + // For each of these two group names, we now create the group and put + // attributes into these groups. + // Specifically, these are: + // - The dimensions of the waveguide (in $x$ and $y$ directions) + // - The position of the probe (in $x$ and $y$ directions) + // - The number of points in the probe + // - The global refinement level + // - The cavity resonance frequency + // - The number of mirror pairs + // - The material properties + // - The force parameters + // - The PML parameters + // - The frequency parameters + + auto group = data.create_group(group_name); + + group.set_attribute("dimension_x", 2e-5); + group.set_attribute("dimension_y", 2e-8); + group.set_attribute("probe_pos_x", 8e-6); + group.set_attribute("probe_pos_y", 0); + group.set_attribute("probe_width_y", 2e-08); + group.set_attribute("nb_probe_points", 5); + group.set_attribute("grid_level", 1); + group.set_attribute("cavity_resonance_frequency", 20e9); + group.set_attribute("nb_mirror_pairs", 15); + + group.set_attribute("poissons_ratio", 0.27); + group.set_attribute("youngs_modulus", 270000000000.0); + group.set_attribute("material_a_rho", 3200); + + if (group_name == std::string("displacement")) + group.set_attribute("material_b_rho", 2000); + else + group.set_attribute("material_b_rho", 3200); + + group.set_attribute( + "lambda", + group.get_attribute("youngs_modulus") * + group.get_attribute("poissons_ratio") / + ((1 + group.get_attribute("poissons_ratio")) * + (1 - 2 * group.get_attribute("poissons_ratio")))); + group.set_attribute("mu", + group.get_attribute("youngs_modulus") / + (2 * (1 + group.get_attribute( + "poissons_ratio")))); + + group.set_attribute("max_force_amplitude", 1e26); + group.set_attribute("force_sigma_x", 1e-7); + group.set_attribute("force_sigma_y", 1); + group.set_attribute("max_force_width_x", 3e-7); + group.set_attribute("max_force_width_y", 2e-8); + group.set_attribute("force_x_pos", -8e-6); + group.set_attribute("force_y_pos", 0); + + group.set_attribute("pml_x", true); + group.set_attribute("pml_y", false); + group.set_attribute("pml_width_x", 1.8e-6); + group.set_attribute("pml_width_y", 5e-7); + group.set_attribute("pml_coeff", 1.6); + group.set_attribute("pml_coeff_degree", 2); + + group.set_attribute("center_frequency", 20e9); + group.set_attribute("frequency_range", 0.5e9); + group.set_attribute( + "start_frequency", + group.get_attribute("center_frequency") - + group.get_attribute("frequency_range") / 2); + group.set_attribute( + "stop_frequency", + group.get_attribute("center_frequency") + + group.get_attribute("frequency_range") / 2); + group.set_attribute("nb_frequency_points", 400); + + if (group_name == std::string("displacement")) + group.set_attribute( + "simulation_name", std::string("phononic_cavity_displacement")); + else + group.set_attribute( + "simulation_name", std::string("phononic_cavity_calibration")); + + group.set_attribute("save_vtu_files", false); + } { // Displacement simulation. The parameters are read from the diff --git a/examples/step-62/step-62.ipynb b/examples/step-62/step-62.ipynb index 2639aeb0d2..fe9b17f4c6 100644 --- a/examples/step-62/step-62.ipynb +++ b/examples/step-62/step-62.ipynb @@ -1,157 +1,9 @@ { "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Step-62" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Set parameters" - ] - }, { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "%config InlineBackend.figure_formats = ['svg']\n", - "%matplotlib inline\n", - "import numpy as np\n", - "import h5py\n", - "import matplotlib.pyplot as plt\n", - "import subprocess\n", - "import scipy.constants as constants\n", - "import scipy.optimize\n", - "\n", - "# This considerably reduces the size of the svg data\n", - "plt.rcParams['svg.fonttype'] = 'none'\n", - "\n", - "h5_file = h5py.File('results.h5', 'w')\n", - "data = h5_file.create_group('data')\n", - "displacement = data.create_group('displacement')\n", - "calibration = data.create_group('calibration')\n", - "\n", - "# Set the parameters\n", - "for group in [displacement, calibration]:\n", - " # Dimensions of the domain\n", - " # The waveguide length is equal to dimension_x\n", - " group.attrs['dimension_x'] = 2e-5\n", - " # The waveguide width is equal to dimension_y\n", - " group.attrs['dimension_y'] = 2e-8\n", - " \n", - " # Position of the probe that we use to measure the flux\n", - " group.attrs['probe_pos_x'] = 8e-6\n", - " group.attrs['probe_pos_y'] = 0\n", - " group.attrs['probe_width_y'] = 2e-08\n", - " \n", - " # Number of points in the probe\n", - " group.attrs['nb_probe_points'] = 5\n", - " \n", - " # Global refinement\n", - " group.attrs['grid_level'] = 1\n", - "\n", - " # Cavity\n", - " group.attrs['cavity_resonance_frequency'] = 20e9\n", - " group.attrs['nb_mirror_pairs'] = 15\n", - "\n", - " # Material\n", - " group.attrs['poissons_ratio'] = 0.27\n", - " group.attrs['youngs_modulus'] = 270000000000.0\n", - " group.attrs['material_a_rho'] = 3200\n", - " if group == displacement:\n", - " group.attrs['material_b_rho'] = 2000\n", - " else:\n", - " group.attrs['material_b_rho'] = 3200 \n", - " group.attrs['lambda'] = (group.attrs['youngs_modulus'] * group.attrs['poissons_ratio'] /\n", - " ((1 + group.attrs['poissons_ratio']) *\n", - " (1 - 2 * group.attrs['poissons_ratio'])))\n", - " group.attrs['mu']= (group.attrs['youngs_modulus'] / (2 * (1 + group.attrs['poissons_ratio'])))\n", - "\n", - " # Force\n", - " group.attrs['max_force_amplitude'] = 1e26\n", - " group.attrs['force_sigma_x'] = 1e-7\n", - " group.attrs['force_sigma_y'] = 1\n", - " group.attrs['max_force_width_x'] = 3e-7\n", - " group.attrs['max_force_width_y'] = 2e-8\n", - " group.attrs['force_x_pos'] = -8e-6\n", - " group.attrs['force_y_pos'] = 0\n", - "\n", - " # PML\n", - " group.attrs['pml_x'] = True\n", - " group.attrs['pml_y'] = False\n", - " group.attrs['pml_width_x'] = 1.8e-6\n", - " group.attrs['pml_width_y'] = 5e-7\n", - " group.attrs['pml_coeff'] = 1.6\n", - " group.attrs['pml_coeff_degree'] = 2\n", - "\n", - " # Frequency sweep\n", - " group.attrs['center_frequency'] = 20e9\n", - " group.attrs['frequency_range'] = 0.5e9\n", - " group.attrs['start_frequency'] = group.attrs['center_frequency'] - group.attrs['frequency_range'] / 2\n", - " group.attrs['stop_frequency'] = group.attrs['center_frequency'] + group.attrs['frequency_range'] / 2\n", - " group.attrs['nb_frequency_points'] = 400\n", - "\n", - " # Other parameters\n", - " if group == displacement:\n", - " group.attrs['simulation_name'] = 'phononic_cavity_displacement'\n", - " else:\n", - " group.attrs['simulation_name'] = 'phononic_cavity_calibration'\n", - " group.attrs['save_vtu_files'] = False\n", - " \n", - "h5_file.close()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run simulation" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "CompletedProcess(args=['mpiexec', './step-62'], returncode=0)" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Now you can run your simulation locally or in a cluster\n", - "subprocess.run(['mpiexec','./step-62'])" - ] - }, - { - "cell_type": "markdown", "metadata": {}, - "source": [ - "## Analyze data" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, "outputs": [ { "data": { @@ -159,8 +11,8 @@ "\n", "\n", - "\n", - "\n", + "\n", + "\n", " \n", " \n" ], "text/plain": [ - "" + "
" ] }, - "metadata": {}, + "metadata": { + "needs_background": "light" + }, "output_type": "display_data" }, { @@ -450,8 +304,8 @@ "\n", "\n", - "\n", - "\n", + "\n", + "\n", " \n", " \n" ], "text/plain": [ - "" + "
" ] }, - "metadata": {}, + "metadata": { + "needs_background": "light" + }, "output_type": "display_data" } ], "source": [ + "%config InlineBackend.figure_formats = ['svg']\n", + "%matplotlib inline\n", + "import numpy as np\n", + "import h5py\n", + "import matplotlib.pyplot as plt\n", + "import subprocess\n", + "import scipy.constants as constants\n", + "import scipy.optimize\n", + "\n", + "# This considerably reduces the size of the svg data\n", + "plt.rcParams['svg.fonttype'] = 'none'\n", + "\n", "h5_file = h5py.File('results.h5', 'r')\n", "data = h5_file['data']\n", "\n", @@ -772,7 +640,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.3" + "version": "3.7.3" } }, "nbformat": 4, -- 2.39.5