204 lines
5.6 KiB
C++
204 lines
5.6 KiB
C++
#include <string>
|
|
#include <cassert>
|
|
#include <fstream>
|
|
#include <algorithm>
|
|
#include <numeric>
|
|
|
|
#include "MBDynSystem.h"
|
|
#include "CREATE.h"
|
|
#include "FullColumn.h"
|
|
#include "MBDynInitialValue.h"
|
|
#include "MBDynData.h"
|
|
#include "MBDynControlData.h"
|
|
#include "MBDynLabels.h"
|
|
#include "MBDynVariables.h"
|
|
#include "MBDynReferences.h"
|
|
#include "MBDynNodes.h"
|
|
#include "MBDynElements.h"
|
|
|
|
using namespace MbD;
|
|
|
|
void MbD::MBDynSystem::runFile(const char* filename)
|
|
{
|
|
std::ifstream stream(filename);
|
|
std::string line;
|
|
std::vector<std::string> lines;
|
|
while (std::getline(stream, line)) {
|
|
lines.push_back(line);
|
|
}
|
|
eraseComments(lines);
|
|
auto statements = collectStatements(lines);
|
|
|
|
auto system = std::make_shared<MBDynSystem>();
|
|
system->setFilename(filename);
|
|
system->parseMBDyn(statements);
|
|
system->runKINEMATIC();
|
|
}
|
|
|
|
void MbD::MBDynSystem::parseMBDyn(std::vector<std::string>& lines)
|
|
{
|
|
readDataBlock(lines);
|
|
readInitialValueBlock(lines);
|
|
readControlDataBlock(lines);
|
|
readLabels(lines);
|
|
readVariables(lines);
|
|
readReferences(lines);
|
|
readNodesBlock(lines);
|
|
readElementsBlock(lines);
|
|
|
|
}
|
|
|
|
std::shared_ptr<MBDynVariables> MbD::MBDynSystem::mbdynVariables()
|
|
{
|
|
return variables;
|
|
}
|
|
|
|
std::shared_ptr<MBDynReferences> MbD::MBDynSystem::mbdynReferences()
|
|
{
|
|
return references;
|
|
}
|
|
|
|
void MbD::MBDynSystem::runKINEMATIC()
|
|
{
|
|
assert(false);
|
|
}
|
|
|
|
void MbD::MBDynSystem::setFilename(std::string str)
|
|
{
|
|
filename = str;
|
|
}
|
|
|
|
void MbD::MBDynSystem::readDataBlock(std::vector<std::string>& lines)
|
|
{
|
|
std::vector<std::string> tokens{"begin:", "data;"};
|
|
auto beginit = findLineWith(lines, tokens);
|
|
std::vector<std::string> tokens1{"end:", "data;"};
|
|
auto endit = findLineWith(lines, tokens1);
|
|
std::vector<std::string> blocklines = { beginit, endit + 1 };
|
|
dataBlk = std::make_shared<MBDynData>();
|
|
dataBlk->owner = this;
|
|
dataBlk->parseMBDyn(blocklines);
|
|
lines.erase(beginit, endit + 1);
|
|
}
|
|
|
|
void MbD::MBDynSystem::readInitialValueBlock(std::vector<std::string>& lines)
|
|
{
|
|
std::vector<std::string> tokens{"begin:", "initial", "value"};
|
|
auto beginit = findLineWith(lines, tokens);
|
|
std::vector<std::string> tokens1{"end:", "initial", "value"};
|
|
auto endit = findLineWith(lines, tokens1);
|
|
std::vector<std::string> blocklines = { beginit, endit + 1 };
|
|
initialValueBlk = std::make_shared<MBDynInitialValue>();
|
|
initialValueBlk->owner = this;
|
|
initialValueBlk->parseMBDyn(blocklines);
|
|
lines.erase(beginit, endit + 1);
|
|
}
|
|
|
|
void MbD::MBDynSystem::readControlDataBlock(std::vector<std::string>& lines)
|
|
{
|
|
std::vector<std::string> tokens{"begin:", "control", "data"};
|
|
auto beginit = findLineWith(lines, tokens);
|
|
std::vector<std::string> tokens1{"end:", "control", "data"};
|
|
auto endit = findLineWith(lines, tokens1);
|
|
std::vector<std::string> blocklines = { beginit, endit + 1 };
|
|
controlDataBlk = std::make_shared<MBDynControlData>();
|
|
controlDataBlk->owner = this;
|
|
controlDataBlk->parseMBDyn(blocklines);
|
|
lines.erase(beginit, endit + 1);
|
|
}
|
|
|
|
void MbD::MBDynSystem::readLabels(std::vector<std::string>& lines)
|
|
{
|
|
labels = std::make_shared<MBDynLabels>();
|
|
labels->owner = this;
|
|
labels->parseMBDyn(lines);
|
|
}
|
|
|
|
void MbD::MBDynSystem::readVariables(std::vector<std::string>& lines)
|
|
{
|
|
variables = std::make_shared<MBDynVariables>();
|
|
variables->owner = this;
|
|
variables->parseMBDyn(lines);
|
|
}
|
|
|
|
void MbD::MBDynSystem::readReferences(std::vector<std::string>& lines)
|
|
{
|
|
references = std::make_shared<MBDynReferences>();
|
|
references->owner = this;
|
|
references->parseMBDyn(lines);
|
|
}
|
|
|
|
void MbD::MBDynSystem::readNodesBlock(std::vector<std::string>& lines)
|
|
{
|
|
std::vector<std::string> tokens{ "begin:", "nodes;" };
|
|
auto beginit = findLineWith(lines, tokens);
|
|
std::vector<std::string> tokens1{ "end:", "nodes;" };
|
|
auto endit = findLineWith(lines, tokens1);
|
|
std::vector<std::string> blocklines = { beginit, endit + 1 };
|
|
nodesBlk = std::make_shared<MBDynNodes>();
|
|
nodesBlk->owner = this;
|
|
nodesBlk->parseMBDyn(blocklines);
|
|
lines.erase(beginit, endit + 1);
|
|
}
|
|
|
|
void MbD::MBDynSystem::readElementsBlock(std::vector<std::string>& lines)
|
|
{
|
|
std::vector<std::string> tokens{ "begin:", "elements;" };
|
|
auto beginit = findLineWith(lines, tokens);
|
|
std::vector<std::string> tokens1{ "end:", "elements;" };
|
|
auto endit = findLineWith(lines, tokens1);
|
|
std::vector<std::string> blocklines = { beginit, endit + 1 };
|
|
elementsBlk = std::make_shared<MBDynElements>();
|
|
elementsBlk->owner = this;
|
|
elementsBlk->parseMBDyn(blocklines);
|
|
lines.erase(beginit, endit + 1);
|
|
}
|
|
|
|
void MbD::MBDynSystem::eraseComments(std::vector<std::string>& lines)
|
|
{
|
|
for (int i = 0; i < lines.size(); i++)
|
|
{
|
|
auto line = lines[i];
|
|
auto it = line.find('#');
|
|
if (it != std::string::npos) {
|
|
lines[i] = line.substr(0, it);
|
|
}
|
|
}
|
|
for (int i = lines.size() - 1; i >= 0; i--) {
|
|
auto& line = lines[i];
|
|
auto it = std::find_if(line.begin(), line.end(), [](unsigned char ch) { return !std::isspace(ch); });
|
|
if (it == line.end()) lines.erase(lines.begin() + i);
|
|
}
|
|
}
|
|
|
|
std::vector<std::string> MbD::MBDynSystem::collectStatements(std::vector<std::string>& lines)
|
|
{
|
|
auto statements = std::vector<std::string>();
|
|
while (!lines.empty()) {
|
|
std::stringstream ss;
|
|
while (!lines.empty()) {
|
|
auto line = lines[0];
|
|
lines.erase(lines.begin());
|
|
auto i = line.find(';');
|
|
if (i != std::string::npos) {
|
|
ss << line.substr(0, i + 1);
|
|
if (line.size() > i + 1) {
|
|
auto remainder = line.substr(i + 1);
|
|
auto it = std::find_if(remainder.begin(), remainder.end(), [](unsigned char ch) { return !std::isspace(ch); });
|
|
if (it != remainder.end()) lines.insert(lines.begin(), remainder);
|
|
}
|
|
break;
|
|
}
|
|
else {
|
|
ss << line;
|
|
}
|
|
}
|
|
statements.push_back(ss.str());
|
|
}
|
|
return statements;
|
|
}
|
|
|
|
void MbD::MBDynSystem::initialize()
|
|
{
|
|
}
|