File: convergence_table.cc

package info (click to toggle)
deal.ii 9.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 181,876 kB
  • sloc: cpp: 265,739; ansic: 52,054; python: 1,507; perl: 645; sh: 506; xml: 437; makefile: 73
file content (242 lines) | stat: -rw-r--r-- 7,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// ---------------------------------------------------------------------
//
// Copyright (C) 1999 - 2017 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------

#include <deal.II/base/convergence_table.h>
#include <cmath>

DEAL_II_NAMESPACE_OPEN

void ConvergenceTable::evaluate_convergence_rates(const std::string &data_column_key,
                                                  const std::string &reference_column_key,
                                                  const RateMode     rate_mode,
                                                  const unsigned int dim)
{
  Assert(columns.count(data_column_key),
         ExcColumnNotExistent(data_column_key));
  Assert(columns.count(reference_column_key),
         ExcColumnNotExistent(reference_column_key));

  if (rate_mode == none)
    return;

  // reset the auto fill mode flag since we are going to fill columns from
  // the top that don't yet exist
  set_auto_fill_mode (false);

  std::vector<internal::TableEntry> &entries=columns[data_column_key].entries;
  std::vector<internal::TableEntry> &ref_entries=columns[reference_column_key].entries;
  std::string rate_key = data_column_key+"...";

  const unsigned int n = entries.size();
  const unsigned int n_ref = ref_entries.size();
  Assert(n == n_ref, ExcDimensionMismatch(n, n_ref));

  std::vector<double> values(n);
  std::vector<double> ref_values(n_ref);

  for (unsigned int i=0; i<n; ++i)
    {
      values[i]     = entries[i].get_numeric_value();
      ref_values[i] = ref_entries[i].get_numeric_value();
    }

  unsigned int no_rate_entries = 0;

  switch (rate_mode)
    {
    // case none: already considered above
    case reduction_rate:
      rate_key += "red.rate";
      no_rate_entries = columns[rate_key].entries.size();
      // Calculate all missing rate values:
      for (unsigned int i = no_rate_entries; i<n; ++i)
        {
          if (i == 0)
            {
              // no value available for the first row
              add_value(rate_key, std::string("-"));
            }
          else
            {
              add_value(rate_key, values[i-1]/values[i] *
                        ref_values[i]/ref_values[i-1]);
            }
        }
      break;
    case reduction_rate_log2:
      rate_key += "red.rate.log2";
      no_rate_entries = columns[rate_key].entries.size();
      // Calculate all missing rate values:
      for (unsigned int i = no_rate_entries; i<n; ++i)
        {
          if (i == 0)
            {
              // no value available for the first row
              add_value(rate_key, std::string("-"));
            }
          else
            {
              add_value(rate_key, dim*std::log(std::fabs(values[i-1]/values[i])) /
                        std::log(std::fabs(ref_values[i]/ref_values[i-1])));
            }
        }
      break;
    default:
      AssertThrow(false, ExcNotImplemented());
    }

  Assert(columns.count(rate_key), ExcInternalError());
  columns[rate_key].flag = 1;
  set_precision(rate_key, 2);

  const std::string &superkey = data_column_key;
  if (!supercolumns.count(superkey))
    {
      add_column_to_supercolumn(data_column_key, superkey);
      set_tex_supercaption(superkey, columns[data_column_key].tex_caption);
    }

  // only add rate_key to the supercolumn once
  if (no_rate_entries == 0)
    {
      add_column_to_supercolumn(rate_key, superkey);
    }

}



void
ConvergenceTable::evaluate_convergence_rates(const std::string &data_column_key,
                                             const RateMode     rate_mode)
{
  Assert(columns.count(data_column_key), ExcColumnNotExistent(data_column_key));

  // reset the auto fill mode flag since we are going to fill columns from
  // the top that don't yet exist
  set_auto_fill_mode (false);

  std::vector<internal::TableEntry> &entries = columns[data_column_key].entries;
  std::string rate_key=data_column_key+"...";

  const unsigned int n=entries.size();

  std::vector<double> values(n);
  for (unsigned int i=0; i<n; ++i)
    values[i] = entries[i].get_numeric_value();

  unsigned int no_rate_entries = 0;

  switch (rate_mode)
    {
    case none:
      break;

    case reduction_rate:
      rate_key+="red.rate";
      no_rate_entries = columns[rate_key].entries.size();
      // Calculate all missing rate values:
      for (unsigned int i = no_rate_entries; i<n; ++i)
        {
          if (i == 0)
            {
              // no value available for the first row
              add_value(rate_key, std::string("-"));
            }
          else
            {
              add_value(rate_key, values[i-1]/values[i]);
            }
        }
      break;

    case reduction_rate_log2:
      rate_key+="red.rate.log2";
      no_rate_entries = columns[rate_key].entries.size();
      // Calculate all missing rate values:
      for (unsigned int i = no_rate_entries; i<n; ++i)
        {
          if (i == 0)
            {
              // no value available for the first row
              add_value(rate_key, std::string("-"));
            }
          else
            {
              add_value(rate_key, std::log(std::fabs(values[i-1]/values[i]))/std::log(2.0));
            }
        }
      break;

    default:
      AssertThrow(false, ExcNotImplemented());
    }

  Assert(columns.count(rate_key), ExcInternalError());
  columns[rate_key].flag=1;
  set_precision(rate_key, 2);

  // set the superkey equal to the key
  const std::string &superkey=data_column_key;
  // and set the tex caption of the supercolumn to the tex caption of the
  // data_column.
  if (!supercolumns.count(superkey))
    {
      add_column_to_supercolumn(data_column_key, superkey);
      set_tex_supercaption(superkey, columns[data_column_key].tex_caption);
    }

  // only add rate_key to the supercolumn once
  if (no_rate_entries == 0)
    {
      add_column_to_supercolumn(rate_key, superkey);
    }
}



void
ConvergenceTable::omit_column_from_convergence_rate_evaluation(const std::string &key)
{
  Assert(columns.count(key), ExcColumnNotExistent(key));

  const std::map<std::string, Column>::iterator col_iter=columns.find(key);
  col_iter->second.flag=1;
}



void
ConvergenceTable::evaluate_all_convergence_rates(const std::string &reference_column_key,
                                                 const RateMode rate_mode)
{
  for (std::map<std::string, Column>::const_iterator col_iter=columns.begin();
       col_iter!=columns.end(); ++col_iter)
    if (!col_iter->second.flag)
      evaluate_convergence_rates(col_iter->first, reference_column_key, rate_mode);
}



void
ConvergenceTable::evaluate_all_convergence_rates(const RateMode rate_mode)
{
  for (std::map<std::string, Column>::const_iterator col_iter=columns.begin();
       col_iter!=columns.end(); ++col_iter)
    if (!col_iter->second.flag)
      evaluate_convergence_rates(col_iter->first, rate_mode);
}

DEAL_II_NAMESPACE_CLOSE