0

我一直在使用麻省理工学院的 MEEP 来模拟硅光子学中的太赫兹频率光传输。我需要在 MIT 的 MEEP 中制作一组通量检测器,这样我就不必编写许多(添加通量)代码块。

Scheme 的地图似乎是一个很好的解决方案,然而,尽管许多论坛中有很多人在寻找一种方法来做到这一点,但这种代码的实现在网上很少见。因此,我想分享一种方法。

MEEP wiki上的文档中,添加通量检测器的方式如下:

(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

所以,如果我想要 20 个透射检测器和 20 个反射检测器,我将不得不通过硬编码来定义 40 个块……不好。

4

1 回答 1

0

可以在此代码上进行许多变体。下面介绍的是直线探测器。也可以为圆形排列的探测器实现一个;但是,这需要在另一个函数中计算您的角度并将另一个变量添加到检测器函数中。

; 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) 

要记住的最重要的事情是检测器必须包含在列表中。地图本质上是一个列表,所以这就是为什么你不在“检测器”函数中定义一个列表的原因。Append 只是将所有列表加入到一个更大的列表中。并且您必须对“display-fluxes”使用“apply”,因为您在列表中使用它,而不是直接在函数中使用它。希望这可以帮助!

于 2016-02-23T15:40:15.280 回答