2626
2727import sys
2828import pysu2
29- from mpi4py import MPI
29+ import numpy as np
3030
31+ # with mpi:
32+ from mpi4py import MPI
33+ comm = MPI .COMM_WORLD
34+ rank = comm .Get_rank ()
35+ # without mpi:
36+ # comm = 0
3137
3238def main ():
33- """
34- custom source to add buoyancy term.
35- """
36- # parallel
37- comm = MPI .COMM_WORLD
38- # serial
39- #comm = 0
39+
4040
4141 # Initialize the primal driver of SU2, this includes solver preprocessing.
4242 try :
@@ -45,83 +45,65 @@ def main():
4545 print ('A TypeError occured in pysu2.CSinglezoneDriver : ' , exception )
4646 raise
4747
48- print ("\n ------------------------------ Begin Solver -----------------------------" )
49- sys .stdout .flush ()
48+ if rank == 0 :
49+ print ("\n ------------------------------ Begin Solver -----------------------------" )
50+ sys .stdout .flush ()
5051
5152 # we need to add a source term to the energy equation. For this, we need to get the solver and the variable first.
5253 # we then loop over all points and for these points, we add the source term
5354 nDim = driver .GetNumberDimensions ()
5455
5556 # index to the flow solver
5657 iSOLVER = driver .GetSolverIndices ()['INC.FLOW' ]
57- #print("index of flow solver = ",iSOLVER)
5858
5959 # all the indices and the map to the names of the primitives
6060 primindex = driver .GetPrimitiveIndices ()
61- #print("indices of primitives=",primindex)
62- #print("number of primitives:",len(primindex))
63-
64- #print("number of elements:",driver.GetNumberElements())
6561
6662 nVars = driver .GetNumberSolverVars (iSOLVER )
67- #print("number of solver variables:",nVars)
6863 varindex = primindex .copy ()
69- for prim in varindex .copy ():
70- if varindex [prim ] >= nVars :
71- del varindex [prim ]
72- varindex = dict (sorted (varindex .items (), key = lambda item : item [1 ]))
73-
64+ #for prim in varindex.copy():
65+ # if varindex[prim] >=nVars:
66+ # del varindex[prim]
67+ #varindex = dict(sorted(varindex.items(), key=lambda item: item[1]))
7468
75-
76- print ("solver variable names:" ,varindex )
7769 iDENSITY = primindex .get ("DENSITY" )
78- print ("index of density = " ,iDENSITY )
79-
80- index_Vel = varindex .get ("VELOCITY_X" )
81- print ("index of velocity = " ,index_Vel )
82- custom_source_vector = [0.0 for i in range (nVars )]
83- print ("custom source vector = " , custom_source_vector )
70+ #index_Vel = varindex.get("VELOCITY_X")
8471
85- #print("max. number of inner iterations: ",driver.GetNumberInnerIter());
86- #print("max nr of outer iterations: ",driver.GetNumberOuterIter());
87-
88- # is in domain: isdomain = driver.GetNodeDomain(iPoint)
89- #for i_vertex in range(n_vertex)
90- #AllSolutions = driver.GetAllSolutions(iSOLVER)
9172 Body_Force_Vector = [0.0 , - 9.81 , 0.0 ]
9273 DensityInc_0 = driver .GetDensity_FreeStreamND ()
93- #print("rho freestream = ",DensityInc_0)
9474 Force_Ref = driver .GetForce_Ref ()
95- #print("reference force = ",Force_Ref)
9675
97- Iter = driver .GetNumberInnerIter ()
98- print ("1. inner iterations = " ,Iter )
76+ # super important to actually push the commands.
77+ sys .stdout .flush ()
78+
79+ # run N iterations
80+ for inner_iter in range (11 ):
81+ if (rank == 0 ):
82+ print ("python iteration " , inner_iter )
9983
100- for inner_iter in range (Iter ):
10184 # set the source term, per point
102- print (driver .GetNumberNodes () - driver .GetNumberHaloNodes ())
10385 for i_node in range (driver .GetNumberNodes () - driver .GetNumberHaloNodes ()):
104- #SolutionVector = driver.GetSolutionVector(iSOLVER,i_node)
105- PrimitiveVector = driver .GetPrimitiveVector (iSOLVER ,i_node )
106- DensityInc_i = PrimitiveVector [iDENSITY ]
86+ DensityInc_i = driver .Primitives ()(i_node ,iDENSITY )
10787
10888 for iDim in range (nDim ):
109- custom_source_vector [iDim + 1 ] = - (DensityInc_i - DensityInc_0 ) * Body_Force_Vector [iDim ] / Force_Ref
110-
111- #driver.SetPointCustomSource(iSOLVER, i_node, custom_source_vector)
89+ custom_source_vector = (DensityInc_i - DensityInc_0 ) * Body_Force_Vector [iDim ] / Force_Ref
90+ driver .UserDefinedSource (iSOLVER ).Set (i_node ,iDim + 1 ,custom_source_vector )
11291
113- print (" *** inner iteration:" ,inner_iter )
11492 driver .Preprocess (inner_iter )
115-
116- # Run one time iteration.
11793 driver .Run ()
94+
11895 driver .Postprocess ()
11996 driver .Update ()
12097
12198 # Monitor the solver and output solution to file if required.
122- #driver.Monitor(inner_iter)
99+ #stopcalc = driver.Monitor(inner_iter)
123100 driver .Output (inner_iter )
124101
102+ #if (stopcalc):
103+ # if (rank==0):
104+ # "Max iterations or convergence criteria reached, stopping."
105+ # break;
106+
125107 # Finalize the solver and exit cleanly.
126108 driver .Finalize ()
127109
0 commit comments