9 def initial_conditions(my_mesh):
13 y_field = cdmath.Field("Y field", cdmath.CELLS, my_mesh, 1)
14 nbCells = my_mesh.getNumberOfCells()
15 for j in range(nbCells):
16 x = my_mesh.getCell(j).x()
17 y = my_mesh.getCell(j).y()
18 valX = (x - xcentre) * (x - xcentre)
19 valY = (y - ycentre) * (y - ycentre)
20 val = math.sqrt(valX + valY)
31 def sigma_flux(VitesseX, VitesseY, cfl, y_field, indexFacesPerio):
32 # Fluxes calculation #
33 SumFlux = cdmath.Field("Fluxes", cdmath.CELLS, y_field.getMesh(), 1)
34 my_mesh = y_field.getMesh()
35 nbCells = my_mesh.getNumberOfCells()
36 normU = math.sqrt(VitesseX * VitesseX + VitesseY * VitesseY)
37 for j in range(nbCells):
38 Cj = my_mesh.getCell(j)
39 nbFace = Cj.getNumberOfFaces()
42 for k in range(nbFace):
43 indexFace = Cj.getFacesId()[k]
44 Fk = my_mesh.getFace(indexFace)
45 NormalX = Cj.getNormalVector(k, 0)
46 NormalY = Cj.getNormalVector(k, 1)
47 LengthFk = Fk.getMeasure()
48 UN = VitesseX * NormalX + VitesseY * NormalY
49 minlengthFk = min(minlengthFk, LengthFk / abs(UN))
50 minlengthFk = min(minlengthFk, LengthFk / abs(VitesseX))
51 minlengthFk = min(minlengthFk, LengthFk / abs(VitesseY))
55 if (not Fk.isBorder()):
56 indexC1 = Fk.getCellsId()[0]
57 indexC2 = Fk.getCellsId()[1]
58 # hypothese: La cellule d'index indexC1 est la cellule courante index j #
61 cellCourante = indexC1
65 # hypothese non verifie #
66 cellCourante = indexC2
69 # definir la cellule gauche et droite par le prduit vitesse * normale sortante
70 # si u*n>0 : rien a faire sinon inverser la gauche et la droite
72 conc = y_field[cellCourante]
75 conc = y_field[cellAutre]
79 # conditions aux limites neumann homogene #
80 if (Fk.getGroupName() == "LeftEdge" or Fk.getGroupName() == "RightEdge"):
82 conc = y_field[cellCourante]
88 # conditions aux limites periodiques #
89 if (Fk.getGroupName() == "BottomEdge" or Fk.getGroupName() == "TopEdge"):
90 indexFP = indexFacesPerio[indexFace]
91 # une autre manière de recuperer l'index de la face periodique #
92 # int indexFP=my_mesh.getIndexFacePeriodic(indexFace);
93 Fp = my_mesh.getFace(indexFP)
94 indexCp = Fp.getCellsId()[0]
96 conc = y_field[cellCourante]
99 conc = y_field[indexCp]
103 SumF = SumF + UN * LengthFk * conc
105 dt = cfl * minlengthFk / normU
106 SumFlux[j] = dt * SumF / Cj.getMeasure()
111 def EquationTransport2D(tmax, VitesseX, VitesseY, cfl, output_freq, my_mesh, file):
113 # Initial conditions #
114 print("Construction of the initial condition …")
115 y_field = initial_conditions(my_mesh)
117 # MED output of the initial conditions at t=0 and iteration = 0
122 print("Saving the solution at T=" + str(time) + "…")
123 y_field.setTime(time, iteration)
124 y_field.writeMED(file)
125 y_field.writeVTK(file)
126 y_field.writeCSV(file)
129 print("Resolution of the transport equation with an UPWIND scheme …")
131 indexFacesPerio = my_mesh.getIndexFacePeriodic()
133 while (iteration < ntmax and time <= tmax):
134 dt, SumFlux = sigma_flux(VitesseX, VitesseY, cfl, y_field, indexFacesPerio)
135 print("-- Iter: " + str(iteration) + ", Time: " + str(time) + ", dt: " + str(dt))
137 # Advancing time step #
141 # Output every output_freq iterations
142 if (iteration % output_freq == 0):
143 y_field.setTime(time, iteration)
144 y_field.writeMED(file, False)
145 y_field.writeVTK(file, False)
146 y_field.writeCSV(file)
153 print("RESOLUTION OF THE 2D TRANSPORT EQUATION:")
154 print("- DOMAIN: SQUARE [0,1]x[0,1]")
155 print("- MESH: CARTESIAN, INTERNAL GENERATION WITH CDMATH")
156 print("- PERIODIC BC ON TOP AND BOTTOM")
157 print("- HOMOGENEOUS NEUMANN BC ON LEFT AND RIGHT")
166 print("Construction of a cartesian mesh …")
173 my_mesh = cdmath.Mesh(xinf, xsup, nx, yinf, ysup, ny)
175 my_mesh.setGroupAtPlan(xsup, 0, eps, "RightEdge")
176 my_mesh.setGroupAtPlan(xinf, 0, eps, "LeftEdge")
177 my_mesh.setGroupAtPlan(yinf, 1, eps, "BottomEdge")
178 my_mesh.setGroupAtPlan(ysup, 1, eps, "TopEdge")
179 fileOutPutCart = "Exercice1PyTest"
180 EquationTransport2D(tmax, VitesseX, VitesseY, cfl, output_freq, my_mesh, fileOutPutCart)
181 print("CDMATH calculation done.")
185 if __name__ == """__main__""":