1{
2 lib,
3 buildPythonPackage,
4 fetchFromGitHub,
5 pythonOlder,
6 setuptools,
7 autoreconfHook,
8 pkg-config,
9 mpiCheckPhaseHook,
10 gfortran,
11 mpi,
12 blas,
13 lapack,
14 fftw,
15 hdf5-mpi,
16 swig,
17 gsl,
18 harminv,
19 libctl,
20 libGDSII,
21 openssh,
22 guile,
23 python,
24 numpy,
25 scipy,
26 matplotlib,
27 h5py-mpi,
28 cython,
29 autograd,
30 mpi4py,
31}:
32
33assert !blas.isILP64;
34assert !lapack.isILP64;
35
36buildPythonPackage rec {
37 pname = "meep";
38 version = "1.29.0";
39
40 src = fetchFromGitHub {
41 owner = "NanoComp";
42 repo = pname;
43 rev = "refs/tags/v${version}";
44 hash = "sha256-TB85obdk8pSWRaz3+3I6P6+dQtCHosWHRnKGck/wG9Q=";
45 };
46
47 format = "other";
48
49 # https://github.com/NanoComp/meep/issues/2819
50 postPatch = lib.optionalString (!pythonOlder "3.12") ''
51 substituteInPlace configure.ac doc/docs/setup.py python/visualization.py \
52 --replace-fail "distutils" "setuptools._distutils"
53 '';
54
55 # MPI is needed in nativeBuildInputs too, otherwise MPI libs will be missing
56 # at runtime
57 nativeBuildInputs = [
58 autoreconfHook
59 gfortran
60 pkg-config
61 swig
62 mpi
63 ];
64
65 buildInputs = [
66 gsl
67 blas
68 lapack
69 fftw
70 hdf5-mpi
71 harminv
72 libctl
73 libGDSII
74 guile
75 gsl
76 ];
77
78 propagatedBuildInputs =
79 [
80 mpi
81 numpy
82 scipy
83 matplotlib
84 h5py-mpi
85 cython
86 autograd
87 mpi4py
88 ]
89 ++ lib.optionals (!pythonOlder "3.12") [
90 setuptools # used in python/visualization.py
91 ];
92
93 propagatedUserEnvPkgs = [ mpi ];
94
95 dontUseSetuptoolsBuild = true;
96 dontUsePipInstall = true;
97 dontUseSetuptoolsCheck = true;
98
99 enableParallelBuilding = true;
100
101 preConfigure = ''
102 export HDF5_MPI=ON
103 export PYTHON=${python}/bin/${python.executable};
104 '';
105
106 configureFlags = [
107 "--without-libctl"
108 "--enable-shared"
109 "--with-mpi"
110 "--with-openmp"
111 "--enable-maintainer-mode"
112 ];
113
114 passthru = {
115 inherit mpi;
116 };
117
118 /*
119 This test is taken from the MEEP tutorial "Fields in a Waveguide" at
120 <https://meep.readthedocs.io/en/latest/Python_Tutorials/Basics/>.
121 It is important, that the test actually performs a calculation
122 (calls `sim.run()`), as only then MPI will be initialised and MPI linking
123 errors can be caught.
124 */
125 doCheck = true;
126 nativeCheckInputs = [
127 mpiCheckPhaseHook
128 openssh
129 ];
130 checkPhase = ''
131 runHook preCheck
132
133 export PYTHONPATH="$out/${python.sitePackages}:$PYTHONPATH"
134
135 # Generate a python test script
136 cat > test.py << EOF
137 import meep as mp
138 cell = mp.Vector3(16,8,0)
139 geometry = [mp.Block(mp.Vector3(mp.inf,1,mp.inf),
140 center=mp.Vector3(),
141 material=mp.Medium(epsilon=12))]
142 sources = [mp.Source(mp.ContinuousSource(frequency=0.15),
143 component=mp.Ez,
144 center=mp.Vector3(-7,0))]
145 pml_layers = [mp.PML(1.0)]
146 resolution = 10
147 sim = mp.Simulation(cell_size=cell,
148 boundary_layers=pml_layers,
149 geometry=geometry,
150 sources=sources,
151 resolution=resolution)
152 sim.run(until=200)
153 EOF
154
155 ${mpi}/bin/mpiexec -np 2 python3 test.py
156
157 runHook postCheck
158 '';
159
160 meta = with lib; {
161 description = "Free finite-difference time-domain (FDTD) software for electromagnetic simulations";
162 homepage = "https://meep.readthedocs.io/en/latest/";
163 license = licenses.gpl2Only;
164 platforms = platforms.linux;
165 maintainers = with maintainers; [
166 sheepforce
167 markuskowa
168 ];
169 };
170}