| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2010 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
|
| 16 |
#ifndef dealii_mesh_worker_output_h |
| 17 |
#define dealii_mesh_worker_output_h |
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/mg_level_object.h> |
| 22 |
#include <deal.II/base/observer_pointer.h> |
| 23 |
#include <deal.II/base/utilities.h> |
| 24 |
|
| 25 |
#include <deal.II/lac/block_vector.h> |
| 26 |
|
| 27 |
#include <deal.II/meshworker/dof_info.h> |
| 28 |
|
| 29 |
|
| 30 |
DEAL_II_NAMESPACE_OPEN |
| 31 |
|
| 32 |
namespace MeshWorker |
| 33 |
{ |
| 34 |
namespace Assembler |
| 35 |
{ |
| 36 |
/** |
| 37 |
* A class that, instead of assembling into a matrix or vector, outputs |
| 38 |
* the results on a cell to a gnuplot patch. |
| 39 |
* |
| 40 |
* This assembler expects that LocalResults contains quadrature values set |
| 41 |
* with LocalResults::quadrature_value(). When it is initialized with the |
| 42 |
* number of quadrature points in a single (!) space direction and the |
| 43 |
* number of data fields to be displayed, it initializes LocalResults |
| 44 |
* automatically. The number of data fields in local results will be |
| 45 |
* increased by dim in order to accommodate for the coordinates of the |
| 46 |
* data points. |
| 47 |
* |
| 48 |
* While data slots for the space coordinates are allocated automatically, |
| 49 |
* these coordinates are not entered. It is up to the user to enter the |
| 50 |
* coordinates in the first dim data entries at every point. This adds the |
| 51 |
* flexibility to output transformed coordinates or even something |
| 52 |
* completely different. |
| 53 |
* |
| 54 |
* @note In the current implementation, only cell data can be written. |
| 55 |
*/ |
| 56 |
class GnuplotPatch |
| 57 |
{ |
| 58 |
public: |
| 59 |
/** |
| 60 |
* Constructor. |
| 61 |
*/ |
| 62 |
GnuplotPatch(); |
| 63 |
|
| 64 |
/** |
| 65 |
* Initialize for writing <i>n</i> data vectors. The number of points is |
| 66 |
* the number of quadrature points in a single direction in a tensor |
| 67 |
* product formula. It must match the number in the actual Quadrature |
| 68 |
* used to create the patches. The total number of data vectors produced |
| 69 |
* is <tt>n+dim</tt> and the first dim should be the space coordinates |
| 70 |
* of the points. Nevertheless, it is up to the user to set these values |
| 71 |
* to whatever is desired. |
| 72 |
*/ |
| 73 |
void |
| 74 |
initialize(const unsigned int n_points, const unsigned int n_vectors); |
| 75 |
|
| 76 |
/** |
| 77 |
* Set the stream #os to which data is written. If no stream is selected |
| 78 |
* with this function, data goes to @p deallog. |
| 79 |
*/ |
| 80 |
void |
| 81 |
initialize_stream(std::ostream &stream); |
| 82 |
|
| 83 |
/** |
| 84 |
* Initialize the local data in the DoFInfo object used later for |
| 85 |
* assembling. |
| 86 |
* |
| 87 |
* The @p info object refers to a cell if <code>!face</code>, or else to an |
| 88 |
* interior or boundary face. |
| 89 |
*/ |
| 90 |
template <int dim> |
| 91 |
void |
| 92 |
initialize_info(DoFInfo<dim> &info, bool face); |
| 93 |
|
| 94 |
/** |
| 95 |
* Write the patch to the output stream. |
| 96 |
*/ |
| 97 |
template <int dim> |
| 98 |
void |
| 99 |
assemble(const DoFInfo<dim> &info); |
| 100 |
|
| 101 |
/** |
| 102 |
* @warning Not implemented yet |
| 103 |
*/ |
| 104 |
template <int dim> |
| 105 |
void |
| 106 |
assemble(const DoFInfo<dim> &info1, const DoFInfo<dim> &info2); |
| 107 |
|
| 108 |
private: |
| 109 |
/** |
| 110 |
* Write the object T either to the stream #os, if initialize_stream() |
| 111 |
* has been called, or to @p deallog if no pointer has been set. |
| 112 |
*/ |
| 113 |
template <typename T> |
| 114 |
void |
| 115 |
write(const T &t) const; |
| 116 |
|
| 117 |
/** |
| 118 |
* Write an end-of-line marker either to the stream #os, if |
| 119 |
* initialize_stream has been called, or to @p deallog if no pointer has |
| 120 |
* been set. |
| 121 |
*/ |
| 122 |
void |
| 123 |
write_endl() const; |
| 124 |
|
| 125 |
/** |
| 126 |
* The number of output components in each point. |
| 127 |
*/ |
| 128 |
unsigned int n_vectors; |
| 129 |
/** |
| 130 |
* The number of points in one direction. |
| 131 |
*/ |
| 132 |
unsigned int n_points; |
| 133 |
|
| 134 |
/** |
| 135 |
* Stream to which output is to be written. Set by initialize_stream(). |
| 136 |
*/ |
| 137 |
std::ostream *os; |
| 138 |
}; |
| 139 |
|
| 140 |
//----------------------------------------------------------------------// |
| 141 |
|
| 142 |
template <typename T> |
| 143 |
inline void |
| 144 |
GnuplotPatch::write(const T &d) const |
| 145 |
{ |
| 146 |
if (os == nullptr) |
| 147 |
deallog << d; |
| 148 |
else |
| 149 |
(*os) << d; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
inline void |
| 154 |
GnuplotPatch::write_endl() const |
| 155 |
{ |
| 156 |
if (os == nullptr) |
| 157 |
deallog << std::endl; |
| 158 |
else |
| 159 |
(*os) << std::endl; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
inline GnuplotPatch::GnuplotPatch() |
| 164 |
: n_vectors(numbers::invalid_unsigned_int) |
| 165 |
, n_points(numbers::invalid_unsigned_int) |
| 166 |
, os(nullptr) |
| 167 |
{} |
| 168 |
|
| 169 |
|
| 170 |
inline void |
| 171 |
GnuplotPatch::initialize(const unsigned int np, const unsigned int nv) |
| 172 |
{ |
| 173 |
n_vectors = nv; |
| 174 |
n_points = np; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
inline void |
| 179 |
GnuplotPatch::initialize_stream(std::ostream &stream) |
| 180 |
{ |
| 181 |
os = &stream; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
template <int dim> |
| 186 |
inline void |
| 187 |
GnuplotPatch::initialize_info(DoFInfo<dim> &info, bool face) |
| 188 |
{ |
| 189 |
if (face) |
| 190 |
info.initialize_quadrature(Utilities::fixed_power<dim - 1>(n_points), |
| 191 |
n_vectors + dim); |
| 192 |
else |
| 193 |
info.initialize_quadrature(Utilities::fixed_power<dim>(n_points), |
| 194 |
n_vectors + dim); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
template <int dim> |
| 199 |
inline void |
| 200 |
GnuplotPatch::assemble(const DoFInfo<dim> &info) |
| 201 |
{ |
| 202 |
const unsigned int np = info.n_quadrature_points(); |
| 203 |
const unsigned int nv = info.n_quadrature_values(); |
| 204 |
const unsigned int patch_dim = |
| 205 |
(info.face_number == numbers::invalid_unsigned_int) ? dim : (dim - 1); |
| 206 |
const unsigned int row_length = n_points; |
| 207 |
// If patches are 1d, end the |
| 208 |
// patch after a row, else end |
| 209 |
// it after a square |
| 210 |
const unsigned int row_length2 = |
| 211 |
(patch_dim == 1) ? row_length : (row_length * row_length); |
| 212 |
|
| 213 |
// AssertDimension(np, Utilities::fixed_power<dim>(n_points)); |
| 214 |
AssertDimension(nv, n_vectors + dim); |
| 215 |
|
| 216 |
|
| 217 |
for (unsigned int k = 0; k < np; ++k) |
| 218 |
{ |
| 219 |
if (k % row_length == 0) |
| 220 |
write_endl(); |
| 221 |
if (k % row_length2 == 0) |
| 222 |
write_endl(); |
| 223 |
|
| 224 |
for (unsigned int i = 0; i < nv; ++i) |
| 225 |
{ |
| 226 |
write(info.quadrature_value(k, i)); |
| 227 |
write('\t'); |
| 228 |
} |
| 229 |
write_endl(); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
template <int dim> |
| 235 |
inline void |
| 236 |
GnuplotPatch::assemble(const DoFInfo<dim> &info1, const DoFInfo<dim> &info2) |
| 237 |
{ |
| 238 |
assemble(info1); |
| 239 |
assemble(info2); |
| 240 |
} |
| 241 |
} // namespace Assembler |
| 242 |
} // namespace MeshWorker |
| 243 |
|
| 244 |
DEAL_II_NAMESPACE_CLOSE |
| 245 |
|
| 246 |
#endif |