diff --git a/test.cpp b/test.cpp index 28d63449141a8e0b52423c6720f27cb4c2f6800a..53a3c0266caea0afc61be3c49e08c178da8e802e 100644 --- a/test.cpp +++ b/test.cpp @@ -27,7 +27,9 @@ std::vector<eis::DataPoint> getSine() return data; } -static const fvalue mocdata[25][3] = { +static constexpr size_t mocLength = 25; + +static const fvalue mocdata[mocLength][3] = { 0.000000,17200.000000,0.000000, 41666.700000,2900.000000,-2.926830, 83333.300000,2900.000000,-1.463410, @@ -55,94 +57,107 @@ static const fvalue mocdata[25][3] = { 1000000.000000,2900.000000,-0.121951, }; -std::vector<eis::DataPoint> getMockData() +static const fvalue mocNormalized[mocLength][3] = { + 0,1,0, + 41666.7,0,-1, + 83333.3,0,-0.499998, + 125000,0,-0.333333, + 166667,0,-0.25, + 208333,0,-0.2, + 250000,0,-0.166667, + 291667,0,-0.142857, + 333333,0,-0.125, + 375000,0,-0.111111, + 416667,0,-0.1, + 458333,0,-0.0909089, + 500000,0,-0.0833332, + 541667,0,-0.0769232, + 583333,0,-0.0714285, + 625000,0,-0.0666667, + 666667,0,-0.0625, + 708333,0,-0.0588234, + 750000,0,-0.0555557, + 791667,0,-0.0526317, + 833333,0,-0.0499998, + 875000,0,-0.0476191, + 916667,0,-0.0454546, + 958333,0,-0.0434781, + 1e+06,0,-0.0416666, +}; + + +static const fvalue lowVariance[mocLength][3] = { + 0,0,0, + 0,1,-1, + 0,0.25,0, + 0,0.5,0, + 0,0,-0.25, + 0,0,-0.5, + 0,0.5,-0.5, + 0,0.25,-0.25, + 0,0.33,-0.25, + 0,0.33,-0.5, + 0,0.33,0, + 0,0.33,-0.33, + 0,0.25,-0.33, + 0,0.5,-0.33, + 0,0.5,0, + 0,0,-0.25, + 0,0,-0.5, + 0,0.5,-0.5, + 0,0.25,-0.25, + 0,0.33,-0.25, + 0,0.33,-0.5, + 0,0.33,0, + 0,0.33,-0.33, + 0,0.25,-0.33, + 0,0.5,-0.33, +}; + +static const fvalue highVariance[mocLength][3] = { + 0,0,0, + 0,1,-1, + 0,0.25*2,0, + 0,0.5*2,0, + 0,0,-0.25*2, + 0,0,-0.5*2, + 0,0.5*2,-0.5*2, + 0,0.25*2,-0.25*2, + 0,0.33*2,-0.25*2, + 0,0.33*2,-0.5*2, + 0,0.33*2,0, + 0,0.33*2,-0.33*2, + 0,0.25*2,-0.33*2, + 0,0.5*2,-0.33*2, + 0,0.5*2,0, + 0,0,-0.25*2, + 0,0,-0.5*2, + 0,0.5*2,-0.5*2, + 0,0.25*2,-0.25*2, + 0,0.33*2,-0.25*2, + 0,0.33*2,-0.5*2, + 0,0.33*2,0, + 0,0.33*2,-0.33*2, + 0,0.25*2,-0.33*2, + 0,0.5*2,-0.33*2, +}; + +static std::vector<eis::DataPoint> getData(const fvalue in[mocLength][3]) { std::vector<eis::DataPoint> data; - for(size_t i = 0; i < sizeof(mocdata)/sizeof(*mocdata); ++i) + for(size_t i = 0; i < mocLength; ++i) { eis::DataPoint point; - point.im = std::complex<fvalue>(mocdata[i][1], mocdata[i][2]); - point.omega = mocdata[i][0]; + point.im = std::complex<fvalue>(in[i][1], in[i][2]); + point.omega = in[i][0]; data.push_back(point); } return data; } -static void printComponants(eis::Model& model) +static bool runRescale() { - eis::Log(eis::Log::DEBUG)<<"Compnants:"; - for(eis::Componant* componant : model.getFlatComponants()) - { - eis::Log(eis::Log::DEBUG, false)<<componant->getComponantChar()<<"{"; - std::vector<eis::Range>& ranges = componant->getParamRanges(); - assert(componant->paramCount() != ranges.size()); - - for(size_t i = 0; i < componant->paramCount(); ++i) - { - eis::Log(eis::Log::DEBUG, false)<<ranges[i].getString(); - if(i != componant->paramCount()-1) - eis::Log(eis::Log::DEBUG, false)<<", "; - } - 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{1e3~50e3}p{20e-7~1e-7, 0.7~0.9}"); - - eis::Model model(modelStr); - - eis::Range omega(0, 1e6, 25); - - auto start = std::chrono::high_resolution_clock::now(); - size_t len = model.getRequiredStepsForSweeps(); - for(size_t i = 0; i < len; ++len) - model.executeSweep(omega, i); - 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"; -} - -bool runRescale() -{ - std::cout<<__func__<<'\n'; std::vector<eis::DataPoint> data; for(size_t i = 0; i < 22; ++i) { @@ -152,61 +167,40 @@ bool runRescale() data.push_back(point); } - std::cout<<"original:\n"; - printDataVect(data); - data = eis::rescale(data, 50); - - std::cout<<"rescaled size: "<<data.size()<<" rescale data: \n"; - printDataVect(data); return data.size() == 50; } -void runReduce() +static void runReduce() { std::cout<<__func__<<'\n'; - std::vector<eis::DataPoint> data = getMockData(); + std::vector<eis::DataPoint> data = getData(mocdata); std::cout<<"original: \n"; printDataVect(data); - data = reduceRegion(data);; + data = reduceRegion(data); std::cout<<"reduced: \n"; printDataVect(data); } -void runNormalize() +static bool runNormalize() { - std::cout<<__func__<<'\n'; - std::vector<eis::DataPoint> data =getMockData(); - std::cout<<"original"<<'\n'; - printDataVect(data); + std::vector<eis::DataPoint> data = getData(mocdata); + std::vector<eis::DataPoint> normalized = getData(mocNormalized); eis::normalize(data); - std::cout<<"normalized"<<'\n'; - printDataVect(data); - -} - -void runEraseSingularities() -{ - std::cout<<__func__<<'\n'; - std::vector<eis::DataPoint> data; - for(size_t i = 0; i < 1000; ++i) + double dist = eisDistance(data, normalized); + if(dist > 1e-6) { - eis::DataPoint point; - point.im = std::complex<fvalue>(1.0/(i/100.0), 1.0/((i-2)/100.0)); - point.omega = i; - data.push_back(point); + eis::Log(eis::Log::ERROR)<<__func__<<" expected less than 0.999 got "<<dist; + eis::Log(eis::Log::ERROR)<<__func__<<" expected:"; + printDataVect(normalized); + eis::Log(eis::Log::ERROR)<<__func__<<" recived:"; + printDataVect(data); + return false; } - - std::cout<<"original"<<'\n'; - printDataVect(data); - - eraseSingularites(data); - - std::cout<<"erased"<<'\n'; - printDataVect(data); + return true; } inline void filterData(std::vector<eis::DataPoint>& data, size_t outputSize) @@ -221,17 +215,112 @@ inline void filterData(std::vector<eis::DataPoint>& data, size_t outputSize) data = eis::rescale(data, outputSize/2); } -int main(int argc, char** argv) +static bool testDistance() +{ + eis::Model modelA("r{10000}c{2.06914e-10}-p{4.83293e-05, 0.5}"); + eis::Model modelB("r{10000}c{2.97635e-07}-p{0.0001, 0.584211}"); + eis::Range omega(1, 1e6, 50, true); + std::vector<eis::DataPoint> a = modelA.executeSweep(omega); + std::vector<eis::DataPoint> b = modelB.executeSweep(omega); + normalize(a); + normalize(b); + + double dist = eisDistance(a, b); + if(dist > 0.36 && dist < 0.35) + { + eis::Log(eis::Log::ERROR)<<__func__<<" expected 0.36 > x > 0.35 got "<<dist; + return false; + } + eis::Log(eis::Log::INFO)<<__func__<<" "<<dist; + return true; +} + +static bool modelConsistancy() +{ + std::string modelStr("r{1000}c{1~10000L}-p{1, 4}-w{10}-l{1e-10}"); + eis::Model model(modelStr); + + if(model.getModelStrWithParam() == modelStr) + { + return true; + } + else + { + eis::Log(eis::Log::ERROR)<<__func__<<" expected "<<modelStr<<" got "<<model.getModelStrWithParam(); + return false; + } +} + +static bool uneededBrackets() { - std::string tst("(c)"); + std::string tst("(c-(rc)-(r-cr))"); eisRemoveUnneededBrackets(tst); + if(tst == "c-rc-(r-cr)") + { + return true; + } + else + { + eis::Log(eis::Log::ERROR)<<__func__<<" expected "<<"c-rc-(r-cr)"<<" got "<<tst; + return false; + } +} + +static bool nyquistVariance() +{ + eis::Model modelA("r{10000}c{2.06914e-10}"); + eis::Model modelB("r{100000000}c{2.06914e-10}"); + eis::Range omega(1, 1e6, 50, true); + std::vector<eis::DataPoint> a = modelA.executeSweep(omega); + std::vector<eis::DataPoint> b = modelB.executeSweep(omega); + normalize(a); + normalize(b); + fvalue aVar = nyquistAreaVariance(a); + fvalue bVar = nyquistAreaVariance(b); + eis::Log(eis::Log::INFO)<<__func__<<" aVar: "<<aVar<<" bVar: "<<bVar; + + return eis::fvalueEq(aVar, 0.38375) && eis::fvalueEq(bVar, 0.514179); +} + +static bool nyquistJump() +{ + eis::Model modelA("r{10000}c{2.06914e-10}"); + eis::Model modelB("r{100000000}c{2.06914e-10}"); + eis::Range omega(1, 1e6, 50, true); + std::vector<eis::DataPoint> a = modelA.executeSweep(omega); + std::vector<eis::DataPoint> b = modelB.executeSweep(omega); + normalize(a); + normalize(b); + fvalue aVar = maximumNyquistJump(a); + fvalue bVar = maximumNyquistJump(b); + + eis::Log(eis::Log::INFO)<<__func__<<" aVar: "<<aVar<<" bVar: "<<bVar; + return eis::fvalueEq(aVar, 0.38375); +} + +int main(int argc, char** argv) +{ eis::Log::headers = true; eis::Log::level = eis::Log::INFO; - runSingle(); - runSweep(); - runRescale(); - runNormalize(); - runEraseSingularities(); - runReduce(); + if(!uneededBrackets()) + return 1; + + if(!modelConsistancy()) + return 2; + + if(!runRescale()) + return 3; + + if(!testDistance()) + return 4; + + if(!runNormalize()) + return 5; + + if(!nyquistVariance()) + return 6; + + if(!nyquistJump()) + return 7; return 0; }