Skip to content

Commit 73e91de

Browse files
committed
Update Code_Aster adapter
1 parent f00c4cd commit 73e91de

File tree

2 files changed

+78
-73
lines changed

2 files changed

+78
-73
lines changed

adapter.comm

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sys.path.insert(0, adapter_root)
77
from adapter import *
88

99
# Get participant name from environment variable
10-
# Note: In OpenFOAM and CalculiX, this is read from a command line argument (--precice-participant),
10+
# Note: In the OpenFOAM and CalculiX adapters, this is read from a command line argument (--precice-participant),
1111
# but I don't know how to pass my own argument to as_run
1212
participantName = os.environ["PRECICE_PARTICIPANT"]
1313

@@ -19,42 +19,34 @@ participantName = os.environ["PRECICE_PARTICIPANT"]
1919
# floating point exception in Code_Aster, and therefore the .yml file cannot be read!
2020
# ==========================================================================================
2121

22+
# Include adapter configuration .comm.
2223
# Defines: preciceConfigFile, interfaces, participantName
2324
INCLUDE(UNITE=90)
24-
precice = PySolverInterface(participantName, 0, 1)
2525
preciceConfigFile = settings["base-path"] + "/" + settings["precice-config-file"]
26-
precice.configure(preciceConfigFile)
27-
2826
participant = settings["participants"][participantName]
2927
isSteadyState = ("steady-state" in settings["simulation"]) and (settings["simulation"]["steady-state"])
3028

3129
# ==========================================================================================
3230
# Include file in UNITE 91: Setup Aster case
3331
# ==========================================================================================
3432

33+
# Include case definition.comm.
3534
# Defines: MESH, MODEL, BC[], MAT[], MATS
3635
INCLUDE(UNITE=91)
3736

3837
# ==========================================================================================
39-
# Get interface mesh
38+
# Create and initialize the adapter
4039
# ==========================================================================================
4140

42-
adapter = Adapter(precice, participantName, participant["interfaces"], MESH, MODEL, MAT,
41+
adapter = Adapter(preciceConfigFile,
42+
participantName,
43+
participant["interfaces"],
44+
MESH, MODEL, MAT,
4345
isNonLinear=participant["non-linear"])
4446
BCs = [{'CHARGE': bc} for bc in BC]
4547
LOADS = BCs + adapter.LOADS
4648

47-
# ==========================================================================================
48-
# Setup preCICE
49-
# ==========================================================================================
50-
51-
preciceDt = precice.initialize()
52-
53-
if precice.isActionRequired(PyActionWriteInitialData()):
54-
adapter.sendCouplingData(INIT_T, preciceDt)
55-
precice.fulfilledAction(PyActionWriteInitialData())
56-
57-
precice.initializeData()
49+
dt = adapter.initialize(INIT_T)
5850

5951
# ==========================================================================================
6052
# ==========================================================================================
@@ -64,27 +56,12 @@ precice.initializeData()
6456

6557
k = 0
6658
time = 0.0
67-
6859
ICOND = {'CHAM_NO': INIT_T}
6960

70-
while precice.isCouplingOngoing():
61+
while adapter.isCouplingOngoing():
7162

72-
# ======================================================================================
73-
# Write checkpoint if necessary
74-
# ======================================================================================
75-
76-
if precice.isActionRequired(PyActionWriteIterationCheckpoint()):
77-
if time == 0:
78-
ICOND = {'CHAM_NO': INIT_T}
79-
else:
80-
ICOND = {'EVOL_THER': PRV_TEMP}
81-
precice.fulfilledAction(PyActionWriteIterationCheckpoint())
82-
83-
# ======================================================================================
84-
# Read and set boundary conditions
85-
# ======================================================================================
86-
87-
adapter.receiveCouplingData()
63+
adapter.writeCheckpoint()
64+
adapter.readCouplingData()
8865

8966
# ======================================================================================
9067
# Solve
@@ -106,7 +83,7 @@ while precice.isCouplingOngoing():
10683
EXCIT=LOADS,
10784
ETAT_INIT=_F(STATIONNAIRE='OUI'),
10885
)
109-
86+
11087
T = CREA_CHAMP(
11188
RESULTAT=TEMP,
11289
NOM_CHAM='TEMP',
@@ -118,7 +95,7 @@ while precice.isCouplingOngoing():
11895

11996
STEP = DEFI_LIST_REEL(
12097
DEBUT=time,
121-
INTERVALLE=(_F(JUSQU_A=time+preciceDt, NOMBRE=1))
98+
INTERVALLE=(_F(JUSQU_A=time+dt, NOMBRE=1))
12299
)
123100

124101
if participant["non-linear"]:
@@ -139,7 +116,7 @@ while precice.isCouplingOngoing():
139116
INCREMENT=_F(LIST_INST=STEP),
140117
PARM_THETA=1.0
141118
)
142-
119+
143120
T = CREA_CHAMP(
144121
RESULTAT=TEMP,
145122
NOM_CHAM='TEMP',
@@ -148,48 +125,38 @@ while precice.isCouplingOngoing():
148125
NUME_ORDRE=1,
149126
)
150127

151-
# ======================================================================================
152-
# Extract and write boundary values
153-
# ======================================================================================
154-
155-
adapter.sendCouplingData(T, preciceDt)
128+
adapter.writeCouplingData(T)
129+
dt = adapter.advance()
130+
adapter.readCheckpoint()
156131

157-
# ======================================================================================
158-
# Advance
159-
# ======================================================================================
160-
161-
preciceDt = precice.advance(preciceDt)
162-
163-
# ======================================================================================
164-
# Check convergence, read checkpoint if necessary
165-
# ======================================================================================
166-
167-
if precice.isActionRequired(PyActionReadIterationCheckpoint()):
168-
precice.fulfilledAction(PyActionReadIterationCheckpoint())
169-
else:
170-
if time > 0:
171-
DETRUIRE(CONCEPT=_F(NOM=PRV_TEMP))
172-
PRV_TEMP = COPIER(CONCEPT=TEMP)
173-
132+
if adapter.isCouplingTimestepComplete():
133+
134+
# Output if necessary
135+
k += 1
174136
if k % settings["simulation"]["output-frequency"] == 0:
175137
filename = "output-" + str(k) + ".rmed"
176138
DEFI_FICHIER(FICHIER=filename, UNITE=80)
177-
IMPR_RESU(FORMAT='MED', RESU=_F(RESULTAT=TEMP));
139+
IMPR_RESU(FORMAT='MED', RESU=_F(RESULTAT=TEMP))
178140
call(["mv", filename, settings["base-path"] + "/" + participant["directory"]])
179141
DEFI_FICHIER(ACTION='LIBERER', UNITE=80)
180-
181-
k = k + 1
182142

183-
time = time + preciceDt
143+
# Use current solution as initial condition for the next time step
144+
if time > 0:
145+
DETRUIRE(CONCEPT=_F(NOM=TEMP_CPY))
146+
TEMP_CPY = COPIER(CONCEPT=TEMP)
147+
ICOND = {'EVOL_THER': TEMP_CPY}
148+
time = time + dt
184149

185150
# ======================================================================================
186151
# Destroy Aster objects
187152
# ======================================================================================
188-
153+
189154
DETRUIRE(CONCEPT=_F(NOM=TEMP))
190155
DETRUIRE(CONCEPT=_F(NOM=T))
191156

192157
if not isSteadyState:
193158
DETRUIRE(CONCEPT=_F(NOM=STEP))
194159

160+
adapter.finalize()
161+
195162
FIN()

adapter.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@
1717

1818
class Adapter:
1919

20-
def __init__(self, precice, participantName, config, MESH, MODEL, MAT, isNonLinear=False):
20+
def __init__(self, preciceConfigFile, participantName, config, MESH, MODEL, MAT, isNonLinear=False):
2121
self.interfaces = []
2222
self.numInterfaces = len(config)
23-
self.precice = precice
2423
self.MESH = MESH
2524
self.MODEL = MODEL
2625
self.MAT = MAT
2726
self.LOADS = []
2827
self.isNonLinear = isNonLinear
2928
self.participantName = participantName
29+
self.preciceDt = -1
30+
self.precice = PySolverInterface(participantName, 0, 1)
31+
self.precice.configure(preciceConfigFile)
3032
self.configure(config)
3133

3234
def configure(self, config):
3335
L = [None] * self.numInterfaces # Loads
3436
SM = [None] * self.numInterfaces # Shifted meshes
3537
for i in range(self.numInterfaces):
36-
# Shifted mesh
38+
# Shifted mesh (interface nodes displaced by a distance delta in the direction of the surface normal
39+
# towards the inside of the solid)
3740
SM[i] = CREA_MAILLAGE(MAILLAGE=self.MESH, RESTREINT={"GROUP_MA": config[i]["patch"], "GROUP_NO": config[i]["patch"]})
3841
# Create interface
3942
interface = Interface(self.precice, self.participantName, config[i], self.MESH, SM[i], self.MODEL, self.MAT[config[i]["material-id"]], self.isNonLinear)
@@ -43,17 +46,52 @@ def configure(self, config):
4346
interface.setLoad(L[i])
4447
self.LOADS.append({'CHARGE': L[i]})
4548
self.interfaces.append(interface)
49+
50+
def initialize(self, INIT_T):
51+
52+
self.preciceDt = self.precice.initialize()
53+
54+
if self.precice.isActionRequired(PyActionWriteInitialData()):
55+
self.writeCouplingData(INIT_T)
56+
self.precice.fulfilledAction(PyActionWriteInitialData())
57+
58+
self.precice.initializeData()
59+
60+
return self.preciceDt
61+
62+
def isCouplingOngoing(self):
63+
return self.precice.isCouplingOngoing()
4664

47-
def sendCouplingData(self, TEMP, dt=0):
48-
if (dt == 0) or (dt > 0 and self.precice.isWriteDataRequired(dt)):
65+
def writeCouplingData(self, TEMP):
66+
if self.precice.isWriteDataRequired(self.preciceDt):
4967
for interface in self.interfaces:
5068
interface.writeBCs(TEMP)
5169

52-
def receiveCouplingData(self):
70+
def readCouplingData(self):
5371
if self.precice.isReadDataAvailable():
5472
for interface in self.interfaces:
5573
interface.readAndUpdateBCs()
5674

75+
def writeCheckpoint(self):
76+
if self.precice.isActionRequired(PyActionWriteIterationCheckpoint()):
77+
# Do nothing
78+
self.precice.fulfilledAction(PyActionWriteIterationCheckpoint())
79+
80+
def readCheckpoint(self):
81+
if self.precice.isActionRequired(PyActionReadIterationCheckpoint()):
82+
# Do nothing
83+
self.precice.fulfilledAction(PyActionReadIterationCheckpoint())
84+
85+
def isCouplingTimestepComplete(self):
86+
return self.precice.isTimestepComplete()
87+
88+
def advance(self):
89+
self.preciceDt = self.precice.advance(self.preciceDt)
90+
return self.preciceDt
91+
92+
def finalize(self):
93+
self.precice.finalize()
94+
5795

5896
class Interface:
5997

@@ -238,12 +276,12 @@ def writeBCs(self, TEMP):
238276
self.precice.writeBlockScalarData(self.writeTempDataID, self.writeDataSize, self.preciceNodeIndices, writeTemp)
239277

240278
def getBoundaryValues(self, T):
241-
279+
242280
# Sink temperature
243281
TPROJ = PROJ_CHAMP(MAILLAGE_1=self.MESH, MAILLAGE_2=self.SHMESH, CHAM_GD=T, METHODE='COLLOCATION')
244282
writeTemp = TPROJ.EXTR_COMP(lgno=[self.groupName]).valeurs
245283
DETRUIRE(CONCEPT=({'NOM': TPROJ}))
246-
284+
247285
# Heat transfer coefficient
248286
self.updateConductivity(writeTemp)
249287
writeHCoeff = np.array(self.conductivity) / self.delta

0 commit comments

Comments
 (0)