From 130491b1f12af1c9d672abc2f9a83f2dcb27bbf8 Mon Sep 17 00:00:00 2001
From: Carl Philipp Klemm <philipp@uvos.xyz>
Date: Fri, 13 May 2022 14:22:44 +0200
Subject: [PATCH] add inductor model primitive

---
 CMakeLists.txt          |  5 ++++
 eisgenerator/inductor.h | 24 ++++++++++++++++
 inductor.cpp            | 62 +++++++++++++++++++++++++++++++++++++++++
 model.cpp               |  6 ++++
 4 files changed, 97 insertions(+)
 create mode 100644 eisgenerator/inductor.h
 create mode 100644 inductor.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2ada957..4f33bfe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,6 +5,7 @@ project(eisgenerator)
 set(SRC_FILES
 	componant.cpp
 	cap.cpp
+	inductor.cpp
 	resistor.cpp
 	constantphase.cpp
 	warburg.cpp
@@ -14,6 +15,9 @@ set(SRC_FILES
 	log.cpp
 	normalize.cpp
 	basicmath.cpp
+	log.cpp
+	normalize.cpp
+	basicmath.cpp
 )
 
 set(API_HEADERS_CPP_DIR eisgenerator/)
@@ -30,6 +34,7 @@ set(API_HEADERS_CPP
 	${API_HEADERS_CPP_DIR}/basicmath.h
 	${API_HEADERS_CPP_DIR}/eistype.h
 	${API_HEADERS_CPP_DIR}/normalize.h
+	${API_HEADERS_CPP_DIR}/inductor.h
 )
 
 set(API_HEADERS_C_DIR eisgenerator/c/)
diff --git a/eisgenerator/inductor.h b/eisgenerator/inductor.h
new file mode 100644
index 0000000..8edba47
--- /dev/null
+++ b/eisgenerator/inductor.h
@@ -0,0 +1,24 @@
+#pragma once
+#include <complex>
+#include <string>
+#include <vector>
+#include "componant.h"
+
+namespace eis
+{
+
+class Inductor: public Componant
+{
+private:
+	fvalue _L;
+public:
+	Inductor(std::string paramStr);
+	Inductor(fvalue L = 1e-6);
+	virtual std::complex<fvalue> execute(fvalue omega) override;
+	virtual std::vector<fvalue> getParam() override;
+	virtual void setParam(const std::vector<fvalue>& param) override;
+	virtual size_t paramCount() override;
+	virtual ~Inductor() = default;
+};
+
+}
diff --git a/inductor.cpp b/inductor.cpp
new file mode 100644
index 0000000..1c06219
--- /dev/null
+++ b/inductor.cpp
@@ -0,0 +1,62 @@
+#include "inductor.h"
+#include "tokenize.h"
+#include <cstdlib>
+#include <math.h>
+
+#include "log.h"
+
+using namespace eis;
+
+Inductor::Inductor(fvalue L): _L(L)
+{
+
+}
+
+Inductor::Inductor(std::string paramStr)
+{
+	std::vector<std::string> tokens = tokenize(paramStr, ',');
+	if(tokens.empty())
+	{
+		Log(Log::WARN)<<"to few parameters in "<<__func__<<" parameter string: "<<paramStr<<'\n';
+		_L = 1e-6;
+		return;
+	}
+	else
+	{
+		try
+		{
+			_L = std::stod(tokens[0]);
+		}
+		catch(const std::invalid_argument& ia)
+		{
+			Log(Log::WARN)<<"Warning: cant parse parameter in "<<__func__<<" parameter: "<<tokens[0]<<'\n';
+			_L = 1e3;
+		}
+	}
+}
+
+std::complex<fvalue> Inductor::execute(fvalue omega)
+{
+	return std::complex<fvalue>(0, _L*omega);
+}
+
+std::vector<fvalue> Inductor::getParam()
+{
+	return std::vector<fvalue>({_L});
+}
+
+void Inductor::setParam(const std::vector<fvalue>& param)
+{
+	if(param.empty())
+	{
+		Log(Log::WARN)<<"invalid parameter list sent to "<<__func__<<'\n';
+		return;
+	}
+
+	_L = param[0];
+}
+
+size_t Inductor::paramCount()
+{
+	return 1;
+}
diff --git a/model.cpp b/model.cpp
index c9a46cf..4a3178c 100644
--- a/model.cpp
+++ b/model.cpp
@@ -4,6 +4,7 @@
 #include "tokenize.h"
 #include "cap.h"
 #include "resistor.h"
+#include "inductor.h"
 #include "constantphase.h"
 #include "warburg.h"
 #include "paralellseriel.h"
@@ -116,6 +117,11 @@ Componant *Model::processBracket(std::string& str)
 					componants.push_back(new Resistor(getParamStr(nodeStr, i)));
 					i = opposingBraket(nodeStr, i, '}');
 					break;
+				case 'l':
+				case 'L':
+					componants.push_back(new Inductor(getParamStr(nodeStr, i)));
+					i = opposingBraket(nodeStr, i, '}');
+					break;
 				case 'p':
 				case 'P':
 					componants.push_back(new Cpe(getParamStr(nodeStr, i)));
-- 
GitLab