Skip to content

Commit

Permalink
basic GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeuluiz committed Jun 21, 2019
1 parent b55c258 commit d15c1ae
Show file tree
Hide file tree
Showing 23 changed files with 619 additions and 427 deletions.
33 changes: 27 additions & 6 deletions lib/circuit/include/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@

#include <vector>
#include <map>
#include <unordered_map>

#include <tuple>
#include <string>
#include <atomic>

#include <cassert>

#include <cuda_runtime.h>
#include <cusparse.h>
#include <cusolverSp.h>
#include <cusolverRf.h>
Expand Down Expand Up @@ -97,6 +103,12 @@ namespace rtspice::circuit {

struct {

//dynamic parameters (i.e. potentiometers, vgas, etc.)
std::unordered_map<std::string, std::atomic<float>> params;

//inputs
std::unordered_map<std::string, float> inputs;

std::size_t m, nnz; //problem size

cuda_ptr_<int> row, col;
Expand Down Expand Up @@ -163,16 +175,25 @@ namespace rtspice::circuit {
//recover address of current solution entry
entry_reference<const float> get_state(const std::string& node_name) const;

auto& get_param(const std::string& param_name) {
if(system_.params.find(param_name) == system_.params.end())
system_.params[param_name] = 0.5f;
return system_.params[param_name];
};

auto& get_input(const std::string& param_name) {
return system_.inputs[param_name];
};

const float* get_time() const;
const float* get_delta_time() const;

auto& nodes() const {
return nodes_.names;
}
auto& nodes() const { return nodes_.names; }
auto& entries() const { return nodes_.pointers; }

auto& params() { return system_.params; }
auto& inputs() { return system_.inputs; }

auto solution(const std::string& node_name) const {
return system_.x[nodes_.names.at(node_name)];
}
};

} // ----- end of namespace rtspice::circuit -----
Expand Down
8 changes: 2 additions & 6 deletions lib/circuit/src/circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
* GNU General Public License as published by the Free Software Foundation.
*/

#include <cassert>
#include "circuit.hpp"

#include <cstdint>
#include <algorithm>
#include <execution>
#include <numeric>

#include <cuda_runtime.h>
#include "circuit.hpp"

using namespace std;

constexpr auto parallel_tag = execution::unsequenced_policy{};
Expand All @@ -44,8 +42,6 @@ namespace rtspice::circuit {
init_components_();

setup_static_(); //feed static stamps


}

circuit::~circuit() {
Expand Down
13 changes: 6 additions & 7 deletions lib/circuit/test/circuit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ SCENARIO("circuit initialization", "[circuit]") {

c.nr_step_();

CHECK(c.solution("1") == Approx(1.0f));
CHECK(c.solution("2") == Approx(0.5f));
CHECK(*c.get_x("1") == Approx(1.0f));
CHECK(*c.get_x("2") == Approx(0.5f));

constexpr auto NITER = 44100;

Expand All @@ -90,9 +90,8 @@ SCENARIO("circuit initialization", "[circuit]") {
const auto avgTime = nanoseconds{delta}.count()/NITER;

INFO( "average iteration time: " << avgTime << " ns");
CHECK(c.solution("1") == Approx(1.0f));
CHECK(c.solution("2") == Approx(0.5f));

CHECK(*c.get_x("1") == Approx(1.0f));
CHECK(*c.get_x("2") == Approx(0.5f));
}

}
Expand Down Expand Up @@ -317,7 +316,7 @@ SCENARIO("basic circuit simulation", "[circuit]") {
make_component<linear_capacitor>("C4", "1", "2", 51.0e-12),
make_component<basic_diode> ("D1", "1", "2", 4.352e-9f, 1.906f), //1n4148
make_component<basic_diode> ("D2", "2", "1", 4.352e-9f, 1.906f), //1n4148
make_component<linear_resistor> ("R6", "2", "1", 51.0e3 + dist),
make_component<variable_resistor>("R6", "2", "1", 51.0e3, "dist"),

//tone section
make_component<linear_resistor> ("R7", "1", "5", 4.7e3),
Expand All @@ -337,7 +336,7 @@ SCENARIO("basic circuit simulation", "[circuit]") {
circuit c{components};

constexpr float delta_t = 1.0 / 44100.0;
constexpr int niter = 4410000;
constexpr int niter = 44100;

THEN("basic simulation") {

Expand Down
47 changes: 42 additions & 5 deletions lib/components/include/resistor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace rtspice::components {
f_(std::forward<Args>(args)...) {}

virtual bool is_static() const override { return F::static_v; }
virtual bool is_dynamic() const override { return false; }
virtual bool is_dynamic() const override { return F::dynamic_v; }
virtual bool is_nonlinear() const override { return F::nonlinear_v; }

virtual void register_(circuit::circuit& c) override {
Expand All @@ -76,6 +76,9 @@ namespace rtspice::components {

xa_ = c.get_x(na_);
xb_ = c.get_x(nb_);

f_.setup(c);

}

virtual void fill() const noexcept override {
Expand All @@ -98,7 +101,7 @@ namespace rtspice::components {

private:
const std::string na_, nb_;
const F f_;
F f_;

//system references
circuit::entry_reference<float> Aaa_, Aab_, Aba_, Abb_;
Expand All @@ -108,12 +111,12 @@ namespace rtspice::components {

/*!
* @brief class implementing linear resistance characteristics.
*
*/
class linear_resistance {
public:

static constexpr bool static_v = true;
static constexpr bool static_v = true;
static constexpr bool dynamic_v = false;
static constexpr bool nonlinear_v = false;

linear_resistance(float R) :
Expand All @@ -123,17 +126,48 @@ namespace rtspice::components {
return std::make_pair(G_*v, G_);
}

void setup(circuit::circuit& c) {}

private:
const float G_;
};

/*!
* @brief class implementing variable resistance characteristics.
*/
class external_resistance {
public:

static constexpr bool static_v = false;
static constexpr bool dynamic_v = true;
static constexpr bool nonlinear_v = false;

external_resistance(float rmax, std::string param_name) :
Rmax_{ rmax },
param_name_{ std::move(param_name) } {}

inline auto operator()(float v) const noexcept {
const auto G = 1.0/(val_->load(std::memory_order_relaxed) * Rmax_);
return std::make_pair(G*v, G);
}

void setup(circuit::circuit& c) {
val_ = &c.get_param(param_name_);
}

private:
const float Rmax_;
const std::string param_name_;
const std::atomic<float>* val_;
};

/*!
* @brief class implementing shockley equation characteristics
*
*/
class diode_resistance {
public:
static constexpr bool static_v = false;
static constexpr bool dynamic_v = false;
static constexpr bool nonlinear_v = true;

diode_resistance(float IS, float N) :
Expand All @@ -142,6 +176,8 @@ namespace rtspice::components {
e_sat_ ( IS_*std::expm1(v_knee/N_Vt_) ),
df_sat_( IS_*std::exp(v_knee/N_Vt_)/N_Vt_ ) { }

void setup(circuit::circuit& c) {}

inline auto operator()(float v) const noexcept -> std::pair<float,float> {

if(v < v_knee) {
Expand All @@ -168,6 +204,7 @@ namespace rtspice::components {
};

using linear_resistor = resistor<linear_resistance>;
using variable_resistor = resistor<external_resistance>;
using basic_diode = resistor<diode_resistance>;

} // ----- end of namespace rtspice::components -----
Expand Down
Loading

0 comments on commit d15c1ae

Please sign in to comment.