def test_instance(self):
     obtained = symbolic.solve([
         middle == (left + right) / 2,
         right == left + 10,
         right <= 100,
         left >= 0,
         ] for left, middle, right in symbolic.Variables)
     self.assertEqual(obtained[0].value, 90.0)
     self.assertEqual(obtained[1].value, 95.0)
     self.assertEqual(obtained[2].value, 100.0)
Beispiel #2
0
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
Beispiel #3
0
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