Below is a lattice point generation code I write
Clear[lattice];
lattice[basisvec_List, numofcell_List, base_List] :=
Flatten[Transpose[# + Transpose[base]] & /@ (Tuples[Range /@ numofcell].basisvec), 1]
to use it for example,
a = 1.;
list = lattice[{{3 a, 0}, {0, Sqrt[3] a}}, {5,
5}, {{0, 0}, {a, 0}, {-a/2, Sqrt[3] a/2}, {3 a/2,
Sqrt[3] a/2}}];
generate a honeycomb lattice
To generate 4000000 points,
lattice[{{3 a, 0}, {0, Sqrt[3] a}}, {1000,
4000}, {{0, 0}, {a, 0}, {-a/2, Sqrt[3] a/2}, {3 a/2,
Sqrt[3] a/2}}]; // AbsoluteTiming
it takes 4.52389 sec
This is the fastest code I could write at the moment. I also tried compile
as follows
latticecom =
Compile[{{basisvec, _Real, 2}, {numofcell, _Integer,
1}, {base, _Real, 2}}, Clear[lattice];
Flatten[
Transpose[# + Transpose[base]] & /@ (Tuples[
Range /@ numofcell].basisvec), 1], RuntimeOptions -> "Speed",
CompilationTarget -> "C"];
But it will gives errors when using it
CompiledFunction::cflist: Nontensor object generated; proceeding with uncompiled evaluation. >>
Can someone explain this?
I also write a fortran version to test how efficiency can differ between fortran and mathematica. It turns out that fortran done the same thing almost instantly.
So I want to see how this code could be great improved to be comparable to fortran.
Tuples[]
is not compilable, no? $\endgroup$Tuples
is not in a loop, so I think it is harmless $\endgroup$lattice[basisvec_?MatrixQ, numofcell_?VectorQ, base_?MatrixQ] := Flatten[Through[(TranslationTransform /@ (Tuples[Range /@ numofcell].basisvec))[base]], 1]
. $\endgroup$lattice[basisvec_List, numofcell_List, base_List] := Flatten[Outer[Plus, Tuples[Range /@ numofcell].basisvec, base, 1], 1]
. $\endgroup$