Salome HOME
Revert "Synchronize adm files"
[modules/yacs.git] / src / yacsloader / Test / genPascal.py
1 #!/usr/bin/env python
2 # Copyright (C) 2006-2014  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 def triangle(n):
22     """generate a YACS graph for computation of the Pascal triangle
23
24     parameter: rank of the triangle.
25     Use module decimal for an exact calculation with big numbers.
26     The last node gives the sum of rank n (=2**n) and also a direct calculation of 2**n.
27     """
28        
29     print """
30 <proc>
31     <!-- types -->
32     <!-- inline -->
33
34 <inline name="node_0_0" >
35 <script><code>
36 import time
37 from decimal import *"""
38     print "getcontext().prec = " + str(1+n/3)
39     print """
40 aa=Decimal(a)
41 bb=Decimal(b)
42 cc=aa+bb
43 c=str(cc)
44 print "cc=",cc
45 time.sleep(1)
46 </code></script>
47 <inport name="a" type="string"/>
48 <inport name="b" type="string"/>
49 <outport name="c" type="string"/>
50 </inline>"""
51
52     print """
53 <inline name="collect" >
54 <script><code>"""
55     print "import time"
56     print "from decimal import *"
57     print "getcontext().prec = " + str(1+n/3)
58     print "tot = Decimal(0)"
59     print "for i in range (" + str(n+1) + "):"
60     print "    v='a' + str(i)"
61     print "    tot+=Decimal(eval(v))"
62     print "print tot"
63     print "result=str(tot)"
64     print "ref=Decimal(2)**" + str(n)
65     print "reference=str(ref)"
66     print "time.sleep(1)"
67     print "</code></script>"
68     for i in range (n+1):
69         inport='<inport name="a' + str(i) + '" type="string"/>'
70         print inport
71         pass
72     print '<outport name="result" type="string"/>'
73     print '<outport name="reference" type="string"/>'
74     print "</inline>"
75     print
76     
77     for i in range (1,n+1):
78         for j in range (i+1):
79             node="node_" + str(i)   +"_" + str(j)
80             nodetxt='<node name="'+node+'" type="node_0_0"></node>'
81             print nodetxt
82             pass
83         pass
84
85     print """
86
87     <!-- service -->
88     <!-- control -->
89
90     """
91     
92     for i in range (n):
93         for j in range (i+1):
94             fromnode="node_" + str(i)   +"_" + str(j)
95             tonode1="node_" + str(i+1)   +"_" + str(j)
96             tonode2="node_" + str(i+1)   +"_" + str(j+1)
97             control1='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode1+'</tonode> </control>'
98             control2='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode2+'</tonode> </control>'
99             print control1
100             print control2
101             pass
102         pass
103     for i in range (n+1):
104         fromnode="node_" + str(n)   +"_" + str(i)
105         control='<control> <fromnode>'+fromnode+'</fromnode> <tonode>collect</tonode> </control>'
106         print control
107         pass
108
109     print """
110
111     <!-- datalinks -->
112
113     """
114     
115     for i in range (n):
116         for j in range (i+1):
117             fromnode="node_" + str(i)   +"_" + str(j)
118             tonode1="node_" + str(i+1)   +"_" + str(j)
119             tonode2="node_" + str(i+1)   +"_" + str(j+1)
120             datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
121             datato1 ='<tonode>'   + tonode1  + '</tonode> <toport>b</toport>'
122             datato2 ='<tonode>'   + tonode2  + '</tonode> <toport>a</toport>'
123             print '<datalink>'
124             print '   ' + datafrom
125             print '   ' + datato1
126             print '</datalink>'
127             print '<datalink>'
128             print '   ' + datafrom
129             print '   ' + datato2
130             print '</datalink>'
131             pass
132         pass
133     for i in range (n+1):
134         fromnode="node_" + str(n)   +"_" + str(i)
135         datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
136         toport='a' + str(i)
137         datato  ='<tonode>collect</tonode> <toport>' + toport + '</toport>'
138         print '<datalink>'
139         print '   ' + datafrom
140         print '   ' + datato
141         print '</datalink>'
142         
143         
144     print """
145
146     <!--parameters -->
147
148     """
149
150     print """
151     <parameter>
152         <tonode>node_0_0</tonode> <toport>a</toport>
153         <value><string>0</string></value>
154     </parameter>
155     <parameter>
156         <tonode>node_0_0</tonode> <toport>b</toport>
157         <value><string>1</string></value>
158     </parameter>
159     """
160
161     for i in range (1,n+1):
162         node1="node_" + str(i)   +"_" + str(0)
163         node2="node_" + str(i)   +"_" + str(i)
164         tonode1 ='   <tonode>' + node1 + '</tonode> <toport>a</toport>'
165         tonode2 ='   <tonode>' + node2 + '</tonode> <toport>b</toport>'
166         print '<parameter>'
167         print tonode1
168         print '   <value><string>0</string></value>'
169         print '</parameter>'
170         
171         print '<parameter>'
172         print tonode2
173         print '   <value><string>0</string></value>'
174         print '</parameter>'
175
176     print """
177
178 </proc>
179     """
180      
181 if __name__ == "__main__":
182     import sys
183     usage ="""Usage: %s rank > file.xml
184     where rank is positive integer > 2
185     """
186     try:
187         rank = int(sys.argv[1])
188         if rank <2:
189             raise ValueError("rank must be >1")
190     except (IndexError, ValueError):
191         print usage%(sys.argv[0])
192         sys.exit(1)
193         pass
194     triangle(rank)
195     pass