Group Abstract Group Abstract

Message Boards Message Boards

Optimizing code that numerically evaluates line integrals on Mesh/Grid

Posted 6 years ago
4 Replies

Hi Neil,

Thank you for your insight! This did help a lot as well. The actual calculations I run involve even more complicated expressions - but this trick led to a significant performance improvement. I have no issue with this implementation being a bit slower than MATLAB. The time seems like a fair trade-off for the advanced algebra manipulation capabilities. Thank you very much, again.

Best, Tharindu

Thirindu,

I woke up with an additional thought that helps. The problem that I see is your line integral is complicated so it takes time to numerically evaluate and integrate. I tried a FullSimplify on it and I gave up after 20 min. The code below, however, yields a MUCH simpler expression that then drops the time for meshdim =100 from 41 seconds to 15 seconds! The idea is to simplify the intermediate expressions so the large expression does not "blow up" and then FullSimplify can run in a reasonable time.

uPAkxPre[kx_, ky_, M_, phi_, t_, 
   t2_] = (I uPConj[kx, ky, M, phi, t, t2] // 
      Simplify).(D[uP[kx, ky, M, phi, t, t2], kx] // Simplify) // 
   FullSimplify;
uPAkyPre[kx_, ky_, M_, phi_, t_, 
   t2_] = (I uPConj[kx, ky, M, phi, t, t2] // 
      Simplify).(D[uP[kx, ky, M, phi, t, t2], ky] // Simplify) // 
   FullSimplify;

Using Simplify instead of FullSimplify takes less time on this step but runs in 18 seconds (vs the 15). However, since the FullSimplification runs only once, its not so bad (maybe 5 min). Either way you are in much better shape. How does this compare to your MATLAB computations? If you are far behind, I will think a bit more about it.

Regards,

Neil

POSTED BY: Neil Singer

Tharindu,

With just a quick glance at the code, I see one big place to save time. You evaluate the line integrals but do not simplify them. I got more than an order of magnitude speedup by simplifying the complicated expressions.

uPAkxPre[kx_, ky_, M_, phi_, t_, t2_] = 
  I uPConj[kx, ky, M, phi, t, t2].D[uP[kx, ky, M, phi, t, t2], kx] // 
   Simplify;
uPAkyPre[kx_, ky_, M_, phi_, t_, t2_] = 
  I uPConj[kx, ky, M, phi, t, t2].D[uP[kx, ky, M, phi, t, t2], ky] // 
   Simplify;

Doing this one change and adding an accuracy goal on the integrals (to avoid errors when the integrals are zero)

int12 = NIntegrate[uPAkx[kx, y1], {kx, x1, x2}, AccuracyGoal -> 5];

I get 41 seconds of runtime (on a very fast MacBook Pro EDIT for meshdims = 100). The next step might be to try different integration options to see if you can optimize the integrals. I hope this helps.

BTW, I appreciated the nicely commented code -- It makes it much easier to help.

Regards,

Neil

POSTED BY: Neil Singer

Thank you for your insight, Neil - your suggestions did speed my code up significantly! I will still keep trying different integration options.

Best,

Tharindu

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard