Hi Michael,
You can try this:

from openturns import *from math import sqrt
def functionCrue(X) :
    Q, Ks, Zv, Zm, Hd, Zb, L, B = X
    alpha = (Zm - Zv)/L
    H = (Q/(Ks*B*sqrt(alpha)))**(3.0/5.0)
    Zc = H + Zv
    Zd = Zb + Hd
    S = Zc - Zd
    return [H,S]
myFunction = PythonFunction(8, 2, functionCrue)
f = SymbolicFunction(["Q", "Ks", "Zv", "Zm", "Hd", "Zb", "L", "B"], 
["(Q/(Ks*B*sqrt((Zm - Zv)/L)))^(3.0/5.0)", "Zv - (Zb + Hd)"])g = 
SymbolicFunction(["x", "y"], ["x", "x + y"])
mySymbolicFunction = ComposedFunction(g, f)
# To check that everything is oksize = 1000sample = 
ComposedDistribution([Uniform(1.0, 2.0), Uniform(1.0, 2.0), Uniform(1.0, 2.0), 
Uniform(3.0, 4.0), Uniform(1.0, 2.0), Uniform(1.0, 2.0), Uniform(1.0, 2.0), 
Uniform(1.0, 2.0)]).getSample(size)
delta = myFunction(sample) - 
mySymbolicFunction(sample)print(delta.computeRawMoment(2))

It is not a generic solution as it uses the specific structure of your formulas 
but it works. Concerning the performance, the solution based on PythonFunction 
gives a speed of 73473 evals/s, the solution based on a composition of symbolic 
functions gives a speed of 1616000 evals/s (22x faster) and the solution using 
only one symbolic function where H is duplicated gives a speed of 2121000 
evals/s (29x faster). If you want to perform a large sample Monte Carlo 
simulation it may change things...
 Cheers
Régis
 
 Le mercredi 14 février 2018 à 12:31:01 UTC+1, BAUDIN Michael 
<michael.bau...@edf.fr> a écrit : 





  
 
 
Hi,

  

I have a symbolic function that I would like to simplify and I do not see how.

  

Here is the test case. The function has 8 inputs and 2 outputs. In Python it is 
simple to define :

  

def functionCrue(X) :

    Q, Ks, Zv, Zm, Hd, Zb, L, B = X

    alpha = (Zm - Zv)/L

    H = (Q/(Ks*B*sqrt(alpha)))**(3.0/5.0)

    Zc = H + Zv

    Zd = Zb + Hd

    S = Zc - Zd

    return [H,S]

myFunction = PythonFunction(8, 2, functionCrue)

  

As you can see, the code is simplified by intermediate variables which are used 
by subsequent Python statements. For exemple, the slope of the river alpha is 
first computed, then the height H is computed depending on the slope. Then the 
variable S is computed based on Zc and Zd, where Zc is computed depending on 
the height H. 

  

When I define the function as a symbolic function, the current definition is 
more involved. 

  

inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B']

formulas = ['(Q/(Ks*B*sqrt((Zm - Zv)/L)))^(3.0/5.0)','(Q/(Ks*B*sqrt((Zm - 
Zv)/L)))^(3.0/5.0)+Zv-(Zb + Hd)']

myFunction = SymbolicFunction(inputs, formulas)

  

As you can see, I cannot reuse the value of the first output into the second 
output. 

  

The following interface would be much easier to use. The formulas variable is a 
list of couples, where the first item is the name of the output variable and 
the second item is the string to evaluate it.

  

inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B']

output1 = [‘alpha’, '(Zm - Zv)/L']

output2 = [‘H’, '(Q/(Ks*B*sqrt(alpha)))^(3.0/5.0)']

output3 = [‘Zc’, 'H + Zv']

output4 = [‘Zd’, 'Zb + Hd']

output5 = [‘S’, 'Zc - Zd']

formulas = [output1,output2,output3,output4,output5]

myFunction = SymbolicFunction(inputs, formulas)

  

Is there another way of doing this ? Do you agree that the suggestion is worth 
being developed ?

  

Best regards,

  

Michaël





Ce message et toutes les pièces jointes (ci-après le 'Message') sont établis à 
l'intention exclusive des destinataires et les informations qui y figurent sont 
strictement confidentielles. Toute utilisation de ce Message non conforme à sa 
destination, toute diffusion ou toute publication totale ou partielle, est 
interdite sauf autorisation expresse.

Si vous n'êtes pas le destinataire de ce Message, il vous est interdit de le 
copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si 
vous avez reçu ce Message par erreur, merci de le supprimer de votre système, 
ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support 
que ce soit. Nous vous remercions également d'en avertir immédiatement 
l'expéditeur par retour du message.

Il est impossible de garantir que les communications par messagerie 
électronique arrivent en temps utile, sont sécurisées ou dénuées de toute 
erreur ou virus.
____________________________________________________

This message and any attachments (the 'Message') are intended solely for the 
addressees. The information contained in this Message is confidential. Any use 
of information contained in this Message not in accord with its purpose, any 
dissemination or disclosure, either whole or partial, is prohibited except 
formal approval.

If you are not the addressee, you may not copy, forward, disclose or use any 
part of it. If you have received this message in error, please delete it and 
all copies from your system and notify the sender immediately by return message.

E-mail communication cannot be guaranteed to be timely secure, error or 
virus-free.

_______________________________________________
OpenTURNS users mailing list
users@openturns.org
users Info Page

| 
| 
|  | 
users Info Page


 |

 |

 |





_______________________________________________
OpenTURNS users mailing list
users@openturns.org
http://openturns.org/mailman/listinfo/users

Reply via email to