[修改]修改了错误的exporter名称

This commit is contained in:
makotocc0107
2024-09-04 09:51:10 +08:00
committed by Coding
parent 211a89778c
commit d93daaab6b
37 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
.idea/
cmake-build-*/

View File

@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.18)
project(fault_simulation_algorithm)
set(CMAKE_CXX_STANDARD 20)
find_package(nlohmann_json CONFIG REQUIRED)
include_directories(include)
add_subdirectory(periodic_interference)
add_subdirectory(temperature-drift)
add_subdirectory(step)
add_subdirectory(white_noise)
add_subdirectory(simulation-manager)

View File

@@ -0,0 +1,33 @@
//
// Created by fly on 2022/4/6.
//
#ifndef FAULT_SIMULATION_ALGORITHM_ALGORITHM_HPP
#define FAULT_SIMULATION_ALGORITHM_ALGORITHM_HPP
#include <nlohmann/json.hpp>
class algorithm {
public:
virtual void set_config(nlohmann::json const &config) = 0;
virtual nlohmann::json config() = 0;
virtual double eval(double value) = 0;
protected:
double jsonValue(nlohmann::json const &j, std::string const &key) {
if (j.contains(key)) {
if (j[key].is_string()) {
return std::atof(j[key].get<std::string>().c_str());
}
if (j[key].is_number()) {
return j[key].get<double>();
}
}
return std::nan("");
}
};
#endif //FAULT_SIMULATION_ALGORITHM_ALGORITHM_HPP

View File

@@ -0,0 +1,23 @@
add_library(
periodic-interference
INTERFACE
)
target_sources(
periodic-interference
INTERFACE
periodic_interference.cpp
periodic_interference.h
)
target_include_directories(
periodic-interference
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(
periodic-interference
INTERFACE
nlohmann_json::nlohmann_json
)

View File

@@ -0,0 +1,47 @@
//
// Created by baiguwen on 2022/4/23.
//
#include <iostream>
#include "periodic_interference.h"
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
int main() {
periodic_interference algorithm;
algorithm.set_config(
{
{"PERIODIC_INTERFERENCE_AMPLITUDE", 20},
{"PERIODIC_INTERFERENCE_AMPLITUDE_BASE", 1},
{"CYCLE_", 20}
}
);
std::vector <std::vector<double>> user_arr;
std::ifstream fp("C:/data/user_data.csv");
std::string line;
getline(fp,line);
while (getline(fp,line)){
std::vector <double> data_line;
std::string number;
std::istringstream readstr(line);
for(int j = 0;j < 2814;j++){
getline(readstr,number,',');
data_line.push_back(atof(number.c_str()));
}
user_arr.push_back(data_line);
}
std::ofstream outfile;
outfile.open("C:/data/user_data5.csv", std::ios::out);
outfile<<"origin"<<','<<"periodic_interference"<<std::endl;
for (int i = 0; i <2814; ++i) {
user_arr[i][1]=algorithm.eval(user_arr[i][0]);
std::cout << i << "\t" << user_arr[i][1] << std::endl;
outfile<<user_arr[i][0]<<','<<user_arr[i][1]<<std::endl;
}
outfile.close();
return 0;
}

View File

@@ -0,0 +1,28 @@
//
// Created by baiguwen on 2022/4/23.
//
#include "periodic_interference.h"
#include <math.h>
#include <numbers>
#include <random>
void periodic_interference::set_config(const nlohmann::json &config) {
try {
eval_time_ = 0;
amplitude_ = jsonValue(config, "PERIODIC_INTERFERENCE_AMPLITUDE");
amplitude_base_ = jsonValue(config, "PERIODIC_INTERFERENCE_AMPLITUDE_BASE");
cycle_ = jsonValue(config, "CYCLE_");
config_ = config;
} catch (...) {
}
}
nlohmann::json periodic_interference::config() {
return config_;
}
double periodic_interference::eval(double value) {
return value + amplitude_base_ * sin((2 * std::numbers::pi / cycle_) * eval_time_++) + amplitude_;
}

View File

@@ -0,0 +1,28 @@
//
// Created by baiguwen on 2022/4/23.
//
#ifndef FAULT_SIMULATION_ALGORITHM_PERIODIC_INTERFERENCE_H
#define FAULT_SIMULATION_ALGORITHM_PERIODIC_INTERFERENCE_H
#include <algorithm.hpp>
class periodic_interference : public algorithm {
public:
void set_config(const nlohmann::json &config) override;
nlohmann::json config() override;
double eval(double value) override;
protected:
nlohmann::json config_{};
double amplitude_{0};
double amplitude_base_{0};
double cycle_{0};
uint64_t eval_time_{0};
};
#endif //FAULT_SIMULATION_ALGORITHM_PERIODIC_INTERFERENCE_H

View File

@@ -0,0 +1,27 @@
add_library(simulation-manager INTERFACE)
target_sources(
simulation-manager
INTERFACE
simulation_manager.hpp
)
target_include_directories(
simulation-manager
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../include
)
target_link_libraries(
simulation-manager
INTERFACE
periodic-interference
step
temperature-drift
white-noise
)
#add_executable(simulation-manager main.cpp simulation_manager.hpp algorithm_wrap.hpp)

View File

@@ -0,0 +1,68 @@
//
// Created by fly on 2022/4/21.
//
#ifndef FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP
#define FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP
#include <algorithm.hpp>
#include <utility>
class algorithm_wrap : public algorithm {
public:
explicit algorithm_wrap(std::shared_ptr<algorithm> algo) {
algorithm_ = std::move(algo);
}
bool enable() const {
return enable_;
}
void set_enable(bool flag) {
enable_ = flag;
}
int duration_period() const {
return duration_period_;
}
void set_duration_period(int durationPeriod) {
duration_period_ = durationPeriod;
}
void set_config(nlohmann::json const &config) override {
if (algorithm_) {
algorithm_->set_config(config);
if (config.contains("enable") && config["enable"].is_boolean()) {
set_enable(config["enable"].get<bool>());
}
}
}
nlohmann::json config() override {
nlohmann::json config;
config["enable"] = enable();
if (algorithm_) {
return algorithm_->config();
}
return config;
}
double eval(double value) override {
if (algorithm_ && enable_ && duration_period_) {
if (duration_period_ > 0) {
--duration_period_;
}
return algorithm_->eval(value);
}
return value;
}
private:
std::shared_ptr<algorithm> algorithm_;
bool enable_ = false;
int duration_period_ = -1;
};
#endif //FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP

View File

@@ -0,0 +1,10 @@
//
// Created by fly on 2022/4/21.
//
#include "simulation_manager.hpp"
int main(int argc, char *argv) {
return 0;
}

View File

@@ -0,0 +1,106 @@
//
// Created by fly on 2022/4/21.
//
#ifndef FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP
#define FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP
#include "algorithm_wrap.hpp"
#include <periodic_interference.h>
#include <step.h>
#include <white_noise.h>
#include <temperature_drift.h>
#include <spdlog/spdlog.h>
class simulation_manager {
public:
void add_algorithm(std::string const &name, std::shared_ptr<algorithm> algo) {
if (!algorithms_.contains(name)) {
algorithms_.emplace(name, std::make_shared<algorithm_wrap>(algo));
}
}
void remove_algorithm(std::string const &name) {
algorithms_.erase(name);
}
void set_algorithm_enable(std::string const &name, bool flag) {
if (algorithms_.contains(name)) {
algorithms_[name]->set_enable(flag);
}
}
bool algorithm_enable(std::string const &name) {
if (algorithms_.contains(name)) {
return algorithms_[name]->enable();
}
return false;
}
void set_algorithm_duration_period(std::string const &name, int duration_period) {
if (algorithms_.contains(name)) {
algorithms_[name]->set_duration_period(duration_period);
}
}
int algorithm_duration_period(std::string const &name) {
if (algorithms_.contains(name)) {
return algorithms_[name]->duration_period();
}
return -1;
}
void set_algorithm_config(std::string const &name, nlohmann::json const &config) {
if (!algorithms_.contains(name)) {
spdlog::debug("Create {} Algorithm Object", name);
if ("periodic_interference" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<periodic_interference>());
} else if ("step" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<step>());
} else if ("white_noise" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<white_noise>());
} else if ("temperature_drift" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<temperature_drift>());
} else if ("pulse" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<step>());
}
}
if (algorithms_.contains(name)) {
algorithms_[name]->set_config(config);
}
}
nlohmann::json algorithm_config(std::string const &name) {
if (algorithms_.contains(name)) {
return algorithms_[name]->config();
}
return {};
}
nlohmann::json config() {
nlohmann::json j;
for (auto iter = algorithms_.begin(); iter != algorithms_.end(); ++iter) {
j[iter->first] = iter->second->config();
}
return j;
}
void set_config(nlohmann::json const &c) {
for (auto iter = c.begin(); iter != c.end(); ++iter) {
set_algorithm_config(iter.key(), iter.value());
}
}
double eval(double value) {
for (auto &iter: algorithms_) {
value = iter.second->eval(value);
}
return value;
}
private:
std::map<std::string, std::shared_ptr<algorithm_wrap>> algorithms_;
};
#endif //FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP

View File

@@ -0,0 +1,23 @@
add_library(
step
INTERFACE
)
target_sources(
step
INTERFACE
step.cpp
step.h
)
target_include_directories(
step
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(
step
INTERFACE
nlohmann_json::nlohmann_json
)

View File

@@ -0,0 +1,44 @@
//
// Created by baiguwen on 2022/4/20.
//
#include <iostream>
#include "step.h"
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
int main() {
step algorithm;
algorithm.set_config(
{
{"STEP_AMPLITUDE_BASE", 10}
}
);
std::vector <std::vector<double>> user_arr;
std::ifstream fp("C:/data/user_data.csv");
std::string line;
getline(fp,line);
while (getline(fp,line)){
std::vector <double> data_line;
std::string number;
std::istringstream readstr(line);
for(int j = 0;j < 2814;j++){
getline(readstr,number,',');
data_line.push_back(atof(number.c_str()));
}
user_arr.push_back(data_line);
}
std::ofstream outfile;
outfile.open("C:/data/user_data6.csv", std::ios::out);
outfile<<"origin"<<','<<"step"<<std::endl;
for (int i = 0; i <2814; ++i) {
user_arr[i][1]=algorithm.eval(user_arr[i][0]);
std::cout << i << "\t" << user_arr[i][1] << std::endl;
outfile<<user_arr[i][0]<<','<<user_arr[i][1]<<std::endl;
}
outfile.close();
return 0;
}

View File

@@ -0,0 +1,23 @@
//
// Created by baiguwen on 2022/4/20.
//
#include "step.h"
#include <iostream>
void step::set_config(const nlohmann::json &config) {
try {
amplitude_base_ = jsonValue(config, "STEP_AMPLITUDE_BASE");
config_ = config;
} catch (...) {
}
}
nlohmann::json step::config() {
return config_;
}
double step::eval(double value) {
return value + amplitude_base_;
}

View File

@@ -0,0 +1,21 @@
//
// Created by baiguwen on 2022/4/20.
//
#ifndef FAULT_SIMULATION_ALGORITHM_MAIN_STEP_H
#define FAULT_SIMULATION_ALGORITHM_MAIN_STEP_H
#include <algorithm.hpp>
class step : public algorithm {
public:
void set_config(const nlohmann::json &config) override;
nlohmann::json config() override;
double eval(double value) override;
protected:
nlohmann::json config_{};
double amplitude_base_{0};
};
#endif //FAULT_SIMULATION_ALGORITHM_MAIN_STEP_H

View File

@@ -0,0 +1,24 @@
add_library(
temperature-drift
INTERFACE
)
target_sources(
temperature-drift
INTERFACE
temperature_drift.cpp
temperature_drift.h
)
target_include_directories(
temperature-drift
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(
temperature-drift
INTERFACE
nlohmann_json::nlohmann_json
)

View File

@@ -0,0 +1,88 @@
#include <iostream>
#include "temperature_drift.h"
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
int main() {
temperature_drift algorithm;
algorithm.set_config(
{
{"TEMPERATURE_DRIFT_AMPLITUDE", 0.01},
{"TEMPERATURE_DRIFT_AMPLITUDE_BASE", 10}
}
);
std::vector <std::vector<double>> user_arr;
std::ifstream fp("C:/data/user_data.csv");
std::string line;
getline(fp,line);
while (getline(fp,line)){
std::vector <double> data_line;
std::string number;
std::istringstream readstr(line);
for(int j = 0;j < 2814;j++){
getline(readstr,number,',');
data_line.push_back(atof(number.c_str()));
}
user_arr.push_back(data_line);
}
std::ofstream outfile;
outfile.open("C:/data/user_data7.csv", std::ios::out);
outfile<<"origin"<<','<<"temperature_drift"<<std::endl;
for (int i = 0; i <2814; ++i) {
user_arr[i][1]=algorithm.eval(user_arr[i][0]);
std::cout << i << "\t" << user_arr[i][1] << std::endl;
outfile<<user_arr[i][0]<<','<<user_arr[i][1]<<std::endl;
}
outfile.close();
return 0;
}
// std::vector<int> x(100);
// std::generate(x.begin(), x.end(), []() {
// static int i = 0;
// return i++;
// });
// std::vector<double> virtual_data(100, 100.0);
// std::vector<double> temperature_drift_data;
// sciplot::Plot plot;
//
// for (auto iter: virtual_data) {
// temperature_drift_data.push_back(algorithm.eval(iter));
// }
//
// plot.drawCurve(x, virtual_data).label("org");
// plot.drawCurve(x, temperature_drift_data).label("td");
// plot.show();
//
// return 0;
//}
//#include <sciplot/sciplot.hpp>
//using namespace sciplot;
//
//int main(int argc, char** argv)
//{
// // Create values for your x-axis
// Vec x = linspace(0.0, 5.0, 100);
//
// // Create a Plot object
// Plot plot;
//
// // Set color palette
// plot.palette("set2");
//
// // Draw a sine graph putting x on the x-axis and sin(x) on the y-axis
// plot.drawCurve(x, std::sin(x)).label("sin(x)").lineWidth(4);
//
// // Draw a cosine graph putting x on the x-axis and cos(x) on the y-axis
// plot.drawCurve(x, std::cos(x)).label("cos(x)").lineWidth(4);
//
// // Show the plot in a pop-up window
// plot.show();
//
// // Save the plot to a PDF file
// plot.save("plot.pdf");
//}

View File

@@ -0,0 +1,26 @@
//
// Created by fly on 2022/4/6.
//
#include "temperature_drift.h"
void temperature_drift::set_config(nlohmann::json const &config) {
try {
eval_time_ = 0;
amplitude_ = jsonValue(config, "TEMPERATURE_DRIFT_AMPLITUDE");
amplitude_base_ = jsonValue(config, "TEMPERATURE_DRIFT_AMPLITUDE_BASE");
config_ = config;
} catch (...) {
}
}
nlohmann::json temperature_drift::config() {
return config_;
}
double temperature_drift::eval(double value) {
return value + amplitude_base_ + (amplitude_ * ++eval_time_);
}

View File

@@ -0,0 +1,26 @@
//
// Created by fly on 2022/4/6.
//
#ifndef FAULT_SIMULATION_ALGORITHM_TEMPERATURE_DRIFT_H
#define FAULT_SIMULATION_ALGORITHM_TEMPERATURE_DRIFT_H
#include <algorithm.hpp>
class temperature_drift : public algorithm {
public:
void set_config(nlohmann::json const &config) override;
nlohmann::json config() override;
double eval(double value) override;
protected:
nlohmann::json config_{};
double amplitude_{0};
double amplitude_base_{0};
uint64_t eval_time_{0};
};
#endif //FAULT_SIMULATION_ALGORITHM_TEMPERATURE_DRIFT_H

View File

@@ -0,0 +1,23 @@
add_library(
white-noise
INTERFACE
)
target_sources(
white-noise
INTERFACE
white_noise.cpp
white_noise.h
)
target_include_directories(
white-noise
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(
white-noise
INTERFACE
nlohmann_json::nlohmann_json
)

View File

@@ -0,0 +1,80 @@
//
// Created by baiguwen on 2022/4/24.
//
#include <iostream>
#include "white_noise.h"
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
//#include <sciplot/sciplot.hpp>
int main() {
white_noise algorithm;
double white_noise_stddev=2;
double white_noise_mean=0;
algorithm.set_config(
{
{"WHITE_NOISE_MEAN", white_noise_mean},
{"WHITE_NOISE_STDDEV", white_noise_stddev},
}
);
std::vector <std::vector<double>> user_arr;
std::ifstream fp("C:/data/user_data.csv");
std::string line;
getline(fp,line);
while (getline(fp,line)){
std::vector <double> data_line;
std::string number;
std::istringstream readstr(line);
for(int j = 0;j < 2814;j++){
getline(readstr,number,',');
data_line.push_back(atof(number.c_str()));
}
user_arr.push_back(data_line);
}
std::ofstream outfile;
outfile.open("C:/data/user_data8.csv", std::ios::out);
outfile<<"origin"<<','<<"white_noise"<<std::endl;
for (int i = 0; i <2814; ++i) {
user_arr[i][1]=algorithm.eval(user_arr[i][0]);
std::cout << i << "\t" << user_arr[i][1] << std::endl;
outfile<<user_arr[i][0]<<','<<user_arr[i][1]<<std::endl;
}
outfile.close();
return 0;
}
// int length=round(white_noise_stddev)*6;
// std::vector<int> x(length+1,0);
//for(int i=0;i<length+1;i++){
// x[i]=100-length/2+round(white_noise_mean)+i;
//}
//
// std::vector<double> virtual_data(10000, 100.0);
// std::vector<double> white_noise_data;
// std::vector<int> white_noise_data_number(length+1,0);
// sciplot::Plot plot;
//
// for (auto iter: virtual_data) {
// double a=algorithm.eval(iter);
// white_noise_data.push_back (round(a));
// std::cout << iter << "\t" << a << std::endl;
// }
// for(int k=0;k<virtual_data.size();k++){
// for(int l=0;l<length+1;l++){
// if(white_noise_data[k]==x[l])
// white_noise_data_number[l]++;
//
//
// }
//
// }
//
// plot.drawCurve(x, white_noise_data_number).label("td");
// plot.show();
//
// return 0;
//}

View File

@@ -0,0 +1,27 @@
//
// Created by baiguwen on 2022/4/24.
//
#include "white_noise.h"
void white_noise::set_config(const nlohmann::json &config) {
try {
mean_ = jsonValue(config, "WHITE_NOISE_MEAN");
standard_deviation_ = jsonValue(config, "WHITE_NOISE_STDDEV");
normal_distribution_.reset(new std::normal_distribution<>(mean_, standard_deviation_));
config_ = config;
} catch (...) {
}
}
nlohmann::json white_noise::config() {
return config_;
}
double white_noise::eval(double value) {
if (normal_distribution_) {
value = value + normal_distribution_->operator()(mt19937_);
}
return value;
}

View File

@@ -0,0 +1,26 @@
//
// Created by baiguwen on 2022/4/24.
//
#ifndef FAULT_SIMULATION_ALGORITHM_WHITE_NOISE_H
#define FAULT_SIMULATION_ALGORITHM_WHITE_NOISE_H
#include <algorithm.hpp>
#include <math.h>
#include <random>
class white_noise : public algorithm {
public:
void set_config(const nlohmann::json &config) override;
nlohmann::json config() override;
double eval(double value) override;
protected:
nlohmann::json config_{};
std::mt19937 mt19937_{std::random_device()()};
std::shared_ptr <std::normal_distribution<>> normal_distribution_;
double mean_{0};
double standard_deviation_{0};
};
#endif //FAULT_SIMULATION_ALGORITHM_WHITE_NOISE_H