@sdeverywhere/cli 0.7.3 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/c/vensim.c +51 -0
- package/src/c/vensim.h +14 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdeverywhere/cli",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Contains the `sde` command line interface for the SDEverywhere tool suite.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@sdeverywhere/build": "^0.2.0",
|
|
15
|
-
"@sdeverywhere/compile": "^0.7.
|
|
15
|
+
"@sdeverywhere/compile": "^0.7.1",
|
|
16
16
|
"bufx": "^1.0.5",
|
|
17
17
|
"byline": "^5.0.0",
|
|
18
18
|
"ramda": "^0.27.0",
|
package/src/c/vensim.c
CHANGED
|
@@ -464,3 +464,54 @@ double _DELAY_FIXED(double input, FixedDelay* fixed_delay) {
|
|
|
464
464
|
}
|
|
465
465
|
return result;
|
|
466
466
|
}
|
|
467
|
+
|
|
468
|
+
//
|
|
469
|
+
// DEPRECIATE STRAIGHTLINE
|
|
470
|
+
//
|
|
471
|
+
Depreciation* __new_depreciation(Depreciation* depreciation, double dtime, double initial_value) {
|
|
472
|
+
// Construct a Depreciation struct with a ring buffer for the time steps in the depreciation time.
|
|
473
|
+
// We don't know the size until runtime, so it must be dynamically allocated.
|
|
474
|
+
// The depreciation time is quantized to an integral number of time steps.
|
|
475
|
+
// The Depreciation should be constructed at init time to latch the depreciation time and initial value.
|
|
476
|
+
// Allocate memory on the first call only. Pass the same pointer back in on subsequent runs.
|
|
477
|
+
size_t n = (size_t)ceil(dtime / _time_step);
|
|
478
|
+
size_t bufsize = n * sizeof(double);
|
|
479
|
+
if (depreciation == NULL) {
|
|
480
|
+
// Create the Depreciation object and allocate its data buffer.
|
|
481
|
+
depreciation = malloc(sizeof(Depreciation));
|
|
482
|
+
depreciation->data = malloc(bufsize);
|
|
483
|
+
} else if (depreciation->n != n) {
|
|
484
|
+
// The depreciation time has changed since a previous run. Reallocate the data buffer.
|
|
485
|
+
free(depreciation->data);
|
|
486
|
+
depreciation->data = malloc(bufsize);
|
|
487
|
+
}
|
|
488
|
+
// Reset state at the start of each run.
|
|
489
|
+
memset(depreciation->data, 0, bufsize);
|
|
490
|
+
depreciation->n = n;
|
|
491
|
+
depreciation->data_index = 0;
|
|
492
|
+
depreciation->dtime = dtime;
|
|
493
|
+
depreciation->initial_value = initial_value;
|
|
494
|
+
return depreciation;
|
|
495
|
+
}
|
|
496
|
+
double _DEPRECIATE_STRAIGHTLINE(double input, Depreciation* depreciation) {
|
|
497
|
+
// Distribute the input at this time step over the depreciation time in a ring buffer.
|
|
498
|
+
// Return the depreciation amout at the current time.
|
|
499
|
+
double result;
|
|
500
|
+
// Require the buffer size to be positive to protect from buffer overflows.
|
|
501
|
+
if (depreciation->n > 0) {
|
|
502
|
+
// Distribute input from the stream over the depreciation time.
|
|
503
|
+
double distribution = input / depreciation->dtime;
|
|
504
|
+
for (size_t i = 0; i < depreciation->n; i++) {
|
|
505
|
+
size_t pos = (depreciation->data_index + i) % depreciation->n;
|
|
506
|
+
depreciation->data[pos] += distribution;
|
|
507
|
+
}
|
|
508
|
+
result = depreciation->data[depreciation->data_index];
|
|
509
|
+
// Move to the next time step by pushing zero and shifting.
|
|
510
|
+
depreciation->data[depreciation->data_index] = 0;
|
|
511
|
+
depreciation->data_index = (depreciation->data_index + 1) % depreciation->n;
|
|
512
|
+
} else {
|
|
513
|
+
// For a zero deprecitation time, take the value directly from the input.
|
|
514
|
+
result = input;
|
|
515
|
+
}
|
|
516
|
+
return result;
|
|
517
|
+
}
|
package/src/c/vensim.h
CHANGED
|
@@ -84,6 +84,20 @@ typedef struct {
|
|
|
84
84
|
double _DELAY_FIXED(double input, FixedDelay* fixed_delay);
|
|
85
85
|
FixedDelay* __new_fixed_delay(FixedDelay* fixed_delay, double delay_time, double initial_value);
|
|
86
86
|
|
|
87
|
+
//
|
|
88
|
+
// DEPRECIATE STRAIGHTLINE
|
|
89
|
+
//
|
|
90
|
+
typedef struct {
|
|
91
|
+
double* data;
|
|
92
|
+
size_t n;
|
|
93
|
+
size_t data_index;
|
|
94
|
+
double dtime;
|
|
95
|
+
double initial_value;
|
|
96
|
+
} Depreciation;
|
|
97
|
+
|
|
98
|
+
double _DEPRECIATE_STRAIGHTLINE(double input, Depreciation* depreciation);
|
|
99
|
+
Depreciation* __new_depreciation(Depreciation* depreciation, double dtime, double initial_value);
|
|
100
|
+
|
|
87
101
|
#ifdef __cplusplus
|
|
88
102
|
}
|
|
89
103
|
#endif
|