Commit ae3ed270 by Kyle Fiedler

merge

parents 6e8afaa1 b15e7c19
No preview for this file type
......@@ -96,10 +96,18 @@ cktsim = (function() {
// load circuit from JSON netlist (see schematic.js)
Circuit.prototype.load_netlist = function(netlist) {
// set up mapping for ground node always called '0' in JSON netlist
this.node_map['0'] = this.gnd_node();
// set up mapping for all ground connections
for (var i = netlist.length - 1; i >= 0; --i) {
var component = netlist[i];
var type = component[0];
if (type == 'g') {
var connections = component[3];
this.node_map[connections[0]] = this.gnd_node();
}
}
// process each component in the JSON netlist (see schematic.js for format)
var found_ground = false;
for (var i = netlist.length - 1; i >= 0; --i) {
var component = netlist[i];
var type = component[0];
......@@ -120,6 +128,7 @@ cktsim = (function() {
var node = connections[j];
var index = this.node_map[node];
if (index == undefined) index = this.node(node,T_VOLTAGE);
else if (index == this.gnd_node()) found_ground = true;
connections[j] = index;
}
......@@ -137,12 +146,19 @@ cktsim = (function() {
else if (type == 'i') // current source
this.i(connections[0],connections[1],properties['value'],name);
else if (type == 'o') // op amp
this.opamp(connections[0],connections[1],connections[2],properties['A'],name);
this.opamp(connections[0],connections[1],connections[2],connections[3],properties['A'],name);
else if (type == 'n') // n fet
this.n(connections[0],connections[1],connections[2],properties['W/L'],name);
else if (type == 'p') // p fet
this.p(connections[0],connections[1],connections[2],properties['W/L'],name);
}
if (!found_ground) { // No ground on schematic
alert('Please make at least one connection to ground (inverted T symbol)');
return false;
}
return true;
}
// if converges: updates this.solution, this.soln_max, returns iter count
......@@ -187,6 +203,8 @@ cktsim = (function() {
// DC analysis
Circuit.prototype.dc = function() {
// Allocation matrices for linear part, etc.
this.finalize();
// Load up the linear part.
......@@ -624,6 +642,17 @@ cktsim = (function() {
return this.add_device(d, name);
}
Circuit.prototype.opamp = function(np,nn,no,ng,A,name) {
// try to convert string value into numeric value, barf if we can't
if ((typeof A) == 'string') {
ratio = parse_number(A,undefined);
if (A === undefined) return undefined;
}
var branch = this.node(undefined,T_CURRENT);
var d = new Opamp(np,nn,no,ng,branch,A,name);
return this.add_device(d, name);
}
Circuit.prototype.n = function(d,g,s, ratio, name) {
// try to convert string value into numeric value, barf if we can't
if ((typeof ratio) == 'string') {
......@@ -1090,6 +1119,15 @@ cktsim = (function() {
src.value = function(t) { return v; } // closure
}
// post-processing for impulse sources
// impulse(height,width)
else if (src.fun == 'impulse') {
var h = arg_value(src.args,0,1); // default height: 1
var w = Math.abs(arg_value(src.args,2,1e-9)); // default width: 1ns
src.args = [h,w]; // remember any defaulted values
pwl_source(src,[0,0,w/2,h,w,0],false);
}
// post-processing for step sources
// step(v_init,v_plateau,t_delay,t_rise)
else if (src.fun == 'step') {
......@@ -1263,8 +1301,8 @@ cktsim = (function() {
// MNA stamp for independent voltage source
ckt.add_to_Gl(this.branch,this.npos,1.0);
ckt.add_to_Gl(this.branch,this.nneg,-1.0);
ckt.add_to_Gl(this.npos,this.branch,1.0);
ckt.add_to_Gl(this.nneg,this.branch,-1.0);
ckt.add_to_Gl(this.npos,this.branch,-1.0);
ckt.add_to_Gl(this.nneg,this.branch,1.0);
}
// Source voltage added to b.
......@@ -1448,9 +1486,9 @@ cktsim = (function() {
// MNA stamp for inductor linear part
// L on diag of C because L di/dt = v(n1) - v(n2)
ckt.add_to_Gl(this.n1,this.branch,1);
ckt.add_to_Gl(this.branch,this.n1,1);
ckt.add_to_Gl(this.branch,this.n1,-1);
ckt.add_to_Gl(this.n2,this.branch,-1);
ckt.add_to_Gl(this.branch,this.n2,-1);
ckt.add_to_Gl(this.branch,this.n2,1);
ckt.add_to_C(this.branch,this.branch,this.value)
}
......@@ -1464,6 +1502,51 @@ cktsim = (function() {
Inductor.prototype.load_tran = function(ckt) {
}
///////////////////////////////////////////////////////////////////////////////
//
// Simple Voltage-Controlled Voltage Source Op Amp model
//
///////////////////////////////////////////////////////////////////////////////
function Opamp(np,nn,no,ng,branch,A,name) {
Device.call(this);
this.np = np;
this.nn = nn;
this.no = no;
this.ng = ng;
this.branch = branch;
this.gain = A;
this.name = name;
}
Opamp.prototype = new Device();
Opamp.prototype.constructor = Opamp;
Opamp.prototype.load_linear = function(ckt) {
// MNA stamp for VCVS: 1/A(v(no) - v(ng)) - (v(np)-v(nn))) = 0.
var invA = 1.0/this.gain;
ckt.add_to_Gl(this.no,this.branch,1);
ckt.add_to_Gl(this.ng,this.branch,-1);
ckt.add_to_Gl(this.branch,this.no,-invA);
ckt.add_to_Gl(this.branch,this.ng,invA);
ckt.add_to_Gl(this.branch,this.np,1);
ckt.add_to_Gl(this.branch,this.nn,-1);
}
Opamp.prototype.load_dc = function(ckt,soln,rhs) {
// Op-amp is linear.
}
Opamp.prototype.load_ac = function(ckt) {
}
Opamp.prototype.load_tran = function(ckt) {
}
///////////////////////////////////////////////////////////////////////////////
//
// Simplified MOS FET with no bulk connection and no body effect.
......
......@@ -113,7 +113,7 @@ schematic = (function() {
// else just populate parts bin with all the parts
this.edits_allowed = true;
var parts = input.getAttribute('parts');
if (parts == undefined) {
if (parts == undefined || parts == 'None') {
parts = new Array();
for (var p in parts_map) parts.push(p);
} else if (parts == '') {
......@@ -133,7 +133,8 @@ schematic = (function() {
// use user-supplied list of analyses, otherwise provide them all
// analyses="" means no analyses
var analyses = input.getAttribute('analyses');
if (analyses == undefined) analyses = ['dc','ac','tran'];
if (analyses == undefined || analyses == 'None')
analyses = ['dc','ac','tran'];
else if (analyses == '') analyses = [];
else analyses = analyses.split(',');
......@@ -193,7 +194,6 @@ schematic = (function() {
this.canvas.style.borderStyle = 'solid';
this.canvas.style.borderWidth = '1px';
this.canvas.style.borderColor = grid_style;
//this.canvas.style.position = 'absolute';
this.canvas.style.outline = 'none';
}
......@@ -273,6 +273,9 @@ schematic = (function() {
tr = document.createElement('tr');
table.appendChild(tr);
td = document.createElement('td');
td.style.position = 'relative'; // so we can position subwindows
td.style.left = '0';
td.style.top = '0';
tr.appendChild(td);
td.appendChild(this.canvas);
td = document.createElement('td');
......@@ -634,9 +637,10 @@ schematic = (function() {
// create a circuit from the netlist
var ckt = new cktsim.Circuit();
ckt.load_netlist(netlist);
return ckt;
if (ckt.load_netlist(netlist))
return ckt;
else
return null;
}
Schematic.prototype.dc_analysis = function() {
......@@ -645,6 +649,7 @@ schematic = (function() {
this.redraw_background();
var ckt = this.extract_circuit();
if (ckt === null) return;
// run the analysis
this.operating_point = ckt.dc();
......@@ -712,6 +717,7 @@ schematic = (function() {
Schematic.prototype.ac_analysis = function(npts,fstart,fstop,ac_source_name) {
// run the analysis
var ckt = this.extract_circuit();
if (ckt === null) return;
var results = ckt.ac(npts,fstart,fstop,ac_source_name);
// save a copy of the results for submission
......@@ -731,11 +737,28 @@ schematic = (function() {
var y_values = []; // list of [color, result_array]
var probes = this.find_probes();
var probe_maxv = [];
var probe_color = [];
// Check for proble with near zero transfer function and warn
for (var i = probes.length - 1; i >= 0; --i) {
var color = probes[i][0];
probe_color[i] = probes[i][0];
var label = probes[i][1];
var v = results[label];
probe_maxv[i] = array_max(v); // magnitudes always > 0
}
var all_max = array_max(probe_maxv);
for (var i = probes.length - 1; i >= 0; --i) {
if ((probe_maxv[i] / all_max) < 1.0e-10) {
alert('Near zero ac response, remove ' + probe_color[i] + ' probe');
return;
}
}
for (var i = probes.length - 1; i >= 0; --i) {
var color = probes[i][0];
var label = probes[i][1];
var v = results[label];
// convert values into dB relative to source amplitude
var v_max = 1;
for (var j = v.length - 1; j >= 0; --j)
......@@ -774,6 +797,8 @@ schematic = (function() {
this.dialog('Transient Analysis',content,function(content) {
var sch = content.sch;
var ckt = sch.extract_circuit();
if (ckt === null) return;
// retrieve parameters, remember for next time
sch.tran_npts = content.fields[npts_lbl].value;
......@@ -792,8 +817,8 @@ schematic = (function() {
ckt.parse_number(sch.tran_tstop), probe_names, false);
// save a copy of the results for submission
this.transient_results = {};
for (var i in results) this.transient_results[i] = results[i];
sch.transient_results = {};
for (var i in results) sch.transient_results[i] = results[i];
if (typeof results == 'string')
sch.message(results);
......@@ -1210,6 +1235,7 @@ schematic = (function() {
// just redraw dynamic components
sch.redraw();
//sch.message(sch.canvas.page_x + ',' + sch.canvas.page_y + ';' + sch.canvas.mouse_x + ',' + sch.canvas.mouse_y + ';' + sch.cursor_x + ',' + sch.cursor_y);
return false;
}
......@@ -1317,6 +1343,7 @@ schematic = (function() {
ok_button.appendChild(document.createTextNode('OK'));
ok_button.dialog = dialog; // for the handler to use
ok_button.addEventListener('click',dialog_okay,false);
ok_button.style.display = 'inline';
ok_button.style.border = '1px solid';
ok_button.style.padding = '5px';
ok_button.style.margin = '10px';
......@@ -1326,6 +1353,7 @@ schematic = (function() {
cancel_button.appendChild(document.createTextNode('Cancel'));
cancel_button.dialog = dialog; // for the handler to use
cancel_button.addEventListener('click',dialog_cancel,false);
cancel_button.style.display = 'inline';
cancel_button.style.border = '1px solid';
cancel_button.style.padding = '5px';
cancel_button.style.margin = '10px';
......@@ -1449,9 +1477,9 @@ schematic = (function() {
win.appendChild(content);
content.win = win; // so content can contact us
// compute location in top-level div
win.left = this.canvas.page_x;
win.top = this.canvas.page_y;
// compute location relative to canvas
win.left = this.canvas.mouse_x;
win.top = this.canvas.mouse_y;
// add to DOM
win.style.background = 'white';
......@@ -1461,7 +1489,8 @@ schematic = (function() {
win.style.top = win.top + 'px';
win.style.border = '2px solid';
this.input.parentNode.insertBefore(win,this.input.nextSibling);
this.canvas.parentNode.insertBefore(win,this.canvas);
//this.input.parentNode.insertBefore(win,this.input.nextSibling);
}
// close the window
......@@ -1606,7 +1635,10 @@ schematic = (function() {
if (!event) event = window.event;
var tool = (window.event) ? event.srcElement : event.target;
if (tool.enabled) tool.callback.call(tool.sch);
if (tool.enabled) {
tool.sch.canvas.relMouseCoords(event); // so we can position pop-up window correctly
tool.callback.call(tool.sch);
}
}
cut_icon = 'data:image/gif;base64,R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//////yH5BAEAAAcALAAAAAAQABAAAAQu8MhJqz1g5qs7lxv2gRkQfuWomarXEgDRHjJhf3YtyRav0xcfcFgR0nhB5OwTAQA7';
......@@ -1943,25 +1975,27 @@ schematic = (function() {
var x1 = (index == 0) ? x_values[0] : x_values[index-1];
var x2 = x_values[index];
// for each plot, interpolate and output value at intersection with marker
c.textAlign = 'left';
var tx = graph.left_margin + 4;
var ty = graph.top_margin;
for (var plot = 0; plot < graph.y_values.length; plot++) {
var values = graph.y_values[plot][1];
// interpolate signal value at graph_x using values[index-1] and values[index]
var y1 = (index == 0) ? values[0] : values[index-1];
var y2 = values[index];
var y = y1;
if (graph_x != x1) y += (graph_x - x1)*(y2 - y1)/(x2 - x1);
// annotate plot with value of signal at marker
c.fillStyle = element_style;
c.fillText('\u2588\u2588\u2588\u2588\u2588',tx-3,ty);
c.fillStyle = probe_colors_rgb[graph.y_values[plot][0]];
c.fillText(engineering_notation(y,3,false),tx,ty);
ty += 14;
if (x2 != undefined) {
// for each plot, interpolate and output value at intersection with marker
c.textAlign = 'left';
var tx = graph.left_margin + 4;
var ty = graph.top_margin;
for (var plot = 0; plot < graph.y_values.length; plot++) {
var values = graph.y_values[plot][1];
// interpolate signal value at graph_x using values[index-1] and values[index]
var y1 = (index == 0) ? values[0] : values[index-1];
var y2 = values[index];
var y = y1;
if (graph_x != x1) y += (graph_x - x1)*(y2 - y1)/(x2 - x1);
// annotate plot with value of signal at marker
c.fillStyle = element_style;
c.fillText('\u2588\u2588\u2588\u2588\u2588',tx-3,ty);
c.fillStyle = probe_colors_rgb[graph.y_values[plot][0]];
c.fillText(engineering_notation(y,3,false),tx,ty);
ty += 14;
}
}
}
}
......@@ -3086,7 +3120,8 @@ schematic = (function() {
this.add_connection(0,0); // +
this.add_connection(0,16); // -
this.add_connection(48,8); // output
this.bounding_box = [0,-8,48,24];
this.add_connection(24,32); // ground
this.bounding_box = [0,-8,48,32];
this.update_coords();
}
OpAmp.prototype = new Component();
......@@ -3104,7 +3139,9 @@ schematic = (function() {
// inputs and output
this.draw_line(c,0,0,8,0);
this.draw_line(c,0,16,8,16);
this.draw_text(c,'gnd',37,18,property_size);
this.draw_line(c,40,8,48,8);
this.draw_line(c,24,16,24,32);
// + and -
this.draw_line(c,10,0,16,0);
this.draw_line(c,13,-3,13,3);
......@@ -3176,6 +3213,9 @@ schematic = (function() {
source_functions = {
'dc': ['DC value'],
'impulse': ['Height',
'Width (secs)'],
'step': ['Initial value',
'Plateau value',
'Delay until step (secs)',
......
......@@ -48,6 +48,17 @@ function postJSON(url, data, callback) {
});
}
function postJSONAsync(url, data, callback) {
$.ajax({type:'POST',
url: url,
dataType: 'json',
data: data,
success: callback,
headers : {'X-CSRFToken':getCookie('csrftoken')},
async:true
});
}
// For easy embedding of CSRF in forms
$(function() {
$('#csrfmiddlewaretoken').attr("value", getCookie('csrftoken'))
......@@ -212,7 +223,10 @@ function onPlayerStateChange(event) {
var switched_tab = false; // switch to true when we destroy so we know to call onYouTubePlayerAPIReady()
// clear pings to video status when we switch to a different sequence tab with ajax
function videoDestroy() {
function videoDestroy(id) {
// postJSON('/modx/video/'+id+'/goto_position',
// {'position' : ytplayer.getCurrentTime()});
load_id = 0;
clearInterval(updateytplayerInfoInterval);
clearInterval(ajax_videoInterval);
......@@ -283,7 +297,7 @@ function onytplayerStateChange(newState) {
}
function onPlayerError(errorCode) {
alert("An error occured: " + errorCode);
// alert("An error occured: " + errorCode);
}
function updateytplayerInfo() {
......@@ -318,6 +332,8 @@ function loadNewVideo(id, startSeconds) {
catch(e) {
window['console'].log(JSON.stringify(e));
}
//$("#slider").slider("option","value",startSeconds);
//seekTo(startSeconds);
}
function cueNewVideo(id, startSeconds) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment