Saving Runtime

Saving Runtime - Can Runtime be retrieved - Messages

#1 Posted: 3/11/2019 3:23:03 AM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

I finally got this SMath code running for calculating the coupled time-domain transmission line equations for a time dependent E-field driver spatially constant over the line. However, I have one concern. The code runs a relatively long time compared to an identical calculation in Mathcad.

The SMath code runs for about 30 seconds for only 20 time points and 10 spatial cells. Mathcad runs in 6 seconds for 240 time points and 100 spatial cells. To be equivalent to the Mathcad calculation, I need to start the time at 10^(-9) seconds and run to the end time of 0.1 seconds with 30 time points per decade. Since, I anticipate a long run-time for SMath, I would like to be able to retrieve the run-time without having to watch the code run. Is this possible?

If interested, I have attached the SMath code and a pdf of the Mathcad code. I have turned off Auto calculation.

Thanks,
Reg


File not found.File not found.
#2 Posted: 3/11/2019 6:31:10 AM
Fridel Selitsky

Fridel Selitsky

520 likes in 451 posts.

Group: User

Adding one line dramatically reduces the calculation time.
COUPLED 1.sm (79 KiB) downloaded 55 time(s).
1 users liked this post
frapuano 3/11/2019 7:51:00 AM
#3 Posted: 3/11/2019 7:23:35 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Some more improvements:

COUPLED 2.sm (77 KiB) downloaded 52 time(s).

  • Initializations: inside I, Clear(I); this is to avoid when reusing to work over an existing I (values and size of the matrix might cause troubles in this case);
  • Initializations: inside Ans, there's no reason for the first loop, use matrix(rows,columns) to have a null matrix of the desired size;
  • Procedures: when you have variable:line(...) this is hold in memory as it is, hence every time you call it it is fully evaluated every time. If the content of the procedure isn't affected by external values or these external values are constant for more than a single use, better you assign that variable to another so it can be evaluated once (in the ber7 example Ans1:Ans; you can do even Ans:Ans, if the procedure isn't meant to be reused changing some global parameter);
  • Inside Ans, there's no need to do VT:VG at the end of every j-loop, since VT isn't used in that loop;


P.S. I don't know if it is by your design, but your VG assignments has a strange pattern, you overwrite the whole matrix when j==1 or j==Ncell.
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#4 Posted: 3/11/2019 7:42:32 AM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User



Thanks guys. I will give both suggestions a try tomorrow. I am in Colorado, so I probably should go to bed. But at my age (76), I will have a long sleep soon enough! :d :d :d

I clicked on “Thank” for both of you, but I got error messages. Is that because I am responding on my iPad Pro?

Reg
#5 Posted: 3/11/2019 8:56:37 AM
Martin_B

Martin_B

21 likes in 84 posts.

Group: User

Whenever possible use 'numeric otpimization".
Just select everything (Ctrl A on Windows PC) and choose 'numeric otpimization". (Then eval is not needed necessarily.)
Takes only half a second in my PC. (Adding Ans:Ans, of course.)
1 users liked this post
Davide Carpi 3/11/2019 1:27:00 PM
#6 Posted: 3/11/2019 10:36:01 AM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

All,

Thanks all for the helpful hints.

Well, I couldn't sleep; I clearly had to take a look.

A couple of more questions:

I am sorry to be so dumb, but Mathcad is ingrained in my brain. I clearly have a lot to learn about SMath. I don't really know what the eval function is doing but it definitely helps. SMath states it coverts from symbolic to numeric notation. I didn't realize things were in symbolic notation before.

MarB--I am really new to SMath. Could you expand a little with perhaps an example of the syntax for Str-A and numeric optimization.

Davide--I have attached your worksheet but increasing the time points. It reached a floating point overflow when I set the final time NFF = -1 or final time=(10^-1). It finished okay with NFF = -2 or final time =(10^-2). I left the spatial cells at 10. Even with NFF=-6 or final time=(10^-6) as in the original, it had an overflow for the number of time point NPD=30. Is there a way to trace the calculations to see where the overflow is. I have never used the debugger window or trace so I am not sure how that works. Is it explained somewhere? My ultimate goal is NPD=30, NFS=-10, NFF=3, Ncell=100...that's a lot of loops in the code.

Also, Davide, your question about the V definitions at j==1 and j==Ncell...those values have to be calculated at every time point k. It defines the voltage at the end point cable terminations. Do you think they can be moved above the j-loop?

Finally, I don't understand this first statement in the couple TDFD routine: AI:=(VG:=matrix(Ncell,Nt)) What does this do?

COUPLED Reg3.sm (70 KiB) downloaded 45 time(s).

#7 Posted: 3/11/2019 1:25:34 PM
Martin_B

Martin_B

21 likes in 84 posts.

Group: User

Wrote

All,
MarB--I am really new to SMath. Could you expand a little with perhaps an example of the syntax for Str-A and numeric optimization.


Sorry, on Windows keyboards with English layout 'Ctrl + A' is the correct key to select the whole content of the sheet.
Then select from the context menu Optimization -> numeric.

opti.png
#8 Posted: 3/11/2019 1:45:52 PM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

Thanks. I discovered that and it really helped. For the test case mine ran for about 0.8 seconds. Your computer is faster than mine. Mine is five years old. It even runs for total time from 10-9 to 10-1 seconds with 30 points/decade and number of spatial cells = 10. The runtime for that case is about 2 minutes. If I increase the number of spatial cells to 100 it was still running at 1 hour.

Again thank you.
#9 Posted: 3/11/2019 2:59:00 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

Well, I couldn't sleep; I clearly had to take a look.


Hello Reg,
Read slow as you have opened the attached.
Cf(x) is the classical continued fraction that is used to compare
the timing with and without eval(,) in the computation of 'data'.
Calling for the plots has no effect on the timing as they are both
same consumers. So, eval(,) is 15 s ... w/o eval(,) 42 s.
The catch is here ...
If you want to code the Cf(x) in Excel or else pocket calculator,
you will have to code bottom-up. BTW: continued fractions date back
to Mesopotamians, how old ?
At this point, continued fractions can be computed top down using
Wallis algorithm [Wallis was Newton's teacher].
This method was implemented in the first computing machinery,
typical in some IBM kind of Main Frame.
This coding is implemented in machine code in Mathcad, Smath
maybe other CAS. Thus, it is hyperfast so that with or w/o eval(,)
has visible effect but only 1/3 in this example.
Instead, if the function would be a most demanding parametric
function involving ln(x), exp(x) and/or other advanced functions
evaluation of data would range much higher [or lower timing]
ranging from little seconds to fat minutes.
Also, some program have to be included in eval(program).
Does that make you sleep deeper ?

Jean

Reg[eval].sm (29 KiB) downloaded 41 time(s).
#10 Posted: 3/11/2019 3:52:15 PM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

Jean,
Thanks for your help and info. You are way beyond me. I am basically a brute force programmer of Mathcad and new to SMath. Once I get something that works and the answer is close to my simple back of the envelope estimates I am happy.

I am from the days of slide rules with pencil and paper. I remember how great we thought the first HP hand-held calculators were. In the early days of my career, we physicists had programmers doing the coding for us. When the world changed and we could no longer afford the luxury of the programmers, out of necessity, I did a little programming in FORTRAN. When Mathcad came out, I thought that was great because it "looked like pencil and paper."

I did get one additional help from your worksheet and that was the time() function. I did not realize it worked like that in Mathcad. Now I know how long each of these versions run.

Yes, I am now getting sleepy; however, I have to leave for the dentist.

Take care and thanks again,
Reg

#11 Posted: 3/12/2019 12:46:19 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

Once I get something that works


From other replies it seems working, but not Win 7 SS 6179.
The beta, alpha magnitude don't match the exp(,) wrt 't'
Maybe the beta, alpha got corrupted in the transmission ?

ResultAbove.PNG

COUPLED Reg3[UKN].sm (92 KiB) downloaded 28 time(s).
#12 Posted: 3/12/2019 3:33:35 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Hi Reg. You routine in SMath have some issues. In the attached, I guess that it's an approach for the solution, but I don't know if I decouple ok V and I.

Can you upload the pdf of the file Reference:C:\Users\Reg\Reg Documents\MATHLIB\SUBROUTINES_MATHCAD\TDFD SUBROUTINE VS SMATH.xmcd(R) please?

Also, with the same values, I get different length for t at the starts. So, can also upload the first reference file?

Here my attemp:

COUPLED TDFD WITH SNIPPETS.sm (144 KiB) downloaded 40 time(s).

Thanks in advance.
Alvaro.
#13 Posted: 3/12/2019 5:20:41 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

I'm second to the request to see the mathcad code for comparision

Wrote

Finally, I don't understand this first statement in the couple TDFD routine: AI:=(VG:=matrix(Ncell,Nt)) What does this do?



this assign a matrix of zeros with size [Ncell rows, Nt columns] to VG, then assign the same matrix to AI (chained assignment)
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#14 Posted: 3/12/2019 5:29:26 AM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

Hi Razonar,
Thanks for your input. I need to review your coupled equations syntax to make sure I understand it, but I think I prefer it to my brute force method. I am new to SMath so I don't quite understand your method yet.

As far as the decoupling goes, it is done more from the physics than the math. For a spatially constant driving field over the transmission line and approximate shorts on both ends, the voltage on the line is assumed to be approximately zero. It is only an approximate solution, but surprisingly close to the coupled solution. See this pdf file to see the derivation of the decoupling.
Decouple TM Line Equations .pdf (301 KiB) downloaded 46 time(s).

Here is the pdf of the Mathcad file: TDFD SUBROUTINE VS SMATH.pdf (52 KiB) downloaded 45 time(s). Or did you want the actual xmcd file? The VS SMATH in the title is somewhat misleading. It does not have both versions in the Mathcad file. I just used this file to help construct my original SMath Coupled TDFD routine that is in the original post.

Is this the sm file your want: TDFD COUPLED ROUTINE ORIG.sm (15 KiB) downloaded 43 time(s). Or did you want want my points per decade routine. That has not changed since my original post. I have changed the COUPLED TDFD WITH SNIPPETS.sm a little from when the original post when the code ran a long time. I made changes based on the various comments above including the numeric optimization in the whole code Re. Ber7's suggestion. This all made things run much faster. However, I still cannot get the very short run-times of the Mathcad code for NPD=30, NFS=-9, NFS=-1 and Ncell=100.

My current version of the Coupled TDFD code is here: HEMP COUPLED TDFD DBL EXPON DRIVER.sm (76 KiB) downloaded 45 time(s). With the current setting for the points per decade it runs for about 6 minutes on my computer. It runs for less than a second with the original points per decade.

Again, if you want the Mathcad codes in xmcd just let me know.

All of you guys have been most helpful in my quest to learn SMath and I truly appreciate it.

Reg


#15 Posted: 3/12/2019 5:53:44 AM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

I forgot to mention that in the coupled equations the concept goes as.

Loop on time
Loop on space for Voltage at present time using current from previous time
Loop on space for Current at present time using Voltage at present time.
Return for the next loop on time.

Don't know if this helps or not. This is all from a code I wrote 30 years ago in FORTRAN and attempted to recreate in Mathcad. It gave essentially the same results. Now I am trying to recreate it in SMath assuming that my Mathcad license won't last forever...forever for me is probably not that long considering I am 76. :d :d :d
#16 Posted: 3/12/2019 6:51:00 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Thank you

what I don't understand in the code is the assignment of the boundary conditions. In both SMath and mathcad I see that:

VG ← AI ⋅ ZTNcell if j = Ncell

This makes useless the space loop on the voltage, since when you end the loop you assume that any j-th element of VG is the result of AI*ZTNcell, unchanged since the begin of the j-loop (and if you show V at the end of TDFD any point on the line has the same values for any given time)
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#17 Posted: 3/12/2019 7:28:52 AM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

I’m away from my computer and responding on my iPad. I think that is a coding error on my part. I think it should be more like


V[1,k] := AI[1,k-1]*ZT1 if j=1
V[Ncell,k] := AI[Ncell,k-1]*ZTNcell if j=Ncell

Thanks for the catch. I will look at this more later today when I am back at my computer.
Reg
#18 Posted: 3/12/2019 8:24:09 AM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

Davide,

You found where I went down the rabbit hole. The bounty conditions on the cable ends were originally done through the definitions of Cg on the ends...set to a large number to simulate a short forcing the voltage to be zero on the ends. This correctly gives V=0 over the entire cabe since it is driven uniformly along the line. See the attached MATHCAD pdf file. I will look at more details tomorrow and test if I can simulate open circuit terminations and characteristic impedance terminations. What I said in the post above won’t work either because we have AI only at Ncell-1 locations and we have V at Ncell locations.

Mathcad TDFD Original Code.pdf (310 KiB) downloaded 40 time(s).

At this stage in life I do all this to delay dementia; looks like it’s not working very well at present.
#19 Posted: 3/12/2019 10:12:55 AM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

Davide,

One more important point: the voltage on the line is indeed constant at all points on the line at all time because the driving E-field is constant along the line. This is not the case of a voltage driver on one end of the line. The two problems are often confused.. That problem is most easily solved by devloping a Thevenin Equivalent circuit where the impedance of the circuit is the input impedance of the line. The only way you would get a space varying voltage is if the E- field varied spatially or if the terminations were significantly different. Here, the current flows in a loop...down the cable and back through the soil. The current is also essentially constant over the line. If the driving field were to sweep down the line, that would give a spatially varying voltage and current. That case requires solving the transmission line equations in retarded time...a more difficult problem.

This doesn’t negate the error you found in the coding. That still needs to be fixed as per the attached pdf file above. That code also has a typo in the definition of Gg that I need to fix also.
#20 Posted: 3/12/2019 12:07:17 PM
RegCurry

RegCurry

0 likes in 57 posts.

Group: User

Davide and Razonar,

Okay, here is the Mathcad code that I should have started with before I wandered off into never-never-land. This handles the cable terminations correctly for shorts on both ends.

EXPLICIT TDFD DBL EXP DRIVER.pdf (120 KiB) downloaded 47 time(s).

I don't know what I was thinking trying to define the terminations inside the difference equations. Perhaps it's doable; it's just not obvious to me right now.

Notice the run-time is only 12 seconds for 10^-9 <= t <= 10^-1 using 30 points per decade for a total of 240 time points. The number of spatial cells is 100. I am not sure I can approach that with SMath unless there is an alternate programming for the explicit TDFD. Implicit differencing should be faster, but I am not good at that. In fact, I am not sure I am even capable. When I retired I gave all my physics and math books away and started buying golf and fly fishing books.

A side note: In my day the correct spelling was kiloJoule and not kilojoule. The reason is Joule was a real person. Similarly it was kiloVolts, microAmps, etc. We just get no respect these days. :d

Reg
  • New Posts New Posts
  • No New Posts No New Posts