7.1
general documentation
cs_cdo_anderson.h
Go to the documentation of this file.
1 #ifndef __CS_CDO_ANDERSON_H__
2 #define __CS_CDO_ANDERSON_H__
3 
4 /*============================================================================
5  * Build an algebraic CDO face-based system for the Navier-Stokes equations
6  * and solved it as one block (monolithic approach of the velocity-pressure
7  * coupling), Anderson
8  *============================================================================*/
9 
10 /*
11  This file is part of Code_Saturne, a general-purpose CFD tool.
12 
13  Copyright (C) 1998-2021 EDF S.A.
14 
15  This program is free software; you can redistribute it and/or modify it under
16  the terms of the GNU General Public License as published by the Free Software
17  Foundation; either version 2 of the License, or (at your option) any later
18  version.
19 
20  This program is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
23  details.
24 
25  You should have received a copy of the GNU General Public License along with
26  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
27  Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 */
29 
30 /*----------------------------------------------------------------------------*/
31 
32 #include "cs_defs.h"
33 
34 /*----------------------------------------------------------------------------
35  * Standard C library headers
36  *----------------------------------------------------------------------------*/
37 
38 /*----------------------------------------------------------------------------
39  * Local headers
40  *----------------------------------------------------------------------------*/
41 
42 /*----------------------------------------------------------------------------*/
43 
45 
46 /*============================================================================
47  * Macro definitions
48  *============================================================================*/
49 
50 /*============================================================================
51  * Type definitions
52  *============================================================================*/
53 
54 // typedef struct {
55 //
56 // int mMax_aa;
57 // int AAStart;
58 // /* Set of tolerances of matrix conditioning number */
59 // double droptol_;
60 // /* Set the relaxation parameter */
61 // double beta_;
62 // /* Number of Anderson accelerations */
63 // int n_aa_iter;
64 //
65 // bool aa_activated;
66 //
67 // cs_real_t fval_;
68 // cs_real_t gold_;
69 // cs_real_t fold_;
70 // cs_real_t df_;
71 //
72 //
73 // Eigen::MatrixXd R_;
74 //
75 //
76 // } cs_cdo_anderson_t;
77 
78 static const int mMax_aa = 4;
79 static const int AAStart = 0;
80 static const int aa_activated = 0;
81 static const double droptol_ = 50;
82 static const double beta_ = 0.0;
84 int mAA_;
92 cs_sdm_t *R_;
95 
96 /*============================================================================
97  * Static inline public function prototypes
98  *============================================================================*/
99 
100 /*============================================================================
101  * Public function prototypes
102  *============================================================================*/
103 
104 /*----------------------------------------------------------------------------*/
105 inline void aa_init(const int mMax,
106  const cs_lnum_t n_faces) {
107 
108  n_aa_iter = 0;
109  mAA_ = 0;
110  fval_ = NULL;
111  fold_ = NULL;
112  gold_ = NULL;
113  df_ = NULL;
114  DG_ = NULL;
115  Q_ = NULL;
116  R_ = NULL;
117  gamma_ = NULL;
118  tempGamma_ = NULL;
119  gval = NULL;
120 
121  BFT_MALLOC(fval_, n_faces, cs_real_t);
122  BFT_MALLOC(fold_, n_faces, cs_real_t);
123  BFT_MALLOC(gold_, n_faces, cs_real_t);
124  BFT_MALLOC(df_, n_faces, cs_real_t);
125  BFT_MALLOC(DG_, n_faces*mMax, cs_real_t);
126  BFT_MALLOC(Q_, n_faces*mMax, cs_real_t);
127  R_ = cs_sdm_square_create(mMax);
128  cs_sdm_square_init(mMax, R_);
129  BFT_MALLOC(gamma_, mMax, cs_real_t);
131  cs_log_printf(CS_LOG_DEFAULT, " anderson %d %d \n", mMax, n_faces);
132  cs_log_printf(CS_LOG_DEFAULT, " anderson R_ %d %lf \n", R_->n_rows, R_->val[1]);
133 }
134 
135 /*----------------------------------------------------------------------------*/
136 
137 inline void aa_term() {
138 
139  BFT_FREE(fval_);
140  BFT_FREE(fold_);
141  BFT_FREE(gold_);
142  BFT_FREE(df_);
143  BFT_FREE(DG_);
144  BFT_FREE(Q_);
145  R_ = cs_sdm_free(R_);
146  BFT_FREE(gamma_);
148 }
149 
150 /*----------------------------------------------------------------------------*/
151 
152  inline void qrdelete(cs_real_t *Q, cs_sdm_t *R, int n_faces, int mMax){
153 
154  int n_rows = R->n_rows;
155  for (int i=0 ; i<mMax-1 ; i++)
156  {
157  cs_log_printf(CS_LOG_DEFAULT, " n_rows %d \n", n_rows);
158  //int n_cols = R->n_cols;
159  assert(R->n_cols == R->n_rows);
160  const double temp = sqrt(R->val[i*n_rows+i+1]*R->val[i*n_rows+i+1]+R->val[(i+1)*n_rows+i+1]*R->val[(i+1)*n_rows+i+1]);
161 
162  const double c = R->val[i*n_rows+i+1]/temp;
163  const double s = R->val[(i+1)*n_rows+i+1]/temp;
164 
165  R->val[i*n_rows+i+1] = temp;
166  R->val[(i+1)*n_rows+i+1]=0.0;
167  if (i<mMax-2)
168  { //diff % matlab
169  for (int j=i+2 ; j<mMax ; j++)
170  {
171  const double temp0 = c*R->val[i*n_rows+j]+s*R->val[(i+1)*n_rows+j];
172  R->val[(i+1)*n_rows+j] = -s*R->val[i*n_rows+j]+c*R->val[(i+1)*n_rows+j];
173  R->val[i*n_rows+j] = temp0;
174  }
175  }
176  cs_real_t *Q_i = Q + i*n_faces;
177  cs_real_t *Q_ip1 = Q + (i+1)*n_faces;
178  for (int l=0 ; l<n_faces ; l++){
179  const double temp1 = c*Q_i[l] + s*Q_ip1[l];
180  Q_ip1[l] = -s*Q_i[l] + c*Q_ip1[l];
181  Q_i[l] = temp1;
182  }
183  }
184 
185  cs_sdm_t *temp2 = cs_sdm_square_create(R->n_rows) ;
186  cs_sdm_square_init(R->n_rows, temp2);
187 
188  //diff % matlab the Q and R matrices are not resized
189  // A block version should be used
190  for (int i=0 ; i<mMax-1 ; i++){
191  for (int j=i ; j<mMax-1 ; j++){
192  temp2->val[i*n_rows+j] = R->val[i*n_rows+j+1];
193  }
194  }
195 
196  cs_sdm_copy(R, temp2);
197  cs_real_t *Q_imax = Q + (mMax-1)*n_faces;
198  for (int i=0 ; i<n_faces ; i++){
199  Q_imax[i] = 0.0;
200  }
201 }
202 
203 /*----------------------------------------------------------------------------*/
204 
205 // inline void shiftColumns( void ){
206 // VECTOR * first=data_[0];
207 // for (int j=0 ; j<colNumber_-1 ; j++) data_[j]=data_[j+1];
208 // data_[colNumber_-1]=first;
209 // colNumber_--;
210 // }
211 
212 /*----------------------------------------------------------------------------*/
213 
215 
216 #endif /* __CS_CDO_ANDERSON_H__ */
int cs_log_printf(cs_log_t log, const char *format,...)
Print log info to a given log type.
Definition: cs_log.c:501
cs_real_t * DG_
Definition: cs_cdo_anderson.h:90
#define BEGIN_C_DECLS
Definition: cs_defs.h:510
cs_real_t * gamma_
Definition: cs_cdo_anderson.h:93
Definition: cs_log.h:50
void aa_init(const int mMax, const cs_lnum_t n_faces)
Definition: cs_cdo_anderson.h:105
cs_sdm_t * cs_sdm_square_create(int n_max_rows)
Allocate and initialize a cs_sdm_t structure Case of a square matrix.
Definition: cs_sdm.c:157
double cs_real_t
Floating-point value.
Definition: cs_defs.h:322
#define BFT_MALLOC(_ptr, _ni, _type)
Allocate memory for _ni elements of type _type.
Definition: bft_mem.h:62
void qrdelete(cs_real_t *Q, cs_sdm_t *R, int n_faces, int mMax)
Definition: cs_cdo_anderson.h:152
int mAA_
Definition: cs_cdo_anderson.h:84
cs_real_t * Q_
Definition: cs_cdo_anderson.h:91
cs_real_t * fold_
Definition: cs_cdo_anderson.h:87
cs_real_t * tempGamma_
Definition: cs_cdo_anderson.h:94
void aa_term()
Definition: cs_cdo_anderson.h:137
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:316
#define END_C_DECLS
Definition: cs_defs.h:511
cs_sdm_t * R_
Definition: cs_cdo_anderson.h:92
cs_real_t * df_
Definition: cs_cdo_anderson.h:89
cs_sdm_t * cs_sdm_free(cs_sdm_t *mat)
Free a cs_sdm_t structure.
Definition: cs_sdm.c:331
cs_real_t * gval
Definition: cs_cdo_anderson.h:86
cs_real_t * fval_
Definition: cs_cdo_anderson.h:85
int n_aa_iter
Definition: cs_cdo_anderson.h:83
cs_real_t * gold_
Definition: cs_cdo_anderson.h:88
#define BFT_FREE(_ptr)
Free allocated memory.
Definition: bft_mem.h:101