|
| 1 | + |
| 2 | +/* |
| 3 | + * Author |
| 4 | + * David Blom, TU Delft. All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | + #include "DataStorage.H" |
| 8 | + |
| 9 | + namespace sdc |
| 10 | + { |
| 11 | + DataStorage::DataStorage( |
| 12 | + std::shared_ptr<fsi::quadrature::IQuadrature<scalar> > quadrature, |
| 13 | + int N ) |
| 14 | + : |
| 15 | + quadrature( quadrature ), |
| 16 | + F( quadrature->get_num_nodes(), N ), |
| 17 | + solStages( quadrature->get_num_nodes(), N ) |
| 18 | + { |
| 19 | + assert( quadrature ); |
| 20 | + } |
| 21 | + |
| 22 | + DataStorage::~DataStorage() |
| 23 | + { |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + const fsi::matrix DataStorage::integrate( const std::vector<scalar> & nodes, scalar dt ) const |
| 28 | + { |
| 29 | + std::vector<scalar> quadratureNodes = quadrature->get_nodes(); |
| 30 | + |
| 31 | + if ( not quadrature->left_is_node() ) |
| 32 | + quadratureNodes.erase( quadratureNodes.begin() ); |
| 33 | + |
| 34 | + fsi::quadrature::Matrix<scalar> q_matrix = fsi::quadrature::compute_q_matrix( quadratureNodes, nodes ); |
| 35 | + |
| 36 | + if ( not quadrature->left_is_node() ) |
| 37 | + { |
| 38 | + fsi::quadrature::Matrix<scalar> q_mat2( q_matrix.rows(), q_matrix.cols() + 1 ); |
| 39 | + q_mat2.setZero(); |
| 40 | + q_mat2.rightCols( q_matrix.cols() ) = q_matrix; |
| 41 | + q_matrix = q_mat2; |
| 42 | + } |
| 43 | + |
| 44 | + fsi::quadrature::Matrix<scalar> data = dt * q_matrix * getFunctions(); |
| 45 | + |
| 46 | + for ( int i = 0; i < data.rows(); i++ ) |
| 47 | + data.row( i ) += getSolutions().row( 0 ); |
| 48 | + |
| 49 | + return data; |
| 50 | + } |
| 51 | + |
| 52 | + template<typename scalar> |
| 53 | + static fsi::quadrature::Matrix<scalar> compute_interpolation_matrix( |
| 54 | + const std::vector<scalar> & from, |
| 55 | + const std::vector<scalar> & to |
| 56 | + ) |
| 57 | + { |
| 58 | + const size_t to_size = to.size(); |
| 59 | + const size_t from_size = from.size(); |
| 60 | + assert( to_size >= 1 && from_size >= 1 ); |
| 61 | + |
| 62 | + fsi::quadrature::Matrix<scalar> q_mat = fsi::quadrature::Matrix<scalar>::Zero( to_size, from_size ); |
| 63 | + |
| 64 | + for ( size_t m = 0; m < from_size; ++m ) |
| 65 | + { |
| 66 | + fsi::quadrature::Polynomial<scalar> p = fsi::quadrature::build_polynomial( m, from ); |
| 67 | + |
| 68 | + auto den = p.evaluate( from[m] ); |
| 69 | + |
| 70 | + for ( size_t j = 0; j < to_size; ++j ) |
| 71 | + { |
| 72 | + q_mat( j, m ) = p.evaluate( to[j] ) / den; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return q_mat; |
| 77 | + } |
| 78 | + |
| 79 | + const fsi::matrix DataStorage::interpolate( |
| 80 | + const fsi::matrix functions, |
| 81 | + const std::vector<scalar> & nodes ) const |
| 82 | + { |
| 83 | + std::vector<scalar> quadratureNodes = quadrature->get_nodes(); |
| 84 | + fsi::quadrature::Matrix<scalar> q_matrix = compute_interpolation_matrix( quadratureNodes, nodes ); |
| 85 | + |
| 86 | + return q_matrix * functions; |
| 87 | + } |
| 88 | + |
| 89 | + const fsi::matrix & DataStorage::getFunctions() const |
| 90 | + { |
| 91 | + assert( F.rows() > 0 ); |
| 92 | + assert( F.cols() > 0 ); |
| 93 | + return F; |
| 94 | + } |
| 95 | + |
| 96 | + const fsi::matrix & DataStorage::getSolutions() const |
| 97 | + { |
| 98 | + assert( solStages.rows() > 0 ); |
| 99 | + return solStages; |
| 100 | + } |
| 101 | + |
| 102 | + const fsi::vector DataStorage::getFunction( int substep ) const |
| 103 | + { |
| 104 | + assert( substep <= F.rows() ); |
| 105 | + assert( F.cols() > 0 ); |
| 106 | + assert( F.rows() > 0 ); |
| 107 | + return F.row( substep ); |
| 108 | + } |
| 109 | + |
| 110 | + const fsi::vector DataStorage::getSolution( int substep ) const |
| 111 | + { |
| 112 | + assert( substep <= solStages.rows() ); |
| 113 | + assert( solStages.cols() > 0 ); |
| 114 | + assert( solStages.rows() > 0 ); |
| 115 | + return solStages.row( substep ); |
| 116 | + } |
| 117 | + |
| 118 | + const fsi::vector DataStorage::getLastSolution() const |
| 119 | + { |
| 120 | + return getSolution( quadrature->get_num_nodes() - 1 ); |
| 121 | + } |
| 122 | + |
| 123 | + void DataStorage::initialize( int k, int N ) |
| 124 | + { |
| 125 | + assert( N > 0 ); |
| 126 | + assert( k >= 2 ); |
| 127 | + F.resize( k, N ); |
| 128 | + solStages.resize( k, N ); |
| 129 | + F.setZero(); |
| 130 | + solStages.setZero(); |
| 131 | + } |
| 132 | + |
| 133 | + void DataStorage::storeFunction( const fsi::vector & f, int substep ) |
| 134 | + { |
| 135 | + assert( f.rows() == F.cols() ); |
| 136 | + assert( substep <= F.rows() ); |
| 137 | + assert( F.cols() > 0 ); |
| 138 | + assert( F.rows() > 0 ); |
| 139 | + F.row( substep ) = f; |
| 140 | + } |
| 141 | + |
| 142 | + void DataStorage::storeSolution( const fsi::vector & sol, int substep ) |
| 143 | + { |
| 144 | + assert( sol.rows() == solStages.cols() ); |
| 145 | + assert( substep <= solStages.rows() ); |
| 146 | + assert( solStages.cols() > 0 ); |
| 147 | + assert( solStages.rows() > 0 ); |
| 148 | + solStages.row( substep ) = sol; |
| 149 | + } |
| 150 | + } |
0 commit comments