Salome HOME
Changed copyright
[samples/genericsolver.git] / resources / yacsgen_example.py
1 #!/usr/bin/env python
2 #
3 #  Copyright (C) 2009-2011 EDF R&D
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 #  $Id$
22 #
23
24 from module_generator import *
25
26 # This example shows how to create a component used as a calculation code for OpenTURNS
27 # with YACSGEN tool. This example is very basic. See YACSGEN documentation and DEVIATION
28 # component for more advanced features.
29
30 # edit those values to reflect your environment:
31 prerequisites_script_path = "/path/to/prerequisites.sh"
32 kernel_path = "/path/to/KERNEL_install" 
33 openturns_module_path = "/path/to/OPENTURNS_install" 
34
35 c1 = PYComponent("EXAMPLE_COMPONENT",
36                  services=[
37                            Service("Init",
38                                    inport=[("studyID", "long"),
39                                            ("detCaseEntry", "string")],
40                                    body="""
41 # Those values should be initialized from the deterministic case in the study
42 # (see component DEVIATION for an example)
43 self.deterministicValues = {'E' : 210.e9,
44                             'F' : 1000.,
45                             'L' : 1.5,
46                             'I' : 2.e-6}
47 """,
48                                   ),
49                            Service("Exec",
50                                    inport=[("paramInput", "SALOME_TYPES/ParametricInput"),],
51                                    outport=[("paramOutput", "SALOME_TYPES/ParametricOutput")],
52                                    defs = "import SALOME_TYPES",
53                                    body="""
54 # This section creates the point to evaluate "evalPoint" 
55 evalPoint = self.deterministicValues
56 for i in range(len(paramInput.inputVarList)):
57     evalPoint[paramInput.inputVarList[i]] = paramInput.inputValues[i][0][0]
58
59 # This section should be modified according to your needs (it's the evaluation itself)
60 if evalPoint["L"] <= 0: # Test for an invalid parameter and return an error in this case
61     paramOutput = SALOME_TYPES.ParametricOutput(
62             outputValues = [],
63             specificOutputInfos = [],
64             returnCode = 1,
65             errorMessage = "Invalid value: L must be positive")
66 else:
67     resDict = {}
68     resDict["dev"] = (evalPoint["F"] * evalPoint["L"] * evalPoint["L"] * evalPoint["L"]) / \
69                      (3. * evalPoint["E"] * evalPoint["I"])
70
71 # This section builds the returned object
72     outputValues = []
73     for outputVar in paramInput.outputVarList:
74         outputValues.append([[resDict[outputVar]]])
75     paramOutput = SALOME_TYPES.ParametricOutput(outputValues,
76                                                 specificOutputInfos = [],
77                                                 returnCode = 0,
78                                                 errorMessage = "")
79 """,
80                                   ),
81                            Service("Finalize"),
82                           ]
83                 )
84
85 m = Module("EXAMPLE_MODULE",components=[c1],prefix="./EXAMPLE_MODULE_INSTALL")
86
87 context={'update':1,
88          "prerequisites":prerequisites_script_path,
89          "kernel":kernel_path
90          }
91 g=Generator(m,context)
92 g.generate()
93 g.bootstrap()
94 g.configure()
95 g.make()
96 g.install()
97 g.make_appli("example_appli",
98              altmodules={"OPENTURNS":openturns_module_path})