test_optimizer.cpp 1.61 KB
Newer Older
1
2
3
#include <numeric>

#include "catch.hpp"
4
#include "common/config.h"
5
6
7
8
9
#include "optimizers/optimizerbase.h"
#include "quadratures/quadraturebase.h"
#include "solvers/sphericalharmonics.h"

TEST_CASE( "Test the Newton Optimizer", "[optimizers]" ) {
Steffen Schotthöfer's avatar
Steffen Schotthöfer committed
10
    std::string filename = std::string( TESTS_PATH ) + "input/unit_tests/optimizers/unit_optimizerNewton.cfg";
11
12
13
14
15
16
17
18

    // Load Settings from File
    Config* config = new Config( filename );

    // Get Basis
    SphericalHarmonics basis( config->GetMaxMomentDegree() );

    // Get Quadrature
19
    QuadratureBase* quad = QuadratureBase::CreateQuadrature( config );
20
21
22
23
24
25

    // Get Optimizer (Newton)
    OptimizerBase* optimizer = OptimizerBase::Create( config );

    // Get dummy Moment Vector
    unsigned nTotalEntries = basis.GlobalIdxBasis( config->GetMaxMomentDegree(), config->GetMaxMomentDegree() ) + 1;    // = 4
26
27
28
    Vector u( nTotalEntries, -1.5 );
    u[1] = 0.0;
    u[2] = 1.0;
29
30

    // Get inital guess for solution
31
    Vector alpha( nTotalEntries, 27.0 );
32
33

    // Get Moments
34
35
36
    config->SetNQuadPoints( quad->GetNq() );

    VectorVector moments = VectorVector( quad->GetNq() );
37
38
39
40
41
42
43
44
45
    double my, phi;
    VectorVector quadPointsSphere = quad->GetPointsSphere();
    for( unsigned idx_quad = 0; idx_quad < config->GetNQuadPoints(); idx_quad++ ) {
        my  = quadPointsSphere[idx_quad][0];
        phi = quadPointsSphere[idx_quad][1];

        moments[idx_quad] = basis.ComputeSphericalBasis( my, phi );
    }

46
    // Solve
47
48
    optimizer->Solve( alpha, u, moments );

49
    REQUIRE( std::fabs( norm( alpha - u ) ) < config->GetNewtonOptimizerEpsilon() );    // alpha = u for quadratic entropy
50
}