Salome HOME
Added YACSGEN example to generate a component for OpenTURNS
[samples/genericsolver.git] / resources / yacsgen_example.py
1 #!/usr/bin/env python
2
3 from module_generator import *
4
5 # This example shows how to create a component used as a calculation code for OpenTURNS
6 # with YACSGEN tool. This example is very basic. See YACSGEN documentation and DEVIATION
7 # component for more advanced features.
8
9 # edit those values to reflect your environment:
10 prerequisites_script_path = "/path/to/prerequisites.sh"
11 kernel_path = "/path/to/KERNEL_install" 
12 openturns_module_path = "/path/to/OPENTURNS_install" 
13
14 c1 = PYComponent("EXAMPLE_COMPONENT",
15                  services=[
16                            Service("Init",
17                                    inport=[("studyID", "long"),
18                                            ("detCaseEntry", "string")],
19                                    body="""
20 # Those values should be initialized from the deterministic case in the study
21 # (see component DEVIATION for an example)
22 self.deterministicValues = {'E' : 210.e9,
23                             'F' : 1000.,
24                             'L' : 1.5,
25                             'I' : 2.e-6}
26 """,
27                                   ),
28                            Service("Exec",
29                                    inport=[("paramInput", "SALOME_TYPES/ParametricInput"),],
30                                    outport=[("paramOutput", "SALOME_TYPES/ParametricOutput")],
31                                    defs = "import SALOME_TYPES",
32                                    body="""
33 # This section creates the point to evaluate "evalPoint" 
34 evalPoint = self.deterministicValues
35 for i in range(len(paramInput.inputVarList)):
36     evalPoint[paramInput.inputVarList[i]] = paramInput.inputValues[i][0][0]
37
38 # This section should be modified according to your needs (it's the evaluation itself)
39 if evalPoint["L"] <= 0: # Test for an invalid parameter and return an error in this case
40     paramOutput = SALOME_TYPES.ParametricOutput(
41             outputValues = [],
42             specificOutputInfos = [],
43             returnCode = 1,
44             errorMessage = "Invalid value: L must be positive")
45 else:
46     resDict = {}
47     resDict["dev"] = (evalPoint["F"] * evalPoint["L"] * evalPoint["L"] * evalPoint["L"]) / \
48                      (3. * evalPoint["E"] * evalPoint["I"])
49
50 # This section builds the returned object
51     outputValues = []
52     for outputVar in paramInput.outputVarList:
53         outputValues.append([[resDict[outputVar]]])
54     paramOutput = SALOME_TYPES.ParametricOutput(outputValues,
55                                                 specificOutputInfos = [],
56                                                 returnCode = 0,
57                                                 errorMessage = "")
58 """,
59                                   ),
60                            Service("Finalize"),
61                           ]
62                 )
63
64 m = Module("EXAMPLE_MODULE",components=[c1],prefix="./EXAMPLE_MODULE_INSTALL")
65
66 context={'update':1,
67          "prerequisites":prerequisites_script_path,
68          "kernel":kernel_path
69          }
70 g=Generator(m,context)
71 g.generate()
72 g.bootstrap()
73 g.configure()
74 g.make()
75 g.install()
76 g.make_appli("example_appli",
77              altmodules={"OPENTURNS":openturns_module_path})