aboutsummaryrefslogtreecommitdiff
path: root/src/AMRHierLib/StitchCellWriter.cc
blob: 3f86dae19814bb59f0b18bf08c020e51c8838325 (plain)
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
//------------------------------------------------------------------------------
//
// Project:	AMR Visualization
// Module:	$RCSfile$
// Language:	C++
// Date:	$Date$
// Author:	$Author$
// Version:	$Revision$
//
//------------------------------------------------------------------------------

#include "StitchCellWriter.hh"

// Standard C/C++ Includes
#include <string>

// Own Misc. Helper Includes
#include <BinStream.hh>

// AMR Includes
#include <AMRHierarchy.hh>
#include <AMRGrid.hh>
#include <AMRGridCell.hh>

StitchCellWriter::StitchCellWriter(const AMRHierarchy& amrHier, const char* baseFilename, int maxLevel)
  : mMaxLevel(maxLevel),
    mGridStartPointIdx(new int[amrHier.numGrids()+1]),
    mPositionStream((std::string(baseFilename) + std::string(".pts")).c_str()),
    mValueStream((std::string(baseFilename) + std::string(".vals")).c_str()),
    mNumPoints(0),
    mTetStream((std::string(baseFilename) + std::string(".tets")).c_str()),
    mNumTets(0),
    mPyramidStream((std::string(baseFilename) + std::string(".pyramids")).c_str()),
    mNumPyramids(0),
    mTrianglePrismStream((std::string(baseFilename) + std::string(".triangleprisms")).c_str()),
    mNumTrianglePrisms(0),
    mHexahedronStream((std::string(baseFilename) + std::string(".hexahedra")).c_str()),
    mNumHexahedra(0)
{
  // Write 0 as number of points, tets, ...
  // Destructor seeks back and writes actual numbers when they are known (no more points,
  // tets, ...) added
  BinStream::binaryOut(mPositionStream, mNumPoints);
  BinStream::binaryOut(mTetStream, mNumTets);
  BinStream::binaryOut(mPyramidStream, mNumPyramids);
  BinStream::binaryOut(mTrianglePrismStream, mNumTrianglePrisms);
  BinStream::binaryOut(mHexahedronStream, mNumHexahedra);

  mGridStartPointIdx[0] = 0;
  for (int currGridIdx=0; currGridIdx < amrHier.numGrids(); ++currGridIdx) {
    AMRGrid *currGrid = amrHier.getGridByIndex(currGridIdx);

    if (currGrid->level > mMaxLevel) {
      // Skip grid
      std::cout << "Skipping grid " << currGrid->index() << ", level " << currGrid->level << " > " << mMaxLevel << std::endl;
      mGridStartPointIdx[currGridIdx+1] = mGridStartPointIdx[currGridIdx];
    }
    else {
      currGrid->loadCells();
      // Write data points
      Vertex pos;
      int i, j, k;
      OutputFPType out;
      // (i, j, k) -> mGridStartPointIdx[gridIdx] + (k * dims[1] + j) * dims[0] + i
      for (k=0, pos.z = currGrid->origin[2]+0.5*currGrid->delta[2]; k < currGrid->dims[2]; ++k, pos.z += currGrid->delta[2])
	for (j=0, pos.y = currGrid->origin[1]+0.5*currGrid->delta[1]; j < currGrid->dims[1]; ++j, pos.y += currGrid->delta[1])
	  for (i=0, pos.x = currGrid->origin[0]+0.5*currGrid->delta[0]; i < currGrid->dims[0]; ++i, pos.x += currGrid->delta[0]) {
	    out = pos.x; BinStream::binaryOut(mPositionStream, out);
	    out = pos.y; BinStream::binaryOut(mPositionStream, out);
	    out = pos.z; BinStream::binaryOut(mPositionStream, out);
	    out = currGrid->getValue(i, j, k); BinStream::binaryOut(mValueStream, out);
	  }
      int numPointsThisGrid = currGrid->dims[0]*currGrid->dims[1]*currGrid->dims[2];
      mGridStartPointIdx[currGridIdx+1] = mGridStartPointIdx[currGridIdx] + numPointsThisGrid;
      mNumPoints += numPointsThisGrid;

      AMRGrid::IntersectionMap *intersectionMap = currGrid->computeIntersectionMap(AMRGrid::IntersectionMap::RefinedCells);
      // Write dual grid
      for (i=0; i<currGrid->dims[0]-1; ++i)
	for (j=0; j<currGrid->dims[1]-1; ++j)
	  for (k=0; k<currGrid->dims[2]-1; ++k)
	    if (((*intersectionMap)(i  , j  , k  ) == -1) && ((*intersectionMap)(i  , j  , k+1) == -1) &&
		((*intersectionMap)(i  , j+1, k  ) == -1) && ((*intersectionMap)(i  , j+1, k+1) == -1) && 
		((*intersectionMap)(i+1, j  , k  ) == -1) && ((*intersectionMap)(i+1, j  , k+1) == -1) &&
		((*intersectionMap)(i+1, j+1, k  ) == -1) && ((*intersectionMap)(i+1, j+1, k+1) == -1)
		|| currGrid->level == maxLevel)
	      handleHexahedronCell(
		  AMRGridCell(currGrid, i  , j  , k  ),
		  AMRGridCell(currGrid, i+1, j  , k  ),
		  AMRGridCell(currGrid, i+1, j+1, k  ),
		  AMRGridCell(currGrid, i  , j+1, k  ),
		  AMRGridCell(currGrid, i  , j  , k+1),
		  AMRGridCell(currGrid, i+1, j  , k+1),
		  AMRGridCell(currGrid, i+1, j+1, k+1),
		  AMRGridCell(currGrid, i  , j+1, k+1)
				  );
      delete intersectionMap;
    }
  }
}

StitchCellWriter::~StitchCellWriter()
{
  // Write actual number of points, tets, ...
  mPositionStream.seekp(0);
  BinStream::binaryOut(mPositionStream, mNumPoints);
  std::cout << "Wrote " << mNumPoints << " positions and values." << std::endl;
  mTetStream.seekp(0);
  BinStream::binaryOut(mTetStream, mNumTets);
  std::cout << "Wrote " << mNumTets << " tetrahedra." << std::endl;
  mPyramidStream.seekp(0);
  BinStream::binaryOut(mPyramidStream, mNumPyramids);
  std::cout << "Wrote " << mNumPyramids << " pyramids." << std::endl;
  mTrianglePrismStream.seekp(0);
  BinStream::binaryOut(mTrianglePrismStream, mNumTrianglePrisms);
  std::cout << "Wrote " << mNumTrianglePrisms << " triangle prisms." << std::endl;
  mHexahedronStream.seekp(0);
  BinStream::binaryOut(mHexahedronStream, mNumHexahedra);
  std::cout << "Wrote " << mNumHexahedra << " hexahedra." << std::endl;
}

void StitchCellWriter::handleTetrahedronCell(const AMRGridCell& c0, const AMRGridCell& c1, const AMRGridCell& c2, const Vertex& p3, double v3)
{
  // Adapt vertex ordering to Nelson Max's ordering scheme
  BinStream::binaryOut(mTetStream, pointIdxForGridCell(c0));
  BinStream::binaryOut(mTetStream, pointIdxForGridCell(c2));
  BinStream::binaryOut(mTetStream, pointIdxForGridCell(c1));
  BinStream::binaryOut(mTetStream, addPoint(p3, v3));
  ++mNumTets;
}

void StitchCellWriter::handleTetrahedronCell(const AMRGridCell& c0, const AMRGridCell& c1, const AMRGridCell& c2, const AMRGridCell& c3)
{
  // Adapt vertex ordering to Nelson Max's ordering scheme
  BinStream::binaryOut(mTetStream, pointIdxForGridCell(c0));
  BinStream::binaryOut(mTetStream, pointIdxForGridCell(c2));
  BinStream::binaryOut(mTetStream, pointIdxForGridCell(c1));
  BinStream::binaryOut(mTetStream, pointIdxForGridCell(c3));
  ++mNumTets;
}

void StitchCellWriter::handlePyramidCell(const AMRGridCell& c0, const AMRGridCell& c1, const AMRGridCell& c2, const AMRGridCell& c3, const Vertex& p4, double v4)
{
  // Adapt vertex ordering to Nelson Max's ordering scheme
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c0));
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c1));
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c2));
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c3));
  BinStream::binaryOut(mPyramidStream, addPoint(p4, v4));
  ++mNumPyramids;
}

void StitchCellWriter::handlePyramidCell(const AMRGridCell& c0, const AMRGridCell& c1, const AMRGridCell& c2, const AMRGridCell& c3, const AMRGridCell& c4)
{
  // Adapt vertex ordering to Nelson Max's ordering scheme
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c0));
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c1));
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c2));
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c3));
  BinStream::binaryOut(mPyramidStream, pointIdxForGridCell(c4));
  ++mNumPyramids;
}

void StitchCellWriter::handleTrianglePrismCell(const AMRGridCell& c0, const AMRGridCell& c1, const AMRGridCell& c2, const AMRGridCell& c3, const AMRGridCell& c4, const AMRGridCell& c5)
{
  // Adapt vertex ordering to Nelson Max's ordering scheme
  BinStream::binaryOut(mTrianglePrismStream, pointIdxForGridCell(c1));
  BinStream::binaryOut(mTrianglePrismStream, pointIdxForGridCell(c2));
  BinStream::binaryOut(mTrianglePrismStream, pointIdxForGridCell(c3));
  BinStream::binaryOut(mTrianglePrismStream, pointIdxForGridCell(c0));
  BinStream::binaryOut(mTrianglePrismStream, pointIdxForGridCell(c4));
  BinStream::binaryOut(mTrianglePrismStream, pointIdxForGridCell(c5));
  ++mNumTrianglePrisms;
}

void StitchCellWriter::handleHexahedronCell(const AMRGridCell& c0, const AMRGridCell& c1, const AMRGridCell& c2, const AMRGridCell& c3, const AMRGridCell& c4, const AMRGridCell& c5, const AMRGridCell& c6, const AMRGridCell& c7)
{
  // Adapt vertex ordering to Nelson Max's ordering scheme
  BinStream::binaryOut(mHexahedronStream, pointIdxForGridCell(c7));
  BinStream::binaryOut(mHexahedronStream, pointIdxForGridCell(c6));
  BinStream::binaryOut(mHexahedronStream, pointIdxForGridCell(c2));
  BinStream::binaryOut(mHexahedronStream, pointIdxForGridCell(c1));
  BinStream::binaryOut(mHexahedronStream, pointIdxForGridCell(c4));
  BinStream::binaryOut(mHexahedronStream, pointIdxForGridCell(c5));
  BinStream::binaryOut(mHexahedronStream, pointIdxForGridCell(c3));
  BinStream::binaryOut(mHexahedronStream, pointIdxForGridCell(c0));
  ++mNumHexahedra;
}