1# -*- coding: utf-8 -*-
2
3#-------------------------------------------------------------------------------
4
5# This file is part of Code_Saturne, a general-purpose CFD tool.
6#
7# Copyright (C) 1998-2021 EDF S.A.
8#
9# This program is free software; you can redistribute it and/or modify it under
10# the terms of the GNU General Public License as published by the Free Software
11# Foundation; either version 2 of the License, or (at your option) any later
12# version.
13#
14# This program is distributed in the hope that it will be useful, but WITHOUT
15# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17# details.
18#
19# You should have received a copy of the GNU General Public License along with
20# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
21# Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23#-------------------------------------------------------------------------------
24
25"""
26This module defines the differents possible outputs : listings, for ensights
27chronologic and historic files .... probes .... used variables ...
28
29This module defines the following classes:
30- NumericalParamGlobalModel
31- NumericalParamGlobalModelTestCase
32
33"""
34
35#-------------------------------------------------------------------------------
36# Library modules import
37#-------------------------------------------------------------------------------
38
39import unittest
40
41#-------------------------------------------------------------------------------
42# Application modules import
43#-------------------------------------------------------------------------------
44
45from code_saturne.model.Common import *
46from code_saturne.model.XMLvariables import Variables, Model
47from code_saturne.model.XMLmodel import ModelTest
48from code_saturne.model.OutputVolumicVariablesModel import OutputVolumicVariablesModel
49
50#-------------------------------------------------------------------------------
51# NumericalParamGlobal model class
52#-------------------------------------------------------------------------------
53
54class NumericalParamGlobalModel(Model):
55    """
56    Manage the input/output markups in the xml doc about Turbulence
57    """
58    def __init__(self, case):
59        """
60        Xml node declaration and supplementary default value settings.
61        """
62        self.case = case
63        self.node_np = self.case.xmlInitNode('numerical_parameters')
64
65
66    def _defaultValues(self):
67        """
68        default values
69        """
70        self.default = {}
71        self.default['gradient_transposed'] = 'on'
72        self.default['pressure_relaxation'] = 1.
73        self.default['density_relaxation'] = 0.95
74        self.default['velocity_pressure_coupling'] ='off'
75        self.default['hydrostatic_pressure'] ='on'
76        self.default['hydrostatic_equilibrium'] ='off'
77        self.default['time_scheme_order'] = 1
78        self.default['gradient_reconstruction'] = 'default'
79        self.default['extended_neighborhood'] = 'default'
80        self.default['algo_density_variation'] = 'default'
81        return self.default
82
83
84    @Variables.noUndo
85    def getTransposedGradient(self):
86        """
87        Return status of transposed gradient
88        """
89        status = None
90        node = self.node_np.xmlGetNode('gradient_transposed')
91        if node:
92            status = node['status']
93        if not status:
94            status = self._defaultValues()['gradient_transposed']
95        return status
96
97
98    @Variables.noUndo
99    def getVelocityPressureCoupling(self):
100        """
101        Return status of IPUCOU value is activated or not
102        """
103        status = None
104        node = self.node_np.xmlGetNode('velocity_pressure_coupling')
105        if node:
106            status = node['status']
107        if not status:
108            status = self._defaultValues()['velocity_pressure_coupling']
109        return status
110
111
112    @Variables.noUndo
113    def getHydrostaticEquilibrium(self):
114        """
115        Return status of ICFGRP value (for hydrostatic equilibrium) is activated or not
116        """
117        status = None
118        node = self.node_np.xmlGetNode('hydrostatic_equilibrium')
119        if node:
120            status = node['status']
121        if not status:
122            status = self._defaultValues()['hydrostatic_equilibrium']
123        return status
124
125
126    @Variables.noUndo
127    def getHydrostaticPressure(self):
128        """
129        Return status of hydrostatic pressure:
130        'off' if standard, 'on' if improved
131        """
132        status = None
133        node = self.node_np.xmlGetNode('hydrostatic_pressure')
134        if node:
135            status = node['status']
136        if not status:
137            status = self._defaultValues()['hydrostatic_pressure']
138        return status
139
140
141    @Variables.noUndo
142    def getPressureRelaxation(self):
143        """
144        Return RELAXP value
145        """
146        value = self.node_np.xmlGetDouble('pressure_relaxation')
147        if value == None:
148            value = self._defaultValues()['pressure_relaxation']
149        return value
150
151
152    @Variables.noUndo
153    def getDensityRelaxation(self):
154        """
155        Return SRROM value
156        """
157        value = self.node_np.xmlGetDouble('density_relaxation')
158        if value == None:
159            value = self._defaultValues()['density_relaxation']
160            self.setDensityRelaxation(value)
161        return value
162
163
164    @Variables.noUndo
165    def getGradientReconstruction(self):
166        """
167        Return gradient reconstruction method
168        """
169        choice = None
170        node = self.node_np.xmlGetNode('gradient_reconstruction')
171        if node:
172            choice = node['choice']
173        if not choice:
174            choice = self._defaultValues()['gradient_reconstruction']
175        return choice
176
177
178    @Variables.noUndo
179    def getExtendedNeighborType(self):
180        """
181        Return extended neighborhood type
182        """
183        choice = None
184        node = self.node_np.xmlGetNode('extended_neighborhood')
185        if node:
186            choice = node['choice']
187        if not choice:
188            choice = self._defaultValues()['extended_neighborhood']
189        return choice
190
191
192    @Variables.undoLocal
193    def setTransposedGradient(self, status):
194        """
195        Put status of gradient transposed
196        """
197        self.isOnOff(status)
198        node = self.node_np.xmlInitNode('gradient_transposed', 'status')
199        if status == self._defaultValues()['gradient_transposed']:
200            node.xmlRemoveNode()
201        else:
202            node['status'] = status
203
204
205    @Variables.undoLocal
206    def setVelocityPressureCoupling(self, status):
207        """
208        Put status of velocity_pressure_coupling
209        """
210        self.isOnOff(status)
211        node_ipucou = self.node_np.xmlInitNode('velocity_pressure_coupling', 'status')
212        node_ipucou['status'] = status
213        if status == 'on':
214            node_Tx = node_ipucou.xmlInitNode('property', name='weight_matrix_X')
215            node_Ty = node_ipucou.xmlInitNode('property', name='weight_matrix_Y')
216            node_Tz = node_ipucou.xmlInitNode('property', name='weight_matrix_Z')
217
218            for (node, val) in [(node_Tx, 'weight_matrix_X'),
219                                (node_Ty, 'weight_matrix_Y'),
220                                (node_Tz, 'weight_matrix_Z')]:
221                if not node['label']: node['label'] = dicoLabel(val)
222                node.xmlInitChildNode('listing_printing', status="off")
223                node.xmlInitChildNode('postprocessing_recording', status="off")
224        else:
225            for node in node_ipucou.xmlGetNodeList('property'):
226                node.xmlRemoveNode()
227            node_ipucou.xmlRemoveNode()
228
229
230    @Variables.undoLocal
231    def setHydrostaticEquilibrium(self, var):
232        """
233        Put status of hydrostatic equilibrium
234        """
235        self.isOnOff(var)
236        node = self.node_np.xmlInitNode('hydrostatic_equilibrium', 'status')
237        if var == self._defaultValues()['hydrostatic_equilibrium']:
238            node.xmlRemoveNode()
239        else:
240            node['status'] = var
241
242
243    @Variables.undoLocal
244    def setHydrostaticPressure(self, var):
245        """
246        Put status of hydrostatic pressure
247        """
248        self.isOnOff(var)
249        node = self.node_np.xmlInitNode('hydrostatic_pressure', 'status')
250        if var == self._defaultValues()['hydrostatic_pressure']:
251            node.xmlRemoveNode()
252        else:
253            node['status'] = var
254
255
256    @Variables.undoLocal
257    def setPressureRelaxation(self, value):
258        """
259        Put value of pressure_relaxation
260        """
261        self.isPositiveFloat(value)
262        self.node_np.xmlSetData('pressure_relaxation', value,
263                                default=self._defaultValues()['pressure_relaxation'])
264
265
266    @Variables.undoLocal
267    def setDensityRelaxation(self, value):
268        """
269        Put value of density_relaxation
270        """
271        self.isGreaterOrEqual(value, 0.0)
272        self.isLower(value, 1.0)
273        self.node_np.xmlSetData('density_relaxation', value,
274                                default=self._defaultValues()['density_relaxation'])
275
276
277    @Variables.undoLocal
278    def setGradientReconstruction(self, value):
279        """
280        Put value of gradient_reconstruction
281        """
282        node = self.node_np.xmlInitNode('gradient_reconstruction', 'choice')
283        if value == self._defaultValues()['gradient_reconstruction']:
284            node.xmlRemoveNode()
285        else:
286            node['choice'] = value
287
288
289    @Variables.undoLocal
290    def setExtendedNeighborType(self, value):
291        """
292        Put value of extended_neighborhood
293        """
294        node = self.node_np.xmlInitNode('extended_neighborhood', 'choice')
295        if value == self._defaultValues()['extended_neighborhood']:
296            node.xmlRemoveNode()
297        else:
298            node['choice'] = value
299
300
301    @Variables.undoLocal
302    def getTimeSchemeOrder(self):
303        """
304        Return time scheme order for NumericalParamEquationModel
305        """
306        # getTimeSchemeOrder: used only by NumericalParamEquationModel
307        node = self.node_np.xmlGetNode('time_scheme_order')
308        if node:
309            order = self.node_np.xmlGetInt('time_scheme_order')
310        else:
311            order = self._defaultValues()['time_scheme_order']
312        return order
313
314
315    @Variables.undoLocal
316    def setTimeSchemeOrder(self, order):
317        """
318        Set or remove markup of time scheme order for turbulence (LES)
319        """
320        # setTimeSchemeOrder : used only by Turbulence Model
321        self.isInt(order)
322        if order == 2:
323            self.node_np.xmlSetData('time_scheme_order', 2)
324        else:
325            self.node_np.xmlRemoveChild('time_scheme_order')
326
327
328    @Variables.noUndo
329    def getDensityVar(self):
330        """
331        Return the algorithm for density variation in time
332        """
333        choice = None
334        node = self.node_np.xmlGetNode('algo_density_variation')
335        if node:
336            choice = node['choice']
337        if not choice:
338            choice = self._defaultValues()['algo_density_variation']
339        return choice
340
341
342    @Variables.undoLocal
343    def setDensityVar(self, value):
344        """
345        Put the algorithm for density variation in time
346        """
347        node = self.node_np.xmlInitNode('algo_density_variation', 'choice')
348        if value == self._defaultValues()['algo_density_variation']:
349            node.xmlRemoveNode()
350        else:
351            node['choice'] = value
352
353
354#-------------------------------------------------------------------------------
355# NumericalParamEquat test case
356#-------------------------------------------------------------------------------
357
358class NumericalParamGlobalTestCase(ModelTest):
359    """
360    """
361    def checkNumericalParamGlobalInstantiation(self):
362        """
363        Check whether the NumericalParamEquatModel class could be instantiated
364        """
365        model = None
366        model = NumericalParamGlobalModel(self.case)
367        assert model != None, 'Could not instantiate NumericalParamGlobalModel'
368
369
370    def checkSetandGetGradTransp(self):
371        """
372        Check whether the NumericalParamEquatModel class
373        could be set and get gradient transposed
374        """
375        model = NumericalParamGlobalModel(self.case)
376        model.setTransposedGradient('on')
377        doc = """<numerical_parameters>
378                    <gradient_transposed status="on"/>
379                 </numerical_parameters>"""
380
381        assert model.node_np == self.xmlNodeFromString(doc),\
382                'Could not set gradient transposed in NumericalParamGlobalModel'
383        assert model.getTransposedGradient() == 'on',\
384                'Could not get gradient transposed in NumericalParamGlobalModel'
385
386    def checkSetandGetVelPesCoupl(self):
387        """
388        Check whether the NumericalParamEquatModel class
389        could be set and get velocity pressure coupling
390        """
391        model = NumericalParamGlobalModel(self.case)
392        model.setVelocityPressureCoupling('on')
393        doc = '''<numerical_parameters>
394                    <velocity_pressure_coupling status="on">
395                        <property label="VPsolve1" name="weight_matrix_X">
396                            <listing_printing status="off"/>
397                            <postprocessing_recording status="off"/>
398                        </property>
399                        <property label="VPsolve2" name="weight_matrix_Y">
400                            <listing_printing status="off"/>
401                            <postprocessing_recording status="off"/>
402                        </property>
403                        <property label="VPsolve3" name="weight_matrix_Z">
404                            <listing_printing status="off"/>
405                            <postprocessing_recording status="off"/>
406                        </property>
407                    </velocity_pressure_coupling>
408                 </numerical_parameters>'''
409        assert model.node_np == self.xmlNodeFromString(doc),\
410                'Could not set velocity_pressure_coupling in NumericalParamGlobalModel'
411        assert model.getVelocityPressureCoupling() == 'on',\
412                'Could not get velocity_pressure_coupling in NumericalParamGlobalModel'
413
414    def checkGetandSetHydrostaticPressure(self):
415        """
416        Check whether the hydrostatic pressure could be set and get
417        """
418        model = NumericalParamGlobalModel(self.case)
419        model.setHydrostaticPressure('on')
420        doc = '''<numerical_parameters>
421                    <hydrostatic_pressure status="on"/>
422                 </numerical_parameters>'''
423        assert model.node_np == self.xmlNodeFromString(doc), \
424                    'Could not set hydrostatic pressure'
425        assert model.getHydrostaticPressure() == 'on',\
426                                'Could not get hydrostatic pressure'
427
428    def checkSetandGetPressureRelaxation(self):
429        """
430        Check whether the NumericalParamEquatModel class could be set
431        and get pressure relaxation
432        """
433        model = None
434        model = NumericalParamGlobalModel(self.case)
435        model.setPressureRelaxation(0.88)
436        doc = '''<numerical_parameters>
437                    <pressure_relaxation>0.88</pressure_relaxation>
438                 </numerical_parameters>'''
439        assert model.node_np == self.xmlNodeFromString(doc),\
440                'Could not set pressure relaxation in NumericalParamGlobalModel'
441        assert model.getPressureRelaxation() == 0.88,\
442                'Could not get pressure relaxation in NumericalParamGlobalModel'
443
444    def checkSetandGetDensityRelaxation(self):
445        """
446        Check whether the NumericalParamEquatModel class could be set
447        and get density relaxation
448        """
449        model = None
450        model = NumericalParamGlobalModel(self.case)
451        model.setDensityRelaxation(0.91)
452        doc = '''<numerical_parameters>
453                    <density_relaxation>0.91</density_relaxation>
454                 </numerical_parameters>'''
455        assert model.node_np == self.xmlNodeFromString(doc),\
456                'Could not set density relaxation in NumericalParamGlobalModel'
457        assert model.getDensityRelaxation() == 0.91,\
458                'Could not get density relaxation in NumericalParamGlobalModel'
459
460    def checkSetandGetGradientReconstructionruction(self):
461        """
462        Check whether the NumericalParamEquatModel class could be set
463        and get gradient_reconstruction
464        """
465        model = None
466        model = NumericalParamGlobalModel(self.case)
467        model.setGradientReconstruction(3)
468        doc = '''<numerical_parameters>
469                    <gradient_reconstruction choice="3"/>
470                 </numerical_parameters>'''
471        assert model.node_np == self.xmlNodeFromString(doc),\
472                'Could not set gradient_reconstruction in NumericalParamGlobalModel'
473        assert model.getGradientReconstruction() == "3",\
474                'Could not get gradient_reconstruction in NumericalParamGlobalModel'
475
476    def checkSetandGetsetTimeSchemeOrder(self):
477        """
478        Check whether the NumericalParamEquatModel class could be set
479        and get time scheme order
480        """
481        model = None
482        model = NumericalParamGlobalModel(self.case)
483        model.setTimeSchemeOrder(2)
484        doc = '''<numerical_parameters>
485                    <time_scheme_order>2</time_scheme_order>
486                 </numerical_parameters>'''
487        assert model.node_np == self.xmlNodeFromString(doc),\
488                'Could not set time scheme order in NumericalParamGlobalModel'
489        assert model.getTimeSchemeOrder() == 2,\
490                'Could not get time scheme order in NumericalParamGlobalModel'
491
492
493def suite():
494    testSuite = unittest.makeSuite(NumericalParamGlobalTestCase, "check")
495    return testSuite
496
497
498def runTest():
499    print("NumericalParamGlobalModelTestCase")
500    runner = unittest.TextTestRunner()
501    runner.run(suite())
502
503
504#-------------------------------------------------------------------------------
505# End
506#-------------------------------------------------------------------------------
507