MEEP: How to make an array of flux detectors?

482 views Asked by At

I have been using MIT's MEEP for simulation of THz frequency light transmission in silicon photonics. I needed to make an array of flux detectors in MIT's MEEP so that I wouldn't have to write many (add-flux) blocks of code.

Scheme's map seemed like a good solution, yet, despite there being many people in many forums searching for a way to do this, an implementation of such code is sparse online. Thus, I wanted to share a way of doing it.

In the documentation on the MEEP wiki, adding flux detectors is done in the follow fashion:

(define-param fcen 0.25) ; pulse center frequency
(define-param df 0.4)    ; pulse width (in frequency)
(define-param nfreq 4001) ; number of frequencies at which to compute flux

(define refl-det       ; reflection detector
    (add-flux freq_c pulse_width num_freqs
      (make flux-region
          (center some-x some-y)
          (size 1 0))))

(define trans-det       ; transmission detector
    (add-flux freq_c pulse_width num_freqs
      (make flux-region
          (center some-other-x some-other-y)
          (size 1 0))))

;;...;; code for running sources

(display-fluxes refl-det trans-det)    ; MEEP's function for outputting flux for frequencies

So, if I want 20 transmission detectors and 20 reflection detectors, I would have to define 40 blocks by hard coding them...not good.

1

There are 1 answers

0
ansebbian0 On

There are many variants which can be made on this code. The one presented below is for detectors in a straight line. It is also possible to implement one for detectors placed in a circular arrangement; however, that requires calculating your angles in another function and adding another variable to the detectors function.

; The following is a sample algorithm you can use to get the x values
(define det-sample-length 5)
(define det-start-x-position 25)
(define (refl-det-x-position n)  (+ det-start-x-position (* n det-sample-length)))

; Here you use map to return a list of the x positions for the detectors
(define det-positions-x (map refl-det-x-position (list 1 2 3 4 5 6 7 8 9))) 

; This is a function to make the array of detectors. It takes the x position as an argument.
(define (detectors det-position-x)
    (add-flux freq_c pulse_width num_freqs
      (make flux-region
          (center det-position-x 0)
          (size 0 1))))

; To return a list of detectors based on the above function, map it against the x position list.
(define my-refl-flux-list
    (map detectors det-positions-x))

; If you need to put another detector not part of the above series, define it as a "list"    
(define source-top-det
    (list (add-flux freq_c pulse_width num_freqs
      (make flux-region
          (center some-x some-y)
          (size 1 0)))))

; Here, again, you can make another detector as a list or another array of detectors.   
(define source-bottom-det
    (list (add-flux freq_c pulse_width num_freqs
      (make flux-region
          (center some-other-x some-other-y)
          (size 1 0)))))

; Finally, before you use "display-fluxes", you must append the detectors into a list.    
(define my-flux-list (append source-top-det source-bottom-det my-refl-flux-list))

; And last, but not least, use Scheme's "apply" on "display-fluxes" over "my-flux-list"
(apply display-fluxes my-flux-list) 

The most important thing to remember is that the detectors must be contained in a list. Map by its nature makes a list, so this is why you don't define a list in the "detectors" function. Append just joins all of the lists into a bigger list. And you have to use "apply" for "display-fluxes" because you are using it on a list, not directly within the function. Hope this helps!