IR_PrintVisualizationSWE.scala 10.5 KB
Newer Older
1
2
3
4
package exastencils.applications.swe.ir

import scala.collection.mutable.ListBuffer

5
6
import exastencils.base.ir.IR_ArrayAccess
import exastencils.base.ir.IR_Assignment
7
8
import exastencils.base.ir.IR_Comment
import exastencils.base.ir.IR_ConstIndex
9
import exastencils.base.ir.IR_Expression
10
11
import exastencils.base.ir.IR_ExpressionIndex
import exastencils.base.ir.IR_ForLoop
12
13
import exastencils.base.ir.IR_ImplicitConversion._
import exastencils.base.ir.IR_PointerDatatype
14
import exastencils.base.ir.IR_PreIncrement
15
import exastencils.base.ir.IR_SpecialDatatype
16
17
18
import exastencils.base.ir.IR_Statement
import exastencils.base.ir.IR_VariableAccess
import exastencils.base.ir.IR_VariableDeclaration
19
import exastencils.baseExt.ir.IR_ExpressionIndexRange
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import exastencils.baseExt.ir.IR_LoopOverDimensions
import exastencils.config.Knowledge
import exastencils.core.Duplicate
import exastencils.field.ir.IR_Field
import exastencils.field.ir.IR_FieldAccess
import exastencils.field.ir.IR_FieldCollection
import exastencils.field.ir.IR_IV_ActiveSlot
import exastencils.logger.Logger
import exastencils.util.ir.IR_Print
import exastencils.visualization.ir.IR_PrintVisualizationTriangles

trait IR_PrintVisualizationSWE extends IR_PrintVisualizationTriangles {
  def numDimsGrid = 2

  def numCells_x = etaDiscLower0.layout.layoutsPerDim(0).numInnerLayers
  def numCells_y = etaDiscLower0.layout.layoutsPerDim(1).numInnerLayers
  def numCells_z = 1
37
  def numCellsPerFrag = 2 * numCells_x * numCells_y * numCells_z
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
  def numPointsPerFrag = if(Knowledge.swe_nodalReductionPrint) (numCells_x+1)*(numCells_y+1) else 6 * numCells_x * numCells_y

  def bath = IR_FieldCollection.getByIdentifier("bath", level).get

  def etaDiscLower0 = IR_FieldCollection.getByIdentifier("etaDiscLower0", level).get
  def etaDiscLower1 = IR_FieldCollection.getByIdentifier("etaDiscLower1", level).get
  def etaDiscLower2 = IR_FieldCollection.getByIdentifier("etaDiscLower2", level).get
  def etaDiscUpper0 = IR_FieldCollection.getByIdentifier("etaDiscUpper0", level).get
  def etaDiscUpper1 = IR_FieldCollection.getByIdentifier("etaDiscUpper1", level).get
  def etaDiscUpper2 = IR_FieldCollection.getByIdentifier("etaDiscUpper2", level).get

  def uDiscLower0 = IR_FieldCollection.getByIdentifier("uDiscLower0", level).get
  def uDiscLower1 = IR_FieldCollection.getByIdentifier("uDiscLower1", level).get
  def uDiscLower2 = IR_FieldCollection.getByIdentifier("uDiscLower2", level).get
  def uDiscUpper0 = IR_FieldCollection.getByIdentifier("uDiscUpper0", level).get
  def uDiscUpper1 = IR_FieldCollection.getByIdentifier("uDiscUpper1", level).get
  def uDiscUpper2 = IR_FieldCollection.getByIdentifier("uDiscUpper2", level).get

  def vDiscLower0 = IR_FieldCollection.getByIdentifier("vDiscLower0", level).get
  def vDiscLower1 = IR_FieldCollection.getByIdentifier("vDiscLower1", level).get
  def vDiscLower2 = IR_FieldCollection.getByIdentifier("vDiscLower2", level).get
  def vDiscUpper0 = IR_FieldCollection.getByIdentifier("vDiscUpper0", level).get
  def vDiscUpper1 = IR_FieldCollection.getByIdentifier("vDiscUpper1", level).get
  def vDiscUpper2 = IR_FieldCollection.getByIdentifier("vDiscUpper2", level).get

  def etaDisc = ListBuffer(etaDiscLower0, etaDiscLower1, etaDiscLower2, etaDiscUpper0, etaDiscUpper1, etaDiscUpper2)
  def uDisc = ListBuffer(uDiscLower0, uDiscLower1, uDiscLower2, uDiscUpper0, uDiscUpper1, uDiscUpper2)
  def vDisc = ListBuffer(vDiscLower0, vDiscLower1, vDiscLower2, vDiscUpper0, vDiscUpper1, vDiscUpper2)

  def optLocalOrderLower = IR_FieldCollection.getByIdentifier("local_orderLower0", level, suppressError = true)
  def optLocalOrderUpper = IR_FieldCollection.getByIdentifier("local_orderUpper0", level, suppressError = true)

  def someCellField = etaDiscLower0

  def numFields = 4 + (if (optLocalOrderLower.isDefined && optLocalOrderUpper.isDefined) 1 else 0)

  // nodal data reduction
75
76
77
78
  def reducedCellPrint(buf : IR_VariableAccess, discFields : ListBuffer[IR_Field]) : ListBuffer[IR_Statement] = {

    val low : ListBuffer[IR_Field] = discFields.take(3)
    val upp : ListBuffer[IR_Field] = discFields.takeRight(3)
79
80
81
82
83

    if (discFields.length != 6) {
      Logger.error("Wrong usage of \"addReducedNodePrint\" in IR_PrintVtkSWE.")
    }

84
85
86
87
88
89
90
91
92
    def getIdxNodalLoop(idx : IR_ExpressionIndex) = IR_ExpressionIndexRange(
      IR_ExpressionIndex((0 until numDimsGrid).toArray.map(dim => etaDiscLower0.layout.idxById("IB", dim) - Duplicate(etaDiscLower0.referenceOffset(dim)) : IR_Expression)),
      IR_ExpressionIndex((0 until numDimsGrid).toArray.map(dim => 1 + etaDiscLower0.layout.idxById("IE", dim) - Duplicate(etaDiscLower0.referenceOffset(dim)) : IR_Expression))
    ).linearizeIndex(idx)

    def storeOperation(toStore : IR_Expression, idx : IR_ExpressionIndex) = buf.datatype match {
      case IR_SpecialDatatype("std::ofstream") => IR_Print(buf, toStore, IR_Print.newline)
      case IR_PointerDatatype(_) => IR_Assignment(IR_ArrayAccess(buf, getIdxNodalLoop(idx)), toStore)
    }
93
94
95
96
97
98
99
100
101

    def constructLoopForDim(dim : Int, offStart : Int, offEnd : Int, loopStmts : IR_Statement*) = IR_ForLoop(
      IR_VariableDeclaration(IR_LoopOverDimensions.defItForDim(dim), etaDiscLower0.layout.idxById("IB", dim) + offStart - Duplicate(etaDiscLower0.referenceOffset(dim))),
      IR_LoopOverDimensions.defItForDim(dim) < etaDiscLower0.layout.idxById("IE", dim) - offEnd - Duplicate(etaDiscLower0.referenceOffset(dim)),
      IR_PreIncrement(IR_LoopOverDimensions.defItForDim(dim)),
      loopStmts.to[ListBuffer])

    var statementsFragLoop : ListBuffer[IR_Statement] = ListBuffer()

102
103
104
105
106
107
108
109
110
111
112
113
    // expression indices for the edges
    val baseIdx_edgeLower = IR_ExpressionIndex(IR_LoopOverDimensions.defItForDim(0), 0)
    val baseIdx_edgeLeft  = IR_ExpressionIndex(0, IR_LoopOverDimensions.defItForDim(1))
    val baseIdx_edgeRight = IR_ExpressionIndex(someCellField.layout.layoutsPerDim(0).numInnerLayers, IR_LoopOverDimensions.defItForDim(1))
    val baseIdx_edgeUpper = IR_ExpressionIndex(IR_LoopOverDimensions.defItForDim(0), someCellField.layout.layoutsPerDim(1).numInnerLayers)

    // expression indices for the corners
    val baseIdx_cornerLL = IR_ExpressionIndex(0, 0)
    val baseIdx_cornerLR = IR_ExpressionIndex(low(1).layout.layoutsPerDim(0).numInnerLayers, 0)
    val baseIdx_cornerUL = IR_ExpressionIndex(0, low(2).layout.layoutsPerDim(1).numInnerLayers)
    val baseIdx_cornerUR = IR_ExpressionIndex((0 until numDimsGrid).map(d => someCellField.layout.layoutsPerDim(d).numInnerLayers).toArray)

114
    statementsFragLoop += IR_Comment("lower left corner: vertex contained by 1 triangle")
115
116
117
    statementsFragLoop += storeOperation(
      IR_FieldAccess(low(0), IR_IV_ActiveSlot(low(0)), baseIdx_cornerLL),
      baseIdx_cornerLL)
118
119
120

    statementsFragLoop += IR_Comment("lowermost row w/o corners: vertex contained by 3 triangles")
    statementsFragLoop += constructLoopForDim(dim = 0, offStart = 1, offEnd = 0,
121
122
123
124
125
      storeOperation(
        (IR_FieldAccess(low(0), IR_IV_ActiveSlot(low(0)), baseIdx_edgeLower) +
          IR_FieldAccess(low(1), IR_IV_ActiveSlot(low(1)), baseIdx_edgeLower + IR_ConstIndex(-1, 0)) +
          IR_FieldAccess(upp(2), IR_IV_ActiveSlot(upp(2)), baseIdx_edgeLower + IR_ConstIndex(-1, 0))) / 3.0,
        baseIdx_edgeLower))
126
127

    statementsFragLoop += IR_Comment("lower right corner: vertex contained by 2 triangles")
128
129
130
131
    statementsFragLoop += storeOperation(
      (IR_FieldAccess(low(1), IR_IV_ActiveSlot(low(1)), baseIdx_cornerLR + IR_ConstIndex(-1, 0)) +
        IR_FieldAccess(upp(2), IR_IV_ActiveSlot(upp(2)), baseIdx_cornerLR + IR_ConstIndex(-1, 0))) / 2.0,
      baseIdx_cornerLR)
132
133
134
135

    statementsFragLoop += IR_Comment("inner rows")
    statementsFragLoop += constructLoopForDim(dim = 1, offStart = 1, offEnd = 0,
      IR_Comment("leftmost column: vertex contained by 3 triangles"),
136
137
138
139
140
      storeOperation(
        (IR_FieldAccess(low(0), IR_IV_ActiveSlot(low(0)), baseIdx_edgeLeft) +
          IR_FieldAccess(low(2), IR_IV_ActiveSlot(low(2)), baseIdx_edgeLeft + IR_ConstIndex(0, -1)) +
          IR_FieldAccess(upp(1), IR_IV_ActiveSlot(upp(1)), baseIdx_edgeLeft + IR_ConstIndex(0, -1))) / 3.0,
        baseIdx_edgeLeft),
141
142
      IR_Comment("inner points: vertex contained by 6 triangles"),
      constructLoopForDim(dim = 0, offStart = 1, offEnd = 0,
143
        storeOperation(
144
145
146
147
148
149
          (IR_FieldAccess(low(0), IR_IV_ActiveSlot(low(0)), IR_LoopOverDimensions.defIt(numDimsGrid)) +
            IR_FieldAccess(low(1), IR_IV_ActiveSlot(low(1)), IR_LoopOverDimensions.defIt(numDimsGrid) + IR_ConstIndex(-1, 0)) +
            IR_FieldAccess(upp(2), IR_IV_ActiveSlot(upp(2)), IR_LoopOverDimensions.defIt(numDimsGrid) + IR_ConstIndex(-1, 0)) +
            IR_FieldAccess(low(2), IR_IV_ActiveSlot(low(2)), IR_LoopOverDimensions.defIt(numDimsGrid) + IR_ConstIndex(0, -1)) +
            IR_FieldAccess(upp(1), IR_IV_ActiveSlot(upp(1)), IR_LoopOverDimensions.defIt(numDimsGrid) + IR_ConstIndex(0, -1)) +
            IR_FieldAccess(upp(0), IR_IV_ActiveSlot(upp(0)), IR_LoopOverDimensions.defIt(numDimsGrid) + IR_ConstIndex(-1, -1))) / 6.0,
150
          IR_LoopOverDimensions.defIt(numDimsGrid))),
151
      IR_Comment("rightmost column: vertex contained by 3 triangles"),
152
153
154
155
156
      storeOperation(
        (IR_FieldAccess(upp(0), IR_IV_ActiveSlot(upp(0)), baseIdx_edgeRight + IR_ConstIndex(-1, -1)) +
          IR_FieldAccess(low(1), IR_IV_ActiveSlot(low(1)), baseIdx_edgeRight + IR_ConstIndex(-1, 0)) +
          IR_FieldAccess(upp(2), IR_IV_ActiveSlot(upp(2)), baseIdx_edgeRight + IR_ConstIndex(-1, 0))) / 3.0,
        baseIdx_edgeRight)
157
158
159
    )

    statementsFragLoop += IR_Comment("upper left corner: vertex contained by 2 triangles")
160
161
162
163
    statementsFragLoop += storeOperation(
      (IR_FieldAccess(low(2), IR_IV_ActiveSlot(low(2)), baseIdx_cornerUL + IR_ConstIndex(0, -1)) +
        IR_FieldAccess(upp(1), IR_IV_ActiveSlot(upp(1)), baseIdx_cornerUL + IR_ConstIndex(0, -1))) / 2.0,
      baseIdx_cornerUL)
164
165
166

    statementsFragLoop += IR_Comment("uppermost row w/o corners: vertex contained by 3 triangles")
    statementsFragLoop += constructLoopForDim(dim = 0, offStart = 1, offEnd = 0,
167
168
169
170
171
      storeOperation(
        (IR_FieldAccess(upp(0), IR_IV_ActiveSlot(upp(0)), baseIdx_edgeUpper + IR_ConstIndex(-1, -1)) +
          IR_FieldAccess(low(2), IR_IV_ActiveSlot(low(2)), baseIdx_edgeUpper + IR_ConstIndex(0, -1)) +
          IR_FieldAccess(upp(1), IR_IV_ActiveSlot(upp(1)), baseIdx_edgeUpper + IR_ConstIndex(0, -1))) / 3.0,
        baseIdx_edgeUpper))
172
173

    statementsFragLoop += IR_Comment("upper right corner: vertex contained by 1 triangle")
174
175
176
    statementsFragLoop += storeOperation(
      IR_FieldAccess(upp(0), IR_IV_ActiveSlot(upp(0)), baseIdx_cornerUR + IR_ConstIndex(-1, -1)),
      baseIdx_cornerUR)
177
178
179
180

    statementsFragLoop
  }
}