From 8907000eafcb50fc7ec2dbfc9d4eea43f250ebee Mon Sep 17 00:00:00 2001
From: Carl Klemm <carl@uvos.xyz>
Date: Tue, 10 May 2022 08:21:08 +0200
Subject: [PATCH] add missing files

---
 CMakeLists.txt           |   1 +
 eisgenerator/eistype.h   |  45 +++++++++++++
 eisgenerator/log.h       |  66 +++++++++++++++++++
 eisgenerator/normalize.h |   1 +
 log.cpp                  |  66 +++++++++++++++++++
 normalize.cpp            |  16 +++++
 test.cpp                 | 136 +++++++++++++++++++++++++++++++++++++++
 tokenize.h               |   6 ++
 8 files changed, 337 insertions(+)
 create mode 100644 eisgenerator/eistype.h
 create mode 100644 eisgenerator/log.h
 create mode 100644 log.cpp
 create mode 100644 test.cpp
 create mode 100644 tokenize.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f3955a6..c6b8912 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 0000000..43c6e3c
--- /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 0000000..e07cb23
--- /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 21409e8..1d96888 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 0000000..3079291
--- /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 8494339..af6cc1d 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 0000000..9746084
--- /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 0000000..5f36a46
--- /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');
-- 
GitLab