GridWriter.hh 6.57 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
    for (int i = 0; i < 3; i++)
      step[i].set(0.0);
37
38
39
40
41
42
43

    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;
    }

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

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

59
    // norm basis, get steps
60
61
62
63
64
65
66
67
    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 */
68
69
70
    int localNumPoints[3] = {1, 1, 1};
    for (int i = 0; i < dim; i++)
      localNumPoints[i] = numPoints[i];    
71
72
73
74
75
76
77

    // 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   
78
	  // clac current coords
79
80
81
82
83
84
85
	  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
	  }

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

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

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

123
124
125
126
127

  template<typename T>
  void GridWriter<T>::writeGrid(const WorldVector<double> *p,
				int *numPoints, 
				double *dist,
128
				std::vector<DOFVector<T>*> vec,
129
130
131
132
133
134
135
				const char *filename,
                                int outFilePrecision)
  {
    FUNCNAME("GridWriter<T>::writeGrid()");

    TEST_EXIT(vec.size() > 0)("no dof vector\n");
    TEST_EXIT(filename)("no filename\n");
136
    TEST_EXIT(vec[0]->getFeSpace()->getBasisFcts())("no basis fcts\n");
137
138
139
140
 
    std::ofstream outFile(filename);
    outFile.precision(outFilePrecision);
    outFile.setf(std::ios_base::scientific);    
141
    const Mesh *mesh = vec[0]->getFeSpace()->getMesh();
142
143
144
145
146
147
148
    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];
149
150
    for (int i = 0; i < 3; i++)
      step[i].set(0.0);
151
152
153
154
155
156
157
158
159
160

    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;
161
    const BasisFunction *basFcts = vec[0]->getFeSpace()->getBasisFcts();
162
    ElementVector uhLoc(basFcts->getNumber());
163
164
165
166

    // get basis of grid 
    for (int i = 0; i < dim; i++) {
      for (int j = 0; j < dim; j++) {
167
	basis[i][j] = p[i + 1][j] ;
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
	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
218
219
220
221
222
223
224
225
	    double value; 
	    for (int rr = 0; rr < vec.size(); rr++) {
	      vec[rr]->getLocalVector(elp, uhLoc);
	      value = basFcts->evalUh(bary, uhLoc);
	      // write value
	      outFile << value <<"\t";
	    }
	    outFile << std::endl;
226
227
228
229
230
231
232
233
234
235
236
237
238
239
	  }
	}
	if (localNumPoints[2] > 1) 
	  outFile << std::endl;
      }
      if (localNumPoints[1] > 1) 
	outFile << std::endl;
    }
  
    delete [] basis;
    delete [] lengthBasis;
    delete [] step;
  }

240
}