conditional_io

Three models, A, B1, & B2, that conditionally pass messages. Model A receives input from a tab delimited table and sends output to both models B1 & B2. The outputs to models B1 & B2 only succeed if the data satisfies conditions described in the YAML. Output from both models B1 & B2 are sent to a tab delimited table.

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_function1(float in_val, float* in_val_copy, float* out_val) {
 5  // Only valid if in_val <= 2
 6  in_val_copy[0] = in_val;
 7  out_val[0] = pow(in_val, 2);
 8  printf("modelB_function1(%f) = %f\n", in_val, *out_val);
 9  return 0;
10}
 1#include <stdio.h>
 2#include <math.h>
 3
 4int modelB_function2(float in_val, float* in_val_copy, float* out_val) {
 5  // Only valid if in_val > 2
 6  in_val_copy[0] = in_val;
 7  out_val[0] = 2 * pow(in_val, 2);
 8  printf("modelB_function2(%f) = %f\n", in_val, *out_val);
 9  return 0;
10}

Model YAML:

 1models:
 2  - name: c_modelA
 3    language: c
 4    args: ./src/conditional_io_modelA.c
 5    function: modelA_function
 6    outputs:
 7      - name: c_modelA:output
 8        vars:
 9          - out_val
10
11  - name: c_modelB1
12    language: c
13    args: ./src/conditional_io_modelB1.c
14    function: modelB_function1
15    outputs:
16      - name: c_modelB1:output
17        vars:
18          - in_val_copy
19          - out_val
20
21  - name: c_modelB2
22    language: c
23    args: ./src/conditional_io_modelB2.c
24    function: modelB_function2
25    outputs:
26      - name: c_modelB2: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_modelB1:input
38        filter:
39          statement: "%x% <= 2"
40      - name: c_modelB2:input
41        filter:
42          function: ./src/conditional_io_modelB2.py:condition_function2
43  - inputs:
44      - c_modelB1:output  # Connection between model B, function 1 output and file
45      - c_modelB2:output  # Connection between model B, function 2 output and file
46    output: ./output.txt
47    filetype: table
48    field_names: InputMass,Mass
49    field_units: g,g**2
50    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_function1(float in_val, float &in_val_copy, float &out_val) {
 5  // Only valid if in_val <= 2
 6  in_val_copy = in_val;
 7  out_val = pow(in_val, 2);
 8  std::cout << "modelB_function1(" << in_val << ") = " << out_val << std::endl;
 9  return 0;
10}
 1#include <iostream>
 2#include <math.h>
 3
 4int modelB_function2(float in_val, float &in_val_copy, float &out_val) {
 5  // Only valid if in_val > 2
 6  in_val_copy = in_val;
 7  out_val = 2 * pow(in_val, 2);
 8  std::cout << "modelB_function2(" << in_val << ") = " << out_val << std::endl;
 9  return 0;
10}

Model YAML:

 1models:
 2  - name: cpp_modelA
 3    language: c++
 4    args: ./src/conditional_io_modelA.cpp
 5    function: modelA_function
 6
 7  - name: cpp_modelB1
 8    language: c++
 9    args: ./src/conditional_io_modelB1.cpp
10    function: modelB_function1
11
12  - name: cpp_modelB2
13    language: c++
14    args: ./src/conditional_io_modelB2.cpp
15    function: modelB_function2
16
17connections:
18  - input: ./Input/input.txt  # Connection between file and model A input
19    output: cpp_modelA:input
20    filetype: table
21  - input: cpp_modelA:output  # Connection between model A output & model B input
22    outputs:
23      - name: cpp_modelB1:input
24        filter:
25          statement: "%x% <= 2"
26      - name: cpp_modelB2:input
27        filter:
28          function: ./src/conditional_io_modelB2.py:condition_function2
29  - inputs:
30      - cpp_modelB1:output  # Connection between model B, function 1 output and file
31      - cpp_modelB2:output  # Connection between model B, function 2 output and file
32    output: ./output.txt
33    filetype: table
34    field_names: InputMass,Mass
35    field_units: g,g**2
36    format_str: "%.1lf\t%.1lf\n"

Fortran Version

Model Code:

1subroutine modelA_function(in_val, out_val)
2  real, intent(in) :: in_val
3  real, intent(out) :: out_val
4  out_val = in_val
5  print *, "modelA_function(", in_val, ") = ", out_val
6end subroutine modelA_function
1subroutine modelB_function1(in_val, in_val_copy, out_val)
2  ! Only valid if in_val <= 2
3  real, intent(in) :: in_val
4  real, intent(out) :: in_val_copy
5  real, intent(out) :: out_val
6  in_val_copy = in_val
7  out_val = in_val**2
8  print *, "modelB_function1(", in_val, ") = ", out_val
9end subroutine modelB_function1
1subroutine modelB_function2(in_val, in_val_copy, out_val)
2  ! Only valid if in_val > 2
3  real, intent(in) :: in_val
4  real, intent(out) :: in_val_copy
5  real, intent(out) :: out_val
6  in_val_copy = in_val
7  out_val = 2 * (in_val**2)
8  print *, "modelB_function2(", in_val, ") = ", out_val
9end subroutine modelB_function2

Model YAML:

 1models:
 2  - name: fortran_modelA
 3    language: fortran
 4    args: ./src/conditional_io_modelA.f90
 5    function: modelA_function
 6    outputs:
 7      - name: fortran_modelA:output
 8        vars:
 9          - out_val
10
11  - name: fortran_modelB1
12    language: fortran
13    args: ./src/conditional_io_modelB1.f90
14    function: modelB_function1
15    outputs:
16      - name: fortran_modelB1:output
17        vars:
18          - in_val_copy
19          - out_val
20
21  - name: fortran_modelB2
22    language: fortran
23    args: ./src/conditional_io_modelB2.f90
24    function: modelB_function2
25    outputs:
26      - name: fortran_modelB2: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: fortran_modelA:input
34    filetype: table
35  - input: fortran_modelA:output  # Connection between model A output & model B input
36    outputs:
37      - name: fortran_modelB1:input
38        filter:
39          statement: "%x% <= 2"
40      - name: fortran_modelB2:input
41        filter:
42          function: ./src/conditional_io_modelB2.py:condition_function2
43  - inputs:
44      - fortran_modelB1:output  # Connection between model B, function 1 output and file
45      - fortran_modelB2:output  # Connection between model B, function 2 output and file
46    output: ./output.txt
47    filetype: table
48    field_names: InputMass,Mass
49    field_units: g,g**2
50    format_str: "%.1lf\t%.1lf\n"

Julia Version

Model Code:

1using Printf
2
3function modelA_function(in_val)
4  out_val = in_val
5  @printf("modelA_function(%s) = %s", in_val, out_val)
6  return out_val
7end
1using Printf
2
3function modelB_function1(in_val)
4  # Only valid if in_val <= 2
5  out_val = in_val^2
6  @printf("modelB_function1(%s) = %s", in_val, out_val)
7  return in_val, out_val
8end
1using Printf
2
3function modelB_function2(in_val)
4  # Only valid if in_val > 2
5  out_val = 2 * in_val^2
6  @printf("modelB_function2(%s) = %s", in_val, out_val)
7  return in_val, out_val
8end

Model YAML:

 1models:
 2  - name: julia_modelA
 3    language: julia
 4    args: ./src/conditional_io_modelA.jl
 5    function: modelA_function
 6
 7  - name: julia_modelB1
 8    language: julia
 9    args: ./src/conditional_io_modelB1.jl
10    function: modelB_function1
11
12  - name: julia_modelB2
13    language: julia
14    args: ./src/conditional_io_modelB2.jl
15    function: modelB_function2
16
17connections:
18  - input: ./Input/input.txt  # Connection between file and model A input
19    output: julia_modelA:input
20    filetype: table
21  - input: julia_modelA:output  # Connection between model A output & model B input
22    outputs:
23      - name: julia_modelB1:input
24        filter:
25          statement: "%x% <= 2"
26      - name: julia_modelB2:input
27        filter:
28          function: ./src/conditional_io_modelB2.py:condition_function2
29  - inputs:
30      - julia_modelB1:output  # Connection between model B, function 1 output and file
31      - julia_modelB2:output  # Connection between model B, function 2 output and file
32    output: ./output.txt
33    filetype: table
34    field_names: InputMass,Mass
35    field_units: g,g**2
36    format_str: "%.1lf\t%.1lf\n"

Matlab Version

Model Code:

1function out_val = conditional_io_modelA(in_val)
2  out_val = in_val;
3  disp(sprintf('modelA_function(%f) = %f', separateUnits(in_val), separateUnits(out_val)));
4end
1function [in_val, out_val] = conditional_io_modelB1(in_val)
2  % Only valid if in_val <= 2
3  out_val = in_val^2;
4  disp(sprintf('modelB_function1(%f) = %f', separateUnits(in_val), separateUnits(out_val)));
5end
1function [in_val, out_val] = conditional_io_modelB2(in_val)
2  % Only valid if in_val > 2
3  out_val = 2 * in_val^2;
4  disp(sprintf('modelB_function2(%f) = %f', separateUnits(in_val), separateUnits(out_val)));
5end

Model YAML:

 1models:
 2  - name: matlab_modelA
 3    language: matlab
 4    args: ./src/conditional_io_modelA.m
 5    function: conditional_io_modelA
 6    use_symunit: true
 7
 8  - name: matlab_modelB1
 9    language: matlab
10    args: ./src/conditional_io_modelB1.m
11    function: conditional_io_modelB1
12    use_symunit: true
13
14  - name: matlab_modelB2
15    language: matlab
16    args: ./src/conditional_io_modelB2.m
17    function: conditional_io_modelB2
18    use_symunit: true
19
20connections:
21  - input: ./Input/input.txt  # Connection between file and model A input
22    output: matlab_modelA:input
23    filetype: table
24  - input: matlab_modelA:output  # Connection between model A output & model B input
25    outputs:
26      - name: matlab_modelB1:input
27        filter:
28          statement: "%x% <= 2"
29      - name: matlab_modelB2:input
30        filter:
31          function: ./src/conditional_io_modelB2.py:condition_function2
32  - inputs:
33      - matlab_modelB1:output  # Connection between model B, function 1 output and file
34      - matlab_modelB2:output  # Connection between model B, function 2 output and file
35    output: ./output.txt
36    filetype: table
37    field_names: InputMass,Mass
38    field_units: g,g**2
39    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_function1(in_val):
2    # Only valid if in_val <= 2
3    out_val = in_val**2
4    print("modelB_function1(%s) = %s" % (in_val, out_val))
5    return in_val, out_val
1def modelB_function2(in_val):
2    # Only valid if in_val > 2
3    out_val = 2 * in_val**2
4    print("modelB_function2(%s) = %s" % (in_val, out_val))
5    return in_val, out_val
6
7
8def condition_function2(in_val):
9    return (in_val > 2)

Model YAML:

 1models:
 2  - name: python_modelA
 3    language: python
 4    args: ./src/conditional_io_modelA.py
 5    function: modelA_function
 6
 7  - name: python_modelB1
 8    language: python
 9    args: ./src/conditional_io_modelB1.py
10    function: modelB_function1
11
12  - name: python_modelB2
13    language: python
14    args: ./src/conditional_io_modelB2.py
15    function: modelB_function2
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_modelB1:input
24        filter:
25          statement: "%x% <= 2"
26      - name: python_modelB2:input
27        filter:
28          function: ./src/conditional_io_modelB2.py:condition_function2
29  - inputs:
30      - python_modelB1:output  # Connection between model B, function 1 output and file
31      - python_modelB2:output  # Connection between model B, function 2 output and file
32    output: ./output.txt
33    filetype: table
34    field_names: InputMass,Mass
35    field_units: g,g**2
36    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_function1 <- function(in_val) {
2  # Only valid if in_val <= 2
3  out_val <- in_val^2
4  print(sprintf("modelB_function1(%f) = %f", in_val, out_val))
5  return(list(in_val, out_val))
6}
1modelB_function2 <- function(in_val) {
2  # Only valid if in_val > 2
3  out_val <- 2 * in_val^2
4  print(sprintf("modelB_function2(%f) = %f", in_val, out_val))
5  return(list(in_val, out_val))
6}

Model YAML:

 1models:
 2  - name: R_modelA
 3    language: R
 4    args: ./src/conditional_io_modelA.R
 5    function: modelA_function
 6
 7  - name: R_modelB1
 8    language: R
 9    args: ./src/conditional_io_modelB1.R
10    function: modelB_function1
11
12  - name: R_modelB2
13    language: R
14    args: ./src/conditional_io_modelB2.R
15    function: modelB_function2
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_modelB1:input
24        filter:
25          statement: "%x% <= 2"
26      - name: R_modelB2:input
27        filter:
28          function: ./src/conditional_io_modelB2.py:condition_function2
29  - inputs:
30      - R_modelB1:output  # Connection between model B, function 1 output and file
31      - R_modelB2:output  # Connection between model B, function 2 output and file
32    output: ./output.txt
33    filetype: table
34    field_names: InputMass,Mass
35    field_units: g,g**2
36    format_str: "%.1lf\t%.1lf\n"