16 Replies Latest reply: Mar 12, 2012 11:36 AM by ptc-4310378 RSS

    Using Maximize Function within a program?

    ptc-4310378 Silver

      In the attached file, the Maximise Function is used against an  5 x 1 Array and is manully repeated 5 times, using a different guess for each case.

       

      Trying to streamline the process, tried to do the five steps with a program but the program fails.

       

      It looks like the maximise function inside the Program is not accepting the Guess value assigned within the program and is picking up the a Global Value for the guess, outside of the program, different from the assign value inside the program.

       

      What would be the proper way to use Maximize within a Program>

       

      Thank you

       

      Laurent

        • Re: Using Maximize Function within a program?
          ptc-4310378 Silver

          Looks like I found the answer to this one in an older collab thread::

           

          http://communities.ptc.com/thread/29684

           

          This thread was replied to by people who appear to be Ueber Users (tom gutman, jmgiraud, eden mei), so I would tend to trust the answer:

           

          ........ any solver that can be defined as a function can be passed in a program. The ones that can't are Maximize/Minimize.

           

          jmG

           

          So I won't waste more time trying to use Maximize in a Program. But would have been nice if the Manual had a warning about that!

            • Re: Using Maximize Function within a program?
              A.Non PTC Community Champion

              You can turn a maximize solve block into a function, and can therefore use it in a program (you cannot embed it in a program, but you can use it in a program). I'm just really busy right now and I'm out at a conference next week. I'll see if I can find time to look at your worksheet tomorrow, but I'm not making any promises.

                • Re: Using Maximize Function within a program?
                  ptc-4310378 Silver

                  Somehow the sheet attached to the top post is not the one I had intended to post. I had prepared a simplified version with collapsed region that made it easier to get to the heart of the matter.

                   

                  The user friendlier one is attached here.

                   

                  I noticed another issue. I loaded the sheet from 2001 to 15 before posting, and the errors shown are different. In 2001 the program would return results for some of the iterations, where the external global guess that Maximize is (unfortunately) looking for would work. In 15, all steps are in error, so it looks like there are a few changes there.

                   

                  I will give a shot at looking at the zero of the derivative with a Find function and see if I have better luck than with Maximize.. But the zero will still depend on the exact same good initial guesses.

                   

                  My HW was turned in and next week is Spring Break. I am just trying to get ready for the HW after Spring Break, where we will have to build on the previous HW. So there is some time left for this one.......

                    • Re: Using Maximize Function within a program?
                      AlanStevens Platinum

                      No need to keep redefining the function.  See bottom of the attached file.

                       

                      Alan

                        • Re: Using Maximize Function within a program?
                          ptc-4310378 Silver

                          It looks so simple and elegant once you see the solution to the puzzle!  And it is fast too.

                           

                          But I was light years away from getting there, and would have never thought about passing the guess as an argument of the function anyway. So thanks a lot for the help on that.

                           

                          May I ask the reason for assigning step 1 outside of the loop and running the loop from 2 to 5 instead of doing it all in a 1 to 5 loop?

                           

                          Thanks.

                           

                          Laurent

                           

                           

                          PS: Works fine with 2001 too.......Great.

                            • Re: Using Maximize Function within a program?
                              AlanStevens Platinum

                              Laurent Muller wrote:

                               

                               

                              May I ask the reason for assigning step 1 outside of the loop and running the loop from 2 to 5 instead of doing it all in a 1 to 5 loop?

                               

                               

                              Stupidity on my part!  It was a left-over from my first attempt, where I tried to use the solution at step i-1 as the guess for step i.  That was before I noticed exactly how you had set your initial guesses.  You can safely delete step 1 and run the loop from 1 to 5.

                               

                              In fact, you don't even need the programming structure. Just copy the line within the loop and paste it outside the programming structure and it runs ok (the results are contained in SSSwf not PSSwf of course).

                               

                              Alan

                                • Re: Using Maximize Function within a program?
                                  ptc-4310378 Silver

                                  I am starting to get it now: Full analogy with a Solver based UDF, which I completely missed. Cannot get more compact than that!

                                   

                                  Your pointer about passing the guess directly as an argument to the Solver function will also help for structuring a program later on in the file, where a Solver function will have to go through a loop.

                                   

                                  So this is a lot of help. Thank you.

                                  • Re: Using Maximize Function within a program?
                                    ptc-4310378 Silver

                                    Very strange.....

                                     

                                    -- Using the Solver Function instead of manually running the Maximize function for each layer is not to the liking of  the next solver that calls this Solver Function.

                                     

                                    2001 returns an "unknown error" for the solver and doesn't calculate. 15 goes ahead but the calculation takes for ever. Runing out of ideas, I added two dummy steps below the Solver Function that are supposed to mimic the "manual" structure that was in place prior to implementing the Solver Function. Vectorizing the Solver Function was no help either.

                                     

                                    These two dummy steps results on the subsequent Solver to regain its speed. Absolutely no clue about may be happening there.

                                      • Re: Using Maximize Function within a program?
                                        AlanStevens Platinum

                                        Laurent Muller wrote:

                                         

                                        Absolutely no clue about may be happening there.

                                        Possibly because you used a function when a simple vector would do.  I've also simplified your Given ... Find solve block significantly - see attached.

                                         

                                        Alan

                                          • Re: Using Maximize Function within a program?
                                            ptc-4310378 Silver

                                            Thanks. That may explain a few other funny things happening in the file too!

                                             

                                            My original Layer by Layer approach, before your writing the Maximize function, was returning a vector...which I had equaled to the function notation as I was reusing a constructs from another file that was based on a function and wanted to save time changing the previous construct. Paying for that now! I better learn how to use Find/Replace as I need to clean up the mess now anyhow.

                                             

                                            Oops.....I see you have cleaned up the mess between the two Solve Blocks.....using Find/Replace I suppose?

                                             

                                            Thanks a lot also for the elegant clean up of the Solve Block. This is exactly what I was trying to work on when I got hit with what you solved above.....

                                             

                                            It was messy because it was built in a hurry for a HW, based on a previous single layer file. Keeping the clunky multi single layer look helped me find which layer would crash, which  I have problem doing in the indexed environment, for lack of error trapping knowledge. Cannot just learn it all at the same time.....also have to keep an eye on the course material......

                                             

                                            Thanks also for showing the technique for a clean display of the output  data. It looks great. Much appreciated.

                                            • Re: Using Maximize Function within a program?
                                              ptc-4310378 Silver

                                              2001 doesn't want to have anything to do with the compact structure for the Solve...Given you came up with in 15! To begin with, 2001 wants to have the Guess as an Array the same size as the Solution Array....

                                               

                                              As the calcs are also faster in 15, I'll have to plug away in 15 for this one.

                                               

                                              I would have a question about the Guess for the Solve: How did you come up with the initial guess value of 0?

                                               

                                              I have never seen anything like that with a Solver before: 0 is for sure not a possible solution, but the Solver fails for any other value of the guess. Strange....and scary!  But seing how methodic you are, there is most likely some logic behind this choice?

                                               

                                              Thank you.

                                                • Re: Using Maximize Function within a program?
                                                  AlanStevens Platinum

                                                  Laurent Muller wrote:

                                                   

                                                  2001 doesn't want to have anything to do with the compact structure for the Solve...Given you came up with in 15! To begin with, 2001 wants to have the Guess as an Array the same size as the Solution Array....

                                                   

                                                  As the calcs are also faster in 15, I'll have to plug away in 15 for this one.

                                                   

                                                  I would have a question about the Guess for the Solve: How did you come up with the initial guess value of 0?

                                                   

                                                  I have never seen anything like that with a Solver before: 0 is for sure not a possible solution, but the Solver fails for any other value of the guess. Strange....and scary!  But seing how methodic you are, there is most likely some logic behind this choice?

                                                   

                                                  Thank you.

                                                  Can't help with the 2001 version.  I haven't had that version available to play with for years now!

                                                   

                                                  There was no great logic behind the choice of 0 as an initial guess.  It's just a number of the same order as the outputs that were produced (where they were produced) by your initial guess of 0.5.  As it happened it worked - it might not have!

                                                   

                                                  I think the strange behaviour that you observed (I observed some myself with your original file) might be related to the fact that you chose to use the word "time" as a range variable.  Unfortunately, "time" is a built-in function that doesn't seem to like being re-defined.  The strange behaviour disappeared when I used a different variable name.

                                                   

                                                  Alan

                                                    • Re: Using Maximize Function within a program?
                                                      ptc-4310378 Silver

                                                      Thanks for the tip on the pitfall of redefining "time". I see 15 is good at warning against this type of blunders.

                                                       

                                                      With your stramlined solver in 15, the calcs are so much faster that there is no turning back to 2001 with this file.

                                                       

                                                      Also hard to live without the program debugger once discovered in 15.....

                                                        • Re: Using Maximize Function within a program?
                                                          AlanStevens Platinum

                                                          Incidentally, you might want to check your values of krw.  Since some values of Swd are negative the corrsponding values of krw are complex (I assume these are unphysical) - That's why not all the values have plotted on your graphs.

                                                           

                                                          Alan

                                                            • Re: Using Maximize Function within a program?
                                                              ptc-4310378 Silver

                                                              And you are a mind reader too....

                                                               

                                                              I was just wondering about that. As you say, the imaginary values are "unphysical" (the relative permeabilities of oil [kro] and water [krw] have no meaning below their minimum saturation level).

                                                               

                                                              This is why I was going through the pain of plotting the kr's against their active saturation band only. But yesterday, to save the extra step, I tried to plot against the full saturation range from 0 to 100%, not just the physical saturation band, and it all plotted just fine and I was wondering why.

                                                               

                                                              I had forgotten about the imaginary values, which is what must be automatically cutting off the unwanted display from the graph. Thanks. I was still scratching my head on this one.

                                                               

                                                              kro values above 1 have no meaning either, but they are simply cut off by sizing the axis of the graph.

                                          • Re: Using Maximize Function within a program?
                                            ptc-4310378 Silver

                                            Precisely one of the things that was wrong: I was attempting to use Maximize itself in the Program, instead of a UDF of Maximize. The analogy to a Solver UDF didn't down on me. Alan pointed out that to top it all, the Maximize UDF can handle Arrays by itself so there is not even the need to wrap it up in a Program (and no Vectorization required in this case either).

                                             

                                            Thank you.