-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelics_input.c
212 lines (181 loc) · 6 KB
/
helics_input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2024-2024, Lawrence Livermore National Security
* See the top-level NOTICE for additional details. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
#define S_FUNCTION_NAME helics_input /* Defines and Includes */
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include "helics/helics.h"
#include "math.h"
#include <string.h>
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 2);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch reported by the Simulink engine*/
}
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S,0,3);
ssSetInputPortDataType(S, 0, SS_DOUBLE );
if (!ssSetNumOutputPorts(S,1)) return;
const mxArray *params=ssGetSFcnParam(S, 1);
char *param_string=mxArrayToString(params);
int output_width=1;
int dataType=HELICS_DATA_TYPE_DOUBLE;
char *token=NULL;
token=strtok(param_string," ,");
while (token){
ssPrintf("got token %s\n",token);
if (strncmp(token,"output_size=",12)==0)
{
output_width=atoi(token+12);
ssPrintf("got token output_size read as %s converted to %d\n",token+12,output_width);
}
else if (strncmp(token,"output_type=",12)==0)
{
dataType=helicsGetDataType(token+12);
ssPrintf("got token output_type read as %s converted to %d\n",token+12,dataType);
}
token=strtok(NULL," ,");
}
ssSetOutputPortWidth(S, 0, output_width);
switch (dataType)
{
case HELICS_DATA_TYPE_DOUBLE: case HELICS_DATA_TYPE_VECTOR:
ssSetOutputPortDataType(S, 0, SS_DOUBLE);
break;
case HELICS_DATA_TYPE_INT:
ssSetOutputPortDataType(S, 0, SS_INT32);
break;
case HELICS_DATA_TYPE_BOOLEAN:
ssSetOutputPortDataType(S, 0, SS_BOOLEAN);
break;
case HELICS_DATA_TYPE_COMPLEX: case HELICS_DATA_TYPE_COMPLEX_VECTOR:
ssSetOutputPortDataType(S, 0, SS_DOUBLE);
//something with complex indications
break;
}
ssSetInputPortRequiredContiguous(S,0,1);
/* sample times */
ssSetNumSampleTimes( S, 1 );
/* Set this S-function as runtime thread-safe for multicore execution */
ssSetRuntimeThreadSafetyCompliance(S, RUNTIME_THREAD_SAFETY_COMPLIANCE_TRUE);
ssSetOptions(S,SS_OPTION_CALL_TERMINATE_ON_EXIT);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime( S, 0, INHERITED_SAMPLE_TIME );
ssSetOffsetTime( S, 0, FIXED_IN_MINOR_STEP_OFFSET );
ssSetModelReferenceSampleTimeDefaultInheritance(S);
}
#define MDL_START
void mdlStart(SimStruct *S)
{
}
#define MDL_INITIALIZE_CONDITIONS
void mdlInitializeConditions(SimStruct *S)
{
SimStruct *root_ss=ssGetRootSS(S);
HelicsFederate fed=ssGetUserData(root_ss);
if (!helicsFederateIsValid(fed)){
ssPrintf("HELICS federate is not valid\n");
ssSetErrorStatus(S,"HELICS federate is not valid");
return;
}
const mxArray *index_param=ssGetSFcnParam(S, 0);
mxClassID paramType=mxGetClassID(index_param);
HelicsInput inp;
switch (paramType)
{
case mxCHAR_CLASS:
{
char *input_name=mxArrayToString(index_param);
HelicsError err=helicsErrorInitialize();
inp=helicsFederateGetInput(fed,input_name,&err);
if (err.error_code!=HELICS_OK)
{
ssSetErrorStatus(S,err.message);
return;
}}
break;
case mxDOUBLE_CLASS:
{
double input_index=mxGetScalar(index_param);
HelicsError err=helicsErrorInitialize();
inp=helicsFederateGetInputByIndex(fed,(int32_t)(input_index),&err);
if (err.error_code!=HELICS_OK)
{
ssSetErrorStatus(S,err.message);
return;
}
}
break;
}
ssPrintf("Got input name %s\n",helicsInputGetName(inp));
ssSetUserData(S,inp);
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
HelicsInput inp=ssGetUserData(S);
DTypeId outputDataType = ssGetOutputPortDataType(S, 0);
int width=ssGetOutputPortWidth(S,0);
switch(outputDataType){
case SS_UINT8:
case SS_INT8:
break;
case SS_UINT16:
case SS_UINT32:
case SS_INT16:
case SS_INT32:
break;
case SS_DOUBLE:
case SS_SINGLE:
{
if (helicsGetDataType(helicsInputGetType(inp))==HELICS_DATA_TYPE_COMPLEX)
{
}
else if (width>1)
{
ssPrintf("input input width =%d\n",width);
real_T *y = ssGetOutputPortRealSignal(S,0);
int sz;
HelicsError err=helicsErrorInitialize();
helicsInputGetVector(inp,y,width,&sz,&err);
ssPrintf("got input size %d input[0]=%f",sz,y[0]);
if (err.error_code!=HELICS_OK)
{
}
else
{
if (sz<width)
{
int ii=0;
for (ii=sz;ii<width;++ii)
{
y[ii]=HELICS_INVALID_PROPERTY_VALUE;
}
}
}
}
else
{
ssPrintf("input input width =%d\n",width);
double val=helicsInputGetDouble(inp,NULL);
real_T *y = ssGetOutputPortRealSignal(S,0);
y[0]=val;
}
}
break;
case SS_BOOLEAN:
break;
}
}
#define MDL_UPDATE
void mdlUpdate(SimStruct *S, int_T tid)
{
}
static void mdlTerminate(SimStruct *S)
{
}
#include "simulink.c" /* MEX-file interface mechanism */