Basic sampling tutorial#
In this tutorial you will learn the basics of the sampling workflow. This tutorial builds off of the external model tutorial
#First, let's import the relavant modules:
import romtools
import os
import numpy as np
from matplotlib import pyplot as plt
from romtools.workflows import sampling
from romtools.workflows import ParameterSpace
module 'mpi4py' is not installed
#Now, we can run a sampling algorithm (https://pressio.github.io/rom-tools-and-workflows/romtools/workflows/sampling/sampling.html)
if __name__ == "__main__":
# As a starting point, we will use the model for the advection diffusion equation created in the external_model tutorial
from ipynb.fs.full.external_model import adrExternalRomToolsModel
myModel = adrExternalRomToolsModel()
# We will use the parameter space created in the parameter space tutorial
from ipynb.fs.full.parameter_space import BasicParameterSpace
myParameterSpace = BasicParameterSpace()
#The sampling algorithm requires a directory argument of where to put all the generated samples, files, etc.
work_directory = os.getcwd() + '/sampling_tutorial/'
#Now we can run the sampling algorithm.
sample_directories = sampling.run_sampling(
model = myModel,
parameter_space = myParameterSpace,
absolute_sampling_directory=work_directory,
number_of_samples = 5)
#The sample_directories returned by the algorithm contains all the directories that samples were run in:
# work_directory/run_0
# work_directory/run_1
# ...
# work_directory/run_4
======= Sample 0 ============
Running
Sample complete, run time = 0.10615849494934082
======= Sample 1 ============
Running
Sample complete, run time = 0.10457086563110352
======= Sample 2 ============
Running
Sample complete, run time = 0.10466408729553223
======= Sample 3 ============
Running
Sample complete, run time = 0.10459375381469727
======= Sample 4 ============
Running
Sample complete, run time = 0.10463738441467285
#Now we can look at the solutions
for (i,sample_dir) in enumerate(sample_directories):
data = np.load(sample_dir + '/solution.npz')
plt.plot(data['x'],data['u'],label='Run ' + str(i))
plt.legend()
plt.xlabel(r'$x$')
plt.ylabel(r'$u$')
plt.show()
#That's it!
