|
| 1 | +import pygmsh |
| 2 | +import meshio |
| 3 | +import numpy as np |
| 4 | +import numpy.linalg as npla |
| 5 | +from numba import jit |
| 6 | + |
| 7 | + |
| 8 | +def norm2(pointA, pointB): |
| 9 | + return np.sqrt( (pointA[0] - pointB[0])**2 + (pointA[1] - pointB[1])**2 ) |
| 10 | + |
| 11 | +def sortDistanceNode(nodeRef, nodeList): |
| 12 | + distanceList = [] |
| 13 | + for i in range(0, len(nodeList),1): |
| 14 | + distanceList.append([i,norm2(nodeRef, nodeList[i])]) |
| 15 | + distanceList.sort(key=lambda distanceList:distanceList[1]) |
| 16 | + |
| 17 | + return distanceList |
| 18 | +@jit |
| 19 | +def geneAdjacentNode(nodeNum, cellList): |
| 20 | + adjacentList = [] |
| 21 | + for i in range(0, nodeNum, 1): |
| 22 | + print("geneAdjacentNode:", i, "in", nodeNum) |
| 23 | + local = [] |
| 24 | + for k in range(len(cellList)): |
| 25 | + cell = cellList[k] |
| 26 | + for j in range(3): |
| 27 | + if i - cell[j] == 0: |
| 28 | + local.append(cell[0]) |
| 29 | + local.append(cell[1]) |
| 30 | + local.append(cell[2]) |
| 31 | + local = list(set(local)) |
| 32 | + adjacentList.append(local) |
| 33 | + |
| 34 | + return adjacentList |
| 35 | +@jit |
| 36 | +def geneAdjacentTriangle(nodeNum, cellList): |
| 37 | + adjacentList = [] |
| 38 | + for i in range(0, nodeNum, 1): |
| 39 | + print("geneAdjacentTriangle:", i, "in", nodeNum) |
| 40 | + local = [] |
| 41 | + for j in range(len(cellList)): |
| 42 | + if i in cellList[j]: |
| 43 | + local.append(j) |
| 44 | + local = list(set(local)) |
| 45 | + adjacentList.append(local) |
| 46 | + |
| 47 | + return adjacentList |
| 48 | + |
| 49 | +def genelocalAttributes(nodeList,cellList): |
| 50 | + # judge acute or obtuse |
| 51 | + localAngleList = [] |
| 52 | + localLineList = [] |
| 53 | + for i in range(0, len(cellList), 1): |
| 54 | + pointA = nodeList[cellList[i][0]] |
| 55 | + pointB = nodeList[cellList[i][1]] |
| 56 | + pointC = nodeList[cellList[i][2]] |
| 57 | + #vectAC = pointC - pointA |
| 58 | + b = lineAC = norm2(pointA, pointC) |
| 59 | + c = lineAB = norm2(pointA, pointB) |
| 60 | + a = lineBC = norm2(pointB, pointC) |
| 61 | + cosA = (b**2 + c**2 - a**2)/(2*b*c) |
| 62 | + cosB = (a**2 + c**2 - b**2)/(2*a*c) |
| 63 | + cosC = (a**2 + b**2 - c**2)/(2*a*b) |
| 64 | + localAngleList.append([np.arccos(cosA), np.arccos(cosB),np.arccos(cosC)]) |
| 65 | + localLineList.append([a,b,c]) |
| 66 | + return localAngleList, localLineList |
| 67 | + |
| 68 | +@jit |
| 69 | +def localSolver(tA, tB, tC, a,b,c, alpha, beta, fC): |
| 70 | + Theta = abs(tB-tA)/ (c*fC) |
| 71 | + if Theta <= 1 : |
| 72 | + theta = np.arcsin((tB-tA)/ (c*fC)) |
| 73 | + flag = 0 |
| 74 | + if max(0, alpha - np.pi/2 )<= theta and theta <= np.pi/2 - beta: |
| 75 | + flag = 1 |
| 76 | + if alpha - np.pi/2 <=theta and theta <= min(0, np.pi/2 - beta): |
| 77 | + flag = 1 |
| 78 | + if flag == 1: |
| 79 | + h = a * np.sin(alpha - theta) |
| 80 | + if h < 0: |
| 81 | + print("h < 0") |
| 82 | + H = b * np.sin(beta + theta) |
| 83 | + if H < 0: |
| 84 | + print("H < 0") |
| 85 | + tC = min(tC, 1/2 * ((h * fC + tB)+ (H * fC + tA))) |
| 86 | + else: |
| 87 | + tC = min(tC,tA + b* fC, tB+ a*fC) |
| 88 | + else: |
| 89 | + tC = min(tC,tA + b* fC, tB+ a*fC) |
| 90 | + return tC |
| 91 | + |
| 92 | +def initPointValue(nodeNum): |
| 93 | + pointValue = [] |
| 94 | + for i in range(0, nodeNum, 1): |
| 95 | + pointValue.append(10000) |
| 96 | + pointValue[2] = 0 |
| 97 | + return pointValue |
| 98 | + |
| 99 | +def initField(nodeNum): |
| 100 | + field = [] |
| 101 | + for i in range(0, nodeNum, 1): |
| 102 | + field.append(1) |
| 103 | + return field |
| 104 | + |
| 105 | +def errorCal(pointList, pointValue, srcPointIndex): |
| 106 | + srcx = pointList[srcPointIndex][0] |
| 107 | + srcy = pointList[srcPointIndex][1] |
| 108 | + print(srcx, srcy) |
| 109 | + err = [] |
| 110 | + for i in range(len(pointList)): |
| 111 | + dist = norm2(pointList[i], pointList[srcPointIndex]) |
| 112 | + err.append(abs(dist - pointValue[i])/ dist) |
| 113 | + return err |
| 114 | + |
| 115 | +def geneMesh(meshSize): |
| 116 | + with pygmsh.geo.Geometry() as geom: |
| 117 | + geom.add_polygon( |
| 118 | + [ |
| 119 | + [0.0, 0.0], |
| 120 | + [1.0, 0.0], |
| 121 | + [1.0, 1.0], |
| 122 | + [0.0, 1.0], |
| 123 | + ], |
| 124 | + mesh_size=meshSize, |
| 125 | + ) |
| 126 | + mesh = geom.generate_mesh() |
| 127 | + # mesh.points, mesh.cells, ... |
| 128 | + # print(mesh.points) |
| 129 | + mesh.write("out.vtk") |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + meshSize = 0.02 |
| 133 | + geneMesh(meshSize) |
| 134 | + fURL = "out" |
| 135 | + mesh = meshio.read(fURL + ".vtk") |
| 136 | + pointList = mesh.points |
| 137 | + pointNum = np.int64(len(pointList)) |
| 138 | + cellList = np.array(list(mesh.cells[1][1])) |
| 139 | + cellNum = len(cellList) |
| 140 | + print(pointNum, cellNum) |
| 141 | + print(pointNum.dtype) |
| 142 | + print(cellList.dtype) |
| 143 | + adjacentNodeList = geneAdjacentNode(pointNum, cellList) |
| 144 | + adjacentTriangleList = geneAdjacentTriangle(pointNum, cellList) |
| 145 | + angleList, lineList = genelocalAttributes(pointList, cellList) |
| 146 | + #np.save("out_" + str(meshSize)+ "_adjacentNodeList", adjacentNodeList) |
| 147 | + #np.save("out_" + str(meshSize)+ "_adjacentTriangleList", adjacentTriangleList) |
| 148 | + #np.save("out_" + str(meshSize)+ "_angleList", angleList) |
| 149 | + #np.save("out_" + str(meshSize)+ "_lineList", lineList) |
| 150 | + # |
| 151 | + print("Load mesh Over") |
| 152 | + pointValue = initPointValue(pointNum) |
| 153 | + field = initField(pointNum) |
| 154 | + # Local Solver Part |
| 155 | + #refList = [[0,0], [0,1], [1,0], [1,1]] |
| 156 | + refList = [[-100.0,-100.0], [-100.0,100.0], [100.0,-100.0], [100.,100.]] |
| 157 | + lastValue = pointValue.copy() |
| 158 | + MaxIte = 3 |
| 159 | + print("------calc Start ------------") |
| 160 | + for i in range(MaxIte): |
| 161 | + for rerf in refList: # Run once |
| 162 | + ascentList = sortDistanceNode(rerf, pointList) |
| 163 | + descentList = ascentList.copy() |
| 164 | + descentList.reverse() |
| 165 | + disList = [ascentList, descentList] |
| 166 | + for ad in range(0,2,1): |
| 167 | + for i in range(0, pointNum, 1): |
| 168 | + loaclPointIndex = disList[ad][i][0] |
| 169 | + for j in range(0 , len(adjacentTriangleList[loaclPointIndex]), 1): # |
| 170 | + localCellIndex = adjacentTriangleList[loaclPointIndex][j] # |
| 171 | + localCell = list(cellList[localCellIndex]) # |
| 172 | + rangeIndex = (localCell.index(loaclPointIndex)) |
| 173 | + AList = [1,0,0] |
| 174 | + BList = [2,2,1] |
| 175 | + Aflag = AList[rangeIndex] |
| 176 | + Bflag = BList[rangeIndex] |
| 177 | + tA = pointValue[localCell[Aflag]] |
| 178 | + tB = pointValue[localCell[Bflag]] |
| 179 | + tC = pointValue[localCell[rangeIndex]] |
| 180 | + a = lineList[localCellIndex][Aflag] |
| 181 | + b = lineList[localCellIndex][Bflag] |
| 182 | + c = lineList[localCellIndex][rangeIndex] |
| 183 | + alpha = angleList[localCellIndex][Aflag] |
| 184 | + beta = angleList[localCellIndex][Bflag] |
| 185 | + gamma = angleList[localCellIndex][rangeIndex] |
| 186 | + fC = field[loaclPointIndex] |
| 187 | + if gamma > np.pi /2: |
| 188 | + print("Error: Acute Angle") |
| 189 | + else: |
| 190 | + tC = localSolver(tA, tB, tC, a,b,c, alpha, beta, fC) |
| 191 | + if tC < 0: print("Error: tC negative") |
| 192 | + #if tC == 10000: print("Error: not update",i ) |
| 193 | + pointValue[localCell[rangeIndex]] = tC |
| 194 | + lastValue = pointValue |
| 195 | + print(npla.norm(np.array(pointValue) - np.array(lastValue))) |
| 196 | + print("-------------------------") |
| 197 | + erp = [] |
| 198 | + for i in range(len(pointValue)): |
| 199 | + if pointValue[i] == 10000: |
| 200 | + print(i) |
| 201 | + pointValue[i] = 0 |
| 202 | + erp.append(i) |
| 203 | + pointValue = list(pointValue) |
| 204 | + pointList = list(pointList) |
| 205 | + print(max(pointValue), min(pointValue)) |
| 206 | + # Save as Vtk |
| 207 | + pv1 = {} |
| 208 | + pv1["u"] = list(pointValue) |
| 209 | + #mwrite = meshio.Mesh(mesh.points,mesh.cells).write("res.vtk") |
| 210 | + meshio.write_points_cells("res.vtk", mesh.points, mesh.cells, pv1) |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
0 commit comments