forked from tslarkin/Mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMWeather.m
83 lines (70 loc) · 1.82 KB
/
HMWeather.m
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
//
// HMWeather.m
// Hernix
//
// Created by Timothy Larkin on 1/11/15.
//
//
#import "HMWeather.h"
#import "HMOutput.h"
@implementation HMWeather
-(NSArray*)data
{
return data;
}
-(void)setData:(NSArray*)array
{
[data release];
data = array;
[data retain];
}
-(void)initialize
{
[super initialize];
baseIndex = 1;
eoa = data.count;
ncols = ((NSArray*)data[0]).count;
}
- (void)reportEOA
{
NSValue *v = [[self finalInputValues] objectAtIndex:0];
Value *val = (Value*)[v pointerValue];
char *path = pathValue(val);
NSException *e = [NSException exceptionWithName:@"Simulation forced to end"
reason:[NSString stringWithFormat:@"End of file reached in file %s", path]
userInfo:nil];
[e raise];
}
-(NSTimeInterval)findLatestStartTime:(NSTimeInterval)start
{
NSTimeInterval zero = ((NSNumber*)data[1][0]).doubleValue;
return zero;
}
-(void)updateRates
{
extern HMOutput *gTime;
NSTimeInterval currentTime = floatValue([gTime value]);
NSUInteger top = baseIndex;
while (top < eoa && ((NSNumber*)data[top][0]).floatValue < currentTime) {
top++;
}
if (top == eoa) {
[self reportEOA];
}
if (baseIndex < top) {
baseIndex = top - 1;
}
float t1 = ((NSNumber*)data[baseIndex][0]).floatValue, t2 = ((NSNumber*)data[top][0]).floatValue;
float delta = 0.0;
if (t2 > t1) {
delta = (currentTime - t1) / (t2 - t1);
}
for (int i = 1; i < ncols; i++) {
float z1 = ((NSNumber*)data[baseIndex][i]).floatValue;
float diff = ((NSNumber*)data[top][i]).floatValue - z1;
HMOutput *output = outputs[i - 1];
float z = z1 + diff * delta;
setFloatValue([output value], z);
}
}
@end