composite_function

One model receives input from 3 different source, 2 being value constants and one being a file. 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		   double** out, size_t* out_length) {
 5  out_length[0] = 3;
 6  out[0] = (double*)realloc(out[0], 3 * sizeof(double));
 7  for (int i = 0; i < 3; i++) {
 8    if (a)
 9      out[0][i] = b * pow(i, generic_map_get_double(c, "c1"));
10    else
11      out[0][i] = b * pow(i, generic_map_get_double(c, "c2"));
12  }
13  return 1;
14}

Model YAML:

 1models:
 2  - name: c_model
 3    language: c
 4    args: ./src/model_function.c
 5    function: model_function
 6    inputs:
 7      - name: a
 8        default_value: true
 9      - name: b
10        default_value: 5.0
11      - name: c
12        default_file:
13          name: ./Input/input.txt
14          filetype: map
15    outputs:
16      - name: out
17        default_file:
18          name: ./output.txt
19          filetype: table
20          as_array: true
21          field_names: x

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		   std::vector<double>& out) {
 7  for (int i = 0; i < 3; i++) {
 8    if (a)
 9      out.push_back(b * pow(i, c["c1"]));
10    else
11      out.push_back(b * pow(i, c["c2"]));
12  }
13  return 1;
14}

Model YAML:

 1models:
 2  - name: cpp_model
 3    language: c++
 4    args: ./src/model_function.cpp
 5    function: model_function
 6    inputs:
 7      - name: a
 8        default_value: true
 9      - name: b
10        default_value: 5.0
11      - name: c
12        default_file:
13          name: ./Input/input.txt
14          filetype: map
15    outputs:
16      - name: output
17        default_file:
18          name: ./output.txt
19          filetype: table
20          as_array: true
21          field_names: x

Fortran Version

Model Code:

 1function model_function(a, b, c, out) result(retval)
 2  logical, intent(in) :: a
 3  real(kind=8), intent(in) :: b
 4  type(ygggeneric), intent(in) :: c
 5  real(kind=8), dimension(:), pointer :: out
 6  real(kind=8), pointer :: cparam
 7  integer(kind=4) :: i
 8  logical :: retval
 9
10  allocate(out(3))
11  do i = 1, 3
12     if (a) then
13        call generic_map_get(c, "c1", cparam)
14     else
15        call generic_map_get(c, "c2", cparam)
16     end if
17     out(i) = b * ((dble(i - 1) ** cparam))
18  end do
19
20  retval = .true.
21end 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: a
 8        default_value: true
 9      - name: b
10        default_value: 5.0
11      - name: c
12        default_file:
13          name: ./Input/input.txt
14          filetype: map
15    outputs:
16      - name: output
17        default_file:
18          name: ./output.txt
19          filetype: table
20          as_array: true
21          field_names: x

Julia Version

Model Code:

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

Model YAML:

 1models:
 2  - name: julia_model
 3    language: julia
 4    args: ./src/model_function.jl
 5    function: model_function
 6    inputs:
 7      - name: a
 8        default_value: true
 9      - name: b
10        default_value: 5.0
11      - name: c
12        default_file:
13          name: ./Input/input.txt
14          filetype: map
15    outputs:
16      - name: output
17        default_file:
18          name: ./output.txt
19          filetype: table
20          as_array: true
21          field_names: x

Matlab Version

Model Code:

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

Model YAML:

 1models:
 2  - name: matlab_model
 3    language: matlab
 4    args: ./src/model_function.m
 5    function: model_function
 6    inputs:
 7      - name: a
 8        default_value: true
 9      - name: b
10        default_value: 5.0
11      - name: c
12        default_file:
13          name: ./Input/input.txt
14          filetype: map
15    outputs:
16      - name: output
17        default_file:
18          name: ./output.txt
19          filetype: table
20          as_array: true
21          field_names: x

Python Version

Model Code:

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

Model YAML:

 1models:
 2  - name: python_model
 3    language: python
 4    args: ./src/model_function.py
 5    function: model_function
 6    inputs:
 7      - name: a
 8        default_value: true
 9      - name: b
10        default_value: 5.0
11      - name: c
12        default_file:
13          name: ./Input/input.txt
14          filetype: map
15    outputs:
16      - name: output
17        default_file:
18          name: ./output.txt
19          filetype: table
20          as_array: true
21          field_names: x

R Version

Model Code:

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

Model YAML:

 1models:
 2  - name: r_model
 3    language: r
 4    args: ./src/model_function.R
 5    function: model_function
 6    inputs:
 7      - name: a
 8        default_value: true
 9      - name: b
10        default_value: 5.0
11      - name: c
12        default_file:
13          name: ./Input/input.txt
14          filetype: map
15    outputs:
16      - name: output
17        default_file:
18          name: ./output.txt
19          filetype: table
20          as_array: true
21          field_names: x