def process_analysis(an_list, circ, outfile, verbose, cli_tran_method=None, guess=True, disable_step_control=False): """ Processes an analysis vector: an_list: the list of analysis to be performed, as returned by netlist_parser circ: the circuit instance, returned by netlist_parser outfile: a filename. Results will be written to it. If set to stdout, prints to stdout verbose: verbosity level cli_tran_method: force the specified method in each tran analysis (see transient.py) guess: use the builtin method get_dc_guess to guess x0 Returns: None """ x0_op = None x0_ic_dict = {} results = {} for directive in [x for x in an_list if x["type"] == "ic"]: x0_ic_dict.update({ directive["name"]:\ dc_analysis.build_x0_from_user_supplied_ic(circ, voltages_dict=directive["vdict"], currents_dict=directive["cdict"]) }) for an in an_list: if outfile != 'stdout': data_filename = outfile + "." + an["type"] else: data_filename = outfile if an["type"] == "ic": continue if an["type"] == "op": if not an.has_key('guess_label') or an["guess_label"] is None: x0_op = dc_analysis.op_analysis(circ, guess=guess, data_filename=data_filename, verbose=verbose) else: if not an["guess_label"] in x0_ic_dict: printing.print_warning( "op: guess is set but no matching .ic directive was found." ) printing.print_warning( "op: using built-in guess method: " + str(guess)) x0_op = dc_analysis.op_analysis(circ, guess=guess, verbose=verbose) else: x0_op = dc_analysis.op_analysis( circ, guess=False, x0=x0_ic_dict[an["guess_label"]], verbose=verbose) sol = x0_op elif an["type"] == "dc": if an["source_name"][0].lower() == "v": elem_type = "vsource" elif an["source_name"][0].lower() == "i": elem_type = "isource" else: printing.print_general_error( "Type of sweep source is unknown: " + an[1][0]) sys.exit(1) sol = dc_analysis.dc_analysis( circ, start=an["start"], stop=an["stop"], step=an["step"], \ type_descr=(elem_type, an["source_name"][1:]), xguess=x0_op, data_filename=data_filename, guess=guess, stype=an['stype'], verbose=verbose) #{"type":"tran", "tstart":tstart, "tstop":tstop, "tstep":tstep, "uic":uic, "method":method, "ic_label":ic_label} elif an["type"] == "tran": if cli_tran_method is not None: tran_method = cli_tran_method.upper() elif an["method"] is not None: tran_method = an["method"].upper() else: tran_method = options.default_tran_method # setup the initial condition (t=0) according to uic # uic = 0 -> all node voltages and currents are zero # uic = 1 -> node voltages and currents are those computed in the last OP analysis # uic = 2 -> node voltages and currents are those computed in the last OP analysis # combined with the ic=XX directive found in capacitors and inductors # uic = 3 -> use a .ic directive defined by the user uic = an["uic"] if uic == 0: x0 = None elif uic == 1: if x0_op is None: printing.print_general_error( "uic is set to 1, but no op has been calculated yet.") sys.exit(51) x0 = x0_op elif uic == 2: if x0_op is None: printing.print_general_error( "uic is set to 2, but no op has been calculated yet.") sys.exit(51) x0 = dc_analysis.modify_x0_for_ic(circ, x0_op) elif uic == 3: if an["ic_label"] is None: printing.print_general_error( "uic is set to 3, but param ic=<ic_label> was not defined." ) sys.exit(53) elif not an["ic_label"] in x0_ic_dict: printing.print_general_error("uic is set to 3, but no .ic directive named %s was found." \ %(str(an["ic_label"]),)) sys.exit(54) x0 = x0_ic_dict[an["ic_label"]] sol = transient.transient_analysis(circ, \ tstart=an["tstart"], tstep=an["tstep"], tstop=an["tstop"], \ x0=x0, mna=None, N=None, verbose=verbose, data_filename=data_filename, \ use_step_control=(not disable_step_control), method=tran_method) elif an["type"] == "shooting": if an["method"] == "brute-force": sol = bfpss.bfpss(circ, period=an["period"], step=an["step"], mna=None, Tf=None, \ D=None, points=an["points"], autonomous=an["autonomous"], x0=x0_op, \ data_filename=data_filename, verbose=verbose) elif an["method"] == "shooting": sol = shooting.shooting(circ, period=an["period"], step=an["step"], mna=None, \ Tf=None, D=None, points=an["points"], autonomous=an["autonomous"], \ data_filename=data_filename, verbose=verbose) elif an["type"] == "symbolic": if not 'subs' in an.keys(): an.update({'subs': None}) sol = symbolic.solve(circ, an['source'], opts={'ac': an['ac']}, subs=an['subs'], verbose=verbose) elif an["type"] == "ac": sol = ac.ac_analysis(circ=circ, start=an['start'], nsteps=an['nsteps'], \ stop=an['stop'], step_type='LOG', xop=x0_op, mna=None,\ data_filename=data_filename, verbose=verbose) elif an["type"] == "temp": constants.T = utilities.Celsius2Kelvin(an['temp']) results.update({an["type"]: sol}) return results
def shooting(circ, period, step=None, mna=None, Tf=None, D=None, points=None, autonomous=False, data_filename='stdout', vector_norm=lambda v: max(abs(v)), verbose=3): """Performs a periodic steady state analysis based on the algorithm described in Brambilla, A.; D'Amore, D., "Method for steady-state simulation of strongly nonlinear circuits in the time domain," Circuits and Systems I: Fundamental Theory and Applications, IEEE Transactions on, vol.48, no.7, pp.885-889, Jul 2001 URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=933329&isnumber=20194 The results have been computed again by me, the formulas are not exactly the same, but the idea behind the shooting algorithm is. This method allows us to have a period with many points without having to invert a huge matrix (and being limited to the maximum matrix size). A tran is performed to initialize the solver. We compute the change in the last point, calculating several matrices in the process. From that, with the same matrices we calculate the changes in all points, starting from 0 (which is the same as the last one), then 1, ... Key points: - Only not autonomous circuits are supported. - The time step is constant - Implicit euler is used as DF Parameters: circ is the circuit description class period is the period of the solution mna, D, Tf are not compulsory they will be computed if they're set to None step is the time step between consecutive points points is the number of points to be used step and points are mutually exclusive options: - if step is specified, the number of points will be automatically determined - if points is set, the step will be automatically determined - if none of them is set, options.shooting_default_points will be used as points autonomous has to be False, autonomous circuits are not supported data_filename is the output filename. Defaults to stdout. verbose is set to zero (print errors only) if datafilename == 'stdout'. Returns: nothing """ if data_filename == "stdout": verbose = 0 printing.print_info_line(("Starting periodic steady state analysis:", 3), verbose) printing.print_info_line(("Method: shooting", 3), verbose) if mna is None or Tf is None: (mna, Tf) = dc_analysis.generate_mna_and_N(circ) mna = utilities.remove_row_and_col(mna) Tf = utilities.remove_row(Tf, rrow=0) elif not mna.shape[0] == Tf.shape[0]: printing.print_general_error( "mna matrix and N vector have different number of rows.") sys.exit(0) if D is None: D = transient.generate_D(circ, [mna.shape[0], mna.shape[0]]) D = utilities.remove_row_and_col(D) elif not mna.shape == D.shape: printing.print_general_error( "mna matrix and D matrix have different sizes.") sys.exit(0) (points, step) = check_step_and_points(step, points, period) print "points", points print "step", step n_of_var = mna.shape[0] locked_nodes = circ.get_locked_nodes() printing.print_info_line(( "Starting transient analysis for algorithm init: tstop=%g, tstep=%g... " % (10 * points * step, step), 3), verbose, print_nl=False) xtran = transient.transient_analysis(circ=circ, tstart=0, tstep=step, tstop=10*points*step, method="TRAP", x0=None, mna=mna, N=Tf, \ D=D, use_step_control=False, data_filename=data_filename+".tran", return_req_dict={"points":points}, verbose=0) if xtran is None: print "failed." return None printing.print_info_line(("done.", 3), verbose) x = [] for index in range(points): x.append(xtran[index * n_of_var:(index + 1) * n_of_var, 0]) tick = ticker.ticker(increments_for_step=1) MAass_static, MBass = build_static_MAass_and_MBass(mna, D, step) # This contains # the time invariant part, Tf # time variable component: Tt this is always the same, since the time interval is the same # this holds all time-dependent sources (both V/I). Tass_static_vector = build_Tass_static_vector(circ, Tf, points, step, tick, n_of_var, verbose) converged = False printing.print_info_line(("Solving... ", 3), verbose, print_nl=False) tick.reset() tick.display(verbose > 2) iteration = 0 # newton iteration counter conv_counter = 0 while True: dx = [] Tass_variable_vector = [] MAass_variable_vector = [] for index in range(points): if index == 0: xn_minus_1 = x[points - 1] else: xn_minus_1 = x[index - 1] MAass_variable, Tass_variable = get_variable_MAass_and_Tass( circ, x[index], xn_minus_1, mna, D, step, n_of_var) MAass_variable_vector.append(MAass_variable + MAass_static) Tass_variable_vector.append(Tass_variable + Tass_static_vector[index]) dxN = compute_dxN(circ, MAass_variable_vector, MBass, Tass_variable_vector, n_of_var, points, verbose=verbose) td = dc_analysis.get_td(dxN, locked_nodes, n=-1) x[points - 1] = td * dxN + x[points - 1] for index in range(points - 1): if index == 0: dxi_minus_1 = dxN else: dxi_minus_1 = dx[index - 1] dx.append( compute_dx(MAass_variable_vector[index], MBass, Tass_variable_vector[index], dxi_minus_1)) td = dc_analysis.get_td(dx[index], locked_nodes, n=-1) x[index] = td * dx[index] + x[index] dx.append(dxN) if (vector_norm_wrapper(dx, vector_norm) < min(options.ver, options.ier) * vector_norm_wrapper( x, vector_norm) + min(options.vea, options.iea)): #\ #and (dc_analysis.vector_norm(residuo) < options.er*dc_analysis.vector_norm(x) + options.ea): if conv_counter == 3: converged = True break else: conv_counter = conv_counter + 1 elif vector_norm(dx[points - 1]) is numpy.nan: #needs work fixme raise OverflowError #break else: conv_counter = 0 tick.step(verbose > 2) if options.shooting_max_nr_iter and iteration == options.shooting_max_nr_iter: printing.print_general_error("Hitted SHOOTING_MAX_NR_ITER (" + str(options.shooting_max_nr_iter) + "), iteration halted.") converged = False break else: iteration = iteration + 1 tick.hide(verbose > 2) if converged: printing.print_info_line(("done.", 3), verbose) t = numpy.mat(numpy.arange(points) * step) t = t.reshape((1, points)) xmat = x[0] for index in xrange(1, points): xmat = numpy.concatenate((xmat, x[index]), axis=1) sol = results.pss_solution(circ=circ, method="shooting", period=period, outfile=data_filename, t_array=t, x_array=xmat) #print_results(circ, x, fdata, points, step) else: print "failed." sol = None return sol
def shooting(circ, period, step=None, x0=None, points=None, autonomous=False, mna=None, Tf=None, D=None, outfile='stdout', vector_norm=lambda v: max(abs(v)), verbose=3): """Performs a periodic steady state analysis based on the algorithm described in Brambilla, A.; D'Amore, D., "Method for steady-state simulation of strongly nonlinear circuits in the time domain," Circuits and Systems I: Fundamental Theory and Applications, IEEE Transactions on, vol.48, no.7, pp.885-889, Jul 2001 URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=933329&isnumber=20194 The results have been computed again by me, the formulas are not exactly the same, but the idea behind the shooting algorithm is. This method allows us to have a period with many points without having to invert a huge matrix (and being limited to the maximum matrix size). A tran is performed to initialize the solver. We compute the change in the last point, calculating several matrices in the process. From that, with the same matrices we calculate the changes in all points, starting from 0 (which is the same as the last one), then 1, ... Key points: - Only not autonomous circuits are supported. - The time step is constant - Implicit euler is used as DF Parameters: circ is the circuit description class period is the period of the solution mna, D, Tf are not compulsory they will be computed if they're set to None step is the time step between consecutive points points is the number of points to be used step and points are mutually exclusive options: - if step is specified, the number of points will be automatically determined - if points is set, the step will be automatically determined - if none of them is set, options.shooting_default_points will be used as points autonomous has to be False, autonomous circuits are not supported outfile is the output filename. Defaults to stdout. verbose is set to zero (print errors only) if datafilename == 'stdout'. Returns: nothing """ if outfile == "stdout": verbose = 0 printing.print_info_line( ("Starting periodic steady state analysis:", 3), verbose) printing.print_info_line(("Method: shooting", 3), verbose) if isinstance(x0, results.op_solution): x0 = x0.asmatrix() if mna is None or Tf is None: (mna, Tf) = dc_analysis.generate_mna_and_N(circ, verbose=verbose) mna = utilities.remove_row_and_col(mna) Tf = utilities.remove_row(Tf, rrow=0) elif not mna.shape[0] == Tf.shape[0]: printing.print_general_error( "mna matrix and N vector have different number of rows.") sys.exit(0) if D is None: D = transient.generate_D(circ, [mna.shape[0], mna.shape[0]]) D = utilities.remove_row_and_col(D) elif not mna.shape == D.shape: printing.print_general_error( "mna matrix and D matrix have different sizes.") sys.exit(0) (points, step) = check_step_and_points(step, points, period) print "points", points print "step", step n_of_var = mna.shape[0] locked_nodes = circ.get_locked_nodes() printing.print_info_line( ("Starting transient analysis for algorithm init: tstop=%g, tstep=%g... " % (10 * points * step, step), 3), verbose, print_nl=False) xtran = transient.transient_analysis( circ=circ, tstart=0, tstep=step, tstop=10 * points * step, method="TRAP", x0=None, mna=mna, N=Tf, D=D, use_step_control=False, outfile=outfile + ".tran", return_req_dict={"points": points}, verbose=0) if xtran is None: print "failed." return None printing.print_info_line(("done.", 3), verbose) x = [] for index in range(points): x.append(xtran[index * n_of_var:(index + 1) * n_of_var, 0]) tick = ticker.ticker(increments_for_step=1) MAass_static, MBass = build_static_MAass_and_MBass(mna, D, step) # This contains # the time invariant part, Tf # time variable component: Tt this is always the same, since the time interval is the same # this holds all time-dependent sources (both V/I). Tass_static_vector = build_Tass_static_vector( circ, Tf, points, step, tick, n_of_var, verbose) converged = False printing.print_info_line(("Solving... ", 3), verbose, print_nl=False) tick.reset() tick.display(verbose > 2) iteration = 0 # newton iteration counter conv_counter = 0 while True: dx = [] Tass_variable_vector = [] MAass_variable_vector = [] for index in range(points): if index == 0: xn_minus_1 = x[points - 1] else: xn_minus_1 = x[index - 1] MAass_variable, Tass_variable = get_variable_MAass_and_Tass( circ, x[index], xn_minus_1, mna, D, step, n_of_var) MAass_variable_vector.append(MAass_variable + MAass_static) Tass_variable_vector.append( Tass_variable + Tass_static_vector[index]) dxN = compute_dxN(circ, MAass_variable_vector, MBass, Tass_variable_vector, n_of_var, points, verbose=verbose) td = dc_analysis.get_td(dxN, locked_nodes, n=-1) x[points - 1] = td * dxN + x[points - 1] for index in range(points - 1): if index == 0: dxi_minus_1 = dxN else: dxi_minus_1 = dx[index - 1] dx.append( compute_dx(MAass_variable_vector[index], MBass, Tass_variable_vector[index], dxi_minus_1)) td = dc_analysis.get_td(dx[index], locked_nodes, n=-1) x[index] = td * dx[index] + x[index] dx.append(dxN) if (vector_norm_wrapper(dx, vector_norm) < min(options.ver, options.ier) * vector_norm_wrapper(x, vector_norm) + min(options.vea, options.iea)): # \ # and (dc_analysis.vector_norm(residuo) < # options.er*dc_analysis.vector_norm(x) + options.ea): if conv_counter == 3: converged = True break else: conv_counter = conv_counter + 1 elif vector_norm(dx[points - 1]) is numpy.nan: # needs work fixme raise OverflowError # break else: conv_counter = 0 tick.step(verbose > 2) if options.shooting_max_nr_iter and iteration == options.shooting_max_nr_iter: printing.print_general_error( "Hitted SHOOTING_MAX_NR_ITER (" + str(options.shooting_max_nr_iter) + "), iteration halted.") converged = False break else: iteration = iteration + 1 tick.hide(verbose > 2) if converged: printing.print_info_line(("done.", 3), verbose) t = numpy.mat(numpy.arange(points) * step) t = t.reshape((1, points)) xmat = x[0] for index in xrange(1, points): xmat = numpy.concatenate((xmat, x[index]), axis=1) sol = results.pss_solution( circ=circ, method="shooting", period=period, outfile=outfile, t_array=t, x_array=xmat) # print_results(circ, x, fdata, points, step) else: print "failed." sol = None return sol
def process_analysis(an_list, circ, outfile, verbose, cli_tran_method=None, guess=True, disable_step_control=False): """ Processes an analysis vector: an_list: the list of analysis to be performed, as returned by netlist_parser circ: the circuit instance, returned by netlist_parser outfile: a filename. Results will be written to it. If set to stdout, prints to stdout verbose: verbosity level cli_tran_method: force the specified method in each tran analysis (see transient.py) guess: use the builtin method get_dc_guess to guess x0 Returns: None """ x0_op = None x0_ic_dict = {} results = {} for directive in [ x for x in an_list if x["type"] == "ic" ]: x0_ic_dict.update({ directive["name"]:\ dc_analysis.build_x0_from_user_supplied_ic(circ, voltages_dict=directive["vdict"], currents_dict=directive["cdict"]) }) for an in an_list: if outfile != 'stdout': data_filename = outfile + "." + an["type"] else: data_filename = outfile if an["type"] == "ic": continue if an["type"] == "op": if not an.has_key('guess_label') or an["guess_label"] is None: x0_op = dc_analysis.op_analysis(circ, guess=guess, data_filename=data_filename, verbose=verbose) else: if not an["guess_label"] in x0_ic_dict: printing.print_warning("op: guess is set but no matching .ic directive was found.") printing.print_warning("op: using built-in guess method: "+str(guess)) x0_op = dc_analysis.op_analysis(circ, guess=guess, verbose=verbose) else: x0_op = dc_analysis.op_analysis(circ, guess=False, x0=x0_ic_dict[an["guess_label"]], verbose=verbose) sol = x0_op elif an["type"] == "dc": if an["source_name"][0].lower() == "v": elem_type = "vsource" elif an["source_name"][0].lower() == "i": elem_type = "isource" else: printing.print_general_error("Type of sweep source is unknown: " + an[1][0]) sys.exit(1) sol = dc_analysis.dc_analysis( circ, start=an["start"], stop=an["stop"], step=an["step"], \ type_descr=(elem_type, an["source_name"][1:]), xguess=x0_op, data_filename=data_filename, guess=guess, stype=an['stype'], verbose=verbose) #{"type":"tran", "tstart":tstart, "tstop":tstop, "tstep":tstep, "uic":uic, "method":method, "ic_label":ic_label} elif an["type"] == "tran": if cli_tran_method is not None: tran_method = cli_tran_method.upper() elif an["method"] is not None: tran_method = an["method"].upper() else: tran_method = options.default_tran_method # setup the initial condition (t=0) according to uic # uic = 0 -> all node voltages and currents are zero # uic = 1 -> node voltages and currents are those computed in the last OP analysis # uic = 2 -> node voltages and currents are those computed in the last OP analysis # combined with the ic=XX directive found in capacitors and inductors # uic = 3 -> use a .ic directive defined by the user uic = an["uic"] if uic == 0: x0 = None elif uic == 1: if x0_op is None: printing.print_general_error("uic is set to 1, but no op has been calculated yet.") sys.exit(51) x0 = x0_op elif uic == 2: if x0_op is None: printing.print_general_error("uic is set to 2, but no op has been calculated yet.") sys.exit(51) x0 = dc_analysis.modify_x0_for_ic(circ, x0_op) elif uic == 3: if an["ic_label"] is None: printing.print_general_error("uic is set to 3, but param ic=<ic_label> was not defined.") sys.exit(53) elif not an["ic_label"] in x0_ic_dict: printing.print_general_error("uic is set to 3, but no .ic directive named %s was found." \ %(str(an["ic_label"]),)) sys.exit(54) x0 = x0_ic_dict[an["ic_label"]] sol = transient.transient_analysis(circ, \ tstart=an["tstart"], tstep=an["tstep"], tstop=an["tstop"], \ x0=x0, mna=None, N=None, verbose=verbose, data_filename=data_filename, \ use_step_control=(not disable_step_control), method=tran_method) elif an["type"] == "shooting": if an["method"]=="brute-force": sol = bfpss.bfpss(circ, period=an["period"], step=an["step"], mna=None, Tf=None, \ D=None, points=an["points"], autonomous=an["autonomous"], x0=x0_op, \ data_filename=data_filename, verbose=verbose) elif an["method"]=="shooting": sol = shooting.shooting(circ, period=an["period"], step=an["step"], mna=None, \ Tf=None, D=None, points=an["points"], autonomous=an["autonomous"], \ data_filename=data_filename, verbose=verbose) elif an["type"] == "symbolic": if not 'subs' in an.keys(): an.update({'subs':None}) sol = symbolic.solve(circ, an['source'], opts={'ac':an['ac']}, subs=an['subs'], verbose=verbose) elif an["type"] == "ac": sol = ac.ac_analysis(circ=circ, start=an['start'], nsteps=an['nsteps'], \ stop=an['stop'], step_type='LOG', xop=x0_op, mna=None,\ data_filename=data_filename, verbose=verbose) elif an["type"] == "temp": constants.T = utilities.Celsius2Kelvin(an['temp']) results.update({an["type"]:sol}) return results