Schematic: How to create a loop that saves data to files with different names?

I am using a program (TracePro) which uses Scheme which I have not used that much. I want to create code that changes some initial values, starts the simulation, and then saves the resulting data table to a file, then changes the values โ€‹โ€‹again, starts the simulation, saves the data, etc. 90 times. The code I've created so far:

(raytrace:set-beam-orientation-euler-degrees (gvector 0 90 -90))
(raytrace:grid)
(edit:select (car (cdr (entity:faces (entity 12)))))
(analysis:incident)
(analysis:incident-save "C:/Users/Admin/Desktop/testdata/incident0.csv" "csv")

      

Is there a way to create a loop that will run this piece of code with a different angle of incidence, from 0 to 90, and a different file name, from incident0.csv all the way to incident90.csv, without having to copy the code 90 times and change it manually ... ? I have an idea how to handle the changing angle of incidence, but don't know how to change the filename.

Thanks in advance.

+3


source to share


1 answer


I don't know the specifics of TracePro, but in any Scheme program you can use recursion in a loop. First, we have to refactor the details in the code that will change and make them parameters, encapsulating the code into a function. I'm not sure what value the incidence angle should be, adjust if necessary:

(define (run-simulation angle)
  (raytrace:set-beam-orientation-euler-degrees
   (gvector angle 90 -90)) ; assuming that the first parameter is the angle
  (raytrace:grid)
  (edit:select
   (car (cdr (entity:faces (entity 12)))))
  (analysis:incident)
  (analysis:incident-save
   (string-append          ; this is how we can dynamically create file names
    "C:/Users/Admin/Desktop/testdata/incident"
    (number->string angle)
    ".csv")
   "csv"))

      

With the above procedure, it is easy to loop over it, calling it as many times as necessary. Notice how we use the base recursion register to stop, and how we increment the current value in the recursive step:

(define (loop init end)
  (cond ((> init end) 'done)      ; base case of recursion
        (else                     ; otherwise
         (run-simulation init)    ; call the previous procedure
         (loop (+ init 1) end)))) ; advance recursion

      

We now need to provide the appropriate start and stop parameters during the call to the loop:



(loop 0 90)

      

If we are smart about this, we can use the same looping procedure for other purposes, note that the only thing that can change is the procedure called, so we can pass it as a parameter too!

(define (loop func init end)
  (cond ((> init end) 'done)
        (else
         (func init)
         (loop func (+ init 1) end))))

(loop run-simulation 0 90)

      

There are other tricks you can apply to make the program more flexible, but for now, the code above should put you on the right track. Happy scheme!

+4


source







All Articles