YANE-Framework Tutorial 1.1.0

# examples/linearequation/src/Problems/electricalnetwork.cpp

```00001 /***************************************************************************
00002  *                                                                         *
00003  * Copyright (C) 2011 by www.nonlinearmpc.com                              *
00004  *                                                                         *
00005  * Authors:                                                                *
00006  *  Juergen Pannek <juergen.pannek@googlemail.com>                         *
00007  *                                                                         *
00008  ***************************************************************************/
00009
00010 #include "electricalnetwork.h"
00011
00012 using namespace std;
00013
00014 #include "libexport.h"
00015
00016 LIB_EXPORT yane::EqSys::ElectricalNetwork::ElectricalNetwork ( void ) :
00017         LinearEquationProblem ( )
00018 {
00019 }
00020
00021 LIB_EXPORT yane::EqSys::ElectricalNetwork::~ElectricalNetwork ( void )
00022 {
00023 }
00024
00025 LIB_EXPORT void yane::EqSys::ElectricalNetwork::setupMatrix ( int N )
00026 {
00027         _dimensionRows = _dimensionColumns = N * N;
00028         try
00029         {
00030                 _A = new double * [ _dimensionRows ];
00031                 for ( int i = 0; i < _dimensionRows; i++ )
00032                 {
00033                         _A [ i ] = new double [ _dimensionColumns ];
00034                 }
00035         }
00036         catch ( std::bad_alloc )
00037         {
00038                 throw yane::Utils::MemoryException ( __DEBUGMESS (
00039                         "Allocation of matrix failed" ) );
00040         }
00041
00042         int index1, index2;
00043
00044         for ( int i = 0; i < N; i++ )
00045         {
00046                 index1 = i * N;
00047                 for ( int j = 0; j < N; j++ )
00048                 {
00049                         _A [ index1 + j ] [ index1 + j ] = 4.;
00050                         if ( j > 0 )
00051                         {
00052                                 _A [ index1 + j ] [ index1 + j - 1 ] = - 1.;
00053                         }
00054                         if ( j < N - 1 )
00055                         {
00056                                 _A [ index1 + j ] [ index1 + j + 1 ] = - 1.;
00057                         }
00058                 }
00059                 if ( i > 0 )
00060                 {
00061                         index2 = ( i - 1 ) * N;
00062                         for ( int j = 0; j < N; j++ )
00063                         {
00064                                 _A [ index2 + j ] [ index1 + j ] = - 1.;
00065                         }
00066                 }
00067                 if ( i < N - 1 )
00068                 {
00069                         index2 = ( i + 1 ) * N;
00070                         for ( int j = 0; j < N; j++ )
00071                         {
00072                                 _A [ index2 + j ] [ index1 + j ] = - 1.;
00073                         }
00074                 }
00075         }
00076 }
00077
00078 LIB_EXPORT void yane::EqSys::ElectricalNetwork::setupVector ( int N )
00079 {
00080         _dimensionB = N * N;
00081
00082         try
00083         {
00084                 _b = new double [ _dimensionB ];
00085         }
00086         catch ( std::bad_alloc )
00087         {
00088                 throw yane::Utils::MemoryException ( __DEBUGMESS (
00089                         "Allocation of right hand side failed" ) );
00090         }
00091
00092         for ( int i = 0; i < N; i++ )
00093         {
00094                 _b [ i ] += pow ( ( - N + 2. * i + 1. ) / N, 2. );
00095                 _b [ ( i + 1 ) * N - 1 ] += pow ( ( - N + 2. * i + 1. ) / N, 2. );
00096                 _b [ ( N - 1 ) * N + i ] += pow ( ( - N + 2. * i + 1. ) / N, 2. );
00097                 _b [ i * N ] += pow ( ( - N + 2. * i + 1. ) / N, 2. );
00098         }
00099 }
```