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
|
# -*- 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.
#-------------------------------------------------------------------------------
import sys, unittest
from code_saturne.model.XMLvariables import Model
from code_saturne.model.XMLengine import *
from code_saturne.model.XMLmodel import *
from code_saturne.model.MainFieldsModel import *
from code_saturne.model.LocalizationModel import LocalizationModel
from code_saturne.model.ThermodynamicsModel import ThermodynamicsModel
from code_saturne.model.SpeciesModel import SpeciesModel
from code_saturne.model.NonCondensableModel import NonCondensableModel
from code_saturne.model.NotebookModel import NotebookModel
class MainFieldsInitializationModel(MainFieldsModel, Variables, Model):
"""
This class manages the turbulence objects in the XML file
"""
def __init__(self, case):
"""
Constuctor.
"""
#
# XML file parameters
MainFieldsModel.__init__(self, case)
self.case = case
self.XMLThermo = self.case.xmlGetNode('thermophysical_models')
self.XMLvariables = self.XMLThermo.xmlGetNode('variables')
self.XMLNonCondvariables = self.XMLThermo.xmlGetNode('non_condensable_list')
self.XMLUserScalar = self.case.xmlGetNode('additional_scalars')
self.XMLScalar = self.XMLUserScalar.xmlInitNode('scalars')
def defaultValues(self):
default = {}
default['enthalpyModel'] = "temperature"
return default
def __verifyZone(self, zone):
"""Private method.
Verify if zone exists and raise ValueError if not.
"""
self.isInt(int(zone))
self.isInList(zone, LocalizationModel('VolumicZone', self.case).getCodeNumbersList())
@Variables.undoLocal
def setFormulaPressure(self, zone, str):
"""
Gives a formula for pressure
"""
self.__verifyZone(zone)
node = self.XMLvariables.xmlGetNode('variable', field_id="none", name="pressure")
if not node:
msg = "There is an error: this node " + str(node) + "should be existed"
raise ValueError(msg)
n = node.xmlInitChildNode('initial_value', zone_id=zone)
n.xmlSetData('formula', str)
@Variables.noUndo
def getFormulaPressure(self, zone):
"""
Return a formula for pressure
"""
self.__verifyZone(zone)
node = self.XMLvariables.xmlGetNode('variable', field_id="none", name="pressure")
if not node:
msg = "There is an error: this node " + str(node) + "should be existed"
raise ValueError(msg)
n = node.xmlInitChildNode('initial_value', zone_id=zone)
return n.xmlGetString('formula')
@Variables.noUndo
def getPressureFormulaComponents(self, zone):
exp = self.getFormulaPressure(zone)
req = [('pressure', 'pressure')]
sym = [('x', "X cell's gravity center"),
('y', "Y cell's gravity center"),
('z', "Z cell's gravity center")]
for (name, val) in NotebookModel(self.case).getNotebookList():
sym.append((name, 'value (notebook) = ' + str(val)))
return exp, req, sym
@Variables.undoLocal
def setFormula(self, zone, fieldId, var_name, str):
"""
Gives a formula for initial values
"""
self.__verifyZone(zone)
self.isInList(fieldId, self.getFieldIdList())
node = self.XMLvariables.xmlGetNode('variable', field_id=fieldId, name=var_name)
n = node.xmlInitChildNode('initial_value', zone_id=zone)
n.xmlSetData('formula', str)
@Variables.noUndo
def getFormula(self, zone, fieldId, var_name):
"""
Return a formula for initial values
"""
self.__verifyZone(zone)
self.isInList(str(fieldId),self.getFieldIdList())
node = self.XMLvariables.xmlGetNode('variable', field_id=fieldId, name=var_name)
n = node.xmlInitChildNode('initial_value', zone_id=zone)
return n.xmlGetString('formula')
@Variables.noUndo
def getFormulaComponents(self, zone, fieldId, var_name):
exp = self.getFormula(zone, fieldId, var_name)
if var_name == 'velocity':
req = [('u', 'Velocity along X'),
('v', 'Velocity along Y'),
('w', 'Velocity along Z')]
sym = [('x', "X cell's gravity center"),
('y', "Y cell's gravity center"),
('z', "Z cell's gravity center")]
elif var_name == 'volume_fraction':
req = [('vol_f', 'volume fraction')]
sym = [('x', "X cell's gravity center"),
('y', "Y cell's gravity center"),
('z', "Z cell's gravity center")]
elif var_name == 'enthalpy':
th_sca_label = self.getEnergyModel(zone, fieldId)
req = [(th_sca_label, str(th_sca_label))]
sym = [('x', "X cell's gravity center"),
('y', "Y cell's gravity center"),
('z', "Z cell's gravity center")]
for (name, val) in NotebookModel(self.case).getNotebookList():
sym.append((name, 'value (notebook) = ' + str(val)))
return exp, req, sym
@Variables.undoLocal
def setFormulaNonCondensable(self, zone, fieldId, var_name, str):
"""
Gives a formula for initial values
"""
self.__verifyZone(zone)
self.isInList(fieldId, self.getFieldIdList())
node = self.XMLNonCondvariables.xmlGetNode('variable', field_id=fieldId, name=var_name)
n = node.xmlInitChildNode('initial_value', zone_id=zone)
n.xmlSetData('formula', str)
@Variables.noUndo
def getFormulaNonCondensable(self, zone, fieldId, var_name):
"""
Return a formula for initial values
"""
self.__verifyZone(zone)
self.isInList(str(fieldId),self.getFieldIdList())
node = self.XMLNonCondvariables.xmlGetNode('variable', field_id=fieldId, name=var_name)
n = node.xmlInitChildNode('initial_value', zone_id=zone)
return n.xmlGetString('formula')
@Variables.noUndo
def getNonCondensableFormulaComponents(self, zone, fieldId, var_name):
exp = self.getFormulaNonCondensable(zone, fieldId, var_name)
label = NonCondensableModel(self.case).getNonCondLabel(var_name)
req = [(label, str(label))]
sym = [('x', "X cell's gravity center"),
('y', "Y cell's gravity center"),
('z', "Z cell's gravity center")]
for (name, val) in NotebookModel(self.case).getNotebookList():
sym.append((name, 'value (notebook) = ' + str(val)))
return exp, req, sym
@Variables.undoLocal
def setFormulaScalar(self, zone, fieldId, var_name, str):
"""
Gives a formula for initial values
"""
self.__verifyZone(zone)
self.isInList(fieldId, self.getFieldIdList())
node = self.XMLScalar.xmlGetNode('variable', field_id=fieldId, name=var_name)
n = node.xmlInitChildNode('initial_value', zone_id=zone)
n.xmlSetData('formula', str)
@Variables.noUndo
def getFormulaScalar(self, zone, fieldId, var_name):
"""
Return a formula for initial values
"""
self.__verifyZone(zone)
self.isInList(str(fieldId),self.getFieldIdList())
node = self.XMLScalar.xmlGetNode('variable', field_id=fieldId, name=var_name)
n = node.xmlInitChildNode('initial_value', zone_id=zone)
return n.xmlGetString('formula')
@Variables.noUndo
def getScalarFormulaComponents(self, zone, fieldId, var_name):
exp = self.getFormulaScalar(zone, fieldId, var_name)
scalar_label = SpeciesModel(self.case).getScalarLabelByName(var_name)
req = [(scalar_label, str(scalar_label))]
sym = [('x', "X cell's gravity center"),
('y', "Y cell's gravity center"),
('z', "Z cell's gravity center")]
for (name, val) in NotebookModel(self.case).getNotebookList():
sym.append((name, 'value (notebook) = ' + str(val)))
return exp, req, sym
@Variables.undoLocal
def setEnergyModel(self, zone, fieldId, model) :
"""
Public method.
Set model for initialization of energy initialization
"""
self.__verifyZone(zone)
self.isInList(model, ('enthalpy', 'temperature', 'hsat_P'))
self.isInList(str(fieldId),self.getFieldIdList())
node = self.XMLvariables.xmlGetNode('variable', field_id=fieldId, name="enthalpy")
if not node:
msg = "There is an error: this node " + str(node) + " should be existed"
raise ValueError(msg)
n = node.xmlInitChildNode('initial_type', zone_id=zone)
n.xmlSetTextNode(model)
if model == "hsat_P" :
n = node.xmlGetNode('initial_value', zone_id=zone)
if n :
n.xmlRemoveNode()
@Variables.noUndo
def getEnergyModel(self, zone, fieldId) :
"""
Public method.
Return model for initialization of energy initialization
"""
self.__verifyZone(zone)
self.isInList(str(fieldId),self.getFieldIdList())
node = self.XMLvariables.xmlGetNode('variable', field_id=fieldId, name="enthalpy")
if not node:
msg = "There is an error: this node " + str(node) + " should be existed"
raise ValueError(msg)
nodem = node.xmlGetChildNode('initial_type', zone_id=zone)
if nodem == None:
model = self.defaultValues()['enthalpyModel']
if ThermodynamicsModel(self.case).getMaterials(fieldId) == 'user_material' :
model = 'enthalpy'
self.setEnergyModel(zone, fieldId, model)
nodem = node.xmlGetChildNode('initial_type', zone_id=zone)
model = nodem.xmlGetTextNode()
return model
#-------------------------------------------------------------------------------
# DefineUsersScalars test case
#-------------------------------------------------------------------------------
class MainFieldsInitializationTestCase(ModelTest):
"""
"""
def checkMainFieldsInitializationInstantiation(self):
"""Check whether the MainFieldsInitialization class could be instantiated"""
model = None
model = MainFieldsInitializationModel(self.case)
assert model != None, 'Could not instantiate MainFieldsInitiaziationModel'
def checkGetandSetInitialVelocity(self):
"""Check whether the MainFieldsInitiaziationModel class could set and get InitialVelocity"""
MainFieldsModel(self.case).addField()
mdl = MainFieldsInitializationModel(self.case)
zone = '1'
mdl.setInitialVelocity(zone, '1', 'VelocityX', 4.5)
mdl.setInitialVelocity(zone, '1', 'VelocityY', -2.5)
mdl.setInitialVelocity(zone, '1', 'VelocityZ', 8.5)
doc = '''<variables>
<variable field_id="none" label="Pressure" name="pressure">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="enthalpy1" name="enthalpy">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="vol_f_1" name="volume_fraction">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="U1" name="VelocityX">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
<initial_value zone_id="1">
4.5
</initial_value>
</variable>
<variable field_id="1" label="V1" name="VelocityY">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
<initial_value zone_id="1">
-2.5
</initial_value>
</variable>
<variable field_id="1" label="W1" name="VelocityZ">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
<initial_value zone_id="1">
8.5
</initial_value>
</variable>
</variables>'''
assert mdl.XMLvariables == self.xmlNodeFromString(doc),\
'Could not set InitialVelocity'
assert mdl.getInitialVelocity('1', '1') == [4.5, -2.5, 8.5],\
'Could not get InitialVelocity'
def checkGetandSetInitialFraction(self):
"""Check whether the MainFieldsInitiaziationModel class could set and get InitialFraction"""
MainFieldsModel(self.case).addField()
mdl = MainFieldsInitializationModel(self.case)
mdl.setInitialFraction('1','1',0.1)
doc = '''<variables>
<variable field_id="none" label="Pressure" name="pressure">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="enthalpy1" name="enthalpy">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="vol_f_1" name="volume_fraction">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
<initial_value zone_id="1">
0.1
</initial_value>
</variable>
<variable field_id="1" label="U1" name="VelocityX">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="V1" name="VelocityY">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="W1" name="VelocityZ">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
</variables>'''
assert mdl.XMLvariables == self.xmlNodeFromString(doc),\
'Could not set InitialFraction'
assert mdl.getInitialFraction('1','1') == 0.1,\
'Could not get InitialFraction'
def checkGetandSetInitialEnergy(self):
"""Check whether the MainFieldsInitiaziationModel class could set and get InitialEnergy"""
MainFieldsModel(self.case).addField()
mdl = MainFieldsInitializationModel(self.case)
mdl.setInitialEnergy('1','1',10)
doc = '''<variables>
<variable field_id="none" label="Pressure" name="pressure">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="enthalpy1" name="enthalpy">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
<initial_value zone_id="1">
10
</initial_value>
</variable>
<variable field_id="1" label="vol_f_1" name="volume_fraction">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="U1" name="VelocityX">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="V1" name="VelocityY">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="W1" name="VelocityZ">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
</variables>'''
assert mdl.XMLvariables == self.xmlNodeFromString(doc),\
'Could not set InitialEnergy'
assert mdl.getInitialEnergy('1','1') == 10,\
'Could not get InitialEnergy'
def checkGetandSetInitialNonCondensable(self):
"""Check whether the MainFieldsInitiaziationModel class could set and get InitialNonCondensable"""
MainFieldsModel(self.case).addField()
MainFieldsModel(self.case).addDefinedField("2", "field2", 'dispersed', 'gas', 'on', 'on', 'off', 2)
from NonCondensableModel import NonCondensableModel
NonCondensableModel(self.case).addNonCondensable()
mdl = MainFieldsInitializationModel(self.case)
mdl.setInitialNonCondensable('1','2','mass_fraction_non_condensable_gas_0',0.23)
doc = '''<non_condensable_list>
<variable field_id="2" label="XX_2" name="mass_fraction_non_condensable_gas_0">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
<initial_value zone_id="1">
0.23
</initial_value>
</variable>
</non_condensable_list>'''
assert mdl.XMLNonCondvariables == self.xmlNodeFromString(doc),\
'Could not set InitialNonCondensable'
assert mdl.getInitialNonCondensable('1','2','mass_fraction_non_condensable_gas_0') == 0.23,\
'Could not get InitialNonCondensable'
def checkGetandSetEnergyModel(self):
"""Check whether the MainFieldsInitiaziationModel class could set and get EnergyModel"""
MainFieldsModel(self.case).addField()
mdl = MainFieldsInitializationModel(self.case)
mdl.setEnergyModel('1','1','temperature')
doc = '''<variables>
<variable field_id="none" label="Pressure" name="pressure">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="enthalpy1" name="enthalpy">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
<initial_type zone_id="1">
temperature
</initial_type>
</variable>
<variable field_id="1" label="vol_f_1" name="volume_fraction">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="U1" name="VelocityX">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="V1" name="VelocityY">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
<variable field_id="1" label="W1" name="VelocityZ">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
</variable>
</variables>'''
assert mdl.XMLvariables == self.xmlNodeFromString(doc),\
'Could not set EnergyModel'
assert mdl.getEnergyModel('1','1') == 'temperature',\
'Could not get EnergyModel'
def checkGetandSetInitialValuePressure(self):
"""Check whether the ThermodynamicsModel class could set and get InitialValuePressure"""
MainFieldsModel(self.case).addField()
mdl = MainFieldsInitializationModel(self.case)
zone = '1'
mdl.setInitialValuePressure(zone, 700000)
doc = '''<variables>
<variable field_id="none" label="Pressure" name="pressure">
<listing_printing status="on"/>
<postprocessing_recording status="on"/>
<initial_value zone_id="1">
700000
</initial_value>
</variable>
</variables>'''
assert mdl.XMLvariables == self.xmlNodeFromString(doc),\
'Could not set Initial Value pressure'
assert mdl.getInitialValuePressure('1') == 700000,\
'Could not get Initial Value pressure'
mdl = ThermodynamicsModel(self.case)
mdl.setInitialValuePressure(1.23)
doc = '''<thermodynamics>
<reference_pressure>
1.23
</reference_pressure>
</thermodynamics>'''
assert mdl.getXMLThermo() == self.xmlNodeFromString(doc),\
'Could not set InitialValuePressure'
assert mdl.getInitialValuePressure() == 1.23,\
'Could not get InitialValuePressure'
def suite():
testSuite = unittest.makeSuite(MainFieldsInitializationTestCase, "check")
return testSuite
def runTest():
print("MainFieldsInitializationTestCase")
runner = unittest.TextTestRunner()
runner.run(suite())
|