transformed_io

Three models, A, B, & C, pass messages which are transformed based on YAML parameters. Model A receives input from a tab delimited table and sends output to both models B & C. The output to each model is transformed in different ways controlled by the transform comm model parameter. Models B & C both send output to tables.

C Version

Model Code:

1#include <stdio.h>
2
3int modelA_function(float in_val, float* out_val) {
4  out_val[0] = in_val;
5  printf("modelA_function(%f) = %f\n", in_val, *out_val);
6  return 0;
7}
1#include <stdio.h>
2#include <math.h>
3
4int modelB_function(float in_val, float* in_val_copy, float* out_val) {
5  in_val_copy[0] = in_val;
6  out_val[0] = 3 * in_val;
7  printf("modelB_function(%f) = %f\n", in_val, *out_val);
8  return 0;
9}
1#include <stdio.h>
2#include <math.h>
3
4int modelC_function(float in_val, float* in_val_copy, float* out_val) {
5  in_val_copy[0] = in_val;
6  out_val[0] = 2 * in_val;
7  printf("modelC_function(%f) = %f\n", in_val, *out_val);
8  return 0;
9}

Model YAML:

 1models:
 2  - name: c_modelA
 3    language: c
 4    args: ./src/transformed_io_modelA.c
 5    function: modelA_function
 6    outputs:
 7      - name: c_modelA:output
 8        vars:
 9          - out_val
10
11  - name: c_modelB
12    language: c
13    args: ./src/transformed_io_modelB.c
14    function: modelB_function
15    outputs:
16      - name: c_modelB:output
17        vars:
18          - in_val_copy
19          - out_val
20
21  - name: c_modelC
22    language: c
23    args: ./src/transformed_io_modelC.c
24    function: modelC_function
25    outputs:
26      - name: c_modelC:output
27        vars:
28          - in_val_copy
29          - out_val
30
31connections:
32  - input: ./Input/input.txt  # Connection between file and model A input
33    output: c_modelA:input
34    filetype: table
35  - input: c_modelA:output  # Connection between model A output & model B input
36    outputs:
37      - name: c_modelB:input
38        transform:
39          statement: "%x%*5"
40      - name: c_modelC:input
41        transform:
42          function: ./src/transformed_io_modelC.py:transform_function
43  - inputs: c_modelB:output  # Connection between model B output and file
44    output: ./outputB.txt
45    filetype: table
46    field_names: InputMassRate,OuputMassRate
47    field_units: g/s,g/s
48    format_str: "%.1lf\t%.1lf\n"
49  - inputs: c_modelC:output  # Connection between model C output and file
50    output: ./outputC.txt
51    filetype: table
52    field_names: InputMassRate,OuputMassRateDensity
53    field_units: g/(s*(cm**2)),g/(s*(cm**2))
54    format_str: "%.1lf\t%.1lf\n"

C++ Version

Model Code:

1#include <iostream>
2
3int modelA_function(float in_val, float &out_val) {
4  out_val = in_val;
5  std::cout << "modelA_function(" << in_val << ") = " << out_val << std::endl;
6  return 0;
7}
1#include <iostream>
2#include <math.h>
3
4int modelB_function(float in_val, float &in_val_copy, float &out_val) {
5  in_val_copy = in_val;
6  out_val = 3 * in_val;
7  std::cout << "modelB_function(" << in_val << ") = " << out_val << std::endl;
8  return 0;
9}
1#include <iostream>
2#include <math.h>
3
4int modelC_function(float in_val, float &in_val_copy, float &out_val) {
5  in_val_copy = in_val;
6  out_val = 2 * in_val;
7  std::cout << "modelC_function(" << in_val << ") = " << out_val << std::endl;
8  return 0;
9}

Model YAML:

 1models:
 2  - name: cpp_modelA
 3    language: cpp
 4    args: ./src/transformed_io_modelA.cpp
 5    function: modelA_function
 6    outputs:
 7      - name: cpp_modelA:output
 8        vars:
 9          - out_val
10
11  - name: cpp_modelB
12    language: cpp
13    args: ./src/transformed_io_modelB.cpp
14    function: modelB_function
15    outputs:
16      - name: cpp_modelB:output
17        vars:
18          - in_val_copy
19          - out_val
20
21  - name: cpp_modelC
22    language: cpp
23    args: ./src/transformed_io_modelC.cpp
24    function: modelC_function
25    outputs:
26      - name: cpp_modelC:output
27        vars:
28          - in_val_copy
29          - out_val
30
31connections:
32  - input: ./Input/input.txt  # Connection between file and model A input
33    output: cpp_modelA:input
34    filetype: table
35  - input: cpp_modelA:output  # Connection between model A output & model B input
36    outputs:
37      - name: cpp_modelB:input
38        transform:
39          statement: "%x%*5"
40      - name: cpp_modelC:input
41        transform:
42          function: ./src/transformed_io_modelC.py:transform_function
43  - inputs: cpp_modelB:output  # Connection between model B output and file
44    output: ./outputB.txt
45    filetype: table
46    field_names: InputMassRate,OuputMassRate
47    field_units: g/s,g/s
48    format_str: "%.1lf\t%.1lf\n"
49  - inputs: cpp_modelC:output  # Connection between model C output and file
50    output: ./outputC.txt
51    filetype: table
52    field_names: InputMassRate,OuputMassRateDensity
53    field_units: g/(s*(cm**2)),g/(s*(cm**2))
54    format_str: "%.1lf\t%.1lf\n"

Matlab Version

Model Code:

1function out_val = transformed_io_modelA(in_val)
2  out_val = in_val;
3  disp(sprintf('modelA_function(%f) = %f', in_val, out_val));
4end
1function [in_val, out_val] = transformed_io_modelB(in_val)
2  out_val = 3 * in_val;
3  disp(sprintf('modelB_function(%f) = %f', in_val, out_val));
4end
1function [in_val, out_val] = transformed_io_modelC(in_val)
2  out_val = 2 * in_val;
3  disp(sprintf('modelC_function(%f) = %f', in_val, out_val));
4end

Model YAML:

 1models:
 2  - name: matlab_modelA
 3    language: matlab
 4    args: ./src/transformed_io_modelA.m
 5    function: transformed_io_modelA
 6
 7  - name: matlab_modelB
 8    language: matlab
 9    args: ./src/transformed_io_modelB.m
10    function: transformed_io_modelB
11
12  - name: matlab_modelC
13    language: matlab
14    args: ./src/transformed_io_modelC.m
15    function: transformed_io_modelC
16
17connections:
18  - input: ./Input/input.txt  # Connection between file and model A input
19    output: matlab_modelA:input
20    filetype: table
21  - input: matlab_modelA:output  # Connection between model A output & model B input
22    outputs:
23      - name: matlab_modelB:input
24        transform:
25          statement: "%x%*5"
26      - name: matlab_modelC:input
27        transform:
28          function: ./src/transformed_io_modelC.py:transform_function
29  - input: matlab_modelB:output  # Connection between model B, function 1 output and file
30    output: ./outputB.txt
31    filetype: table
32    field_names: InputMassRate,OuputMassRate
33    field_units: g/s,g/s
34    format_str: "%.1lf\t%.1lf\n"
35  - input: matlab_modelC:output  # Connection between model C, function 1 output and file
36    output: ./outputC.txt
37    filetype: table
38    field_names: InputMassRate,OuputMassRateDensity
39    field_units: g/(s*(cm**2)),g/(s*(cm**2))
40    format_str: "%.1lf\t%.1lf\n"

Python Version

Model Code:

1def modelA_function(in_val):
2    out_val = in_val
3    print("modelA_function(%s) = %s" % (in_val, out_val))
4    return out_val
1def modelB_function(in_val):
2    out_val = 3 * in_val
3    print("modelB_function(%s) = %s" % (in_val, out_val))
4    return in_val, out_val
 1from yggdrasil import units
 2
 3
 4def modelC_function(in_val):
 5    out_val = 2 * in_val
 6    print("modelC_function(%s) = %s" % (in_val, out_val))
 7    return in_val, out_val
 8
 9
10def transform_function(in_val):
11    return (in_val / units.add_units(10, 'cm**2'))

Model YAML:

 1models:
 2  - name: python_modelA
 3    language: python
 4    args: ./src/transformed_io_modelA.py
 5    function: modelA_function
 6
 7  - name: python_modelB
 8    language: python
 9    args: ./src/transformed_io_modelB.py
10    function: modelB_function
11
12  - name: python_modelC
13    language: python
14    args: ./src/transformed_io_modelC.py
15    function: modelC_function
16
17connections:
18  - input: ./Input/input.txt  # Connection between file and model A input
19    output: python_modelA:input
20    filetype: table
21  - input: python_modelA:output  # Connection between model A output & model B input
22    outputs:
23      - name: python_modelB:input
24        transform:
25          statement: "%x%*5"
26      - name: python_modelC:input
27        transform:
28          function: ./src/transformed_io_modelC.py:transform_function
29  - input: python_modelB:output  # Connection between model B, function 1 output and file
30    output: ./outputB.txt
31    filetype: table
32    field_names: InputMassRate,OuputMassRate
33    field_units: g/s,g/s
34    format_str: "%.1lf\t%.1lf\n"
35  - input: python_modelC:output  # Connection between model C, function 1 output and file
36    output: ./outputC.txt
37    filetype: table
38    field_names: InputMassRate,OuputMassRateDensity
39    field_units: g/(s*(cm**2)),g/(s*(cm**2))
40    format_str: "%.1lf\t%.1lf\n"

R Version

Model Code:

1modelA_function <- function(in_val) {
2  out_val <- in_val
3  print(sprintf("modelA_function(%f) = %f", in_val, out_val))
4  return(out_val)
5}
1modelB_function <- function(in_val) {
2  out_val <- 3 * in_val
3  print(sprintf("modelB_function(%f) = %f", in_val, out_val))
4  return(list(in_val, out_val))
5}
1modelC_function <- function(in_val) {
2  out_val <- 2 * in_val
3  print(sprintf("modelC_function(%f) = %f", in_val, out_val))
4  return(list(in_val, out_val))
5}

Model YAML:

 1models:
 2  - name: R_modelA
 3    language: R
 4    args: ./src/transformed_io_modelA.R
 5    function: modelA_function
 6
 7  - name: R_modelB
 8    language: R
 9    args: ./src/transformed_io_modelB.R
10    function: modelB_function
11
12  - name: R_modelC
13    language: R
14    args: ./src/transformed_io_modelC.R
15    function: modelC_function
16
17connections:
18  - input: ./Input/input.txt  # Connection between file and model A input
19    output: R_modelA:input
20    filetype: table
21  - input: R_modelA:output  # Connection between model A output & model B input
22    outputs:
23      - name: R_modelB:input
24        transform:
25          statement: "%x%*5"
26      - name: R_modelC:input
27        transform:
28          function: ./src/transformed_io_modelC.py:transform_function
29  - input: R_modelB:output  # Connection between model B, function 1 output and file
30    output: ./outputB.txt
31    filetype: table
32    field_names: InputMassRate,OuputMassRate
33    field_units: g/s,g/s
34    format_str: "%.1lf\t%.1lf\n"
35  - input: R_modelC:output  # Connection between model C, function 1 output and file
36    output: ./outputC.txt
37    filetype: table
38    field_names: InputMassRate,OuputMassRateDensity
39    field_units: g/(s*(cm**2)),g/(s*(cm**2))
40    format_str: "%.1lf\t%.1lf\n"