Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test files for all algorithm except unmerged Preconditioned BiCGSTAB(l) #9

Merged
merged 8 commits into from Sep 9, 2019
2 changes: 2 additions & 0 deletions include/BlazeIterative.hpp
Expand Up @@ -12,4 +12,6 @@

#include "solvers/solvers.hpp"

#define EPSILON 1.0e-8

#endif //BLAZE_ITERATIVE_BLAZEITERATIVE_HPP
22 changes: 20 additions & 2 deletions tests/CMakeLists.txt
@@ -1,2 +1,20 @@
add_executable(blz_iter_test main.cpp)
target_link_libraries(blz_iter_test PRIVATE BlazeIterative)
add_executable(test_arnoldi main_Arnoldi.cpp)
target_link_libraries(test_arnoldi PRIVATE BlazeIterative)

add_executable(test_lanczos main_Lanczos.cpp)
target_link_libraries(test_lanczos PRIVATE BlazeIterative)

add_executable(test_preconditionedcg main_PreconditionedCG.cpp)
target_link_libraries(test_preconditionedcg PRIVATE BlazeIterative)

add_executable(test_gmres main_GMRES.cpp)
target_link_libraries(test_gmres PRIVATE BlazeIterative)

add_executable(test_cg main_CG.cpp)
target_link_libraries(test_cg PRIVATE BlazeIterative)

add_executable(test_bicgstab main_BiCGSTAB.cpp)
target_link_libraries(test_bicgstab PRIVATE BlazeIterative)

add_executable(test_preconditionedbicgstab main_PreconditionedBiCGSTAB.cpp)
target_link_libraries(test_preconditionedbicgstab PRIVATE BlazeIterative)
48 changes: 48 additions & 0 deletions tests/main_Arnoldi.cpp
@@ -0,0 +1,48 @@
// Copyright (c) 2017 Tyler Olsen
// 2018 Patrick Diehl
// 2019 Nanmiao Wu
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "BlazeIterative.hpp"
#include <iostream>
#include <cstdlib>

using namespace blaze;
using namespace blaze::iterative;

int main() {

// Test Arnoldi

std::size_t N = 6;
DynamicMatrix<double,columnMajor> A(N,N,0);
band<0>(A) = {0, 1, 2, 3, 4, 100000};

DynamicVector<double> b(N, 1);
DynamicVector<complex<double>,columnVector> w(N);
DynamicMatrix<complex<double>,rowMajor> V(N,N);
DynamicVector<complex<double>,columnVector> w1(N);
eigen(A,w,V);

w1 = {w[5],w[0],w[4],w[1],w[2],w[3]};

std::size_t n = N;
ArnoldiTag tag;
DynamicVector<complex<double>,columnVector> w2(n);
DynamicMatrix<complex<double>,rowMajor> V2(n,n);
auto res1 = solve(A,b,tag,n);
auto sub_h = submatrix( res1.second, 0UL, 0UL, (res1.second.rows()-1), res1.second.columns());
eigen(sub_h,w2,V2);

auto error = real(norm(w1 - w2));

if (error < EPSILON){
std::cout << " Pass test of Arnoldi" << std::endl;
return EXIT_SUCCESS;
} else{
std::cout << "Fail test of Arnoldi" << std::endl;
return EXIT_FAILURE;
}
}

42 changes: 42 additions & 0 deletions tests/main_BiCGSTAB.cpp
@@ -0,0 +1,42 @@
// Copyright (c) 2017 Tyler Olsen
// 2018 Patrick Diehl
// 2019 Nanmiao Wu
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "BlazeIterative.hpp"
#include <iostream>
#include <cstdlib>

using namespace blaze;
using namespace blaze::iterative;

int main() {

// Test BiCGSTAB

std::size_t N = 10;
DynamicMatrix<double,false> A(N,N, 0.0);
DynamicVector<double> b(N, 0.0);
DynamicVector<double> x1(N, 0.5);
for(int i=0; i<N; ++i) {
A(i,i) = 2.0;
b[i] = 1.0*(1+i);
x1[i] += x1[i-1];
}

BiCGSTABTag tag;
tag.do_log() = true;
auto x2 = solve(A,b,tag);

auto error = norm(x1 - x2);

if (error < EPSILON){
std::cout << " Pass test of BiCGSTAB" << std::endl;
return EXIT_SUCCESS;
} else{
std::cout << "Fail test of BiCGSTAB" << std::endl;
return EXIT_FAILURE;
}

}
42 changes: 42 additions & 0 deletions tests/main_CG.cpp
@@ -0,0 +1,42 @@
// Copyright (c) 2017 Tyler Olsen
// 2018 Patrick Diehl
// 2019 Nanmiao Wu
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "BlazeIterative.hpp"
#include <iostream>
#include <cstdlib>

using namespace blaze;
using namespace blaze::iterative;

int main() {

// Test CG

std::size_t N = 10;
DynamicMatrix<double,false> A(N,N, 0.0);
DynamicVector<double> b(N, 0.0);
DynamicVector<double> x1(N, 0.5);
for(int i=0; i<N; ++i) {
A(i,i) = 2.0;
b[i] = 1.0*(1+i);
x1[i] += x1[i-1];
}

ConjugateGradientTag tag;
tag.do_log() = true;
auto x2 = solve(A,b,tag);

auto error = norm(x1 - x2);

if (error < EPSILON){
std::cout << " Pass test of CG" << std::endl;
return EXIT_SUCCESS;
} else{
std::cout << "Fail test of CG" << std::endl;
return EXIT_FAILURE;
}

}
43 changes: 43 additions & 0 deletions tests/main_GMRES.cpp
@@ -0,0 +1,43 @@
// Copyright (c) 2017 Tyler Olsen
// 2018 Patrick Diehl
// 2019 Nanmiao Wu
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "BlazeIterative.hpp"
#include <iostream>
#include <cstdlib>

using namespace blaze;
using namespace blaze::iterative;

int main() {

// Test GMRES

std::size_t N = 5;
DynamicMatrix<double,false> A(N,N);
DynamicVector<double> b(N);
A = {{0.46, 0.60, 0.74, 0.61, 0.85}, {0.56, 0.31, 0.80, 0.94, 0.76},{0.41, 0.19, 0.15, 0.33, 0.06},{0.03, 0.92, 0.15, 0.56, 0.08},{0.09, 0.06, 0.69, 0.42, 0.96}};
b = {1.788, 1.891, 0.458, 0.818, 1.53};
DynamicVector<double> x1{0.1, 0.3, 0.5, 0.7, 0.9};

GMRESTag tag;
tag.do_log() = true;
DynamicVector<double> x0(N,0);
std::size_t n = N;
auto x2 = solve(A,b,x0,tag,n);

auto error = norm(x1 - x2);

if (error < EPSILON){
std::cout << " Pass test of GMRES" << std::endl;
return EXIT_SUCCESS;
} else{
std::cout << "Fail test of GMRES" << std::endl;
return EXIT_FAILURE;
}

}


49 changes: 49 additions & 0 deletions tests/main_Lanczos.cpp
@@ -0,0 +1,49 @@

// Copyright (c) 2017 Tyler Olsen
// 2018 Patrick Diehl
// 2019 Nanmiao Wu
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "BlazeIterative.hpp"
#include <iostream>
#include <cstdlib>

using namespace blaze;
using namespace blaze::iterative;

int main() {

// Test Lanczos

std::size_t N = 6;
DynamicMatrix<double,columnMajor> A(N,N,0);
band<0>(A) = {0, 1, 2, 3, 4, 100000};

DynamicVector<double> b(N, 1);
DynamicVector<complex<double>,columnVector> w(N);
DynamicMatrix<complex<double>,rowMajor> V(N,N);
DynamicVector<complex<double>,columnVector> w1(N);
eigen(A,w,V);
w1 = {w[5],w[0],w[4],w[1],w[2],w[3]};

std::size_t n = N;
LanczosTag tag;
DynamicVector<complex<double>,columnVector> w2(n);
DynamicMatrix<complex<double>,rowMajor> V2(n,n);
auto res = solve(A,b,tag,n);
eigen(res.second,w2,V2);

auto error = real(norm(w1 - w2));

if (error < EPSILON){
std::cout << " Pass test of Lanczos" << std::endl;
return EXIT_SUCCESS;
} else{
std::cout << "Fail test of Lanczos" << std::endl;
return EXIT_FAILURE;
}

}


43 changes: 43 additions & 0 deletions tests/main_PreconditionedBiCGSTAB.cpp
@@ -0,0 +1,43 @@
// Copyright (c) 2017 Tyler Olsen
// 2018 Patrick Diehl
// 2019 Nanmiao Wu
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "BlazeIterative.hpp"
#include <iostream>
#include <cstdlib>

using namespace blaze;
using namespace blaze::iterative;

int main() {

// Test Preconditioned BiCGSTAB

std::size_t N = 10;
DynamicMatrix<double,false> A(N,N, 0.0);
DynamicVector<double> b(N, 0.0);
DynamicVector<double> x1(N, 0.5);
for(int i=0; i<N; ++i) {
A(i,i) = 2.0;
b[i] = 1.0*(1+i);
x1[i] += x1[i-1];
}

PreconditionBiCGSTABTag tag;
tag.do_log() = true;
auto x2 = solve(A,b,tag, "Cholesky");

auto error = norm(x1 - x2);

if (error < EPSILON){
std::cout << " Pass test of Preconditioned BiCGSTAB" << std::endl;
return EXIT_SUCCESS;
} else{
std::cout << "Fail test of Preconditioned BiCGSTAB" << std::endl;
return EXIT_FAILURE;
}

}

42 changes: 42 additions & 0 deletions tests/main_PreconditionedCG.cpp
@@ -0,0 +1,42 @@
// Copyright (c) 2017 Tyler Olsen
// 2018 Patrick Diehl
// 2019 Nanmiao Wu
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "BlazeIterative.hpp"
#include <iostream>
#include <cstdlib>

using namespace blaze;
using namespace blaze::iterative;

int main() {

// Test Preconditioned CG

std::size_t N = 10;
DynamicMatrix<double,false> A(N,N, 0.0);
DynamicVector<double> b(N, 0.0);
DynamicVector<double> x1(N, 0.5);
for(int i=0; i<N; ++i) {
A(i,i) = 2.0;
b[i] = 1.0*(1+i);
x1[i] += x1[i-1];
}

PreconditionCGTag tag;
tag.do_log() = true;
auto x2 = solve(A,b,tag, "incomplete Cholesky factorization");

auto error = norm(x1 - x2);

if (error < EPSILON){
std::cout << " Pass test of Preconditioned CG" << std::endl;
return EXIT_SUCCESS;
} else{
std::cout << "Fail test of Preconditioned CG" << std::endl;
return EXIT_FAILURE;
}

}