************ Installation ************

The codes consist of two parts: Matlab codes for stochastic and time discretization and solution of the inverse problem,
and Python codes for construction of spatial discretization matrices in FEniCS.
The Matlab codes require TT-Toolbox.
Please download it from https://github.com/oseledets/TT-Toolbox/tree/small
Unpack it and invoke setup.m from Matlab.
It will add all directories to Matlab path.

Another recommended toolbox is HSL MI20 AMG preconditioner, see http://www.hsl.rl.ac.uk/catalogue/hsl_mi20.html
Follow instructions there to download, compile and add to the path the MEX AMG library.
This toolbox is needed for all forward problems and the 3D inverse problem.
Alternatively, you can force the methods to use backslash for 2D problems.
Define the global variable avoid_hsl_prec in the main Matlab workspace by invoking
    global avoid_hsl_prec
    avoid_hsl_prec = true;
However, the 3D experiments will still use HSL.

The Python codes are more complicated.
We used FEniCS 1.5.0 with a special module for dumping a matrix to a Matrix Market (.mtx) file.
Without that module, it's impossible to reproduce all stages of the experiments without changing some codes.
Matlab tests can be run using some precomputed matrices from the directory precomputed_mtx/
The subdirectories' titles denote the number of pressure basis functions (Q1).
Copy all .mtx files from either subdirectory here to the main directory.
Alternatively, you can save the matrices from FEniCS to any other format
(change the lines "mfile = File ("A.mtx")" in .py files),
and find a way to read that format from Matlab
(change all "...mmread..." lines in .m files accordingly).



************ File list ************

test_expseries_2d.m		Main run file to run series of 2D experiments for different parameter values
test_preconditioners_2d.m	Run one experiment with 2D problem. Optionally, switch between the two Schur complement preconditioners
test_oneexp_3d.m		Run one 3D experiment. This file uses iterative AMG solver for the velocity equation
test_eigenv.m			Test the eigenvalues of the preconditioned matrix on small grids.

oneexp_2d.m			Run one 2D experiment. This file is called from test_expseries_2d.m, don't use it directly
locsolve_direct.m		Solve reduced system for the spatial TT block in amen_block_solve procedure. Forward problem uses backslash
locsolve_iter.m			Solve reduced system for the spatial TT block in amen_block_solve procedure. Forward problem uses Stokes prec. + AMG
schurprec_forw.m	  	Stokes preconditioner + AMG for the forward problem
mmread.m			Read a matrix from .mtx file
mmwrite.m			Write a matrix to .mtx file
fgmres.m			Flexible GMRES

G0_G1.mat, G0_G1_small.mat	Stochastic Galerkin matrices

gen_SBmat_2d.py			Generate 2D spatial discretization matrices via FEniCS
gen_SBmat_3d.py			Generate 3D spatial discretization matrices via FEniCS
gen_SBmat_small.py		Generate small 2D discretization matrices for test_eigenv




************ Running series of 2D experiments via test_expseries_2d.m ************

This script has two arguments: exptype, expindex.
exptype should be a character string containing any of the following abbreviations:
  nt		vary the number of time steps from 2^5 to 2^{12}
  T		vary the time interval from 0.01 to 100
  beta		vary the beta regularization parameter from 1e-10 to 1e3
  alpha		vary the beta regularization parameter from 0 to 1e3
  tol		vary the TT tolerance from 1e-8 to 1e-4
  nu1		vary nu_1 (variance parameter) from 0 to 0.9
  ns		vary the spatial grid level         !!!!! SEE EXTRA INFO BELOW !!!!
  K		vary the Brinkman jump coefficient  !!!!! SEE EXTRA INFO BELOW !!!!

expindex is a vector of integer values, indicating which tests from each series to run.
For example, if exptype='nt' and expindex=1:3, the script will run the 'nt' experiment with nt=2^5, 2^6, 2^7.
The maximal possible value in expindex is thus limited by the number of parameter values:
  nt		8
  T		5
  beta		9
  alpha		8
  tol		5
  nu1		8
  ns		4
  K		7
This is, of course, if you don't modify the parameter sets in test_expseries_2d.m.

The 'ns' and 'K' tests depend on the spatial matrices, 
therefore you cannot simply pass expindex with several values -- this will just repeat the same test.
For different ns or K, you need to rebuild the matrices in FEniCS.
So you can either run these tests one by one, calling the Python script before each new test,
or precompute several sets of matrices (cf. precomputed_mtx/), 
and modify oneexp_2d.m such that it loads different matrices from different subdirectories, for example.

The output is saved to .dat files, having the following name pattern:
  stokescontrol_exptype_np_minexpindex-maxexpindex.dat
where np is the number of pressure basis functions.
The data is stored in the following columns:
  parameter  global_ttimes  max(global_resids,[],2)  global_errs  global_ttranks  global_mem  global_errobs  global_moments  global_iters
where 
  parameter 		is the varying parameter according to exptype, the column is of size numel(expindex) x 1
  global_ttimes		CPU times, size numel(expindex) x 1
  global_resids		residuals of the KKT system (separately for each row), size numel(expindex) x 3, hence max(global_resids,[],2) is a single column,
  global_errs		error estimates of solution components, size numel(expindex) x 3
  global_ttranks	maximal TT ranks, size numel(expindex) x 1
  global_mem		memory needed for the TT format of the solution, size numel(expindex) x 1
  global_errobs		misfit with the desired state, size numel(expindex) x 1
  global_moments	statistical moments mean and standard deviation, size numel(expindex) x 2
  global_iters		total number of fgmres iterations for the spatial system, size numel(expindex) x 1

These data can be plotted in Matlab or TikZ.


************ Running preconditioning or 3D tests: test_preconditioners_2d.m, test_oneexp_3d.m ************

Either of these scripts runs a single test for fixed parameters.
The parameters can be modified in the beginning of the file.
The output is not dumped to a file, it is only printed in the Matlab console.
Since this is a single test, global_errs is unavailable.
The 3D test solves the "forward" problem (in the preconditioning step for the inverse problem)
iteratively, using the commutator-based Stokes preconditioner, with the HSL preconditioner for the velocity equation.
The preconditioners are compared by changing the parameter locsolve_prec in test_preconditioners_2d.m.


************ Plotting eigenvalues of the preconditioned matrix: test_eigenv.m ************

This script varies beta and alpha and plots eigenvalues of the matrices from Theorem 1.
Note that the dense inverse matrices there are computed explicitly, so run this script
only for !!!!extremely!!! small grid sizes.

