eisgenerator 2.1.x
model.h
1//SPDX-License-Identifier: LGPL-3.0-or-later
2/* * eisgenerator - a shared library and application to generate EIS spectra
3 * Copyright (C) 2022-2024 Carl Philipp Klemm <carl@uvos.xyz>
4 *
5 * This file is part of eisgenerator.
6 *
7 * eisgenerator is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * eisgenerator is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with eisgenerator. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <complex>
24#include <cstddef>
25#include <string>
26#include <vector>
27#include <functional>
28#include <kisstype/type.h>
29
30#include "componant/componant.h"
31#include "symmetry.h"
32
33namespace eis
34{
35
36struct CompiledObject;
37
43
47class Model
48{
49private:
50 Componant *processBrackets(std::string& str, size_t& bracketCounter, size_t paramSweepCount, bool defaultToRange);
51 Componant *processBracket(std::string& str, size_t paramSweepCount, bool defaultToRange);
52 std::string getParamStr(const std::string& str, size_t index);
53 static size_t paramSkipIndex(const std::string& str, size_t index);
54 static void addComponantToFlat(Componant* componant, std::vector<Componant*>* flatComponants);
55
56 static void sweepThreadFn(std::vector<std::vector<DataPoint>>* data, Model* model, size_t start, size_t stop, const std::vector<fvalue>& omega);
57
58 size_t getActiveParameterCount();
59
60private:
61 Componant *_model = nullptr;
62 std::vector<Componant*> _bracketComponants;
63 std::string _modelStr;
64 std::vector<Componant*> _flatComponants;
65 std::string _modelUuid;
66 CompiledObject* _compiledModel = nullptr;
67
68public:
69
78 Model(const std::string& str, size_t paramSweepCount = 100, bool defaultToRange = true);
79 Model(const Model& in);
80 Model& operator=(const Model& in);
81 ~Model();
82
89 size_t setParamSweepCountClosestTotal(size_t totalCount);
90
100 DataPoint execute(fvalue omaga, size_t index = 0);
101
111 std::vector<DataPoint> executeSweep(const Range& omega, size_t index = 0);
112
120 std::vector<DataPoint> executeSweep(const std::vector<fvalue>& omega, size_t index = 0);
121
130 std::vector<std::vector<DataPoint>> executeSweeps(const Range& omega, const std::vector<size_t>& indecies, bool parallel = false);
131
140 std::vector<std::vector<DataPoint>> executeSweeps(const std::vector<fvalue>& omega, const std::vector<size_t>& indecies, bool parallel = false);
141
150 std::vector<std::vector<DataPoint>> executeAllSweeps(const Range& omega);
151
157 std::string getModelStr() const;
158
167 std::string getModelStrWithParam(size_t index);
168
176 std::string getModelStrWithParam() const;
177
186 std::string getModelStrWithParamAndCount() const;
187
195 size_t getUuid() const;
196
205 std::vector<Componant*> getFlatComponants(Componant *model = nullptr);
206
217
223 std::vector<fvalue> getFlatParameters();
224
230 std::vector<Range> getFlatParameterRanges();
231
237 std::vector<Range> getDefaultParameters();
238
244 std::vector<std::string> getParameterNames();
245
252
268 bool compile();
269
275
281 bool isReady();
282
288 void resolveSteps(int64_t index);
289
296
303
315
321 std::string getCode();
322
330 std::string getTorchScript();
331
337 std::string getCompiledFunctionName() const;
338
350 std::vector<size_t> getRecommendedParamIndices(eis::Range omegaRange, double distance, bool threaded = false);
351
359 bool allElementsContribute(eis::Range omegaRange, fvalue threashold = 0.01);
360
368 bool hasSeriesDifference(eis::Range omegaRange, fvalue threashold = 0.1);
369
402 std::vector<ParameterPermutation> getEquivalentParameterPermutations(
403 eis::Range omegaRange, fvalue tolerance = 1e-3, size_t limit = 4096, size_t sweepIndex = 0);
404
410 static void removeSeriesResitance(std::string& model);
411};
412
417
422
427
429
430}
Definition componant.h:32
bool allElementsContribute(eis::Range omegaRange, fvalue threashold=0.01)
Attempts to check if all elements contribute to the result.
static void removeSeriesResitance(std::string &model)
Removes the series reistance from a model string (if any).
std::vector< Componant * > getFlatComponants(Componant *model=nullptr)
Returns a vector of pointers to the circuit elements in this model.
std::vector< DataPoint > executeSweep(const Range &omega, size_t index=0)
Executes a frequency sweep along the given range.
std::string getCompiledFunctionName() const
Gets the function name in the code for this model.
std::vector< fvalue > getFlatParameters()
Gets the values of the parameters of the circuit elements at the current parameter sweep step.
std::vector< std::vector< DataPoint > > executeSweeps(const std::vector< fvalue > &omega, const std::vector< size_t > &indecies, bool parallel=false)
Executes a frequency and parameter sweep at the given parameter indecies.
Model(const std::string &str, size_t paramSweepCount=100, bool defaultToRange=true)
Constructor.
std::vector< std::vector< DataPoint > > executeSweeps(const Range &omega, const std::vector< size_t > &indecies, bool parallel=false)
Executes a frequency and parameter sweep at the given parameter indecies.
std::vector< std::string > getParameterNames()
Gets the names of all the parameters in this model.
std::vector< Range > getDefaultParameters()
Gets the default ranges of the parameters of each type of circuit element used in the model.
bool hasDefaultedParameters()
Checks if the model string is fully parameterized.
std::string getModelStr() const
Returns the model string corresponding to this model object, without embedded parameters.
Componant * getModelRoot() const
Returns the root of the circuit element tree.
bool isReady()
This member determines if the model is in a state ready to execute.
std::string getTorchScript()
Compiles this model into TorchScript.
std::string getModelStrWithParamAndCount() const
Like getModelStrWithParam(), but each swept range also carries its step count.
std::string getModelStrWithParam(size_t index)
Returns the model string corresponding to this model object, with embedded parameters.
std::vector< Range > getFlatParameterRanges()
Gets the ranges of the parameters of the circuit elements.
std::vector< size_t > getRecommendedParamIndices(eis::Range omegaRange, double distance, bool threaded=false)
Gets a set of indecies that together create a iso-difference set of spectra.
size_t getRequiredStepsForSweeps()
Gets the total number of parameter sweep steps for the applied sweep.
std::string getModelStrWithParam() const
Returns the model string corresponding to this model object, with embedded parameters.
DataPoint execute(fvalue omaga, size_t index=0)
Gets the impedance at the given frequency.
size_t getUuid() const
Returns a unique id.
std::string getCode()
Compiles this model into efficient c++ code corresponding to the circuit of this model.
size_t getParameterCount()
Gets the total number of parameters used by all the circuit elements in this model.
bool hasSeriesDifference(eis::Range omegaRange, fvalue threashold=0.1)
Checks if all elements in series with one another dont have too similar impedance.
std::vector< DataPoint > executeSweep(const std::vector< fvalue > &omega, size_t index=0)
Executes a frequency sweep with the given omega values.
void resolveSteps(int64_t index)
This member resolves the parameters of all circuit elements at the given parameter sweep step.
size_t setParamSweepCountClosestTotal(size_t totalCount)
Adjusts the sweep count so that the total of spectra that need to be generated in the parameter sweep...
bool compile()
This function compiles the model into native vectorized code for faster execution.
void dropCompiled()
This function drops the compiled object code, reverting to graph execution.
bool isParamSweep()
Checks if the model is a sweep (i.e.
std::vector< std::vector< DataPoint > > executeAllSweeps(const Range &omega)
Executes a frequency sweep with the given omega values for each parameter combination in the applied ...
std::vector< ParameterPermutation > getEquivalentParameterPermutations(eis::Range omegaRange, fvalue tolerance=1e-3, size_t limit=4096, size_t sweepIndex=0)
Lists the other assignments of this model's parameters that give the same spectrum.
eisgenerator Copyright (C) 2021 Carl Klemm
Definition basicmath.h:26