|
| 1 | +# Processes |
| 2 | + |
| 3 | +Processes in ngcsimlib are a way of defining a specific transition across a |
| 4 | +given model. They take in as many compilable method's across any number of |
| 5 | +components and produce a single top level method and a varying number of |
| 6 | +submethods needed to execute the entire chain of compilable methods in one step. |
| 7 | +This is done to interface nicely with just in time compilers and minimize the |
| 8 | +amount of read and write calls done across the chain of methods. |
| 9 | + |
| 10 | +## Building the chain |
| 11 | + |
| 12 | +Building the chain that a process will use is done in an iterative process. Once |
| 13 | +the process object is created steps are added using either `.then()` or `>>`. |
| 14 | +As an example |
| 15 | + |
| 16 | +``` |
| 17 | +myProcess.then(myCompA.forward).then(myCompB.forward).then(myCompA.evolve).then(myCompB.evolve) |
| 18 | +``` |
| 19 | + |
| 20 | +or |
| 21 | + |
| 22 | +``` |
| 23 | +myProcess >> myCompA.forward >> myCompB.forward >> myCompA.evolve >> myCompB.evolve |
| 24 | +``` |
| 25 | + |
| 26 | +In both cases this process will chain the four methods together into a single |
| 27 | +step, only updating the final state all steps are complete. |
| 28 | + |
| 29 | +## Types of processes |
| 30 | + |
| 31 | +There are two types of processes, the above example would be with what is |
| 32 | +referred to as a `MethodProcess`, these are used to chain together any |
| 33 | +compilable |
| 34 | +methods from any number of different components. The second type of process, |
| 35 | +called a `JointProcess`, is used to chain together entire processes. |
| 36 | +JointProcesses are especially useful if there are multiple method processes that |
| 37 | +need to be called but different orders of the processes are needed at different |
| 38 | +times. |
| 39 | + |
| 40 | +## Extras |
| 41 | + |
| 42 | +There are a few extra methods that come standard with each process type that can |
| 43 | +be useful for both regular operation and debugging. |
| 44 | + |
| 45 | +### Viewing the compiled method |
| 46 | + |
| 47 | +Behind the scenes a process is transforming and compiling down all the steps |
| 48 | +used to build it, these means that the exact code it is running to do its |
| 49 | +calculation is not what the user wrote. To allow for the end user to view and |
| 50 | +make sure that the two pieces of code; theirs and the compiled version, are |
| 51 | +equivalent every process has a `view_compiled_method()` call that can be used |
| 52 | +after the model is compiled. This call will return the code that it is running |
| 53 | +as a string. There will be some stark differences between the produced code and |
| 54 | +the code in the components used to build the steps. Please refer to the |
| 55 | +compiling page for a more indepth guide to comparing the outputs. |
| 56 | + |
| 57 | +### Needed Keywords |
| 58 | + |
| 59 | +As some methods will require external values such as `t` or `dt` for a given |
| 60 | +execution a process will also track all the keyword arguments that are needed to |
| 61 | +run their compiled process. To view which keywords a given process is expected |
| 62 | +calling `get_keywords()`. This is mostly used for debugging or a sanity check. |
| 63 | + |
| 64 | +### Packing keywords |
| 65 | + |
| 66 | +To add onto the needed keywords the process also provides an interface to |
| 67 | +produce the keywords needed to run in the form of two methods. The first method |
| 68 | +is `pack_keywords(...)`, this method packs together a single row of values |
| 69 | +needed to run a single execution of the process. The arguments are |
| 70 | +the `row_seed` which is a seed to be passed to all the keyword generators (only |
| 71 | +needed if generators are being used). The second set of arguments are keyword |
| 72 | +arguments that are either constant `dt=0.1` or |
| 73 | +generators `lambda row_seed: 0.1 * row_seed`. The second method for generating |
| 74 | +the keywords for a process is with `pack_rows(...)`. This method created many |
| 75 | +sets of keywords needed to run multiple iterations of the process. The arguments |
| 76 | +are slightly different, first it now has a `length` argument to indicate the |
| 77 | +number of rows being produced, second it has a `seed_generator` use to generate |
| 78 | +the seed of each row (for example to have only even |
| 79 | +seeds `seed_generator = lambda x: 2 * x`) if the generator |
| 80 | +is `None` `seed_generator = lamda x: x` is used. After that the same keyword |
| 81 | +arguments to define the needed parameters is used as in `pack_keywords` |
| 82 | + |
0 commit comments