Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
matlab_io.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef MATLAB_IO_HH
5#define MATLAB_IO_HH
6
7#include <array>
8#include <cstdio>
9#include <string>
10
12
22template<class GridType>
23FILE* openFile(const std::string& filename, GridType& grid)
24{
25 enum {dim = GridType::dimension};
26
27 // Open file
28 FILE* fp = fopen(filename.c_str(), "w");
29
30 typedef typename GridType::template Codim<dim>::LevelIterator VertexIterator;
31 VertexIterator vit = grid.template lbegin<dim>(grid.maxLevel());
32 VertexIterator vendit = grid.template lend<dim>(grid.maxLevel());
33
34 for (; vit!=vendit; ++vit)
35 fprintf(fp, "%g ", vit->geometry().corner(0)[0]);
36
37 fprintf(fp,"\n");
38
39 return fp;
40}
41
42
43
49template<class GridType>
50FILE* openFileDD(const std::string& filename, const std::array<GridType*, 2>& grid)
51{
52 enum {dim = GridType::dimension};
53
54 // Open file
55 FILE* fp = fopen(filename.c_str(), "w");
56
57 typedef typename GridType::template Codim<dim>::LevelIterator VertexIterator;
58
59 // Loop over the subgrids
60 for (int i=0; i<2; i++) {
61
62 VertexIterator vit = grid[i]->template lbegin<dim>(grid[i]->maxLevel());
63 VertexIterator vendit = grid[i]->template lend<dim>(grid[i]->maxLevel());
64
65 for (; vit!=vendit; ++vit) {
66
67 fprintf(fp, "%g ", vit->geometry().corner(0)[0]);
68
69 }
70
71 }
72
73 fprintf(fp,"\n");
74
75 return fp;
76}
77
78
79
82template<class VectorType>
83void write(FILE* fp, const VectorType& u)
84{
85 typedef typename VectorType::ConstIterator ConstIterator;
86 ConstIterator uIt = u.begin();
87 ConstIterator uEndIt = u.end();
88
89 for (; uIt!=uEndIt; ++uIt)
90 fprintf(fp, "%g ", (*uIt)[0]);
91
92 fprintf(fp, "\n");
93}
94
97template<class VectorType>
98void write(FILE* fp, const VectorType& u, int sd)
99{
100 // The subDomain which needs to be written next
101 // (due to the file format the two domains have to be written alternatingly)
102 static int subDomain = 0;
103
104 if (subDomain!=sd)
105 DUNE_THROW(Dune::IOError, "You sent subdomain " << sd << ", but subdomain " << subDomain
106 << "is scheduled next!");
107
108 typedef typename VectorType::ConstIterator Iterator;
109 Iterator uIt = u.begin();
110 Iterator uEndIt = u.end();
111
112 for (; uIt!=uEndIt; ++uIt)
113 fprintf(fp, "%3.15e ", (*uIt)[0]);
114
115 if (subDomain==1)
116 fprintf(fp, "\n");
117
118 // alternate between two subdomains
119 subDomain = (subDomain+1)%2;
120}
121
122
123#endif
124
int maxLevel() const
void write(FILE *fp, const VectorType &u)
Append the content of v as one ascii line to fp.
Definition matlab_io.hh:83
FILE * openFileDD(const std::string &filename, const std::array< GridType *, 2 > &grid)
Open file for writing and write the coordinates of the grid vertices FOR TWO GRIDS.
Definition matlab_io.hh:50
FILE * openFile(const std::string &filename, GridType &grid)
Open file for writing and write the coordinates of the grid vertices.
Definition matlab_io.hh:23
size_type dim() const
#define DUNE_THROW(E,...)
int maxLevel() const
T c_str(T... args)