TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
CSR_Builder.h
1/****************************************************************************
2* Copyright (c) 2022, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15#ifndef CSR_Builder_included
16#define CSR_Builder_included
17
18/*! @brief This class is a helper to build CSR stored matrices
19 *
20 */
22{
23public:
25 initialize(int nb_lines);
26 inline void add_coeff(int line, int column, double coeff);
28protected:
30 // List data contains groups of 2 integers like this:
31 // column, next_index, column, next_index, ...
32 // column is the parameter passed to add_coeff
33 // next_index is the group number of the next coefficient on the same line
34 // or -1 if this is the last coefficient.
35 // if not -1, the next group is
36 // column = list_data_[2*next_index]
37 // next_index = list_data_[2*next_index+1]
38 // the coefficient is at coeff_data_[next_index]
39 // The first coefficient on line "l" is at list_head[l]
40 ArrOfInt list_head_;
41 // list_tail_[l] = -1 if the line "l" is empty, otherwise, gives the index
42 // of the last "next_index" on the line (we have list_data_[2*tail+1]=-1)
43 ArrOfInt list_tail_;
44 ArrOfInt list_data_;
45 ArrOfDouble coeff_data_;
46};
47
48inline void CSR_Builder::add_coeff(int line, int column, double coeff)
49{
50 assert(line >= 0 && line < nb_lines_);
51
52 // We put the new coefficient here:
53 const int n = coeff_data_.size_array();
54 assert(list_data_.size_array() == n*2); // mixing calls with and without "coeff" is forbidden
55 list_data_.resize_array(n*2+2);
56 list_data_[n*2] = column;
57 list_data_[n*2+1] = -1; // new end of list
58 coeff_data_.append_array(coeff);
59
60 const int tail = list_tail_[line];
61 if (tail < 0)
62 {
63 // this line is empty, initialize list_head_
64 list_head_[line] = n;
65 }
66 else
67 {
68 // update the last next_index of the line:
69 list_data_[2*tail+1] = n;
70 }
71 list_tail_[line] = n;
72}
73
74inline void CSR_Builder::add_coeff(int line, int column)
75{
76 assert(line >= 0 && line < nb_lines_);
77 assert(coeff_data_.size_array() == 0); // mixing calls with and without "coeff" is forbidden
78 // We put the new coefficient here:
79 const int n = list_data_.size_array() / 2;
81 list_data_[n*2] = column;
82 list_data_[n*2+1] = -1; // new end of list
83
84 const int tail = list_tail_[line];
85 if (tail < 0)
86 {
87 // this line is empty, initialize list_head_
88 list_head_[line] = n;
89 }
90 else
91 {
92 // update the last next_index of the line:
93 list_data_[2*tail+1] = n;
94 }
95 list_tail_[line] = n;
96}
97#endif
ArrOfInt list_head_
Definition CSR_Builder.h:40
ArrOfDouble coeff_data_
Definition CSR_Builder.h:45
build_matrix(Matrice_Bloc &)
ArrOfInt list_tail_
Definition CSR_Builder.h:43
ArrOfInt list_data_
Definition CSR_Builder.h:44
void add_coeff(int line, int column, double coeff)
Definition CSR_Builder.h:48
initialize(int nb_lines)
_SIZE_ size_array() const
void resize_array(_SIZE_ new_size, RESIZE_OPTIONS opt=RESIZE_OPTIONS::COPY_INIT)