-
Notifications
You must be signed in to change notification settings - Fork 1
/
atom.xml
1972 lines (1519 loc) · 386 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>FLOW Lab</title>
<link href="http://flow.byu.edu/atom.xml" rel="self"/>
<link href="http://flow.byu.edu/"/>
<updated>2025-01-08T17:29:37-07:00</updated>
<id>http://flow.byu.edu</id>
<author>
<name>Andrew Ning</name>
<email></email>
</author>
<entry>
<title>Optimization Book Available</title>
<link href="http://flow.byu.edu/posts/opt-book"/>
<updated>2021-10-15T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/opt-book</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:share:6853387400052391936" height="698" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>BEM Paper</title>
<link href="http://flow.byu.edu/posts/bem-paper"/>
<updated>2021-07-30T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/bem-paper</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:share:6825450879324434432" height="622" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>Eduardo Research Update</title>
<link href="http://flow.byu.edu/posts/eduardo-latest"/>
<updated>2021-03-01T00:00:00-07:00</updated>
<id>http://flow.byu.edu/posts/eduardo-latest</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:6779795433318420480" height="1142" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>Reformulated VPM</title>
<link href="http://flow.byu.edu/posts/reformulated-vpm"/>
<updated>2021-02-01T00:00:00-07:00</updated>
<id>http://flow.byu.edu/posts/reformulated-vpm</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:6767685749157588992" height="990" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>Optimization Book Announcement</title>
<link href="http://flow.byu.edu/posts/optimization-book"/>
<updated>2021-01-20T00:00:00-07:00</updated>
<id>http://flow.byu.edu/posts/optimization-book</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:share:6758135437300154368" height="679" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>PJ Defense</title>
<link href="http://flow.byu.edu/posts/pj-defense"/>
<updated>2020-09-30T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/pj-defense</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:share:6714312529692237824" height="641" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>VPM Paper</title>
<link href="http://flow.byu.edu/posts/vpm-paper"/>
<updated>2020-08-30T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/vpm-paper</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:6711406263634419712" height="857" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>Airborne Wind with Vortex Particle Method</title>
<link href="http://flow.byu.edu/posts/wind-harvesting"/>
<updated>2020-08-16T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/wind-harvesting</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:6694076731474739200" height="949" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>Eduardo's Past Three Years</title>
<link href="http://flow.byu.edu/posts/eduardo-three-years"/>
<updated>2020-08-10T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/eduardo-three-years</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:6689985497042944000" height="797" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>FLOWUnsteady in Google Drive</title>
<link href="http://flow.byu.edu/posts/google-drive-vpm"/>
<updated>2020-08-02T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/google-drive-vpm</id>
<content type="html"><iframe src="https://www.linkedin.com/embed/feed/update/urn:li:share:6689003185174118400" height="1094" width="504" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
</content>
</entry>
<entry>
<title>Takeoff and Performance Tradeoffs of Retrofit Distributed Electric Propulsion for Urban Transport</title>
<link href="http://flow.byu.edu/posts/electric-stol"/>
<updated>2019-09-05T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/electric-stol</id>
<content type="html"><p><img src="https://byu.box.com/shared/static/2ilze6i6aq4srqita1xqdu2sd6m62cqb.gif" alt="Electric Aircraft Optimization" /></p>
<p>A trade study investigating the use of distributed electric propulsion on fixed-wing aircraft to shorten takeoff distance. This aircraft configuration could potentially compete with VTOL aircraft in urban air transportation. The trade study was done using a MDO framework including blade element momentum method, vortex lattice method, linear-beam finite element analysis, classical laminate theory, composite failure, empirically-based blade noise modeling, motor and motor-controller mass models, and gradient-based optimization.</p>
<p>Summary:</p>
<ul>
<li>Validation of propeller-on-wing modeling, propeller performance, noise, and motor performance</li>
<li>New method for smoothly evaluating propeller effects on discrete VLM</li>
<li>Retrofit application maintaining the P2006T airframe, maximum takeoff weight, and balance</li>
<li>Pareto fronts of takeoff distance for varying cruise speeds and noise levels</li>
<li>System performance tradeoff evaluation including augmented lift, thrust, and takeoff profile</li>
</ul>
<ol class="bibliography nonum" reversed=""><li><span id="Moore2019">Moore, K., and Ning, A., “Takeoff and Performance Tradeoffs of Retrofit Distributed Electric Propulsion for Urban Transport,” <i>Journal of Aircraft</i>, Vol. 56, No. 5, pp. 1880–1892, Sep. 2019. doi:10.2514/1.C035321</span>
<a href="#nowhere" onclick="toggle_visibility('Moore2019-bibtex');">[BibTeX]</a> <a href="http://dx.doi.org/10.2514/1.C035321">[DOI]</a> <a href="https://scholarsarchive.byu.edu/cgi/viewcontent.cgi?article=4204&amp;context=facpub">[PDF]</a>
<pre id="Moore2019-bibtex" class="bibtex">@article{Moore2019,
author = {Moore, Kevin and Ning, Andrew},
doi = {10.2514/1.C035321},
journal = {Journal of Aircraft},
month = sep,
number = {5},
pages = {1880-1892},
title = {Takeoff and Performance Tradeoffs of Retrofit Distributed Electric Propulsion for Urban Transport},
volume = {56},
year = {2019}
}
</pre>
</li></ol>
</content>
</entry>
<entry>
<title>Multirotor - Vortex Particle Method</title>
<link href="http://flow.byu.edu/posts/multirotor-vpm2"/>
<updated>2019-07-01T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/multirotor-vpm2</id>
<content type="html"><iframe width="560" height="315" src="https://www.youtube.com/embed/SLpnVIBpkps" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>
<p>The viscous vortex particle method is a mesh-free Lagrangian CFD method for the numerical solution of Navier-Stokes in their vorticity form. It’s about two orders of magnitude faster than mesh-based CFD (RANS or LES). We are exploring its potential for modeling the complicated aerodynamics interactions in eVTOL aircraft design. All codes are in-house developed at BYU’s FLOW Lab,</p>
<p>Slides available <a href="https://lnkd.in/gEDYPch">here</a></p>
<p>SUMMARY:</p>
<ul>
<li>Validation of predicted multirotor interactions in both hover and forward flight, at both low and high Reynolds numbers.</li>
<li>We ran a parametric study with 1152 simulations taking ~3.5 days in a desktop computer (~4 mins per simulation).</li>
<li>We built response surfaces of unsteady loading and performance drop during rotor-on-rotor interactions of the APC 10x7 propeller.</li>
</ul>
<p>FUTURE WORK</p>
<ul>
<li>Coupling with noise code for prediction of acoustic interactions.</li>
<li>Automatic derivatives for gradient-based design optimization.</li>
</ul>
<ol class="bibliography nonum" reversed=""><li><span id="Alvarez2019">Alvarez, E. J., and Ning, A., “Modeling Multirotor Aerodynamic Interactions Through the Vortex Particle Method,” <i>AIAA Aviation Forum</i>, Dallas, TX, Jun. 2019. doi:10.2514/6.2019-2827</span>
<a href="#nowhere" onclick="toggle_visibility('Alvarez2019-bibtex');">[BibTeX]</a> <a href="http://dx.doi.org/10.2514/6.2019-2827">[DOI]</a> <a href="https://scholarsarchive.byu.edu/cgi/viewcontent.cgi?article=4145&amp;context=facpub">[PDF]</a>
<pre id="Alvarez2019-bibtex" class="bibtex">@inproceedings{Alvarez2019,
address = {Dallas, TX},
author = {Alvarez, Eduardo J. and Ning, Andrew},
booktitle = {{AIAA} Aviation Forum},
doi = {10.2514/6.2019-2827},
month = jun,
title = {Modeling Multirotor Aerodynamic Interactions Through the Vortex Particle Method},
year = {2019}
}
</pre>
</li></ol>
</content>
</entry>
<entry>
<title>Development of a Vertical-Axis Wind Turbine Wake Model</title>
<link href="http://flow.byu.edu/posts/vawt-wake"/>
<updated>2019-06-05T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/vawt-wake</id>
<content type="html"><p><img src="/posts/vawtV.gif" alt="Wake Visualization" /></p>
<p>Wind farm designers use mathematical models to describe turbine wakes, and through optimization, turbines can be positioned to minimize the negative wake interference of the wind farm. This method has been conducted by numerous researchers for the well-known horizontal-axis wind turbine, but the same type of research has been much more limited for a vertical-axis wind turbine (VAWT), as shown below. VAWT wake behavior is quite different from that of a horizontal axis turbine. Preliminary research has shown that VAWTs are capable of being placed closer together and even increasing their overall power production by harnessing the slight wind velocity increases of neighboring turbines.</p>
<p><img src="/posts/vawt.png" width="250px" class="img-left" /></p>
<p>We developed a wake model that computes the wind velocity profile around a VAWT from input parameters such as rotation speed, turbine blade size, and wind speed. To establish a foundation for the wake model, we ran hundreds of computer simulations of a two-dimensional VAWT at many different speed and geometry configurations, as shown by the example at the top of the post. Using the wake behavior obtained from these simulations, we used statistical analysis to simply the complex behavior down to fundamental nondimensional relationships between turbine speed and geometry that influence the wake’s profile both downstream and to the sides of the turbine. This predictive ability is important to take advantage of closely spaced VAWT pairs. Typical horizontal axis turbine wake models predict wake velocities behind a turbine, but to take advantage of the unique behavior of VAWTs, we needed to predict velocities <em>around</em> the turbine (there is often a speedup around the sides).</p>
<p>The new wake model runs in about a tenth of second, as compared to the original CFD simulation that takes several hours, and shows reasonable agreement against experimental wake studies. The VAWT wake model was further combined with the aerodynamic loading of turbine blades to translate wind velocity speeds around the turbine to turbine power production. While this wake model is a preliminary attempt to parametrically define VAWT wakes, it does show promise in its use for wind farm analysis and optimization.</p>
<p>The journal paper describing the wake model development and access to the wake model code can be found in the links below.</p>
<ol class="bibliography nonum" reversed=""><li><span id="Tingey2019">Tingey, E., and Ning, A., “Development of a Parameterized Reduced-Order Vertical-Axis Wind Turbine Wake Model,” <i>Wind Engineering</i>, Jun. 2019. doi:10.1177/0309524X19849864</span>
<a href="#nowhere" onclick="toggle_visibility('Tingey2019-bibtex');">[BibTeX]</a> <a href="http://dx.doi.org/10.1177/0309524X19849864">[DOI]</a> <a href="https://scholarsarchive.byu.edu/cgi/viewcontent.cgi?article=4122&amp;context=facpub">[PDF]</a> <a href="https://github.com/byuflowlab/vawt-wake-model">[Code]</a>
<pre id="Tingey2019-bibtex" class="bibtex">@article{Tingey2019,
author = {Tingey, Eric and Ning, Andrew},
doi = {10.1177/0309524X19849864},
journal = {Wind Engineering},
month = jun,
title = {Development of a Parameterized Reduced-Order Vertical-Axis Wind Turbine Wake Model},
year = {2019}
}
</pre>
</li></ol>
</content>
</entry>
<entry>
<title>Polynomial Chaos for Wind Farm Power Prediction</title>
<link href="http://flow.byu.edu/posts/pc-windfarm"/>
<updated>2019-05-28T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/pc-windfarm</id>
<content type="html"><center>
<img src="/posts/binned.png" alt="convergence of AEP" width="300px" />
</center>
<p>In a wind farm, it is important to understand how both the wind speed and wind direction varies. The variability in wind direction is captured in what is called a wind rose (see above). The longer bars in the wind rose indicate directions from which wind is more likely to come from. The power produced by the wind farm can change dramatically as the wind direction changes. This variation in power is often reduced to a single number—the expected value of power (an average). In the wind energy field, a common metric of interest is the annual energy production (AEP), which is the expected value of power (multiplied by time in a year and availability).</p>
<p>The wind rose is really a continuous distribution, but we often discretize it into bins like shown above. This discretization provides a convenient way to compute average power production. This is done by dividing the uncertain variable (wind direction in this example) into discrete bins, multiplying the power for each direction by its probability, then summing up over all bins. This approach is the familiar rectangle rule. It’s a simple approach and is almost always the approach used in wind farm analysis.</p>
<p>In a collaboration between our lab and the <a href="http://adl.stanford.edu">Aerospace Design Laboratory</a> at Stanford we’ve been exploring an alternative approach based on polynomial chaos. Polynomial chaos is not a new method. It is widely known and used in applications of uncertainty quantification, but has not yet been applied to wind farm analysis. The main idea is that rather than discretize into bins, we can use polynomials to approximate the continuous power response, allowing us to use fewer samples to approximate the AEP.</p>
<p>Santiago led a recent study where we explored using polynomial chaos, with uncertainty in both wind direction and wind speed, to predict AEP. We explored using both quadrature approaches and regression approaches and found the latter to be more effective. On average, polynomial chaos reduced the number of simulations required by a factor of 5 as compared to the rectangle rule. For example, the figure below shows how AEP varies with the number of samples with polynomial chaos based on regression to the left, and the rectangle rule to the right. We see that polynomial chaos converges with fewer samples and produces less variability in the AEP estimates from starting the quadrature at different points.</p>
<p><img src="/posts/uq.png" alt="convergence of AEP" /></p>
<p>Additionally, we applied polynomial chaos to optimizing wind farm layouts. We found that using polynomial chaos with regression allowed for about 1/3 of the simulations required for the rectangle rule (while producing a slightly better AEP). One such case is shown below.</p>
<p><img src="/posts/pc-opt.png" alt="optimization using PC" /></p>
<p>Further details are available in the open-access paper and data linked below.</p>
<ol class="bibliography nonum" reversed=""><li><span id="Padron2019">Padrón, A. S., Thomas, J., Stanley, A. P. J., Alonso, J. J., and Ning, A., “Polynomial Chaos to Efficiently Compute the Annual Energy Production in Wind Farm Layout Optimization,” <i>Wind Energy Science</i>, Vol. 4, pp. 211–231, May 2019. doi:10.5194/wes-4-211-2019</span>
<a href="#nowhere" onclick="toggle_visibility('Padron2019-bibtex');">[BibTeX]</a> <a href="http://dx.doi.org/10.5194/wes-4-211-2019">[DOI]</a> <a href="https://www.wind-energ-sci.net/4/211/2019/wes-4-211-2019.pdf">[PDF]</a> <a href="https://github.com/byuflowlab/windfarm-ouu">[Code]</a>
<pre id="Padron2019-bibtex" class="bibtex">@article{Padron2019,
author = {Padr\'{o}n, A. Santiago and Thomas, Jared and Stanley, Andrew P. J. and Alonso, Juan J. and Ning, Andrew},
doi = {10.5194/wes-4-211-2019},
journal = {Wind Energy Science},
month = may,
pages = {211-231},
title = {Polynomial Chaos to Efficiently Compute the Annual Energy Production in Wind Farm Layout Optimization},
volume = {4},
year = {2019}
}
</pre>
</li></ol>
</content>
</entry>
<entry>
<title>Making Julia as Fast as C++</title>
<link href="http://flow.byu.edu/posts/julia-c++"/>
<updated>2019-04-18T00:00:00-06:00</updated>
<id>http://flow.byu.edu/posts/juliacpp</id>
<content type="html"><center>
<img src="/posts/vortex00.gif" alt="Vid here" width="500px" />
</center>
<h2 id="introduction">Introduction</h2>
<p>The rumor says that Julia can achieve the <a href="https://julialang.org/benchmarks/">same computing
performance</a> as any other compiled language
like C++ and FORTRAN. After coding in Julia for the past two years I have
definitely fell in love with its pythonic syntax, multiple dispatch, and MATLAB-like
handiness in linear algebra, while being able to use compilation features
like explicit type declaration for bug-preventive programming. In summary,
Julia’s philosophy brings all the flexibility of an interpreted language,
meanwhile its Just-In-Time (JIT) compilation makes it a defacto
compiled language.</p>
<p>Julia’s high level syntax makes the language easygoing for programmers from any
background, however, achieving high performance is sort of
an art. In this post I summarize some of the things I’ve learned while crafting
my Julia codes for high-performance computing. I will attempt to show the
process of code optimization through a real-world computing application from
aerodynamics: the <a href="https://scholarsarchive.byu.edu/facpub/2116/">vortex particle
method</a>\(^{[1,\,2]}\).</p>
<h2 id="problem-definition">Problem Definition</h2>
<p>In the <a href="https://scholarsarchive.byu.edu/facpub/2116/">vortex particle method</a> we
are interested in calculating the velocity \(\mathbf{u}\) and velocity Jacobian
\(\mathbf{J}\) that a field of \(N\) vortex particles induces at an arbitrary
position \(\mathbf{x}\). This is calculated as</p>
\[{\bf u}\left( {\bf x} \right) = \sum\limits_p^N g_\sigma\left(
{\bf x}-{\bf x}_p \right)
{\bf K}\left( {\bf x}-{\bf x}_p
\right) \times
\boldsymbol\Gamma_p\]
\[\frac{\partial {\bf u}}{\partial x_j}\left( {\bf x} \right)
= \sum\limits_p^N \left[
\left(
\frac{1}{\sigma }
\frac{\Delta x_j}{\Vert \Delta \mathbf{x} \Vert}
\frac{\partial g}{\partial r}
\left(
\frac{\Vert \Delta\mathbf{x} \Vert}{\sigma}
\right) -
3 g_\sigma\left( \Delta{\bf x} \right)
\frac{\Delta x_j}{\Vert \Delta\mathbf{x} \Vert^2}
\right)
{\bf K}\left( \Delta\mathbf{x} \right) \times \boldsymbol\Gamma_p -
\frac{g_\sigma\left( \Delta{\bf x} \right) }{4\pi \Vert \Delta{\bf
x} \Vert^3}
\delta_{ij} \times \boldsymbol\Gamma_p
\right]
,\]
<p>with \({\bf K}\) the singular Newtonian kernel \({\bf K}\left( {\bf x}\right)=-\frac{ {\bf x} }{4\pi \Vert{\bf x}\Vert^3}\), \(g_\sigma\) a regularizing
function of smoothing radius \(\sigma\), and \(\mathbf{x}_p\) and
\(\boldsymbol{\Gamma}_p\) the position and vectorial strength of the \(p\)-th
particle, respectively. Furthermore, the governing equations of the method
require evaluating the velocity \(\mathbf{u}\) and Jacobian \(\mathbf{J}\) that the
collection of particles induces on itself, leading to the well-known <a href="https://en.wikipedia.org/wiki/N-body_problem">\(N\)-body
problem</a> of computational
complexity \(\mathcal{O}(N^2)\).</p>
<p>Choosing Winckelmans’ regularizing kernel\(^{[1]}\)</p>
\[g(r) = r^3 \frac{r^2 + 5/2}{\left( r^2 + 1 \right)^{5/2}}\]
\[\frac{\partial g}{\partial r} (r) = \frac{15}{2}
\frac{r^2}{\left( r^2 + 1 \right)^{7/2}}
,\]
<p>the above equations are implemented in C++ as follows:</p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Particle-to-particle interactions</span>
<span class="kt">void</span> <span class="nf">P2P</span><span class="p">(</span><span class="n">Particle</span> <span class="o">*</span> <span class="n">P</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">numParticles</span><span class="p">)</span> <span class="p">{</span>
<span class="n">real_t</span> <span class="n">r</span><span class="p">,</span> <span class="n">ros</span><span class="p">,</span> <span class="n">aux</span><span class="p">,</span> <span class="n">g_sgm</span><span class="p">,</span> <span class="n">dgdr</span><span class="p">;</span>
<span class="n">vec3</span> <span class="n">dX</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">numParticles</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">j</span><span class="o">&lt;</span><span class="n">numParticles</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="n">dX</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">X</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">X</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
<span class="n">dX</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">X</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">X</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
<span class="n">dX</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">X</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">X</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">dX</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="n">dX</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">dX</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="n">dX</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">dX</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="n">dX</span><span class="p">[</span><span class="mi">2</span><span class="p">]);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">r</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span>
<span class="n">ros</span> <span class="o">=</span> <span class="n">r</span><span class="o">/</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">sigma</span><span class="p">;</span>
<span class="c1">// Evaluate g_σ and ∂gσ∂r</span>
<span class="n">aux</span> <span class="o">=</span> <span class="n">pow</span><span class="p">(</span><span class="n">ros</span><span class="o">*</span><span class="n">ros</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">);</span>
<span class="n">g_sgm</span> <span class="o">=</span> <span class="n">ros</span><span class="o">*</span><span class="n">ros</span><span class="o">*</span><span class="n">ros</span> <span class="o">*</span> <span class="p">(</span><span class="n">ros</span><span class="o">*</span><span class="n">ros</span> <span class="o">+</span> <span class="mf">2.5</span><span class="p">)</span> <span class="o">/</span> <span class="n">aux</span><span class="p">;</span>
<span class="n">dgdr</span> <span class="o">=</span> <span class="mf">7.5</span> <span class="o">*</span> <span class="n">ros</span><span class="o">*</span><span class="n">ros</span> <span class="o">/</span> <span class="p">(</span> <span class="n">aux</span> <span class="o">*</span> <span class="p">(</span><span class="n">ros</span><span class="o">*</span><span class="n">ros</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">)</span> <span class="p">);</span>
<span class="c1">// u(x) = ∑g_σ(x−xp) K(x−xp) × Γp</span>
<span class="n">aux</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span> <span class="n">const4</span> <span class="o">/</span> <span class="p">(</span><span class="n">r</span><span class="o">*</span><span class="n">r</span><span class="o">*</span><span class="n">r</span><span class="p">))</span> <span class="o">*</span> <span class="n">g_sgm</span><span class="p">;</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">U</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span> <span class="n">dX</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="n">dX</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="p">)</span> <span class="o">*</span> <span class="n">aux</span><span class="p">;</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">U</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span> <span class="n">dX</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">dX</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="p">)</span> <span class="o">*</span> <span class="n">aux</span><span class="p">;</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">U</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span> <span class="n">dX</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">dX</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">)</span> <span class="o">*</span> <span class="n">aux</span><span class="p">;</span>
<span class="c1">// ∂u∂xj(x) = ∑[ ∂gσ∂xj(x−xp) * K(x−xp)×Γp + gσ(x−xp) * ∂K∂xj(x−xp)×Γp]</span>
<span class="n">aux</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span> <span class="n">const4</span> <span class="o">/</span> <span class="p">(</span><span class="n">r</span><span class="o">*</span><span class="n">r</span><span class="o">*</span><span class="n">r</span><span class="p">))</span> <span class="o">*</span> <span class="p">(</span><span class="n">dgdr</span><span class="o">/</span><span class="p">(</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">sigma</span><span class="o">*</span><span class="n">r</span><span class="p">)</span> <span class="o">-</span> <span class="mf">3.0</span><span class="o">*</span><span class="n">g_sgm</span><span class="o">/</span><span class="p">(</span><span class="n">r</span><span class="o">*</span><span class="n">r</span><span class="p">));</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">k</span><span class="o">&lt;</span><span class="mi">3</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">){</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="n">k</span> <span class="o">+</span> <span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span> <span class="n">dX</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="n">dX</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="p">)</span><span class="o">*</span><span class="n">aux</span><span class="o">*</span><span class="n">dX</span><span class="p">[</span><span class="n">k</span><span class="p">];</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span> <span class="n">dX</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">dX</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="p">)</span><span class="o">*</span><span class="n">aux</span><span class="o">*</span><span class="n">dX</span><span class="p">[</span><span class="n">k</span><span class="p">];</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="n">k</span> <span class="o">+</span> <span class="mi">2</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span> <span class="n">dX</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">dX</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">)</span><span class="o">*</span><span class="n">aux</span><span class="o">*</span><span class="n">dX</span><span class="p">[</span><span class="n">k</span><span class="p">];</span>
<span class="p">}</span>
<span class="c1">// Adds the Kronecker delta term</span>
<span class="n">aux</span> <span class="o">=</span> <span class="o">-</span> <span class="n">const4</span> <span class="o">*</span> <span class="n">g_sgm</span> <span class="o">/</span> <span class="p">(</span><span class="n">r</span><span class="o">*</span><span class="n">r</span><span class="o">*</span><span class="n">r</span><span class="p">);</span>
<span class="c1">// k=1</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="mi">0</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="mi">0</span> <span class="o">+</span> <span class="mi">2</span><span class="p">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
<span class="c1">// k=2</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span><span class="p">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
<span class="c1">// k=3</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">0</span><span class="p">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
<span class="n">P</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">J</span><span class="p">[</span><span class="mi">3</span><span class="o">*</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">P</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">Gamma</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p>The figure at the top of the page is a simulation of a 6x6x6 box of particles with vorticity
initially concentrated at its center, diffusing as the simulation progresses due
to viscous effects:
<!-- <center>
<img src="vortex00.gif" alt="Vid here" width="500px">
</center> --></p>
<h2 id="c-benchmark">C++ Benchmark</h2>
<p>Here I have <a href="https://github.com/EdoAlvarezR/post-juliavscpp">coded a benchmark</a>
of the C++ code shown above, evaluating <code class="language-plaintext highlighter-rouge">P2P</code> on a box of 6x6x6=216 particles,
and made it callable in this notebook through the
<a href="https://github.com/JuliaInterop/CxxWrap.jl">CxxWrap</a> Julia package:</p>
<p><strong>In [1]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="cm">#=
This cell loads auxiliary benchmarking functions available
here: https://github.com/EdoAlvarezR/post-juliavscpp
=#</span>
<span class="k">using</span> <span class="n">LinearAlgebra</span>
<span class="c"># Load C++ code wrapped as module CxxVortexTest</span>
<span class="n">include</span><span class="x">(</span><span class="s">"code/vortextest.jl"</span><span class="x">)</span>
<span class="c"># Load benchmarking tools</span>
<span class="n">include</span><span class="x">(</span><span class="s">"code/benchmarking.jl"</span><span class="x">);</span></code></pre></figure>
<p><strong>In [2]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="n">ntests</span> <span class="o">=</span> <span class="mi">1000</span> <span class="c"># Tests to run</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">6</span> <span class="c"># Particles per side</span>
<span class="n">lambda</span> <span class="o">=</span> <span class="mf">1.5</span> <span class="c"># Core overlap</span>
<span class="n">verbose</span> <span class="o">=</span> <span class="nb">true</span>
<span class="c"># Run C++ benchmark</span>
<span class="n">cpptime</span> <span class="o">=</span> <span class="n">CxxVortexTest</span><span class="o">.</span><span class="n">benchmarkP2P_wrap</span><span class="x">(</span><span class="n">ntests</span><span class="x">,</span> <span class="n">n</span><span class="x">,</span> <span class="kt">Float32</span><span class="x">(</span><span class="n">lambda</span><span class="x">),</span> <span class="n">verbose</span><span class="x">)</span>
<span class="c"># Store benchmark result</span>
<span class="n">benchtime</span><span class="x">[</span><span class="s">"C++"</span><span class="x">]</span> <span class="o">=</span> <span class="n">cpptime</span><span class="x">;</span></code></pre></figure>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Samples: 1000
min time: 3.99555 ms
ave time: 4.77778 ms
max time: 6.73414 ms
</code></pre></div></div>
<p>This was ran in my Dell Latitude 5580 laptop (Intel® Core™ i7-7820HQ CPU @
2.90GHz × 8 ) in only one process, and we see that <strong>the C++ kernel, best-case
scenario, is evaluated in ~4 ms</strong>. Let’s move on and code this up in Julia.</p>
<p><strong>NOTE:</strong> The C++ code was compiled with the <code class="language-plaintext highlighter-rouge">-O3</code> flag for code optimization.</p>
<h2 id="optimizing-julia">Optimizing Julia</h2>
<h3 id="julia-baseline-pythonic-programming">Julia Baseline: Pythonic Programming</h3>
<p>Tempted by the Python-like syntax available in Julia, our first inclination is
to make the code as general, simple, and easy to understand as possible. Here is
the most general implementation where no types are specified:</p>
<p><strong>In [3]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="s">"""
This is a particle struct made up of ambiguous (unspecified) types
"""</span>
<span class="k">struct</span><span class="nc"> ParticleAmbiguous</span>
<span class="c"># User inputs</span>
<span class="n">X</span> <span class="c"># Position</span>
<span class="n">Gamma</span> <span class="c"># Vectorial circulation</span>
<span class="n">sigma</span> <span class="c"># Smoothing radius</span>
<span class="c"># Properties</span>
<span class="n">U</span> <span class="c"># Velocity at particle</span>
<span class="n">J</span> <span class="c"># Jacobian at particle J[i,j]=dUi/dxj</span>
<span class="n">ParticleAmbiguous</span><span class="x">(</span><span class="n">X</span><span class="x">,</span> <span class="n">Gamma</span><span class="x">,</span> <span class="n">sigma</span><span class="x">;</span> <span class="n">U</span><span class="o">=</span><span class="n">zeros</span><span class="x">(</span><span class="mi">3</span><span class="x">),</span> <span class="n">J</span><span class="o">=</span><span class="n">zeros</span><span class="x">(</span><span class="mi">3</span><span class="x">,</span><span class="mi">3</span><span class="x">)</span>
<span class="x">)</span> <span class="o">=</span> <span class="n">new</span><span class="x">(</span><span class="n">X</span><span class="x">,</span> <span class="n">Gamma</span><span class="x">,</span> <span class="n">sigma</span><span class="x">,</span> <span class="n">U</span><span class="x">,</span> <span class="n">J</span> <span class="x">)</span>
<span class="k">end</span>
<span class="c"># Empty initializer</span>
<span class="n">Base</span><span class="o">.</span><span class="n">zero</span><span class="x">(</span><span class="o">::</span><span class="kt">Type</span><span class="x">{</span><span class="o">&lt;:</span><span class="n">ParticleAmbiguous</span><span class="x">})</span> <span class="o">=</span> <span class="n">ParticleAmbiguous</span><span class="x">(</span><span class="n">zeros</span><span class="x">(</span><span class="mi">3</span><span class="x">),</span> <span class="n">zeros</span><span class="x">(</span><span class="mi">3</span><span class="x">),</span> <span class="mf">0.0</span><span class="x">)</span>
<span class="c"># Winckelmans regularizing kernel</span>
<span class="n">g_wnk</span><span class="x">(</span><span class="n">r</span><span class="x">)</span> <span class="o">=</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span> <span class="o">*</span> <span class="x">(</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span> <span class="o">+</span> <span class="mf">2.5</span><span class="x">)</span> <span class="o">/</span> <span class="x">(</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="x">)</span><span class="o">^</span><span class="mf">2.5</span>
<span class="n">dgdr_wnk</span><span class="x">(</span><span class="n">r</span><span class="x">)</span> <span class="o">=</span> <span class="mf">7.5</span> <span class="o">*</span> <span class="n">r</span><span class="o">^</span><span class="mi">2</span> <span class="o">/</span> <span class="x">(</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="x">)</span><span class="o">^</span><span class="mf">3.5</span>
<span class="kd">const</span> <span class="n">const4</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="x">(</span><span class="mi">4</span><span class="o">*</span><span class="nb">pi</span><span class="x">)</span>
<span class="s">"""
Pythonic programming approach
"""</span>
<span class="k">function</span><span class="nf"> P2P_pythonic</span><span class="x">(</span><span class="n">particles</span><span class="x">,</span> <span class="n">g</span><span class="x">,</span> <span class="n">dgdr</span><span class="x">)</span>
<span class="k">for</span> <span class="n">Pi</span> <span class="k">in</span> <span class="n">particles</span>
<span class="k">for</span> <span class="n">Pj</span> <span class="k">in</span> <span class="n">particles</span>
<span class="n">dX</span> <span class="o">=</span> <span class="n">Pi</span><span class="o">.</span><span class="n">X</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">norm</span><span class="x">(</span><span class="n">dX</span><span class="x">)</span>
<span class="k">if</span> <span class="n">r</span> <span class="o">!=</span> <span class="mi">0</span>
<span class="c"># g_σ and ∂gσ∂r</span>
<span class="n">gsgm</span> <span class="o">=</span> <span class="n">g</span><span class="x">(</span><span class="n">r</span> <span class="o">/</span> <span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="x">)</span>
<span class="n">dgsgmdr</span> <span class="o">=</span> <span class="n">dgdr</span><span class="x">(</span><span class="n">r</span> <span class="o">/</span> <span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="x">)</span>
<span class="c"># K × Γp</span>
<span class="n">crss</span> <span class="o">=</span> <span class="n">cross</span><span class="x">(</span><span class="o">-</span><span class="n">const4</span> <span class="o">*</span> <span class="x">(</span><span class="n">dX</span><span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">3</span><span class="x">),</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">)</span>
<span class="c"># U = ∑g_σ(x-xp) * K(x-xp) × Γp</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">U</span><span class="x">[</span><span class="o">:</span><span class="x">]</span> <span class="o">+=</span> <span class="n">gsgm</span> <span class="o">*</span> <span class="n">crss</span>
<span class="c"># ∂u∂xj(x) = ∑[ ∂gσ∂xj(x−xp) * K(x−xp)×Γp + gσ(x−xp) * ∂K∂xj(x−xp)×Γp ]</span>
<span class="k">for</span> <span class="n">j</span> <span class="k">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="o">:</span><span class="x">,</span> <span class="n">j</span><span class="x">]</span> <span class="o">+=</span> <span class="x">(</span> <span class="n">dX</span><span class="x">[</span><span class="n">j</span><span class="x">]</span> <span class="o">/</span> <span class="x">(</span><span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="o">*</span><span class="n">r</span><span class="x">)</span> <span class="o">*</span> <span class="x">(</span><span class="n">dgsgmdr</span><span class="o">*</span><span class="n">crss</span><span class="x">)</span> <span class="o">-</span>
<span class="n">gsgm</span> <span class="o">*</span> <span class="x">(</span><span class="mi">3</span><span class="o">*</span><span class="n">dX</span><span class="x">[</span><span class="n">j</span><span class="x">]</span><span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span><span class="x">)</span> <span class="o">*</span> <span class="n">crss</span> <span class="o">-</span>
<span class="n">gsgm</span> <span class="o">*</span> <span class="x">(</span><span class="n">const4</span><span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">3</span><span class="x">)</span> <span class="o">*</span> <span class="n">cross</span><span class="x">([</span><span class="n">i</span><span class="o">==</span><span class="n">j</span> <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">3</span><span class="x">],</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">)</span> <span class="x">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<p><strong>In [4]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="c"># Create a 6x6x6 box of ambiguous particles</span>
<span class="n">particles_amb</span> <span class="o">=</span> <span class="n">generate_particles</span><span class="x">(</span><span class="n">ParticleAmbiguous</span><span class="x">,</span> <span class="n">n</span><span class="x">,</span> <span class="n">lambda</span><span class="x">)</span>
<span class="c"># Run benchmark</span>
<span class="n">args</span> <span class="o">=</span> <span class="x">(</span><span class="n">particles_amb</span><span class="x">,</span> <span class="n">g_wnk</span><span class="x">,</span> <span class="n">dgdr_wnk</span><span class="x">)</span>
<span class="n">compare</span><span class="x">(</span><span class="n">P2P_pythonic</span><span class="x">,</span> <span class="s">"C++"</span><span class="x">,</span> <span class="n">args</span><span class="x">;</span> <span class="n">reverse</span><span class="o">=</span><span class="nb">false</span><span class="x">)</span>
<span class="nd">@benchmark</span> <span class="n">P2P_pythonic</span><span class="x">(</span><span class="n">args</span><span class="o">...</span><span class="x">);</span></code></pre></figure>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>C++ is 58.48 times faster than P2P_pythonic (3.996ms vs 233.679ms)
BenchmarkTools.Trial:
memory estimate: 217.57 MiB
allocs estimate: 4087152
--------------
minimum time: 233.679 ms (5.04% GC)
median time: 238.482 ms (4.99% GC)
mean time: 251.540 ms (9.28% GC)
maximum time: 295.517 ms (19.89% GC)
--------------
samples: 4
evals/sample: 1
</code></pre></div></div>
<p>Here we see that in our pythonic attempt we’ve got code that is <strong>pretty neat
and concise, but ~58x slower than the C++ implementation</strong>.</p>
<h3 id="fix-1-always-work-with-concrete-types">Fix #1: Always work with concrete types</h3>
<p>The problem with the pythonic approach is that variable types are never
declared, and <strong>without foreknowledge of the types to be handled, Julia can’t
optimize the function during compilation</strong>. In order to help us catch ambiguous
(abstract) types in our code, the Julia <code class="language-plaintext highlighter-rouge">Base</code> package provides the macro
<code class="language-plaintext highlighter-rouge">@code_warntype</code>, which prints the lowered and type-inferred AST used during
compilation highlighting any abstract types encountered.</p>
<p>The output is pretty lengthy, so here I have copied and pasted only a snippet:</p>
<div class="language-julia highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@code_warntype</span> <span class="n">P2P_pythonic</span><span class="x">(</span><span class="n">args</span><span class="o">...</span><span class="x">)</span>
<span class="n">Body</span><span class="o">::</span><span class="kt">Nothing</span>
<span class="n">│╻╷╷</span> <span class="n">iterate34</span> <span class="mi">1</span> <span class="n">──</span> <span class="o">%</span><span class="mi">1</span> <span class="o">=</span> <span class="x">(</span><span class="n">Base</span><span class="o">.</span><span class="n">arraylen</span><span class="x">)(</span><span class="n">particles</span><span class="x">)</span><span class="o">::</span><span class="kt">Int64</span>
<span class="n">││╻╷</span> <span class="n">iterate</span> <span class="n">│</span> <span class="o">%</span><span class="mi">2</span> <span class="o">=</span> <span class="x">(</span><span class="n">Base</span><span class="o">.</span><span class="n">sle_int</span><span class="x">)(</span><span class="mi">0</span><span class="x">,</span> <span class="o">%</span><span class="mi">1</span><span class="x">)</span><span class="o">::</span><span class="kt">Bool</span>
<span class="n">│││╻</span> <span class="o">&lt;</span> <span class="n">│</span> <span class="o">%</span><span class="mi">3</span> <span class="o">=</span> <span class="x">(</span><span class="n">Base</span><span class="o">.</span><span class="n">bitcast</span><span class="x">)(</span><span class="kt">UInt64</span><span class="x">,</span> <span class="o">%</span><span class="mi">1</span><span class="x">)</span><span class="o">::</span><span class="kt">UInt64</span>
<span class="n">││││╻</span> <span class="o">&lt;</span> <span class="n">│</span> <span class="o">%</span><span class="mi">4</span> <span class="o">=</span> <span class="x">(</span><span class="n">Base</span><span class="o">.</span><span class="n">ult_int</span><span class="x">)(</span><span class="mh">0x0000000000000000</span><span class="x">,</span> <span class="o">%</span><span class="mi">3</span><span class="x">)</span><span class="o">::</span><span class="kt">Bool</span>
<span class="n">││││╻</span> <span class="o">&amp;</span> <span class="n">│</span> <span class="o">%</span><span class="mi">5</span> <span class="o">=</span> <span class="x">(</span><span class="n">Base</span><span class="o">.</span><span class="n">and_int</span><span class="x">)(</span><span class="o">%</span><span class="mi">2</span><span class="x">,</span> <span class="o">%</span><span class="mi">4</span><span class="x">)</span><span class="o">::</span><span class="kt">Bool</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">│</span> <span class="mi">11</span> <span class="n">┄</span> <span class="o">%</span><span class="mi">33</span> <span class="o">=</span> <span class="n">φ</span> <span class="x">(</span><span class="c">#10 =&gt; %28, #34 =&gt; %149)::ParticleAmbiguous</span>
<span class="n">│</span> <span class="n">│</span> <span class="o">%</span><span class="mi">34</span> <span class="o">=</span> <span class="n">φ</span> <span class="x">(</span><span class="c">#10 =&gt; %29, #34 =&gt; %150)::Int64</span>
<span class="n">│╻</span> <span class="n">getproperty37</span> <span class="n">│</span> <span class="o">%</span><span class="mi">35</span> <span class="o">=</span> <span class="x">(</span><span class="n">Base</span><span class="o">.</span><span class="n">getfield</span><span class="x">)(</span><span class="o">%</span><span class="mi">16</span><span class="x">,</span> <span class="o">:</span><span class="n">X</span><span class="x">)</span><span class="o">::</span><span class="kt">Any</span>
<span class="n">││</span> <span class="n">│</span> <span class="o">%</span><span class="mi">36</span> <span class="o">=</span> <span class="x">(</span><span class="n">Base</span><span class="o">.</span><span class="n">getfield</span><span class="x">)(</span><span class="o">%</span><span class="mi">33</span><span class="x">,</span> <span class="o">:</span><span class="n">X</span><span class="x">)</span><span class="o">::</span><span class="kt">Any</span>
<span class="n">│</span> <span class="n">│</span> <span class="o">%</span><span class="mi">37</span> <span class="o">=</span> <span class="x">(</span><span class="o">%</span><span class="mi">35</span> <span class="o">-</span> <span class="o">%</span><span class="mi">36</span><span class="x">)</span><span class="o">::</span><span class="kt">Any</span>
<span class="n">│</span> <span class="mi">38</span> <span class="n">│</span> <span class="o">%</span><span class="mi">38</span> <span class="o">=</span> <span class="x">(</span><span class="n">Main</span><span class="o">.</span><span class="n">norm</span><span class="x">)(</span><span class="o">%</span><span class="mi">37</span><span class="x">)</span><span class="o">::</span><span class="kt">Any</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
</code></pre></div></div>
<p>Understanding this lowered AST syntax is sort of an art, but you’ll soon learn
that <code class="language-plaintext highlighter-rouge">@code_warntype</code> is your best friend when optimizing code. As we scroll
down the AST we see that code encounters types <code class="language-plaintext highlighter-rouge">Any</code> in the properties of our
<code class="language-plaintext highlighter-rouge">ParticleAmbiguous</code> type, which immediately should raise a red flag to us (<code class="language-plaintext highlighter-rouge">Any</code>
is an abstract type). In fact, when running <code class="language-plaintext highlighter-rouge">@code_warntype</code> the output
automatically highlights those <code class="language-plaintext highlighter-rouge">::Any</code> asserts in red.</p>
<p>We can take care of those abstract types by defining the properties of the
<a href="https://docs.julialang.org/en/v1/manual/performance-
tips/index.html#Type-declarations-1">struct parametrically</a>:</p>
<p><strong>In [5]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="s">"""
This is a particle struct with property types
explicitely/parametrically defined.
"""</span>
<span class="k">struct</span><span class="nc"> Particle</span><span class="x">{</span><span class="n">T</span><span class="x">}</span>
<span class="c"># User inputs</span>
<span class="n">X</span><span class="o">::</span><span class="kt">Array</span><span class="x">{</span><span class="n">T</span><span class="x">,</span> <span class="mi">1</span><span class="x">}</span> <span class="c"># Position</span>
<span class="n">Gamma</span><span class="o">::</span><span class="kt">Array</span><span class="x">{</span><span class="n">T</span><span class="x">,</span> <span class="mi">1</span><span class="x">}</span> <span class="c"># Vectorial circulation</span>
<span class="n">sigma</span><span class="o">::</span><span class="n">T</span> <span class="c"># Smoothing radius</span>
<span class="c"># Properties</span>
<span class="n">U</span><span class="o">::</span><span class="kt">Array</span><span class="x">{</span><span class="n">T</span><span class="x">,</span> <span class="mi">1</span><span class="x">}</span> <span class="c"># Velocity at particle</span>
<span class="n">J</span><span class="o">::</span><span class="kt">Array</span><span class="x">{</span><span class="n">T</span><span class="x">,</span> <span class="mi">2</span><span class="x">}</span> <span class="c"># Jacobian at particle J[i,j]=dUi/dxj</span>
<span class="k">end</span>
<span class="c"># Another initializer alias</span>
<span class="n">Particle</span><span class="x">{</span><span class="n">T</span><span class="x">}(</span><span class="n">X</span><span class="x">,</span> <span class="n">Gamma</span><span class="x">,</span> <span class="n">sigma</span><span class="x">)</span> <span class="k">where</span> <span class="x">{</span><span class="n">T</span><span class="x">}</span> <span class="o">=</span> <span class="n">Particle</span><span class="x">(</span><span class="n">X</span><span class="x">,</span> <span class="n">Gamma</span><span class="x">,</span> <span class="n">sigma</span><span class="x">,</span> <span class="n">zeros</span><span class="x">(</span><span class="n">T</span><span class="x">,</span><span class="mi">3</span><span class="x">),</span> <span class="n">zeros</span><span class="x">(</span><span class="n">T</span><span class="x">,</span> <span class="mi">3</span><span class="x">,</span> <span class="mi">3</span><span class="x">))</span>
<span class="c"># Empty initializer</span>
<span class="n">Base</span><span class="o">.</span><span class="n">zero</span><span class="x">(</span><span class="o">::</span><span class="kt">Type</span><span class="x">{</span><span class="o">&lt;:</span><span class="n">Particle</span><span class="x">{</span><span class="n">T</span><span class="x">}})</span> <span class="k">where</span> <span class="x">{</span><span class="n">T</span><span class="x">}</span> <span class="o">=</span> <span class="n">Particle</span><span class="x">(</span><span class="n">zeros</span><span class="x">(</span><span class="n">T</span><span class="x">,</span> <span class="mi">3</span><span class="x">),</span> <span class="n">zeros</span><span class="x">(</span><span class="n">T</span><span class="x">,</span> <span class="mi">3</span><span class="x">),</span>
<span class="n">zero</span><span class="x">(</span><span class="n">T</span><span class="x">),</span>
<span class="n">zeros</span><span class="x">(</span><span class="n">T</span><span class="x">,</span> <span class="mi">3</span><span class="x">),</span> <span class="n">zeros</span><span class="x">(</span><span class="n">T</span><span class="x">,</span> <span class="mi">3</span><span class="x">,</span> <span class="mi">3</span><span class="x">))</span></code></pre></figure>
<p>No modifications further are needed in our <code class="language-plaintext highlighter-rouge">P2P_pythonic</code> function since Julia’s
multiple dispatch and JIT automatically compiles a version of the function
specialized for our new <code class="language-plaintext highlighter-rouge">Particle{T}</code> type on the fly. Still, we will define an
alias to help us compare benchmarks:</p>
<p><strong>In [6]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="n">P2P_concretetypes</span><span class="x">(</span><span class="n">args</span><span class="o">...</span><span class="x">)</span> <span class="o">=</span> <span class="n">P2P_pythonic</span><span class="x">(</span><span class="n">args</span><span class="o">...</span><span class="x">)</span></code></pre></figure>
<p><strong>In [7]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="c"># Create a 6x6x6 box of concrete Float64 particles</span>
<span class="n">particles</span> <span class="o">=</span> <span class="n">generate_particles</span><span class="x">(</span><span class="n">Particle</span><span class="x">{</span><span class="kt">Float64</span><span class="x">},</span> <span class="n">n</span><span class="x">,</span> <span class="n">lambda</span><span class="x">)</span>
<span class="c"># Run benchmark</span>
<span class="n">args</span> <span class="o">=</span> <span class="x">(</span><span class="n">particles</span><span class="x">,</span> <span class="n">g_wnk</span><span class="x">,</span> <span class="n">dgdr_wnk</span><span class="x">)</span>
<span class="n">compare</span><span class="x">(</span><span class="n">P2P_concretetypes</span><span class="x">,</span> <span class="n">P2P_pythonic</span><span class="x">,</span> <span class="n">args</span><span class="x">)</span>
<span class="nd">@benchmark</span> <span class="n">P2P_concretetypes</span><span class="x">(</span><span class="n">args</span><span class="o">...</span><span class="x">);</span></code></pre></figure>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>P2P_concretetypes is 3.06 times faster than P2P_pythonic (76.488ms vs 233.679ms)
BenchmarkTools.Trial:
memory estimate: 189.93 MiB
allocs estimate: 2275776
--------------
minimum time: 76.488 ms (13.63% GC)
median time: 77.860 ms (13.79% GC)
mean time: 84.567 ms (17.89% GC)
maximum time: 145.918 ms (42.64% GC)
--------------
samples: 12
evals/sample: 1
</code></pre></div></div>
<p>Voilà! By specifying concrete types in our <code class="language-plaintext highlighter-rouge">Particle</code> struct now we have gained
a 3x speed up (we should run <code class="language-plaintext highlighter-rouge">@code_warntype</code> again to make sure we got rid of
all abstract types, but I’ll omit it for brevity).</p>
<p>Let’s new see how we compare to C++:</p>
<p><strong>In [8]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="n">printcomparison</span><span class="x">(</span><span class="n">P2P_concretetypes</span><span class="x">,</span> <span class="s">"C++"</span><span class="x">,</span> <span class="nb">false</span><span class="x">)</span></code></pre></figure>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>C++ is 19.14 times faster than P2P_concretetypes (3.996ms vs 76.488ms)
</code></pre></div></div>
<p>Working with concrete types greatly sped up the computation; however, the C++
version is still ~20x faster than Julia. Let’s see what else can we optimize.</p>
<h3 id="fix-2-avoid-list-comprehensions">Fix #2: Avoid List Comprehensions</h3>
<p>The wonders of list comprehension may tempt you to write some line-efficient
code; however, loosely used may lead to a very inefficient
computation. Take for example this list-comprehension sum:</p>
<p><strong>In [9]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="n">sum_list</span><span class="x">(</span><span class="n">n</span><span class="x">)</span> <span class="o">=</span> <span class="n">sum</span><span class="x">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="mi">1</span><span class="o">:</span><span class="n">n</span><span class="x">])</span>
<span class="nd">@btime</span> <span class="n">sum_list</span><span class="x">(</span><span class="mi">100</span><span class="x">);</span></code></pre></figure>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> 80.975 ns (1 allocation: 896 bytes)
</code></pre></div></div>
<p>Here is the version of the same function unrolled without the list
comprehension, which does the job 60 times faster:</p>
<p><strong>In [10]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="k">function</span><span class="nf"> sum_unrolled</span><span class="x">(</span><span class="n">n</span><span class="x">)</span>
<span class="n">out</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="mi">1</span><span class="o">:</span><span class="n">n</span>
<span class="n">out</span> <span class="o">+=</span> <span class="n">i</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">out</span>
<span class="k">end</span>
<span class="nd">@btime</span> <span class="n">sum_unrolled</span><span class="x">(</span><span class="mi">100</span><span class="x">);</span></code></pre></figure>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> 1.374 ns (0 allocations: 0 bytes)
</code></pre></div></div>
<p>In our P2P function we have a Kronecker delta cross product that we were
calculating in just one line as a list comprehension as</p>
<div class="language-julia highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="c"># ∂u∂xj(x) = ∑[ ∂gσ∂xj(x−xp) * K(x−xp)×Γp + gσ(x−xp) * ∂K∂xj(x−xp)×Γp ]</span>
<span class="k">for</span> <span class="n">j</span> <span class="k">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="o">:</span><span class="x">,</span> <span class="n">j</span><span class="x">]</span> <span class="o">+=</span> <span class="x">(</span> <span class="n">dX</span><span class="x">[</span><span class="n">j</span><span class="x">]</span> <span class="o">/</span> <span class="x">(</span><span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="o">*</span><span class="n">r</span><span class="x">)</span> <span class="o">*</span> <span class="x">(</span><span class="n">dgsgmdr</span><span class="o">*</span><span class="n">crss</span><span class="x">)</span> <span class="o">-</span>
<span class="n">gsgm</span> <span class="o">*</span> <span class="x">(</span><span class="mi">3</span><span class="o">*</span><span class="n">dX</span><span class="x">[</span><span class="n">j</span><span class="x">]</span><span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span><span class="x">)</span> <span class="o">*</span> <span class="n">crss</span> <span class="o">-</span>
<span class="n">gsgm</span> <span class="o">*</span> <span class="x">(</span><span class="n">const4</span><span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">3</span><span class="x">)</span> <span class="o">*</span> <span class="n">cross</span><span class="x">([</span><span class="n">i</span><span class="o">==</span><span class="n">j</span> <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">3</span><span class="x">],</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">)</span> <span class="x">)</span>
<span class="k">end</span>
</code></pre></div></div>
<p>The alternative is to expand it into a few lines as</p>
<div class="language-julia highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="c"># ∂u∂xj(x) = ∑[ ∂gσ∂xj(x−xp) * K(x−xp)×Γp + gσ(x−xp) * ∂K∂xj(x−xp)×Γp ]</span>
<span class="k">for</span> <span class="n">j</span> <span class="k">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="o">:</span><span class="x">,</span> <span class="n">j</span><span class="x">]</span> <span class="o">+=</span> <span class="x">(</span> <span class="n">dX</span><span class="x">[</span><span class="n">j</span><span class="x">]</span> <span class="o">/</span> <span class="x">(</span><span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="o">*</span><span class="n">r</span><span class="x">)</span> <span class="o">*</span> <span class="x">(</span><span class="n">dgsgmdr</span><span class="o">*</span><span class="n">crss</span><span class="x">)</span> <span class="o">-</span>
<span class="n">gsgm</span> <span class="o">*</span> <span class="x">(</span><span class="mi">3</span><span class="o">*</span><span class="n">dX</span><span class="x">[</span><span class="n">j</span><span class="x">]</span><span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span><span class="x">)</span> <span class="o">*</span> <span class="n">crss</span> <span class="x">)</span>
<span class="k">end</span>
<span class="c"># ∂u∂xj(x) = −∑gσ/(4πr^3) δij×Γp</span>
<span class="c"># Adds the Kronecker delta term</span>
<span class="n">aux</span> <span class="o">=</span> <span class="o">-</span><span class="n">const4</span> <span class="o">*</span> <span class="n">gsgm</span> <span class="o">/</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span>
<span class="c"># j=1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="c"># j=2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="c"># j=3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
</code></pre></div></div>
<p>The problem with list comprehension operations is that it has to allocate memory
to build the generated array. Just resist the temptation of using list
comprehensions to save yourself a few lines, and simply unroll it. As seen below
we get a 1.5x speed up by unrolling this line:</p>
<p><strong>In [11]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="s">"""
Unrolling the list comprehension
"""</span>
<span class="k">function</span><span class="nf"> P2P_nocomprehension</span><span class="x">(</span><span class="n">particles</span><span class="x">,</span> <span class="n">g</span><span class="x">,</span> <span class="n">dgdr</span><span class="x">)</span>
<span class="k">for</span> <span class="n">Pi</span> <span class="k">in</span> <span class="n">particles</span>
<span class="k">for</span> <span class="n">Pj</span> <span class="k">in</span> <span class="n">particles</span>
<span class="n">dX</span> <span class="o">=</span> <span class="n">Pi</span><span class="o">.</span><span class="n">X</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">norm</span><span class="x">(</span><span class="n">dX</span><span class="x">)</span>
<span class="k">if</span> <span class="n">r</span> <span class="o">!=</span> <span class="mi">0</span>
<span class="c"># g_σ and ∂gσ∂r</span>
<span class="n">gsgm</span> <span class="o">=</span> <span class="n">g</span><span class="x">(</span><span class="n">r</span> <span class="o">/</span> <span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="x">)</span>
<span class="n">dgsgmdr</span> <span class="o">=</span> <span class="n">dgdr</span><span class="x">(</span><span class="n">r</span> <span class="o">/</span> <span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="x">)</span>
<span class="c"># K × Γp</span>
<span class="n">crss</span> <span class="o">=</span> <span class="n">cross</span><span class="x">(</span><span class="o">-</span><span class="n">const4</span> <span class="o">*</span> <span class="x">(</span><span class="n">dX</span><span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">3</span><span class="x">),</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">)</span>
<span class="c"># U = ∑g_σ(x-xp) * K(x-xp) × Γp</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">U</span><span class="x">[</span><span class="o">:</span><span class="x">]</span> <span class="o">+=</span> <span class="n">gsgm</span> <span class="o">*</span> <span class="n">crss</span>
<span class="c"># ∂u∂xj(x) = ∑[ ∂gσ∂xj(x−xp) * K(x−xp)×Γp + gσ(x−xp) * ∂K∂xj(x−xp)×Γp ]</span>
<span class="k">for</span> <span class="n">j</span> <span class="k">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="o">:</span><span class="x">,</span> <span class="n">j</span><span class="x">]</span> <span class="o">+=</span> <span class="x">(</span> <span class="n">dX</span><span class="x">[</span><span class="n">j</span><span class="x">]</span> <span class="o">/</span> <span class="x">(</span><span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="o">*</span><span class="n">r</span><span class="x">)</span> <span class="o">*</span> <span class="x">(</span><span class="n">dgsgmdr</span><span class="o">*</span><span class="n">crss</span><span class="x">)</span> <span class="o">-</span>
<span class="n">gsgm</span> <span class="o">*</span> <span class="x">(</span><span class="mi">3</span><span class="o">*</span><span class="n">dX</span><span class="x">[</span><span class="n">j</span><span class="x">]</span><span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span><span class="x">)</span> <span class="o">*</span> <span class="n">crss</span> <span class="x">)</span>
<span class="k">end</span>
<span class="c"># ∂u∂xj(x) += −∑gσ/(4πr^3) δij×Γp</span>
<span class="c"># Adds the Kronecker delta term</span>
<span class="n">aux</span> <span class="o">=</span> <span class="o">-</span><span class="n">const4</span> <span class="o">*</span> <span class="n">gsgm</span> <span class="o">/</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span>
<span class="c"># j=1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="c"># j=2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="c"># j=3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<p><strong>In [12]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="n">args</span> <span class="o">=</span> <span class="x">(</span><span class="n">particles</span><span class="x">,</span> <span class="n">g_wnk</span><span class="x">,</span> <span class="n">dgdr_wnk</span><span class="x">)</span>
<span class="n">compare</span><span class="x">(</span><span class="n">P2P_nocomprehension</span><span class="x">,</span> <span class="n">P2P_concretetypes</span><span class="x">,</span> <span class="n">args</span><span class="x">)</span>
<span class="nd">@benchmark</span> <span class="n">P2P_nocomprehension</span><span class="x">(</span><span class="n">args</span><span class="o">...</span><span class="x">);</span></code></pre></figure>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>P2P_nocomprehension is 1.52 times faster than P2P_concretetypes (50.196ms vs 76.488ms)
BenchmarkTools.Trial:
memory estimate: 126.16 MiB
allocs estimate: 1300536
--------------
minimum time: 50.196 ms (11.28% GC)
median time: 51.929 ms (11.65% GC)
mean time: 55.319 ms (15.41% GC)
maximum time: 107.039 ms (50.23% GC)
--------------
samples: 19
evals/sample: 1
</code></pre></div></div>
<h3 id="fix-3-reduce-allocation">Fix #3: Reduce Allocation</h3>
<p>Next, we notice that the benchmarking test is allotting an unusual amount of
memory (126MiB) and allocation operations (1.3M). I am suspicious that this is
an issue with Julia allowing arrays of dynamic sizes. The first step to solve
this is to <strong>do away with creating any internal variables of type arrays</strong>. In
the code bellow, notice that I had replaced the array variables <code class="language-plaintext highlighter-rouge">dX</code> and <code class="language-plaintext highlighter-rouge">crss</code>
with float variables <code class="language-plaintext highlighter-rouge">dX1, dX2, dX3</code>, and <code class="language-plaintext highlighter-rouge">crss1, crss2, crss3</code>, which leads to
having to fully unroll the inner for-loop:</p>
<p><strong>In [13]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="s">"""
Reducing memory allocation
"""</span>
<span class="k">function</span><span class="nf"> P2P_noallocation</span><span class="x">(</span><span class="n">particles</span><span class="x">,</span> <span class="n">g</span><span class="x">,</span> <span class="n">dgdr</span><span class="x">)</span>
<span class="k">for</span> <span class="n">Pi</span> <span class="k">in</span> <span class="n">particles</span>
<span class="k">for</span> <span class="n">Pj</span> <span class="k">in</span> <span class="n">particles</span>
<span class="n">dX1</span> <span class="o">=</span> <span class="n">Pi</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="n">dX2</span> <span class="o">=</span> <span class="n">Pi</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="n">dX3</span> <span class="o">=</span> <span class="n">Pi</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">norm</span><span class="x">(</span><span class="n">Pi</span><span class="o">.</span><span class="n">X</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span><span class="x">)</span>
<span class="k">if</span> <span class="n">r</span> <span class="o">!=</span> <span class="mi">0</span>
<span class="c"># g_σ and ∂gσ∂r</span>
<span class="n">gsgm</span> <span class="o">=</span> <span class="n">g</span><span class="x">(</span><span class="n">r</span> <span class="o">/</span> <span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="x">)</span>
<span class="n">dgsgmdr</span> <span class="o">=</span> <span class="n">dgdr</span><span class="x">(</span><span class="n">r</span> <span class="o">/</span> <span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="x">)</span>
<span class="c"># K × Γp</span>
<span class="n">crss1</span><span class="x">,</span> <span class="n">crss2</span><span class="x">,</span> <span class="n">crss3</span> <span class="o">=</span> <span class="o">-</span><span class="n">const4</span> <span class="o">/</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span> <span class="o">*</span> <span class="n">cross</span><span class="x">(</span><span class="n">Pi</span><span class="o">.</span><span class="n">X</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span><span class="x">,</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">)</span>
<span class="c"># U = ∑g_σ(x-xp) * K(x-xp) × Γp</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">U</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">gsgm</span> <span class="o">*</span> <span class="n">crss1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">U</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">gsgm</span> <span class="o">*</span> <span class="n">crss2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">U</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">gsgm</span> <span class="o">*</span> <span class="n">crss3</span>
<span class="c"># ∂u∂xj(x) = ∑[ ∂gσ∂xj(x−xp) * K(x−xp)×Γp + gσ(x−xp) * ∂K∂xj(x−xp)×Γp ]</span>
<span class="c"># ∂u∂xj(x) += ∑p[(Δxj∂gσ∂r/(σr) − 3Δxjgσ/r^2) K(Δx)×Γp</span>
<span class="n">aux</span> <span class="o">=</span> <span class="n">dgsgmdr</span><span class="o">/</span><span class="x">(</span><span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="o">*</span><span class="n">r</span><span class="x">)</span><span class="o">*</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">gsgm</span> <span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span>
<span class="c"># j=1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss1</span> <span class="o">*</span> <span class="n">dX1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss2</span> <span class="o">*</span> <span class="n">dX1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss3</span> <span class="o">*</span> <span class="n">dX1</span>
<span class="c"># j=2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss1</span> <span class="o">*</span> <span class="n">dX2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss2</span> <span class="o">*</span> <span class="n">dX2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss3</span> <span class="o">*</span> <span class="n">dX2</span>
<span class="c"># j=3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss1</span> <span class="o">*</span> <span class="n">dX3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss2</span> <span class="o">*</span> <span class="n">dX3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss3</span> <span class="o">*</span> <span class="n">dX3</span>
<span class="c"># Adds the Kronecker delta term</span>
<span class="n">aux</span> <span class="o">=</span> <span class="o">-</span><span class="n">const4</span> <span class="o">*</span> <span class="n">gsgm</span> <span class="o">/</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span>
<span class="c"># j=1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="c"># j=2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="c"># j=3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<p><strong>In [14]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="n">args</span> <span class="o">=</span> <span class="x">(</span><span class="n">particles</span><span class="x">,</span> <span class="n">g_wnk</span><span class="x">,</span> <span class="n">dgdr_wnk</span><span class="x">)</span>
<span class="n">compare</span><span class="x">(</span><span class="n">P2P_noallocation</span><span class="x">,</span> <span class="n">P2P_nocomprehension</span><span class="x">,</span> <span class="n">args</span><span class="x">)</span>
<span class="nd">@benchmark</span> <span class="n">P2P_noallocation</span><span class="x">(</span><span class="n">args</span><span class="o">...</span><span class="x">);</span></code></pre></figure>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>P2P_noallocation is 3.68 times faster than P2P_nocomprehension (13.627ms vs 50.196ms)
BenchmarkTools.Trial:
memory estimate: 21.99 MiB
allocs estimate: 325296
--------------
minimum time: 13.627 ms (7.41% GC)
median time: 15.615 ms (8.40% GC)
mean time: 16.582 ms (12.83% GC)
maximum time: 59.011 ms (66.91% GC)
--------------
samples: 61
evals/sample: 1
</code></pre></div></div>
<p>Here we have reduced the memory allocated from 126MiB to 22MiB, leading to a
3.5x speed up. Let’s see what else can we do to decrease memory allocation.</p>
<h3 id="fix-4-no-linear-algebra">Fix #4: No Linear Algebra</h3>
<p>The next thing to consider is that doing <strong>linear algebra operations
using the Julia base library (i.e., <code class="language-plaintext highlighter-rouge">dot(X,X)</code>,
<code class="language-plaintext highlighter-rouge">cross(X,X)</code>, <code class="language-plaintext highlighter-rouge">norm(X,X)</code>) is more expensive that explicitely unfolding the
operation into lines of code</strong>. I am suspicious that this is a memory allocation
problem since these functions need to allocate internal arrays to store the
computation prior to outputting the result. Here is the code without any
functional linear algebra functions from the base library (notice that I no longer use <code class="language-plaintext highlighter-rouge">norm()</code> nor
<code class="language-plaintext highlighter-rouge">cross()</code>):</p>
<p><strong>In [15]:</strong></p>
<figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="s">"""
No linear algebra functions
"""</span>
<span class="k">function</span><span class="nf"> P2P_nolinalg</span><span class="x">(</span><span class="n">particles</span><span class="x">,</span> <span class="n">g</span><span class="x">,</span> <span class="n">dgdr</span><span class="x">)</span>
<span class="k">for</span> <span class="n">Pi</span> <span class="k">in</span> <span class="n">particles</span>
<span class="k">for</span> <span class="n">Pj</span> <span class="k">in</span> <span class="n">particles</span>
<span class="n">dX1</span> <span class="o">=</span> <span class="n">Pi</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="n">dX2</span> <span class="o">=</span> <span class="n">Pi</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="n">dX3</span> <span class="o">=</span> <span class="n">Pi</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span> <span class="o">-</span> <span class="n">Pj</span><span class="o">.</span><span class="n">X</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">sqrt</span><span class="x">(</span><span class="n">dX1</span><span class="o">*</span><span class="n">dX1</span> <span class="o">+</span> <span class="n">dX2</span><span class="o">*</span><span class="n">dX2</span> <span class="o">+</span> <span class="n">dX3</span><span class="o">*</span><span class="n">dX3</span><span class="x">)</span>
<span class="k">if</span> <span class="n">r</span> <span class="o">!=</span> <span class="mi">0</span>
<span class="c"># g_σ and ∂gσ∂r</span>
<span class="n">gsgm</span> <span class="o">=</span> <span class="n">g</span><span class="x">(</span><span class="n">r</span> <span class="o">/</span> <span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="x">)</span>
<span class="n">dgsgmdr</span> <span class="o">=</span> <span class="n">dgdr</span><span class="x">(</span><span class="n">r</span> <span class="o">/</span> <span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="x">)</span>
<span class="c"># K × Γp</span>
<span class="n">crss1</span> <span class="o">=</span> <span class="o">-</span><span class="n">const4</span> <span class="o">/</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span> <span class="o">*</span> <span class="x">(</span> <span class="n">dX2</span><span class="o">*</span><span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span> <span class="o">-</span> <span class="n">dX3</span><span class="o">*</span><span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span> <span class="x">)</span>
<span class="n">crss2</span> <span class="o">=</span> <span class="o">-</span><span class="n">const4</span> <span class="o">/</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span> <span class="o">*</span> <span class="x">(</span> <span class="n">dX3</span><span class="o">*</span><span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span> <span class="o">-</span> <span class="n">dX1</span><span class="o">*</span><span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span> <span class="x">)</span>
<span class="n">crss3</span> <span class="o">=</span> <span class="o">-</span><span class="n">const4</span> <span class="o">/</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span> <span class="o">*</span> <span class="x">(</span> <span class="n">dX1</span><span class="o">*</span><span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span> <span class="o">-</span> <span class="n">dX2</span><span class="o">*</span><span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span> <span class="x">)</span>
<span class="c"># U = ∑g_σ(x-xp) * K(x-xp) × Γp</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">U</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">gsgm</span> <span class="o">*</span> <span class="n">crss1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">U</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">gsgm</span> <span class="o">*</span> <span class="n">crss2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">U</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">gsgm</span> <span class="o">*</span> <span class="n">crss3</span>
<span class="c"># ∂u∂xj(x) = ∑[ ∂gσ∂xj(x−xp) * K(x−xp)×Γp + gσ(x−xp) * ∂K∂xj(x−xp)×Γp ]</span>
<span class="c"># ∂u∂xj(x) += ∑p[(Δxj∂gσ∂r/(σr) − 3Δxjgσ/r^2) K(Δx)×Γp</span>
<span class="n">aux</span> <span class="o">=</span> <span class="n">dgsgmdr</span><span class="o">/</span><span class="x">(</span><span class="n">Pj</span><span class="o">.</span><span class="n">sigma</span><span class="o">*</span><span class="n">r</span><span class="x">)</span><span class="o">*</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">gsgm</span> <span class="o">/</span><span class="n">r</span><span class="o">^</span><span class="mi">2</span>
<span class="c"># j=1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss1</span> <span class="o">*</span> <span class="n">dX1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss2</span> <span class="o">*</span> <span class="n">dX1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss3</span> <span class="o">*</span> <span class="n">dX1</span>
<span class="c"># j=2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss1</span> <span class="o">*</span> <span class="n">dX2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss2</span> <span class="o">*</span> <span class="n">dX2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss3</span> <span class="o">*</span> <span class="n">dX2</span>
<span class="c"># j=3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss1</span> <span class="o">*</span> <span class="n">dX3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss2</span> <span class="o">*</span> <span class="n">dX3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">crss3</span> <span class="o">*</span> <span class="n">dX3</span>
<span class="c"># Adds the Kronecker delta term</span>
<span class="n">aux</span> <span class="o">=</span> <span class="o">-</span><span class="n">const4</span> <span class="o">*</span> <span class="n">gsgm</span> <span class="o">/</span> <span class="n">r</span><span class="o">^</span><span class="mi">3</span>
<span class="c"># j=1</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="c"># j=2</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">3</span><span class="x">,</span> <span class="mi">2</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="c"># j=3</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">1</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">-=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span>
<span class="n">Pi</span><span class="o">.</span><span class="n">J</span><span class="x">[</span><span class="mi">2</span><span class="x">,</span> <span class="mi">3</span><span class="x">]</span> <span class="o">+=</span> <span class="n">aux</span> <span class="o">*</span> <span class="n">Pj</span><span class="o">.</span><span class="n">Gamma</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<p><strong>In [16]:</strong></p>