Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


2 Pages12>
Options
Go to last post Go to first unread
Offline RegCurry  
#1 Posted : 11 March 2019 09:23:03(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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


Mathcad TEST EXPLICIT TDFD VS SMATH.pdf (115kb) downloaded 46 time(s). COUPLED TDFD WITH SNIPPETS.sm (69kb) downloaded 36 time(s).

Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline Ber7  
#2 Posted : 11 March 2019 12:31:10(UTC)
Ber7


Rank: Advanced Member

Groups: Registered
Joined: 15/07/2010(UTC)
Posts: 437
Man
Israel
Location: Beer-Sheva

Was thanked: 520 time(s) in 288 post(s)
Adding one line dramatically reduces the calculation time.
COUPLED 1.sm (80kb) downloaded 43 time(s).
thanks 1 user thanked Ber7 for this useful post.
on 11/03/2019(UTC)
Offline Davide Carpi  
#3 Posted : 11 March 2019 13:23:35(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,647
Man
Italy
Location: Italy

Was thanked: 1329 time(s) in 875 post(s)
Some more improvements:

COUPLED 2.sm (78kb) downloaded 38 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.

Edited by user 11 March 2019 13:30:23(UTC)  | Reason: Not specified

If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects
Offline RegCurry  
#4 Posted : 11 March 2019 13:42:32(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

Good Clap Good

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! Biggrin Biggrin Biggrin

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

Reg
Offline MarB  
#5 Posted : 11 March 2019 14:56:37(UTC)
MarB


Rank: Advanced Member

Groups: Registered
Joined: 21/10/2016(UTC)
Posts: 84

Was thanked: 21 time(s) in 17 post(s)
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.)

Edited by user 11 March 2019 19:17:23(UTC)  | Reason: Not specified

thanks 1 user thanked MarB for this useful post.
on 11/03/2019(UTC)
Offline RegCurry  
#6 Posted : 11 March 2019 16:36:01(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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.Confusion Help smilie

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 (71kb) downloaded 32 time(s).

Offline MarB  
#7 Posted : 11 March 2019 19:25:34(UTC)
MarB


Rank: Advanced Member

Groups: Registered
Joined: 21/10/2016(UTC)
Posts: 84

Was thanked: 21 time(s) in 17 post(s)
Originally Posted by: RegCurry Go to Quoted Post
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
Offline RegCurry  
#8 Posted : 11 March 2019 19:45:52(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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.
Offline Jean Giraud  
#9 Posted : 11 March 2019 20:59:00(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: RegCurry Go to Quoted Post
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 (30kb) downloaded 24 time(s).
Offline RegCurry  
#10 Posted : 11 March 2019 21:52:15(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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. Censored Sad

Take care and thanks again,
Reg

Offline Jean Giraud  
#11 Posted : 12 March 2019 06:46:19(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: RegCurry Go to Quoted Post
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 (93kb) downloaded 14 time(s).
Offline Razonar  
#12 Posted : 12 March 2019 09:33:35(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
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 (145kb) downloaded 24 time(s).

Thanks in advance.
Alvaro.
Offline Davide Carpi  
#13 Posted : 12 March 2019 11:20:41(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,647
Man
Italy
Location: Italy

Was thanked: 1329 time(s) in 875 post(s)
I'm second to the request to see the mathcad code for comparision Good

Originally Posted by: RegCurry Go to Quoted Post
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 consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects
Offline RegCurry  
#14 Posted : 12 March 2019 11:29:26(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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 (302kb) downloaded 27 time(s).

Here is the pdf of the Mathcad file: TDFD SUBROUTINE VS SMATH.pdf (53kb) downloaded 23 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 (16kb) downloaded 26 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 (77kb) downloaded 22 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


Offline RegCurry  
#15 Posted : 12 March 2019 11:53:44(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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. Biggrin Biggrin Biggrin
Offline Davide Carpi  
#16 Posted : 12 March 2019 12:51:00(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,647
Man
Italy
Location: Italy

Was thanked: 1329 time(s) in 875 post(s)
Thank you Good

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

Code:
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)

Edited by user 12 March 2019 12:55:13(UTC)  | Reason: Not specified

If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects
Offline RegCurry  
#17 Posted : 12 March 2019 13:28:52(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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
Offline RegCurry  
#18 Posted : 12 March 2019 14:24:09(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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 (311kb) downloaded 24 time(s).

At this stage in life I do all this to delay dementia; looks like it’s not working very well at present.
Offline RegCurry  
#19 Posted : 12 March 2019 16:12:55(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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.
Offline RegCurry  
#20 Posted : 12 March 2019 18:07:17(UTC)
RegCurry


Rank: Advanced Member

Groups: Registered
Joined: 10/02/2019(UTC)
Posts: 56
Man
United States
Location: Colorado

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 (121kb) downloaded 28 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. Good

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. Biggrin

Reg
Users browsing this topic
Guest
2 Pages12>
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.