GridWriter.hh 6.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include <fstream>

#include "FixVec.h"
#include "DOFVector.h"
#include "BasisFunction.h"
#include "Mesh.h"

namespace AMDiS {

  template<typename T>
  void GridWriter<T>::writeGrid(const WorldVector<double> *p,
12
13
14
				int *numPoints, 
				double *dist,
				DOFVector<T> *vec,
15
16
				const char *filename,
                                int outFilePrecision)
17
18
19
  {
    FUNCNAME("GridWriter<T>::writeGrid()");

20
21
    TEST_EXIT(vec)("no dof vector\n");
    TEST_EXIT(filename)("no filename\n");
22
    TEST_EXIT(vec->getFeSpace()->getBasisFcts())("no basis fcts\n");
23
 
24
25
26
    std::ofstream outFile(filename);
    outFile.precision(outFilePrecision);
    outFile.setf(std::ios_base::scientific);    
27
    const Mesh *mesh = vec->getFeSpace()->getMesh();
28
29
    int dim = mesh->getDim();

30
    TEST_EXIT(dim == Global::getGeo(WORLD))("not for DIM != DIM_OF_WORLD\n");
31

Thomas Witkowski's avatar
Thomas Witkowski committed
32
33
34
    WorldVector<double> *basis = new WorldVector<double>[dim];
    double *lengthBasis = new double[dim];
    WorldVector<double> *step = new WorldVector<double>[3];
35
36
37
38
39
40
41

    for (int i = 0; i < dim; i++) {
      TEST_EXIT(numPoints[i] > 0)("numPoints < 1\n");
      TEST_EXIT(dist[i] >= 0)("dist < 0\n");
      lengthBasis[i] = 0;
    }

42
43
44
    WorldVector<double> curCoord;
    DimVec<double> bary(dim, NO_INIT);
    Element *elp;
45
    const BasisFunction *basFcts = vec->getFeSpace()->getBasisFcts();
46
    const double *uhLoc;
47

48
    // get basis of grid 
49
50
    for (int i = 0; i < dim; i++) {
      for (int j = 0; j < dim; j++) {
51
	basis[i][j] = p[i + 1][j] ; 
52
53
54
55
56
	lengthBasis[i] += basis[i][j] * basis[i][j];
      }
      lengthBasis[i] = sqrt(lengthBasis[i]);
    }

57
    // norm basis, get steps
58
59
60
61
62
63
64
65
    for (int i = 0; i < dim; i++) {
      for (int j = 0; j < dim; j++) {
	basis[i][j] /= lengthBasis[i];
	step[i][j] = basis[i][j] * dist[j];
      }
    }

    /* write grid points */
66
67
68
    int localNumPoints[3] = {1, 1, 1};
    for (int i = 0; i < dim; i++)
      localNumPoints[i] = numPoints[i];    
69
70
71
72
73
74
75

    // Warning "Coords not in mesh domain" should be printed at most one time.
    bool warning = false;

    for (int i = 0; i < localNumPoints[0]; i++) {
      for (int j = 0; j < localNumPoints[1]; j++) {   // pseudo-loop for dim < 2
	for (int k = 0; k < localNumPoints[2]; k++) { // pseudo-loop for dim < 3   
76
	  // clac current coords
77
78
79
80
81
82
83
	  for (int l = 0; l < dim; l++) {
	    curCoord[l] = p[0][l] 
	      + (i * step[0][l])
	      + (j * step[1][l])     // j = 0 for dim > 1
	      + (k * step[2][l]);    // k = 0 for dim > 2
	  }

84
85
86
87
	  int inside = (const_cast<Mesh*>(mesh))->findElementAtPoint(curCoord, 
								     &elp, 
								     bary, 
								     NULL, NULL, NULL);
88

89
90
91
	  // write coords 
	  for (int l = 0; l < dim; l++)
	    outFile << curCoord[l] << " ";	  
92
93
94
95
96
97

	  if (!inside) {
	    if (!warning) {
	      WARNING("Coords not in mesh domain\n");
	      warning = true;
	    }
98
99
	    // write value 
	    outFile << "0.0" << std::endl;
100
	  } else {
101
	    // get value at coords 
102
	    uhLoc = vec->getLocalVector(elp, NULL);
103
	    double value = basFcts->evalUh(bary, uhLoc);
104
	  
105
106
	    // write value
	    outFile << value << std::endl;
107
108
	  }
	}
109
110
	if (localNumPoints[2] > 1) 
	  outFile << std::endl;
111
      }
112
113
      if (localNumPoints[1] > 1) 
	outFile << std::endl;
114
115
    }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
116
    delete [] basis;
Thomas Witkowski's avatar
Thomas Witkowski committed
117
    delete [] lengthBasis;
Thomas Witkowski's avatar
Thomas Witkowski committed
118
    delete [] step;
119
120
  }

121
122
123
124
125
126
127
128
129
130
131
132
133

  template<typename T>
  void GridWriter<T>::writeGrid(const WorldVector<double> *p,
				int *numPoints, 
				double *dist,
				std::vector< DOFVector<T> * >     vec,
				const char *filename,
                                int outFilePrecision)
  {
    FUNCNAME("GridWriter<T>::writeGrid()");

    TEST_EXIT(vec.size() > 0)("no dof vector\n");
    TEST_EXIT(filename)("no filename\n");
134
    TEST_EXIT(vec[0]->getFeSpace()->getBasisFcts())("no basis fcts\n");
135
136
137
138
 
    std::ofstream outFile(filename);
    outFile.precision(outFilePrecision);
    outFile.setf(std::ios_base::scientific);    
139
    const Mesh *mesh = vec[0]->getFeSpace()->getMesh();
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    int dim = mesh->getDim();

    TEST_EXIT(dim == Global::getGeo(WORLD))("not for DIM != DIM_OF_WORLD\n");

    WorldVector<double> *basis = new WorldVector<double>[dim];
    double *lengthBasis = new double[dim];
    WorldVector<double> *step = new WorldVector<double>[3];

    for (int i = 0; i < dim; i++) {
      TEST_EXIT(numPoints[i] > 0)("numPoints < 1\n");
      TEST_EXIT(dist[i] >= 0)("dist < 0\n");
      lengthBasis[i] = 0;
    }

    WorldVector<double> curCoord;
    DimVec<double> bary(dim, NO_INIT);
    Element *elp;
157
    const BasisFunction *basFcts = vec[0]->getFeSpace()->getBasisFcts();
158
159
160
161
162
    const double *uhLoc;

    // get basis of grid 
    for (int i = 0; i < dim; i++) {
      for (int j = 0; j < dim; j++) {
163
	basis[i][j] = p[i + 1][j] ;
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
	lengthBasis[i] += basis[i][j] * basis[i][j];
      }
      lengthBasis[i] = sqrt(lengthBasis[i]);
    }

    // norm basis, get steps
    for (int i = 0; i < dim; i++) {
      for (int j = 0; j < dim; j++) {
	basis[i][j] /= lengthBasis[i];
	step[i][j] = basis[i][j] * dist[j];
      }
    }

    /* write grid points */
    int localNumPoints[3] = {1, 1, 1};
    for (int i = 0; i < dim; i++)
      localNumPoints[i] = numPoints[i];    

    // Warning "Coords not in mesh domain" should be printed at most one time.
    bool warning = false;

    for (int i = 0; i < localNumPoints[0]; i++) {
      for (int j = 0; j < localNumPoints[1]; j++) {   // pseudo-loop for dim < 2
	for (int k = 0; k < localNumPoints[2]; k++) { // pseudo-loop for dim < 3   
	  // clac current coords
	  for (int l = 0; l < dim; l++) {
	    curCoord[l] = p[0][l] 
	      + (i * step[0][l])
	      + (j * step[1][l])     // j = 0 for dim > 1
	      + (k * step[2][l]);    // k = 0 for dim > 2
	  }

	  int inside = (const_cast<Mesh*>(mesh))->findElementAtPoint(curCoord, 
								     &elp, 
								     bary, 
								     NULL, NULL, NULL);

	  // write coords 
	  for (int l = 0; l < dim; l++)
	    outFile << curCoord[l] << " ";	  

	  if (!inside) {
	    if (!warning) {
	      WARNING("Coords not in mesh domain\n");
	      warning = true;
	    }
	    // write value 
	    outFile << "0.0" << std::endl;
	  } else {
	    // get value at coords
              double value; 
	      for(int rr=0; rr<vec.size(); rr++) {
		  uhLoc = vec[rr]->getLocalVector(elp, NULL);
		  value = basFcts->evalUh(bary, uhLoc);
		  /* write value */
		  outFile << value <<"\t";
	      }
	      outFile << std::endl;
	  }
	}
	if (localNumPoints[2] > 1) 
	  outFile << std::endl;
      }
      if (localNumPoints[1] > 1) 
	outFile << std::endl;
    }
  
    delete [] basis;
    delete [] lengthBasis;
    delete [] step;
  }

236
}