composite_function2

One model receives inputs from one source and sends outputs to one destination. One component of the input and the output each are composite types. The model is automatically wrapped by yggdrasil with the appropriate interface call.

C Version

Model Code:

 1#include <stdio.h>
 2
 3int model_function(bool a, double b, generic_t c,
 4		   bool* d, double* e,
 5		   double** f, size_t* f_length) {
 6  d[0] = (!a);
 7  e[0] = generic_map_get_double(c, "c1");
 8  f_length[0] = 3;
 9  f[0] = (double*)realloc(f[0], 3 * sizeof(double));
10  for (int i = 0; i < 3; i++) {
11    if (a)
12      f[0][i] = b * pow(i, generic_map_get_double(c, "c1"));
13    else
14      f[0][i] = b * pow(i, generic_map_get_double(c, "c2"));
15  }
16  return 1;
17}

Model YAML:

 1models:
 2  - name: c_model
 3    language: c
 4    args: ./src/model_function.c
 5    function: model_function
 6    inputs:
 7      - name: input
 8        vars: [a, b, c]
 9        default_file:
10          name: ./Input/input.json
11          filetype: json
12    outputs:
13      - name: output
14        vars: [d, e, f, f_length]
15        default_file:
16          name: ./output.json
17          filetype: json

C++ Version

Model Code:

 1#include <map>
 2#include <vector>
 3
 4int model_function(bool a, double b,
 5		   std::map<std::string, double> c,
 6		   bool& d, double& e,
 7		   std::vector<double>& f) {
 8  d = (!a);
 9  e = c["c1"];
10  for (int i = 0; i < 3; i++) {
11    if (a)
12      f.push_back(b * pow(i, c["c1"]));
13    else
14      f.push_back(b * pow(i, c["c2"]));
15  }
16  return 1;
17}

Model YAML:

 1models:
 2  - name: cpp_model
 3    language: c++
 4    args: ./src/model_function.cpp
 5    function: model_function
 6    inputs:
 7      - name: input
 8        vars: [a, b, c]
 9        default_file:
10          name: ./Input/input.json
11          filetype: json
12    outputs:
13      - name: output
14        vars: [d, e, f]
15        default_file:
16          name: ./output.json
17          filetype: json

Fortran Version

Model Code:

 1function model_function(a, b, c, d, e, f) result(retval)
 2  logical, intent(in) :: a
 3  real(kind=8), intent(in) :: b
 4  type(ygggeneric), intent(in) :: c
 5  logical, intent(out) :: d
 6  real(kind=8), intent(out) :: e
 7  real(kind=8), dimension(:), pointer :: f
 8  real(kind=8), pointer :: cparam
 9  integer(kind=4) :: i
10  logical :: retval
11
12  allocate(f(3))
13  d = (.not.a)
14  call generic_map_get(c, "c1", cparam)
15  e = cparam
16  do i = 1, 3
17     if (a) then
18        call generic_map_get(c, "c1", cparam)
19     else
20        call generic_map_get(c, "c2", cparam)
21     end if
22     f(i) = b * ((dble(i - 1) ** cparam))
23  end do
24
25  retval = .true.
26end function model_function

Model YAML:

 1models:
 2  - name: fortran_model
 3    language: fortran
 4    args: ./src/model_function.f90
 5    function: model_function
 6    inputs:
 7      - name: input
 8        vars: [a, b, c]
 9        default_file:
10          name: ./Input/input.json
11          filetype: json
12    outputs:
13      - name: output
14        vars: [d, e, f]
15        default_file:
16          name: ./output.json
17          filetype: json

Julia Version

Model Code:

 1function model_function(a, b, c)
 2  d = !a
 3  e = c["c1"]
 4  f = Array{Float64}(undef, 3)
 5  for i = 1:3
 6    if (a)
 7      f[i] = b * ((i - 1) ^ c["c1"])
 8    else
 9      f[i] = b * ((i - 1) ^ c["c2"])
10    end
11  end
12  return d, e, f
13end

Model YAML:

 1models:
 2  - name: julia_model
 3    language: julia
 4    args: ./src/model_function.jl
 5    function: model_function
 6    inputs:
 7      - name: input
 8        vars: [a, b, c]
 9        default_file:
10          name: ./Input/input.json
11          filetype: json
12    outputs:
13      - name: output
14        vars: [d, e, f]
15        default_file:
16          name: ./output.json
17          filetype: json

Matlab Version

Model Code:

 1function d, e, f = model_function(a, b, c)
 2  d = (!a);
 3  e = c('c1');
 4  f = zeros(1, 3);
 5  for i = 1:3
 6    if a
 7      f{i} = b * (i ^ c('c1'));
 8    else
 9    end;
10  end;
11end

Model YAML:

 1models:
 2  - name: matlab_model
 3    language: matlab
 4    args: ./src/model_function.m
 5    function: model_function
 6    inputs:
 7      - name: input
 8        vars: [a, b, c]
 9        default_file:
10          name: ./Input/input.json
11          filetype: json
12    outputs:
13      - name: output
14        vars: [d, e, f]
15        default_file:
16          name: ./output.json
17          filetype: json

Python Version

Model Code:

 1import numpy as np
 2
 3
 4def model_function(a, b, c):
 5    d = (not a)
 6    e = c["c1"]
 7    f = np.zeros(3, 'float64')
 8    for i in range(3):
 9        if a:
10            f[i] = b * (i ** c["c1"])
11        else:
12            f[i] = b * (i ** c["c2"])
13    return d, e, f

Model YAML:

 1models:
 2  - name: python_model
 3    language: python
 4    args: ./src/model_function.py
 5    function: model_function
 6    inputs:
 7      - name: input
 8        vars: [a, b, c]
 9        default_file:
10          name: ./Input/input.json
11          filetype: json
12    outputs:
13      - name: output
14        vars: [d, e, f]
15        default_file:
16          name: ./output.json
17          filetype: json

R Version

Model Code:

 1model_function <- function(a, b, c) {
 2  d <- (!a)
 3  e <- c[["c1"]]
 4  f <- double(3)
 5  for (i in 1L:3) {
 6    if (a) {
 7      f[[i]] <- b * (i-1)^c[["c1"]]
 8    } else {
 9      f[[i]] <- b * (i-1)^c[["c2"]]
10    }
11  }
12  return(list(d,e,f))
13}

Model YAML:

 1models:
 2  - name: r_model
 3    language: r
 4    args: ./src/model_function.R
 5    function: model_function
 6    inputs:
 7      - name: input
 8        vars: [a, b, c]
 9        default_file:
10          name: ./Input/input.json
11          filetype: json
12    outputs:
13      - name: output
14        vars: [d, e, f]
15        default_file:
16          name: ./output.json
17          filetype: json