ARTS  2.3.1285(git:92a29ea9-dirty)
test_oem.cc File Reference

Test for the OEM functions. More...

#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include "engine.h"
#include "lin_alg.h"
#include "matpackII.h"
#include "matrix.h"
#include "oem.h"
#include "test_utils.h"
#include "unistd.h"

Go to the source code of this file.

Classes

class  LinearModel
 Linear Forward model. More...
 
class  QuadraticModel
 Quadratic Forward Model. More...
 

Functions

void write_matrix (ConstMatrixView A, const char *filename)
 Write matrix to text file. More...
 
void write_vector (ConstVectorView v, const char *filename)
 Write vector to text file. More...
 
void generate_test_data (VectorView y, VectorView xa, MatrixView Se, MatrixView Sx)
 Generate test data for linear OEM retrieval. More...
 
void generate_linear_model (MatrixView K)
 Generate linear forward model. More...
 
Index run_test_matlab (Engine *eng, string filename)
 Run test script in matlab. More...
 
Index run_oem_matlab (VectorView x, MatrixView G, Engine *eng, string filename)
 Run test script in matlab and return result vector. More...
 
void setup_test_environment (Engine *&eng)
 Setup the test environment. More...
 
void run_plot_script (Engine *eng, string filename, string title)
 Plot benchmark results. More...
 
void tidy_up_test_environment (Engine *eng)
 Tidy up test environment. More...
 
void benchmark_inv (Engine *eng, Index n0, Index n1, Index ntests)
 Matrix inversion benchmark. More...
 
void benchmark_mult (Engine *eng, Index n0, Index n1, Index ntests)
 Matrix multiplication benchmark. More...
 
void benchmark_oem_linear (Engine *eng, Index n0, Index n1, Index ntests)
 Benchmark linear oem. More...
 
void test_oem_linear (Engine *eng, Index m, Index n, Index ntests)
 Test linear_oem. More...
 
void test_oem_gauss_newton (Engine *eng, Index m, Index n, Index ntests)
 Test non-linear OEM. More...
 
void test_oem_levenberg_marquardt (Engine *eng, Index m, Index n, Index ntests)
 Test non-linear OEM. More...
 
void test_oem_gauss_newton_sparse (Index m, Index n, Index ntests)
 Test sparse non-linear OEM. More...
 
void test_oem_levenberg_marquardt_sparse (Index m, Index n, Index ntests)
 Test sparse non-linear OEM. More...
 
int main ()
 

Variables

string source_dir = SOURCEDIR
 
string atmlab_dir = ATMLABDIR
 

Detailed Description

Test for the OEM functions.

Author
Simon Pfreundschuh <simon>
Date
Sat Apr 18 19:50:30 2015

Definition in file test_oem.cc.

Function Documentation

◆ benchmark_inv()

void benchmark_inv ( Engine *  eng,
Index  n0,
Index  n1,
Index  ntests 
)

Matrix inversion benchmark.

Inverts randomly generated matrix matrices in matlab and in arts and compares the performance. Performs ntests numbers of test with matrices linearly increasing in size starting at n0 and ending an n1. Writes the result to standard out and to the file "times_inv.txt" in the current directory. Also generates a plot of the data using matlab stored in "times_inv.png".

Parameters
[in]n0Size of the smallest matrix in the benchmark.
[in]n1Size of the largest matrix in the benchmark.
[in]ntestsNumber of tests to be performed. In each step the size of the matrix is linearly increased from n0 to n1.

Definition at line 381 of file test_oem.cc.

References i, inv(), LinearModel::n, random_fill_matrix(), run_plot_script(), run_test_matlab(), and write_matrix().

◆ benchmark_mult()

void benchmark_mult ( Engine *  eng,
Index  n0,
Index  n1,
Index  ntests 
)

Matrix multiplication benchmark.

Multiplies two identical matrices in matlab and in arts and compares the performance. Performs ntests numbers of test with matrices linearly increasing in size starting at n0 and ending an n1. Writes the result to standard out and to the file "times_mult.txt" in the current directory. Also generates a plot of the data using matlab stored in "times_mult.png".

Parameters
[in]n0Size of the smallest matrix in the benchmark.
[in]n1Size of the largest matrix in the benchmark.
[in]ntestsNumber of tests to be performed. In each step the size of the matrix is linearly increased from n0 to n1.

Definition at line 441 of file test_oem.cc.

References i, mult(), mult_general(), LinearModel::n, random_fill_matrix(), run_plot_script(), run_test_matlab(), and write_matrix().

◆ benchmark_oem_linear()

void benchmark_oem_linear ( Engine *  eng,
Index  n0,
Index  n1,
Index  ntests 
)

Benchmark linear oem.

Run linear oem test and benchmark. Runs ntests numbers of tests with the size of K growing linearly from n0 to n1. Prints for each test the maximum relative error in the result compared to the matlab implementation and the cpu time in milliseconds needed for the execution. The timing results are also written to to a file "times_linear.txt" in the current directory.

Parameters
n0Starting size for the K matrix.
n1Final size for the K matrix.
ntestsNumber of tests to be performed.

Definition at line 499 of file test_oem.cc.

References generate_linear_model(), generate_test_data(), i, inv(), LinearModel::J, mult(), LinearModel::n, run_oem_matlab(), run_plot_script(), write_matrix(), and write_vector().

◆ generate_linear_model()

void generate_linear_model ( MatrixView  K)

Generate linear forward model.

Fills the given matrix K with random values in the range [-10,10].

Parameters
[in,out]KThe matrix representing the forward model.

Definition at line 245 of file test_oem.cc.

References random_fill_matrix().

Referenced by benchmark_oem_linear(), and test_oem_linear().

◆ generate_test_data()

void generate_test_data ( VectorView  y,
VectorView  xa,
MatrixView  Se,
MatrixView  Sx 
)

Generate test data for linear OEM retrieval.

Fills the given matrices and vectors needed for the linear OEM retrieval functions with random values.

Parameters
yThe measurement vector. Filled with random integer values in the range [0, 10].
xaThe a priori vector. Filled with random integer values in the range [0, 10].
KThe linear forward model. Filled with random integer values in the range [-10, 10].
SeThe covariance matrix for observation uncertainties. Filled with random values in the range [-1, 1] (Scaled down from the range [-10, 10]).
SaThe covariance matrix for a priori uncertainties. Filled with random values in the range [-1, 1] (Scaled down from the range [-10, 10]).

Definition at line 220 of file test_oem.cc.

References mult(), random_fill_matrix(), random_fill_matrix_symmetric(), random_fill_vector(), and transpose().

Referenced by benchmark_oem_linear(), test_oem_gauss_newton(), test_oem_levenberg_marquardt(), and test_oem_linear().

◆ main()

int main ( void  )

Definition at line 987 of file test_oem.cc.

References test_oem_gauss_newton_sparse().

◆ run_oem_matlab()

Index run_oem_matlab ( VectorView  x,
MatrixView  G,
Engine *  eng,
string  filename 
)

Run test script in matlab and return result vector.

Runs the oem function from the atmlab package. Runs the given external matlab script. The results of the computation are read from the Matlab workspace variables x and t and returned in the x vector argument and the return value.

Parameters
[out]xVector to write the results of the retrieval to.
[in]engPointer to the matlab engine that manages the running Matlab session.
[in]filenameName of the test script to be run.
Returns
Execution time of oem in matlab in ms.

Definition at line 285 of file test_oem.cc.

References i, LinearModel::m, LinearModel::n, ConstMatrixView::ncols(), and ConstMatrixView::nrows().

Referenced by benchmark_oem_linear().

◆ run_plot_script()

void run_plot_script ( Engine *  eng,
string  filename,
string  title 
)

Plot benchmark results.

Run matlab script that generates a plot of the benchmark results.

Parameters
[in]engPointer to the running matlab engine.
[in]filenameFilename of the file containing the results. Should be 'times_mult.txt' or 'times_linear.txt'.

Definition at line 347 of file test_oem.cc.

Referenced by benchmark_inv(), benchmark_mult(), and benchmark_oem_linear().

◆ run_test_matlab()

Index run_test_matlab ( Engine *  eng,
string  filename 
)

Run test script in matlab.

Runs the test script given by filename in matlab. Reads out the the execution time from the variable t in the current workspace and returns is as return value.

Parameters
[in]engPointer to the running matlab engine.
[in]filenameName of the file to be run.
Returns
The time required for the execution.

Definition at line 258 of file test_oem.cc.

Referenced by benchmark_inv(), and benchmark_mult().

◆ setup_test_environment()

void setup_test_environment ( Engine *&  eng)

Setup the test environment.

Changes to the test directory and initializes the matlab engine. Initialized the atmlab package.

Parameters
[in,out]engPointer variable that will contain the pointer to the initialized Matlab engine.

Definition at line 321 of file test_oem.cc.

References atmlab_dir, and source_dir.

◆ test_oem_gauss_newton()

void test_oem_gauss_newton ( Engine *  eng,
Index  m,
Index  n,
Index  ntests 
)

Test non-linear OEM.

Test the non-linear OEM functions using the Gauss-Newton method using a random quadratic model. Performs ntest numbers of test with a state space of dimension n and a measurement space of dimension m. Test each iteration method and compares the result to the Matlab result. The maximum relative error is printed to stdout.

Parameters
engThe Matlba engine handle.
mThe dimension of the measurement space space.
nThe dimension of the state space.
ntestsThe number of tests to perform.

Definition at line 686 of file test_oem.cc.

References add_noise(), QuadraticModel::evaluate(), generate_test_data(), i, inv(), x0, and LinearModel::y0.

◆ test_oem_gauss_newton_sparse()

void test_oem_gauss_newton_sparse ( Index  m,
Index  n,
Index  ntests 
)

Test sparse non-linear OEM.

Test the non-linear OEM function using Gauss-Newton method with a random quadratic model and sparse matrix arithmetic. In each test, the result is compared to the result obtained using dense arithmetic. Checks both the standard and the normalized case. The maximum relative error of both cases and the Gain matrix is printed to stdout for each test.

Parameters
mThe dimension of the measurement space space.
nThe dimension of the state space.
ntestsThe number of tests to perform.

Definition at line 897 of file test_oem.cc.

References add_noise(), QuadraticModel::evaluate(), i, id_mat(), random_fill_vector(), x0, and LinearModel::y0.

Referenced by main().

◆ test_oem_levenberg_marquardt()

void test_oem_levenberg_marquardt ( Engine *  eng,
Index  m,
Index  n,
Index  ntests 
)

Test non-linear OEM.

Test the non-linear OEM function using Levenberg-Marquardt method with a random quadratic model. Performs ntest numbers of tests with a state space of dimension n and a measurement space of dimension m. Test each iteration method and compares the result to the Matlab result. The maximum relative error is printed to stdout.

Parameters
engThe Matlba engine handle.
mThe dimension of the measurement space space.
nThe dimension of the state space.
ntestsThe number of tests to perform.

Definition at line 790 of file test_oem.cc.

References add_noise(), QuadraticModel::evaluate(), generate_test_data(), i, inv(), x0, and LinearModel::y0.

◆ test_oem_levenberg_marquardt_sparse()

void test_oem_levenberg_marquardt_sparse ( Index  m,
Index  n,
Index  ntests 
)

Test sparse non-linear OEM.

Test the non-linear OEM function using Levenberg-Marquardt method with a random quadratic model and sparse matrix arithmetic. In each test, the result is compared to the result obtained using dense arithmetic. Checks both the standard and the normalized case. The maximum relative error of both cases and the Gain matrix is printed to stdout for each test.

Parameters
mThe dimension of the measurement space space.
nThe dimension of the state space.
ntestsThe number of tests to perform.

Definition at line 985 of file test_oem.cc.

◆ test_oem_linear()

void test_oem_linear ( Engine *  eng,
Index  m,
Index  n,
Index  ntests 
)

Test linear_oem.

Tests the linear_oem function using randomized input data. Performs ntest numbers of tests with a m times n K-matrix. For each test, the maximum relative error is printed to standard out.

Parameters
[in]mSize of the measurement space.
[in]nSize of the state space.
[in]ntestsNumber of tests to be performed.

Definition at line 575 of file test_oem.cc.

References generate_linear_model(), generate_test_data(), i, and LinearModel::J.

◆ tidy_up_test_environment()

void tidy_up_test_environment ( Engine *  eng)

Tidy up test environment.

Deletes temporary test files and closes the Matlab session.

Parameters
[in]engPointer to the running Matlab engine.

Definition at line 360 of file test_oem.cc.

◆ write_matrix()

void write_matrix ( ConstMatrixView  A,
const char *  filename 
)

Write matrix to text file.

Write the given matrix in plain text to the file filename in the current directory.

Parameters
[in]AThe matrix to write to the file.
[in]filenameThe name of the file to write to.

Definition at line 170 of file test_oem.cc.

References i, LinearModel::m, LinearModel::n, ConstMatrixView::ncols(), and ConstMatrixView::nrows().

Referenced by benchmark_inv(), benchmark_mult(), benchmark_oem_linear(), and QuadraticModel::QuadraticModel().

◆ write_vector()

void write_vector ( ConstVectorView  v,
const char *  filename 
)

Write vector to text file.

Write the given vector to the file filename in the current directory.

Parameters
[in]vThe vector to write.
[in]filenameThe name of the file to write to.

Definition at line 192 of file test_oem.cc.

References i, LinearModel::n, and ConstVectorView::nelem().

Referenced by benchmark_oem_linear().

Variable Documentation

◆ atmlab_dir

string atmlab_dir = ATMLABDIR

Definition at line 33 of file test_oem.cc.

Referenced by setup_test_environment().

◆ source_dir

string source_dir = SOURCEDIR

Definition at line 32 of file test_oem.cc.

Referenced by setup_test_environment().