Hi Régis, It works : thank you very much !
Best regards, Michaël Michaël BAUDIN Ingénieur - Chercheur EDF – R&D Département Management des Risques Industriels 6, quai Watier 78401 CHATOU [email protected]<mailto:[email protected]> Tél. : 01 30 87 81 82 Fax : 01 30 87 82 13 De : [email protected] [mailto:[email protected]] Envoyé : vendredi 3 août 2018 18:40 À : [email protected]; BAUDIN Michael <[email protected]> Cc : [email protected] Objet : Re: [ot-users] Simplified symbolic functions Hi Michael, Try this: import openturns as ot Q = 1013. Ks = 30. Zv = 50. Zm = 55. Hd = 8 Zb = 55.5 L = 5000 B = 300 X = [Q,Ks,Zv,Zm,Hd,Zb,L,B] inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B'] formula = 'var alpha := (Zm - Zv)/L; H := (Q/(Ks*B*sqrt(alpha)))^(3.0/5.0); var Zc := H + Zv; var Zd := Zb + Hd; S := Zc - Zd' outputs = ['H', 'S'] myFunction = ot.SymbolicFunction(inputs, outputs, formula) print(myFunction(X)) In the case of multiple outputs defined using a unique formula, you have to: 1) provide the formula as a single string 2) declare the intermediate results you want to reuse but not to output using the 'var' qualifier 3) provide the list of outputs as a sequence of strings Cheers Régis Le vendredi 3 août 2018 à 18:10:15 UTC+2, BAUDIN Michael <[email protected]<mailto:[email protected]>> a écrit : Hi, I tried the feature you showed with the following script : Q = 1013. Ks = 30. Zv = 50. Zm = 55. Hd = 8 Zb = 55.5 L = 5000 B = 300 X = [Q,Ks,Zv,Zm,Hd,Zb,L,B] inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B'] formula = ['var alpha := (Zm - Zv)/L; H := (Q/(Ks*B*sqrt(alpha)))^(3.0/5.0); var Zc := H + Zv; var Zd := Zb + Hd; S := Zc - Zd'] outputs = ['H', 'S'] myFunction = ot.SymbolicFunction(inputs, outputs, formula) print(myFunction(X)) But the following exception is generated : NotImplementedError: Wrong number or type of arguments for overloaded function 'new_SymbolicFunction'. Possible C/C++ prototypes are: OT::SymbolicFunction::SymbolicFunction() OT::SymbolicFunction::SymbolicFunction(OT::String const &,OT::String const &) OT::SymbolicFunction::SymbolicFunction(OT::Description const &,OT::Description const &) OT::SymbolicFunction::SymbolicFunction(OT::Description const &,OT::Description const &,OT::String const &) OT::SymbolicFunction::SymbolicFunction(OT::SymbolicFunction const &) I also tried a simpler version : inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B'] formula = ['alpha := (Zm - Zv)/L; H := (Q/(Ks*B*sqrt(alpha)))^(3.0/5.0); Zc := H + Zv; Zd := Zb + Hd; S := Zc - Zd'] myFunction = ot.SymbolicFunction(inputs, formula) print(myFunction(X)) TypeError: InvalidArgumentException : Errors found when parsing expression 'alpha := (Zm - Zv)/L; H := (Q/(Ks*B*sqrt(alpha)))^(3.0/5.0); Zc := H + Zv; Zd := Zb + Hd; S := Zc - Zd': ERR189 - Undefined symbol: 'alpha' What is the trick you used to make it work ? (This is with OT 1.11). Best regards, Michaël Michaël BAUDIN Ingénieur - Chercheur EDF – R&D Département Management des Risques Industriels 6, quai Watier 78401 CHATOU [email protected]<mailto:[email protected]> Tél. : 01 30 87 81 82 Fax : 01 30 87 82 13 -----Message d'origine----- De : [email protected]<mailto:[email protected]> [mailto:[email protected]<mailto:[email protected]>] Envoyé : lundi 12 mars 2018 21:26 À : BAUDIN Michael <[email protected]<mailto:[email protected]>> Cc : [email protected]<mailto:[email protected]> Objet : Re: [ot-users] Simplified symbolic functions Hello Michael, We investigated using ExprTk to parse analytical formulas instead of muParser, it is known to be faster. Almost all muParser formulas can be parsed by ExprTk without changes, but it also provides many more features. And multiple outputs are trivial with this backend. This has been implemented on current master, your example can be written: inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B'] outputs = ['H', 'S'] formula = """var alpha := (Zm - Zv)/L; H := (Q/(Ks*B*sqrt(alpha)))^(3.0/5.0); var Zc := H + Zv; var Zd := Zb + Hd; S := Zc - Zd;""" myFunction = SymbolicFunction(inputs, outputs, formula) This is almost straightforward; input and output variables are declared, all other variables must be prefixed by the keyword 'var', assignment is ':=', and statements must be separated by semicolons. But Python can be as fast as SymbolicFunction; pull request #699 avoids copies between OT and Python, your code could be rewritten as import numpy as np def functionCrueSample(X) : Q, Ks, Zv, Zm, Hd, Zb, L, B = np.array(X, copy=False).T alpha = (Zm - Zv)/L H = (Q/(Ks*B*np.sqrt(alpha)))**(3.0/5.0) Zc = H + Zv Zd = Zb + Hd S = Zc - Zd; Y = np.zeros((len(H), 2)) Y[:,0] = H Y[:,1] = S return Y myFunction = PythonFunction(8, 2, func_sample=functionCrueSample) Denis 2018-02-14 12:30 GMT+01:00 BAUDIN Michael <[email protected]<mailto:[email protected]>>: > 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 > [email protected]<mailto:[email protected]> > http://openturns.org/mailman/listinfo/users > 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 [email protected]<mailto:[email protected]> http://openturns.org/mailman/listinfo/users 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 [email protected] http://openturns.org/mailman/listinfo/users
