File: OutputFieldsModel.py

package info (click to toggle)
code-saturne 6.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 63,340 kB
  • sloc: ansic: 354,724; f90: 119,812; python: 87,716; makefile: 4,653; cpp: 4,272; xml: 2,839; sh: 1,228; lex: 170; yacc: 100
file content (480 lines) | stat: -rw-r--r-- 17,128 bytes parent folder | download
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
# -*- 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 MainFieldsModel
from code_saturne.model.TimeAveragesModel import TimeAveragesModel

#-------------------------------------------------------------------------------
# Constructor
#-------------------------------------------------------------------------------

class OutputFieldsModel(MainFieldsModel, Variables, Model):
    """
    This class manages the Field objects in the XML file
    """

    def __init__(self, case):
        """
        Constuctor.
        """
        #
        # XML file parameters
        MainFieldsModel.__init__(self, case)
        self.case           = case
        self.XMLNodethermo  = self.case.xmlGetNode('thermophysical_models')
        self.analysis_ctrl  = self.case.xmlInitNode('analysis_control')
        self.node_output    = self.analysis_ctrl.xmlInitNode('output')
        self.node_probe     = self.node_output.xmlGetNodeList('probe','name')


    def defaultValues(self):
        default = {}
        default['listing'] = 'on'
        default['writer']  = 'on'
        return default


    def getVariableLabelsList(self) :
        """
        Return list of label for variable
        """
        lst = []
        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            for node in self.case.xmlGetNodeList(variableType) :
                lst.append(node['label'])
        return lst


    def getVariableProbeList(self):
        """
        Return list of node for probes
        Only for the View
        """
        probeList = []
        for node in self.node_probe:
            probeList.append(node['name'])
        return probeList


    @Variables.undoLocal
    def setVariableLabel(self, fieldId, label, oldlabel) :
        """
        set label
        """
        lst = self.getFieldIdList()
        lst.append("none")
        self.isInList(fieldId, lst)

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            node = self.case.xmlGetNode(variableType, field_id = str(fieldId), label = oldlabel)
            if node != None:
                break

        if node != None:
            if variableType != 'time_average':
                node['label'] = label
            else:
                TimeAveragesModel(self.case).setLabel(node['label'], label)

        else :
            msg = "This variable " + oldlabel + " doesn't exist"
            raise ValueError(msg)

        # udpate label in formula if needed
        if oldlabel != '':
            for no in self.case.xmlGetNodeList('formula'):
                txt = no.xmlGetTextNode()
                if txt != None:
                    f = txt.replace(oldlabel, label)
                    no.xmlSetTextNode(f)


    @Variables.noUndo
    def getVariableLabel(self, fieldId, name) :
        """
        return label of name variable for fieldId
        """
        lst = self.getFieldIdList()
        lst.append("none")
        self.isInList(fieldId, lst)

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            node = self.case.xmlGetNode(variableType, field_id = str(fieldId), name = name)
            if node != None:
                break

        if node != None:
            label = node['label']
            return label
        else :
            msg = "This variable " + name + " doesn't exist"
            raise ValueError(msg)


    @Variables.undoLocal
    def setListingStatus(self, fieldId, label, status) :
        """
        return status for listing output for variable name on fieldId
        """
        self.isOnOff(status)
        lst = self.getFieldIdList()
        lst.append("none")
        self.isInList(fieldId, lst)

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            node = self.case.xmlGetNode(variableType, field_id = str(fieldId), label = label)
            if node != None:
                break

        if node != None:
            n = node.xmlGetNode('listing_printing')
            n['status'] = status
        else :
            msg = "This variable " + label + " doesn't exist"
            raise ValueError(msg)


    @Variables.noUndo
    def getListingStatus(self, fieldId, name) :
        """
        return status for listing output for variable name on fieldId
        """
        lst = self.getFieldIdList()
        lst.append("none")
        self.isInList(fieldId, lst)

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            node = self.case.xmlGetNode(variableType, field_id = str(fieldId), name = name)
            if node != None:
                break

        if node != None:
            value = self.defaultValues()['listing']
            n = node.xmlGetNode('listing_printing')
            if n :
                value = n['status']
            return value
        else :
            msg = "This variable " + name + " doesn't exist"
            raise ValueError(msg)


    @Variables.undoLocal
    def setPostProcessingStatus(self, fieldId, label, status) :
        """
        return status for post processing for variable name on fieldId
        """
        self.isOnOff(status)
        lst = self.getFieldIdList()
        lst.append("none")
        self.isInList(fieldId, lst)

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            node = self.case.xmlGetNode(variableType, field_id = str(fieldId), label = label)
            if node != None:
                break

        if node != None:
            n = node.xmlGetNode('postprocessing_recording')
            n['status'] = status
        else :
            msg = "This variable " + label + " doesn't exist"
            raise ValueError(msg)


    @Variables.noUndo
    def getPostProcessingStatus(self, fieldId, name) :
        """
        return status for post processing for variable name on fieldId
        """
        lst = self.getFieldIdList()
        lst.append("none")
        self.isInList(fieldId, lst)

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            node = self.case.xmlGetNode(variableType, field_id = str(fieldId), name = name)
            if node != None:
                break

        if node != None:
            value = self.defaultValues()['writer']
            n = node.xmlGetNode('postprocessing_recording')
            if n :
                value = n['status']
            return value
        else :
            msg = "This variable " + name + " doesn't exist"
            raise ValueError(msg)


    @Variables.undoGlobal
    def setProbesList(self, fieldId, label, probes):
        """
        return list of probes for variable name on fieldId
        """
        l = self.getFieldIdList()
        l.append("none")
        self.isInList(fieldId, l)

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            node = self.case.xmlGetNode(variableType, field_id = str(fieldId), label = label)
            if node != None:
                break
        if not node:
            msg = "This variable " + label + " doesn't exist"
            raise ValueError(msg)

        l1 = string.split(probes)
        l2 = self.getVariableProbeList()
        l1.sort()
        l2.sort()

        if l1 == l2:
            # if all probes are selected: no markup is nedded in the xml file
            node.xmlRemoveChild('probes')
            node.xmlRemoveChild('no_probe')
            return
        else:
            try:
                node.xmlRemoveChild('probes')
                node.xmlRemoveChild('no_probe')
            except:
                pass

            if l1:
                n = node.xmlInitNode('probes')
                l3 = []
                for probe in l1:
                    if probe not in l3 and probe in l2:
                        l3.append(probe)
                        n.xmlInitChildNodeList('probe_recording', name = probe)
            else:
                n = node.xmlInitNode('no_probe')


    @Variables.noUndo
    def getProbesList(self, fieldId, name):
        """
        return list of probes for variable name on fieldId
        """
        l = self.getFieldIdList()
        l.append("none")
        self.isInList(fieldId, l)

        l1 = self.getVariableProbeList()

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            node = self.case.xmlGetNode(variableType, field_id = str(fieldId), name = name)
            if node != None:
                break
        else:
            msg = "This variable " + name + " doesn't exist"
            raise ValueError(msg)


        node_probes = node.xmlGetChildNode('probes')

        if node.xmlGetChildNode('no_probe'):
            if node_probes:
                node_probes.xmlRemoveNode()
            return []

        if node_probes:
            l2 = node_probes.xmlGetChildNodeList('probe_recording')
            l3 = []

            if l2 == []:
                node_probes.xmlRemoveNode()
            else:
                for n in l2:
                    if n['name'] not in l3:
                        l3.append(n['name'])

            return l3
        else:
            return l1


    def getGlobalVariables(self) :
        """
        return list of variables with none field criteria
        """
        llst = {}
        lst = []

        for variableType in ('variable', 'property', 'scalar', 'time_average') :
            for node in self.case.xmlGetNodeList(variableType, field_id = "none"):
                if not node['name'].startswith("User_"):
                    lst.append(node['name'])
                else:
                    idx = node['name'].split('_')[1]
                    tmp = idx.zfill(4)
                    llst[tmp] = node['name']
        keys = list(llst.keys())
        keys.sort()

        for key in keys:
            lst.append(llst[key])

        return lst


    def getFieldVariables(self, fieldId) :
        """
        return list of variables with none field criteria
        """
        self.isInList(str(fieldId),self.getFieldIdList())
        lst = []
        for variableType in ('variable', 'property', 'scalar'):
            for node in self.case.xmlGetNodeList(variableType, field_id = str(fieldId)):
                lst.append(node['name'])

        return lst

#-------------------------------------------------------------------------------
# DefineUsersScalars test case
#-------------------------------------------------------------------------------

class OutputFieldsTestCase(ModelTest):
    """
    """
    def checkOutputFieldsInstantiation(self):
        """Check whether the OutputFieldsModel class could be instantiated"""
        model = None
        model = OutputFieldsModel(self.case)
        assert model != None, 'Could not instantiate OutputFieldsModel'


    def checkGetVariableLabelsList(self):
        """Check whether the  OutputFieldsModel class could get the VariableLabelsList"""
        MainFieldsModel(self.case).addField()
        mdl = OutputFieldsModel(self.case)
        assert mdl.getVariableLabelsList() == ['Pressure', 'enthalpy1', 'alpha1', 'U1', 'V1', 'W1', 'Temp1', 'density1', 'Lam_vis1', 'Sp_heat1', 'Th_cond1', 'mass_trans1'],\
            'Could not get VariableLabelsList'


#    def checkGetVariableProbeList(self):
#        """Check whether the  NonCondensableModel class could get the VariableLabelsList"""
#        MainFieldsModel(self.case).addField()
#        mdl = OutputFieldsModel(self.case)
#        mdl.setProbesList('1','U1','1')
#        print(mdl.getProbesList('1','VelocityX'))
#        print(mdl.getVariableProbeList())
#        print(mdl.case)
#        assert mdl.getVariableLabelsList() == ,\
#            'Could not get VariableLabelsList'


    def checkGetandSetVariableLabel(self):
        """Check whether the OutputFieldsModel class could set and get VariableLabel"""
        MainFieldsModel(self.case).addField()
        mdl = OutputFieldsModel(self.case)
        mdl.setVariableLabel('1','Vitesse1','U1')
        doc = '''<variable field_id="1" label="Vitesse1" name="velocityX">
                         <listing_printing status="on"/>
                         <postprocessing_recording status="on"/>
                 </variable>'''
        assert mdl.case.xmlGetNode('variable', field_id = str('1'), label = 'Vitesse1') == self.xmlNodeFromString(doc),\
            'Could not set VariableLabel'
        assert mdl.getVariableLabel('1','velocityX') == 'Vitesse1',\
            'Could not get VariableLabel'


    def checkGetandSetListingStatus(self):
        """Check whether the OutputFieldsModel class could set and get ListingStatus"""
        MainFieldsModel(self.case).addField()
        mdl = OutputFieldsModel(self.case)
        mdl.setListingStatus('1','U1','off')
        doc = '''<variable field_id="1" label="U1" name="velocityX">
                         <listing_printing status="off"/>
                         <postprocessing_recording status="on"/>
                 </variable>'''
        assert mdl.case.xmlGetNode('variable', field_id = str('1'), label = 'U1') == self.xmlNodeFromString(doc),\
            'Could not set ListingStatus'
        assert mdl.getListingStatus('1','velocityX') == 'off',\
            'Could not get ListingStatus'


    def checkGetandSetPostProcessingStatus(self):
        """Check whether the OutputFieldsModel class could set and get PostProcessingStatus"""
        MainFieldsModel(self.case).addField()
        mdl = OutputFieldsModel(self.case)
        mdl.setPostProcessingStatus('1','U1','off')
        doc = '''<variable field_id="1" label="U1" name="velocityX">
                         <listing_printing status="on"/>
                         <postprocessing_recording status="off"/>
                 </variable>'''
        assert mdl.case.xmlGetNode('variable', field_id = str('1'), label = 'U1') == self.xmlNodeFromString(doc),\
            'Could not set PostProcessingStatus'
        assert mdl.getPostProcessingStatus('1','velocityX') == 'off',\
            'Could not get PostProcessingStatus'


    def checkGetandSetProbesList(self):
        """Check whether the OutputFieldsModel class could set and get ProbesList"""
        MainFieldsModel(self.case).addField()
        mdl = OutputFieldsModel(self.case)
        mdl.setProbesList('1','U1','1')
        doc = '''<variable field_id="1" label="U1" name="velocityX">
                         <listing_printing status="on"/>
                         <postprocessing_recording status="on"/>
                         <probes>
                                 <probe_recording name="1"/>
                         </probes>
                 </variable>'''
        assert mdl.case.xmlGetNode('variable', field_id = str('1'), label = 'U1') == self.xmlNodeFromString(doc),\
            'Could not set ProbesList'
        assert mdl.getProbesList('1','velocityX') == ['1'],\
            'Could not get ProbesList'


    def checkGetGlobalVariables(self):
        """Check whether the  OutputFieldsModel class could get GlobalVariables"""
        MainFieldsModel(self.case).addField()
        mdl = OutputFieldsModel(self.case)
        assert mdl.getGlobalVariables() == ['Pressure'],\
            'Could not get GlobalVariables'


    def checkGetFieldVariables(self):
        """Check whether the  OutputFieldsModel class could get FieldVariables"""
        MainFieldsModel(self.case).addField()
        mdl = OutputFieldsModel(self.case)
        assert mdl.getFieldVariables('1') == ['Enthalpy', 'VolumeFraction', 'VelocityX', 'VelocityY', 'VelocityZ', 'Temperature', 'density', 'molecular_viscosity', 'specific_heat', 'thermal_conductivity', 'mass_trans'],\
            'Could not get FieldVariables'


def suite():
    testSuite = unittest.makeSuite(OutputFieldsTestCase, "check")
    return testSuite


def runTest():
    print("OutputFieldsTestCase")
    runner = unittest.TextTestRunner()
    runner.run(suite())