Skip to content

Commit

Permalink
adding socket series command
Browse files Browse the repository at this point in the history
  • Loading branch information
aschellenberg74 committed Feb 6, 2024
1 parent 42a05ef commit 5b8b453
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 5 deletions.
211 changes: 211 additions & 0 deletions SRC/domain/pattern/SocketSeries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/* ****************************************************************** **
** OpenSees - Open System for Earthquake Engineering Simulation **
** Pacific Earthquake Engineering Research Center **
** **
** **
** (C) Copyright 1999, The Regents of the University of California **
** All Rights Reserved. **
** **
** Commercial use of this program without express permission of the **
** University of California, Berkeley, is strictly prohibited. See **
** file 'COPYRIGHT' in main directory for information on usage and **
** redistribution, and for a DISCLAIMER OF ALL WARRANTIES. **
** **
** Developed by: **
** Frank McKenna (fmckenna@ce.berkeley.edu) **
** Gregory L. Fenves (fenves@ce.berkeley.edu) **
** Filip C. Filippou (filippou@ce.berkeley.edu) **
** **
** ****************************************************************** */

// Written: Andreas Schellenberg (andreas.schellenberg@gmail.com)
// Created: 03/23
// Revision: A
//
// Description: This file contains the class definition for SocketSeries.
// SocketSeries is a concrete class. A SocketSeries object provides
// a time series by receiving factors through a socket.

#include <SocketSeries.h>

#include <Vector.h>
#include <Channel.h>
#include <TCP_Socket.h>
#include <UDP_Socket.h>

#include <elementAPI.h>


void* OPS_SocketSeries()
{
// pointer to the time series that will be returned
TimeSeries* theSeries = 0;

if (OPS_GetNumRemainingInputArgs() < 2) {
opserr << "WARNING invalid number of arguments\n";
opserr << "Want: timeSeries Socket tag port "
<< "<-udp> <-dt> <-factor f)\n";
return 0;
}

// get tag
int tag;
int numdata = 1;
if (OPS_GetIntInput(&numdata, &tag) != 0) {
opserr << "WARNING invalid timeSeries Socket tag\n";
return 0;
}

// get the port
int ipPort;
if (OPS_GetIntInput(&numdata, &ipPort) != 0) {
opserr << "WARNING invalid ipPort \n";
opserr << "timeSeries Socket series: " << tag << endln;
return 0;
}

// optional parameters
const char* type;
bool udp = false;
bool checkEndianness = false;
double dt = 1.0;
double cFactor = 1.0;
while (OPS_GetNumRemainingInputArgs() > 0) {
type = OPS_GetString();
// udp socket
if (strcmp(type, "-udp") == 0 ||
strcmp(type, "-UDP") == 0) {
udp = true;
}
// dt
else if (strcmp(type, "-dt") == 0 ||
strcmp(type, "-dT") == 0) {
if (OPS_GetDoubleInput(&numdata, &dt) < 0) {
opserr << "WARNING: invalid -dt value\n";
opserr << "timeSeries Socket series: " << tag << endln;
return 0;
}
}
// factor
else if (strcmp(type, "-factor") == 0) {
if (OPS_GetDoubleInput(&numdata, &cFactor) < 0) {
opserr << "WARNING: invalid -cFactor value\n";
opserr << "timeSeries Socket series: " << tag << endln;
return 0;
}
}
}

// now create the time series
theSeries = new SocketSeries(tag, (unsigned int)ipPort, udp,
checkEndianness, dt, cFactor);
if (theSeries == 0) {
opserr << "WARNING ran out of memory creating time series\n";
opserr << "timeSeries Socket series: " << tag << endln;
return 0;
}

return theSeries;
}


SocketSeries::SocketSeries(int tag,
unsigned int port, bool udp, bool checkEndianness,
double theTimeIncr, double theFactor)
: TimeSeries(tag, TSERIES_TAG_SocketSeries),
socketTimeIncr(theTimeIncr), cFactor(theFactor),
recvSize(1), newFactor(1), theChannel(0),
previousTime(0.0), previousFactor(0.0)
{
// setup the connection
if (udp)
theChannel = new UDP_Socket(port, checkEndianness);
else
theChannel = new TCP_Socket(port, checkEndianness);

if (theChannel != 0) {
opserr << "\nChannel successfully created: "
<< "Waiting for client to send time series values...\n";
} else {
opserr << "SocketSeries::SocketSeries() "
<< "- failed to create channel\n";
exit(-1);
}
if (theChannel->setUpConnection() != 0) {
opserr << "SocketSeries::SocketSeries() "
<< "- failed to setup connection\n";
delete theChannel;
theChannel = 0;
}
}


SocketSeries::SocketSeries()
: TimeSeries(TSERIES_TAG_SocketSeries),
socketTimeIncr(0.0), cFactor(1.0),
recvSize(1), newFactor(1), theChannel(0),
previousTime(0.0), previousFactor(0.0)
{
// does nothing
}


SocketSeries::~SocketSeries()
{
if (theChannel != 0)
delete theChannel;
}


TimeSeries* SocketSeries::getCopy()
{
return new SocketSeries(*this);
}


double SocketSeries::getFactor(double pseudoTime)
{
double factor;

// get new factor if time has advanced by >= dt
if (pseudoTime >= previousTime + socketTimeIncr) {
if (theChannel->recvVector(0, 0, newFactor) < 0) {
opserr << "SocketSeries::getFactor() "
<< "- failed to receive data\n";
return -1;
}
previousTime += socketTimeIncr;
}
// in case time is at zero get the first factor
else if (pseudoTime == 0.0) {
if (theChannel->recvVector(0, 0, newFactor) < 0) {
opserr << "SocketSeries::getFactor() "
<< "- failed to receive data\n";
return -1;
}
}

factor = cFactor*newFactor(0);

return factor;
}


int SocketSeries::sendSelf(int commitTag, Channel& theChannel)
{
return -1;
}


int SocketSeries::recvSelf(int commitTag, Channel& theChannel,
FEM_ObjectBroker& theBroker)
{
return -1;
}


void SocketSeries::Print(OPS_Stream& s, int flag)
{
s << "Socket Time Series" << endln;
s << "\tFactor: " << cFactor << endln;
}
83 changes: 83 additions & 0 deletions SRC/domain/pattern/SocketSeries.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* ****************************************************************** **
** OpenSees - Open System for Earthquake Engineering Simulation **
** Pacific Earthquake Engineering Research Center **
** **
** **
** (C) Copyright 1999, The Regents of the University of California **
** All Rights Reserved. **
** **
** Commercial use of this program without express permission of the **
** University of California, Berkeley, is strictly prohibited. See **
** file 'COPYRIGHT' in main directory for information on usage and **
** redistribution, and for a DISCLAIMER OF ALL WARRANTIES. **
** **
** Developed by: **
** Frank McKenna (fmckenna@ce.berkeley.edu) **
** Gregory L. Fenves (fenves@ce.berkeley.edu) **
** Filip C. Filippou (filippou@ce.berkeley.edu) **
** **
** ****************************************************************** */

#ifndef SocketSeries_h
#define SocketSeries_h

// Written: Andreas Schellenberg (andreas.schellenberg@gmail.com)
// Created: 03/23
// Revision: A
//
// Description: This file contains the class definition for SocketSeries.
// SocketSeries is a concrete class. A SocketSeries object provides
// a time series by receiving factors through a socket.

#include <TimeSeries.h>
#include <Vector.h>

class Channel;


class SocketSeries : public TimeSeries
{
public:
// constructors
SocketSeries(int tag,
unsigned int port,
bool udp = false,
bool checkEndianness = false,
double socketTimeIncr = 1.0,
double cFactor = 1.0);
SocketSeries();

// destructor
~SocketSeries();

TimeSeries* getCopy();

// method to get load factor
double getFactor(double pseudoTime);

// none of the following functions should be invoked on this type of object
double getDuration() { return 0.0; } // dummy function
double getPeakFactor() { return cFactor; } // dummy function
double getTimeIncr(double pseudoTime) { return socketTimeIncr; }

// methods for output
int sendSelf(int commitTag, Channel& theChannel);
int recvSelf(int commitTag, Channel& theChannel,
FEM_ObjectBroker& theBroker);

void Print(OPS_Stream& s, int flag = 0);

protected:

private:
double cFactor;
double socketTimeIncr;

int recvSize;
Vector newFactor;
Channel* theChannel;
double previousTime;
double previousFactor;
};

#endif
14 changes: 10 additions & 4 deletions SRC/domain/pattern/TclSeriesCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
** **
** ****************************************************************** */

// $Revision$
// $Date$
// $URL$

// Written: fmk
// Created: 11/00
// Revision: A
Expand All @@ -43,6 +39,7 @@
#include <PathSeries.h>
#include <PeerMotion.h>
#include <PeerNGAMotion.h>
#include <SocketSeries.h>
#include <string.h>

#ifdef _RELIABILITY
Expand Down Expand Up @@ -76,6 +73,7 @@ extern void *OPS_RectangularSeries(void);
extern void *OPS_PulseSeries(void);
extern void *OPS_PeerMotion(void);
extern void *OPS_PeerNGAMotion(void);
extern void* OPS_SocketSeries(void);

#include <elementAPI.h>
extern "C" int OPS_ResetInputNoBuilder(ClientData clientData, Tcl_Interp * interp, int cArg, int mArg, TCL_Char * *argv, Domain * domain);
Expand Down Expand Up @@ -141,6 +139,14 @@ TclTimeSeriesCommand(ClientData clientData,

}

else if ((strcmp(argv[0], "Socket") == 0) || (strcmp(argv[0], "SocketSeries") == 0)) {

void* theResult = OPS_SocketSeries();
if (theResult != 0)
theSeries = (TimeSeries*)theResult;

}

else if ((strcmp(argv[0],"Series") == 0) ||
(strcmp(argv[0],"Path") == 0)) {

Expand Down
5 changes: 4 additions & 1 deletion SRC/interpreter/OpenSeesTimeSeriesCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void* OPS_TriangleSeries();
void* OPS_TrigSeries();
void* OPS_RectangularSeries();
void* OPS_PulseSeries();
void* OPS_SocketSeries();

namespace {

Expand Down Expand Up @@ -223,7 +224,9 @@ namespace {
functionMap.insert(std::make_pair("TriangleSeries", &OPS_TriangleSeries));
functionMap.insert(std::make_pair("Path", &OPS_PathSeries));
functionMap.insert(std::make_pair("Series", &OPS_PathSeries));

functionMap.insert(std::make_pair("Socket", &OPS_SocketSeries));
functionMap.insert(std::make_pair("SocketSeries", &OPS_SocketSeries));

return 0;
}
}
Expand Down

0 comments on commit 5b8b453

Please sign in to comment.