1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
|
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2019 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-------------------------------------------------------------------------------
from code_saturne.model.LocalizationModel import BoundaryZone, Zone
from code_saturne.model.BoundaryNeptune import *
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
class LocalizationModel(object):
"""
Abstract class
"""
def __new__(cls, typeZone, case):
"""
"""
if typeZone == 'BoundaryZone':
return BoundaryZone.__new__(BoundaryLocalizationModel, case)
else:
raise ValueError("Unknown type zone")
def __init__(self, typeZone, case):
"""
"""
self.case = case
self._initModel()
self._typeZone = typeZone
def getLocalizationsZonesList(self):
"""
Return list of localizations used by zones
"""
zones = self.getZones()
locals = []
for zone in zones:
locals.append(zone.getLocalization())
return locals
def getLabelsZonesList(self):
"""
Return list of labels used by zones
"""
zones = self.getZones()
labels = []
for zone in zones:
labels.append(zone.getLabel())
return labels
def getCodeNumbersList(self):
"""
Return list of code numbers used
"""
zones = self.getZones()
codes = []
for zone in zones:
codes.append(zone.getCodeNumber())
return codes
def getZones(self):
"""
Return zones list after XML file reading (virtual method)
"""
return []
def getMaxCodeNumber(self):
"""
Return maximum of code number's values to put on name
"""
zones = self.getZones()
codeNumber = 0
for zone in zones:
codeNumber = max(codeNumber, zone.getCodeNumber())
return codeNumber
def getMaxNumberNature(self, nature):
"""
Return maximum of nature number's values to put on name
"""
Model().isInList(nature, self._natureList)
def renameLabel(self, label, newLabel):
"""
"""
if label == newLabel: return
labels = self.getLabelsZonesList()
Model().isInList(label, labels)
Model().isNotInList(newLabel, labels)
def renameName(self, codeNumber, newCodeNumber):
"""
"""
if codeNumber == newCodeNumber: return
labels = self.getLabelsZonesList()
Model().isInt(newCodeNumber)
def replaceLocalization(self, local, newLocal):
"""
"""
if local == newLocal: return
Model().isNotInList(newLocal, self.getLocalizationsZonesList())
def setLocalization(self, label, localization):
"""
Define a new localization for the current zone (zone.getLabel == label).
"""
labels = self.getLabelsZonesList()
Model().isInList(label, labels)
def setNature(self, label, nature):
"""
Define a new nature number for the current zone (zone.getLabel == label)
"""
# Set nature: nothing here, see other setNature reimplementation methods
pass
def addZone(self, newZone = None):
"""
Add a new zone. Management of default values.
"""
if newZone == None:
newZone = Zone(self._typeZone, case = self.case)
zones = self.getZones()
# Set localization
if newZone.getLocalization() == newZone.defaultValues()['localization']:
oldLocalization = ""
newLocalization = ""
for zone in zones:
oldLocalization = zone.getLocalization()
if oldLocalization != "all[]":
if newLocalization == "":
newLocalization = oldLocalization
else:
newLocalization += " or " + oldLocalization
if newLocalization == "":
newLocalization = newZone.defaultValues()['localization']
if newLocalization != "all[]":
newLocalization = "not (" + newLocalization + ")"
newZone.setLocalization(newLocalization)
else:
# No control on localization is availiable
pass
# Set code number
if newZone.getCodeNumber() == newZone.defaultValues()['codeNumber']:
newZone.setCodeNumber(self.getMaxCodeNumber() + 1)
else:
codes = []
for zone in zones:
codes.append(zone.getCodeNumber())
Model().isNotInList(newZone.getCodeNumber(), codes)
# Set label: search a free label (Type of newLabel is: "default_n")
newLabel = newZone.getLabel()
if newLabel == newZone.defaultValues()['label']:
code = 1
inLabels = 1
while (inLabels):
inLabels = 0
for zone in zones:
if newLabel+str(code) == zone.getLabel():
inLabels = 1
break
code += 1
newLabel = newLabel + str(code-1)
newZone.setLabel(newLabel)
else:
labels = []
for zone in zones:
labels.append(zone.getLabel())
Model().isNotInList(newLabel, labels)
# Set nature: nothing here, see other addZone reimplementation methods
return newZone
def replaceZone(self, old_zone, new_zone):
"""
Replace a zone by another in the XML file
"""
newLabel = new_zone.getLabel()
if newLabel == new_zone.defaultValues()['label']:
newLabel = old_zone.getLabel()
self.renameLabel(old_zone.getLabel(), newLabel)
newCodeNumber = new_zone.getCodeNumber()
if newCodeNumber == new_zone.defaultValues()['codeNumber']:
newCodeNumber = old_zone.getCodeNumber()
self.renameName(old_zone.getCodeNumber(), newCodeNumber)
newLocal = new_zone.getLocalization()
if newLocal == new_zone.defaultValues()['localization']:
newLocal = old_zone.getLocalization()
self.replaceLocalization(old_zone.getLocalization(), newLocal)
return newLabel, newCodeNumber, newLocal
def deleteZone(self, label):
"""
Delete the current zone (zone.getLabel == label)
"""
pass
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
class BoundaryLocalizationModel(LocalizationModel):
"""
"""
def __new__(cls, case):
"""
Constructor
"""
return object.__new__(cls)
def _initModel(self):
"""
Initialize mode
"""
self.__XMLBoundaryConditionsNode = self.case.xmlInitNode('boundary_conditions')
self.__natureList = Zone('BoundaryZone', case = self.case).getNatureList()
@Variables.noUndo
def getZones(self):
"""
Get zones in the XML file
"""
XMLZonesNodes = self.__XMLBoundaryConditionsNode.xmlGetChildNodeList('boundary', 'label', 'name', 'nature')
#
# XML file reading
zones = []
for node in XMLZonesNodes:
label = str(node['label'])
nature = str(node['nature'])
codeNumber = int(node['name'])
localization = str(node.xmlGetTextNode())
zone = Zone('BoundaryZone', case = self.case, label = label, codeNumber = codeNumber, localization = localization, nature = nature)
zones.append(zone)
return zones
@Variables.noUndo
def getMaxNumberNature(self, nature):
"""
Return maximum of nature number's values to put on name
"""
XMLZonesNodes = self.__XMLBoundaryConditionsNode.xmlGetChildNodeList('boundary', 'label', 'name', 'nature')
max = 0
#
# XML file reading
zones = []
for node in XMLZonesNodes:
if node['nature'] == nature:
max = max + 1
return max
@Variables.undoLocal
def setLabel(self, label, newLabel):
"""
Define a new label for the current zone (zone.getLabel == label)
Update XML file
"""
LocalizationModel.renameLabel(self, label, newLabel)
#
# XML file updating
node = self.__XMLBoundaryConditionsNode.xmlGetChildNode('boundary', 'name', 'nature', label = label )
node['label'] = newLabel
nature = node['nature']
#
# Update references
XMLZonesNodes = self.__XMLBoundaryConditionsNode.xmlGetChildNodeList(nature, label = label)
for node in XMLZonesNodes:
node['label'] = newLabel
@Variables.undoLocal
def setLocalization(self, label, localization):
"""
Define a new localization for the current zone (zone.getLabel == label)
Update XML file
"""
LocalizationModel.setLocalization(self, label, localization)
#
# XML file updating
node = self.__XMLBoundaryConditionsNode.xmlGetChildNode('boundary', 'name', 'nature', label = label)
node.xmlSetTextNode(localization)
@Variables.undoLocal
def setCodeNumber(self, label, codeNumber):
"""
Define a new code number for the current zone (zone.getLabel == label)
Update XML file
"""
LocalizationModel.setCodeNumber(self, label, codeNumber)
#
# XML file updating
node = self.__XMLBoundaryConditionsNode.xmlGetChildNode('boundary', 'name', 'nature', label = label)
node['name'] = str(codeNumber)
@Variables.undoGlobal
def setNature(self, label, nature):
"""
Define a new Nature for the current zone (zone.getLabel == label)
Update XML file
"""
LocalizationModel.setNature(self, label, nature)
# XML file updating
node = self.__XMLBoundaryConditionsNode.xmlGetChildNode('boundary', 'name', 'nature', label = label)
oldNature = node['nature']
node['nature'] = str(nature)
# Delete oldNature boundary
Boundary(oldNature, label, self.case).delete()
# Create nature boundary
Boundary(nature, label, self.case)
@Variables.undoGlobal
def addZone(self, zone = None):
"""
Add a new zone in the XML file
"""
newZone = LocalizationModel.addZone(self, zone)
# XML file updating
node = self.__XMLBoundaryConditionsNode.xmlInitNode('boundary',
label = newZone.getLabel(),
name = str(newZone.getCodeNumber()),
nature = newZone.getNature())
node.xmlSetTextNode(newZone.getLocalization())
# Create nature boundary
Boundary(newZone.getNature(), newZone.getLabel(), self.case)
return newZone
@Variables.undoGlobal
def replaceZone(self, old_zone, new_zone):
"""
Replace a zone by another in the XML file
"""
# if nature of zone change we delete old zone
if (new_zone.getNature() != old_zone.getNature()):
Boundary(old_zone.getNature(), old_zone.getLabel(), self.case).delete()
newLabel, newCodeNumber, newLocal = LocalizationModel.replaceZone(self, old_zone, new_zone)
newNature = new_zone.getNature()
Model().isInList(newNature, self.__natureList)
node = self.__XMLBoundaryConditionsNode.xmlGetNode('boundary',
label = old_zone.getLabel())
node['label'] = newLabel
node['name'] = newCodeNumber
node['nature'] = newNature
node.xmlSetTextNode(newLocal)
if (new_zone.getNature() != old_zone.getNature()):
Boundary(new_zone.getNature(), new_zone.getLabel(), self.case)
else:
label = old_zone.getLabel()
codeNumber = old_zone.getCodeNumber()
localis = old_zone.getLocalization()
newLabel = new_zone.getLabel()
if label != newLabel:
self.setLabel(label, newLabel)
newCodeNumber = new_zone.getCodeNumber()
if codeNumber != newCodeNumber:
self.setCodeNumber(codeNumber, newCodeNumber)
newLocal = new_zone.getLocalization()
if localis != newLocal:
self.setLocalization(newLabel, newLocal)
@Variables.undoGlobal
def deleteZone(self, label):
"""
Delete a zone in the XML file
"""
LocalizationModel.deleteZone(self, label)
# Get Nature
node = self.__XMLBoundaryConditionsNode.xmlGetNode('boundary', 'name', 'nature', label = label)
nature = node['nature']
node.xmlRemoveNode()
# Delete nature boundary
Boundary(nature, label, self.case).delete()
def getXMLBoundaryConditionsNode(self):
return self.__XMLBoundaryConditionsNode
#-------------------------------------------------------------------------------
# DefineUsersScalars test case
#-------------------------------------------------------------------------------
class LocalizationTestCase(ModelTest):
"""
"""
def checkLocalizationInstantiation(self):
"""Check whether the LocalizationModel class could be instantiated"""
model = None
model = LocalizationModel("BoundaryZone",self.case)
assert model != None, 'Could not instantiate LocalizationModel'
def checkAddAndDeleteZone(self):
"""Check whether the zone could be added and deleted"""
model = LocalizationModel("BoundaryZone",self.case)
model.addZone()
doc = '''<boundary_conditions>
<boundary label="BC_1" name="1" nature="wall">
all[]
</boundary>
<wall field_id="none" label="BC_1"/>
</boundary_conditions>'''
assert self.case.xmlGetNode('boundary_conditions') == self.xmlNodeFromString(doc),\
'Could not add zone'
model.deleteZone(label='BC_1')
doc = '''<boundary_conditions/>'''
assert self.case.xmlGetNode('boundary_conditions') == self.xmlNodeFromString(doc),\
'Could not delete zone'
def checkReplaceZone(self):
"""Check whether the zone could be replaced for boundary conditions."""
model = LocalizationModel("BoundaryZone", self.case)
zone1 = Zone("BoundaryZone", label='entre1', localization="porte", nature='inlet')
model.addZone(zone1)
zone4 = Zone("BoundaryZone", label='hublot', localization="2 et 3", nature='symmetry')
model.replaceZone(zone1, zone4)
doc = '''<boundary_conditions>
<boundary label="hublot" name="1" nature="symmetry">
2 et 3
</boundary>
</boundary_conditions>'''
assert self.case.xmlGetNode('boundary_conditions') == self.xmlNodeFromString(doc),\
'Could not replace zone in localizationModel for boundaries conditions'
def checkSetLabelAndLocalization(self):
"""Check whether the zone could be replaced for boundary conditions."""
model = LocalizationModel("BoundaryZone", self.case)
zone1 = Zone("BoundaryZone", label='entre1', localization="porte", nature='inlet')
model.addZone(zone1)
model.setLabel('entre1','entry1')
doc = '''<boundary_conditions>
<boundary label="entry1" name="1" nature="inlet">
porte
</boundary>
</boundary_conditions>'''
assert self.case.xmlGetNode('boundary_conditions') == self.xmlNodeFromString(doc),\
'Could not set Label'
model.setLocalization('entry1','door')
doc = '''<boundary_conditions>
<boundary label="entry1" name="1" nature="inlet">
door
</boundary>
</boundary_conditions>'''
assert self.case.xmlGetNode('boundary_conditions') == self.xmlNodeFromString(doc),\
'Could not set Localization'
def checkSetNature(self):
"""Check whether the zone could be replaced for boundary conditions."""
model = LocalizationModel("BoundaryZone", self.case)
zone1 = Zone("BoundaryZone", label='entre1', localization="porte", nature='inlet')
model.addZone(zone1)
model.setNature('entre1','wall')
doc = '''<boundary_conditions>
<boundary label="entre1" name="1" nature="wall">
porte
</boundary>
<wall field_id="none" label="entre1"/>
</boundary_conditions>'''
assert self.case.xmlGetNode('boundary_conditions') == self.xmlNodeFromString(doc),\
'Could not set Nature'
def suite1():
testSuite = unittest.makeSuite(LocalizationTestCase, "check")
return testSuite
def runTest1():
print("LocalizationTestCase")
runner = unittest.TextTestRunner()
runner.run(suite1())
|