Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / coutvector.cc @ fbe00e73

History | View | Annotate | Download (6.26 KB)

1
//========================================================================
2
//  COUTVECTOR.CC - part of
3
//
4
//                 OMNeT++/OMNEST
5
//              Discrete System Simulation in C++
6
//
7
//   Member functions of
8
//    cOutVector         : represents a vector in the output file
9
//
10
//   Original version:  Gabor Lencse
11
//   Rewrite, bugfixes: Andras Varga
12
//========================================================================
13

    
14
/*--------------------------------------------------------------*
15
  Copyright (C) 1992-2008 Andras Varga
16
  Copyright (C) 2006-2008 OpenSim Ltd.
17

18
  This file is distributed WITHOUT ANY WARRANTY. See the file
19
  `license' for details on this and other legal matters.
20
*--------------------------------------------------------------*/
21

    
22
#include <string.h>   // strlen
23
#include "globals.h"
24
#include "coutvector.h"
25
#include "cmodule.h"
26
#include "csimulation.h"
27
#include "cenvir.h"
28
#include "cexception.h"
29
#include "cenum.h"
30

    
31
#ifdef WITH_PARSIM
32
#include "ccommbuffer.h"
33
#endif
34

    
35
USING_NAMESPACE
36

    
37
Register_Class(cOutVector);
38

    
39

    
40
Register_Enum(cOutVector::Type, (cOutVector::TYPE_INT, cOutVector::TYPE_DOUBLE, cOutVector::TYPE_ENUM) );
41
Register_Enum(cOutVector::InterpolationMode, (cOutVector::NONE, cOutVector::SAMPLE_HOLD, cOutVector::BACKWARD_SAMPLE_HOLD, cOutVector::LINEAR));
42

    
43

    
44
cOutVector::cOutVector(const char *name) : cNoncopyableOwnedObject(name)
45
{
46
    setFlag(FL_ENABLED, true);
47
    handle = NULL;
48
    num_received = 0;
49
    num_stored = 0;
50
    record_in_inspector = NULL;
51
    last_t = 0;
52

    
53
    // register early if possible (only required by Akaroa)
54
    if (name) {
55
        handle = ev.registerOutputVector(simulation.getContext()->getFullPath().c_str(), name);
56
        ASSERT(handle!=NULL);
57
    }
58
}
59

    
60
cOutVector::~cOutVector()
61
{
62
    if (handle)
63
        ev.deregisterOutputVector(handle);
64
}
65

    
66
void cOutVector::setName(const char *nam)
67
{
68
    if (handle)
69
        throw cRuntimeError(this,"setName(): changing name of an output vector after record() calls is not allowed");
70

    
71
    cOwnedObject::setName(nam);
72

    
73
    // register early (only needed for Akaroa...)
74
    if (nam) {
75
        handle = ev.registerOutputVector(simulation.getContext()->getFullPath().c_str(), getName());
76
        ASSERT(handle!=NULL);
77
    }
78
}
79

    
80
std::string cOutVector::info() const
81
{
82
    if (!handle)
83
        return std::string("(no values recorded yet)");
84
    std::stringstream out;
85
    out << "received " << num_received << " values, stored " << num_stored;
86
    return out.str();
87
}
88

    
89
void cOutVector::parsimPack(cCommBuffer *buffer)
90
{
91
    throw cRuntimeError(this, "parsimPack() not supported");
92
}
93

    
94
void cOutVector::parsimUnpack(cCommBuffer *buffer)
95
{
96
    throw cRuntimeError(this, "parsimUnpack(): not supported");
97
}
98

    
99
void cOutVector::setUnit(const char *unit)
100
{
101
    if (!handle)
102
        throw cRuntimeError(this,"setUnit(): set the object name first, using setName()");
103
    ev.setVectorAttribute(handle, "unit", unit);
104
}
105

    
106
void cOutVector::setEnum(const char *registeredEnumName)
107
{
108
    cEnum *enumDecl = cEnum::find(registeredEnumName);
109
    if (!enumDecl)
110
        throw cRuntimeError(this, "setEnum(): enum `%s' not found -- is it declared with Register_Enum()?", registeredEnumName);
111
    setEnum(enumDecl);
112
}
113

    
114
void cOutVector::setEnum(cEnum *enumDecl)
115
{
116
    if (!handle)
117
        throw cRuntimeError(this,"setEnum(): set the object name first, using setName()");
118
    ev.setVectorAttribute(handle, "enumname", enumDecl->getName());
119
    ev.setVectorAttribute(handle, "enum", enumDecl->str().c_str());
120
}
121

    
122
void cOutVector::setType(Type type)
123
{
124
    if (!handle)
125
        throw cRuntimeError(this,"setType(): set the object name first, using setName()");
126

    
127
    const char *typeString=NULL;
128
    switch (type)
129
    {
130
        case TYPE_INT:    typeString = "int"; break;
131
        case TYPE_DOUBLE: typeString = "double"; break;
132
        case TYPE_ENUM:   typeString = "enum"; break;
133
        //Note: no "default:" so that compiler can warn of incomplete coverage
134
    }
135
    if (!typeString)
136
        throw cRuntimeError(this, "setType(): invalid type %d", type);
137
    ev.setVectorAttribute(handle, "type", typeString);
138
}
139

    
140
void cOutVector::setInterpolationMode(InterpolationMode mode)
141
{
142
    if (!handle)
143
        throw cRuntimeError(this,"setInterpolationMode(): set the object name first, using setName()");
144

    
145
    const char *modeString=NULL;
146
    switch (mode)
147
    {
148
        case NONE:                 modeString = "none"; break;
149
        case SAMPLE_HOLD:          modeString = "sample-hold"; break;
150
        case BACKWARD_SAMPLE_HOLD: modeString = "backward-sample-hold"; break;
151
        case LINEAR:               modeString = "linear"; break;
152
        //Note: no "default:" so that compiler can warn of incomplete coverage
153
    }
154
    if (!modeString)
155
        throw cRuntimeError(this, "setInterpolationMode(): invalid interpolation mode %d", mode);
156
    ev.setVectorAttribute(handle, "interpolationmode", modeString);
157
}
158

    
159
void cOutVector::setMin(double minValue)
160
{
161
    if (!handle)
162
        throw cRuntimeError(this,"setMin(): set the object name first, using setName()");
163

    
164
    char buf[32];
165
    sprintf(buf, "%g", minValue);
166
    ev.setVectorAttribute(handle, "min", buf);
167
}
168

    
169
void cOutVector::setMax(double maxValue)
170
{
171
    if (!handle)
172
        throw cRuntimeError(this,"setMax(): set the object name first, using setName()");
173

    
174
    char buf[32];
175
    sprintf(buf, "%g", maxValue);
176
    ev.setVectorAttribute(handle, "max", buf);
177
}
178

    
179

    
180
bool cOutVector::record(double value)
181
{
182
    return recordWithTimestamp(simulation.getSimTime(), value);
183
}
184

    
185
bool cOutVector::recordWithTimestamp(simtime_t t, double value)
186
{
187
    // check timestamp
188
    if (t<last_t)
189
        throw cRuntimeError(this,"Cannot record data with an earlier timestamp (t=%s) "
190
                                 "than the previously recorded value", SIMTIME_STR(t));
191
    last_t = t;
192

    
193
    num_received++;
194

    
195
    // pass data to inspector
196
    if (record_in_inspector)
197
        record_in_inspector(data_for_inspector,t,value,0.0);
198

    
199
    if (!isEnabled())
200
        return false;
201

    
202
    if (!getRecordDuringWarmupPeriod() && t < simulation.getWarmupPeriod())
203
        return false;
204

    
205
    // initialize if not yet done
206
    if (!handle)
207
        handle = ev.registerOutputVector(simulation.getContext()->getFullPath().c_str(), getName());
208

    
209
    // pass data to envir for storage
210
    bool stored = ev.recordInOutputVector(handle, t, value);
211
    if (stored)
212
        num_stored++;
213
    return stored;
214
}
215

    
216