diff --git a/CMakeLists.txt b/CMakeLists.txt index f3955a6fec16fd30689c46259cc97606640b3084..c6b891242577f698c71ac668480b81a4f6e7e95f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ set(API_HEADERS_CPP ${API_HEADERS_CPP_DIR}/paralellseriel.h ${API_HEADERS_CPP_DIR}/basicmath.h ${API_HEADERS_CPP_DIR}/eistype.h + ${API_HEADERS_CPP_DIR}/normalize.h ) set(API_HEADERS_C_DIR eisgenerator/c/) diff --git a/eisgenerator/eistype.h b/eisgenerator/eistype.h new file mode 100644 index 0000000000000000000000000000000000000000..43c6e3cdf838abe0ad77493a35e5a6c223f9b0c0 --- /dev/null +++ b/eisgenerator/eistype.h @@ -0,0 +1,45 @@ +#pragma once +#include <complex> + +typedef double fvalue; + +namespace eis +{ +struct DataPoint +{ + std::complex<fvalue> im; + fvalue omega; +}; + +struct Range +{ + fvalue start; + fvalue end; + size_t count; + bool log = false; + + fvalue stepSize() const + { + return (end-start)/count; + } + Range operator*(fvalue in) const + { + return Range(start*in, end*in, count, log); + } + Range operator/(fvalue in) const + { + return operator*(static_cast<fvalue>(1.0)/in); + } + Range operator*(int in) const + { + return operator*(static_cast<fvalue>(in)); + } + Range operator/(int in) const + { + return operator*(static_cast<fvalue>(1.0)/in); + } + Range(fvalue startI, fvalue endI, size_t countI, bool logI = false): start(startI), end(endI), count(countI), log(logI){} + Range() = default; +}; + +} diff --git a/eisgenerator/log.h b/eisgenerator/log.h new file mode 100644 index 0000000000000000000000000000000000000000..e07cb23e8ad7c9b935d30744c024fb60c509e0c3 --- /dev/null +++ b/eisgenerator/log.h @@ -0,0 +1,66 @@ +/** +* eisgenerator +* Copyright (C) 2021 Carl Klemm +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* version 3 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the +* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +* Boston, MA 02110-1301, USA. +*/ + +#pragma once +#include <iostream> +#include <string> + +namespace eis +{ + +class Log +{ +public: + + enum Level + { + DEBUG, + INFO, + WARN, + ERROR + }; + +private: + bool opened = false; + Level msglevel = DEBUG; + + std::string getLabel(Level level); + +public: + + static bool headers; + static Level level; + static bool endline; + + Log() {} + Log(Level type); + ~Log(); + + template<class T> Log &operator<<(const T &msg) + { + if(msglevel >= level) + { + std::cout<<msg; + opened = true; + } + return *this; + } +}; + +} diff --git a/eisgenerator/normalize.h b/eisgenerator/normalize.h index 21409e83a943c369d133b5b1d22f6a71b281f6f7..1d9688825c57970c5b9327ccfe0c899581670815 100644 --- a/eisgenerator/normalize.h +++ b/eisgenerator/normalize.h @@ -11,5 +11,6 @@ namespace eis void normalize(std::vector<eis::DataPoint>& data); std::vector<eis::DataPoint> reduceRegion(const std::vector<eis::DataPoint>& data, fvalue gradThreshFactor = 0.01); void eraseSingularites(std::vector<eis::DataPoint>& data); +std::vector<eis::DataPoint> rescale(const std::vector<eis::DataPoint>& data, size_t outputSize); } diff --git a/log.cpp b/log.cpp new file mode 100644 index 0000000000000000000000000000000000000000..307929146c813cc8543443a807497c3fe2b66b5e --- /dev/null +++ b/log.cpp @@ -0,0 +1,66 @@ +/** +* Lubricant Detecter +* Copyright (C) 2021 Carl Klemm +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* version 3 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the +* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +* Boston, MA 02110-1301, USA. +*/ + +#include "log.h" + +using namespace eis; + +Log::Log(Level type) +{ + msglevel = type; + if(headers) + { + operator << ("["+getLabel(type)+"] "); + } +} + +Log::~Log() +{ + if(opened && endline) + { + std::cout<<'\n'; + } + opened = false; +} + + +std::string Log::getLabel(Level level) +{ + std::string label; + switch(level) + { + case DEBUG: + label = "DEBUG"; + break; + case INFO: + label = "INFO "; + break; + case WARN: + label = "WARN "; + break; + case ERROR: + label = "ERROR"; + break; + } + return label; +} + +bool Log::headers = false; +Log::Level Log::level = WARN; +bool Log::endline = true; diff --git a/normalize.cpp b/normalize.cpp index 849433971f4415cbe6bd343f0f77cb03be5cc6fb..af6cc1daf5b77e4a9353aa571c648f741d404b17 100644 --- a/normalize.cpp +++ b/normalize.cpp @@ -1,4 +1,5 @@ #include "normalize.h" +#include <assert.h> #include <cmath> #include <complex> #include <limits> @@ -88,3 +89,18 @@ std::vector<eis::DataPoint> eis::reduceRegion(const std::vector<eis::DataPoint>& return data; } + +std::vector<eis::DataPoint> eis::rescale(const std::vector<eis::DataPoint>& data, size_t outputSize) +{ + std::vector<eis::DataPoint> output(outputSize); + for(size_t i = 0; i < output.size(); ++i) + { + double position = static_cast<double>(i) / (output.size()-1); + double sourcePosF = (data.size()-1)*position; + size_t sourcePos = (data.size()-1)*position; + double frac = sourcePosF - sourcePos; + output[i].im = data[sourcePos].im*(1-frac) + data[sourcePos+1].im*frac; + output[i].omega = data[sourcePos].omega*(1-frac) + data[sourcePos+1].omega*frac; + } + return output; +} diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..974608479bd7f92d061782faa0718f6d177313e9 --- /dev/null +++ b/test.cpp @@ -0,0 +1,136 @@ +#include <iostream> +#include <complex> +#include <chrono> + +#include "model.h" +#include "log.h" +#include "normalize.h" + +void printComponants(eis::Model& model) +{ + eis::Log(eis::Log::DEBUG)<<"Compnants:"; + for(eis::Componant* componant : model.getFlatComponants()) + { + eis::Log(eis::Log::DEBUG)<<eis::Componant::getComponantChar(componant)<<"{"; + for(size_t i = 0; i < componant->paramCount(); ++i) + { + eis::Log(eis::Log::DEBUG)<<componant->getParam()[i]; + if(i != componant->paramCount()-1) + eis::Log(eis::Log::DEBUG)<<", "; + } + eis::Log(eis::Log::DEBUG)<<"}"; + } +} + +void runSingle() +{ + eis::Log(eis::Log::INFO)<<__func__; + std::string modelStr("w{1e3}p{1e-7, 0.7}"); + + std::vector<eis::DataPoint> results; + + eis::Model model(modelStr); + + printComponants(model); + + eis::Range omega(0, 1e6, 50); + + auto start = std::chrono::high_resolution_clock::now(); + results = model.executeSweep(omega); + auto end = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start); + + for(const eis::DataPoint& res : results) + eis::Log(eis::Log::INFO)<<"omega: "<<res.omega<<" real = "<<res.im.real()<<" im = "<<res.im.imag(); + + eis::Log(eis::Log::INFO)<<"time taken: "<<duration.count()<<" us"; +} + +void sweepCb(std::vector<eis::DataPoint>& data, const std::vector<fvalue>& parameters) +{ + static size_t i = 0; + ++i; + if((i & 0x3FF) == 0) + std::cout<<'.'<<std::flush; +} + +void runSweep() +{ + eis::Log(eis::Log::INFO)<<__func__; + std::string modelStr("w{20e3}p{1e-7, 0.9}"); + + eis::Model model(modelStr); + + std::vector<eis::Range> parameters; + parameters.push_back(eis::Range(1e3, 50e3, 100)); + parameters.push_back(eis::Range(1e-7, 20e-7, 100)); + parameters.push_back(eis::Range(0.7, 1.2, 100)); + + eis::Range omega(0, 1e6, 25); + + auto start = std::chrono::high_resolution_clock::now(); + model.executeParamSweep(parameters, omega, &sweepCb); + auto end = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); + std::cout<<std::endl; + + eis::Log(eis::Log::INFO)<<"time taken: "<<duration.count()<<" ms"; +} + +void runSweepByIndex() +{ + eis::Log(eis::Log::INFO)<<__func__; + std::string modelStr("w{20e3}p{1e-7, 0.9}"); + + eis::Model model(modelStr); + + std::vector<eis::Range> parameters; + parameters.push_back(eis::Range(1e3, 50e3, 100)); + parameters.push_back(eis::Range(1e-7, 20e-7, 100)); + parameters.push_back(eis::Range(0.7, 1.2, 100)); + eis::Range omega(0, 1e6, 25); + + auto start = std::chrono::high_resolution_clock::now(); + std::vector<eis::DataPoint> results = model.executeParamByIndex(parameters, omega, 0); + auto end = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); + + printComponants(model); + + for(const eis::DataPoint& res : results) + eis::Log(eis::Log::INFO)<<"omega: "<<res.omega<<" real = "<<res.im.real()<<" im = "<<res.im.imag(); + eis::Log(eis::Log::INFO)<<"time taken: "<<duration.count()<<" ms"; +} + +void runRescale() +{ + std::vector<eis::DataPoint> data; + for(size_t i = 0; i < 10; ++i) + { + eis::DataPoint point; + point.im = std::complex<fvalue>(i,i); + point.omega = (i+1)*3; + data.push_back(point); + } + + std::cout<<"original: "; + for(const eis::DataPoint& res : data) + std::cout<<res.omega<<','<<res.im.real()<<','<<res.im.imag()<<'\n'; + + data = eis::rescale(data, 5); + + std::cout<<"rescaled: "; + for(const eis::DataPoint& res : data) + std::cout<<res.omega<<','<<res.im.real()<<','<<res.im.imag()<<'\n'; +} + +int main(int argc, char** argv) +{ + eis::Log::headers = true; + eis::Log::level = eis::Log::INFO; + runSingle(); + runSweepByIndex(); + runSweep(); + runRescale(); + return 0; +} diff --git a/tokenize.h b/tokenize.h new file mode 100644 index 0000000000000000000000000000000000000000..5f36a46bfc73ef851a72abb638c739f8c27839cf --- /dev/null +++ b/tokenize.h @@ -0,0 +1,6 @@ +#pragma once +#include <string> +#include <vector> +#include <sstream> + +std::vector<std::string> tokenize(const std::string& str, const char delim = ' ', const char ignBracketStart = '\0', const char ignBracketEnd = '\0');