This is an add-on to a previous question about iterating the values in and openMDAO problem. I want to be able to calculate values with top.run(). Then inspect the changes. However the previous advice does not seem to work.
I have found that prob.root.unknowns does not really change when the problem is modified or ran.
class TestObj(Component):
def __init__(self):
Component.__init__(self)
self.add_param('x',5.0)
self.add_output('y',5.0)
def solve_nonlinear(self,params,unknowns,resids):
unknowns['y']=params['x']
top = Problem()
root = Group()
top.root=root
root.add('split',IndepVarComp('x',2.0),promotes=['*'])
root.add('test',TestObj(),promotes=['*'])
top.setup()
top.root.list_connections()
print 'starting'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print v['val']
top.run()
print 'after first solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print v['val']
top['x']=33.0
print 'after second solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print v['val']
top.run()
print 'after third solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print v['val']
The iterator over a vec_wrapper returns an iterator over the keys, and the meta-data dictionary. The 'val' attribute you're accessing in the meta-data is actually the initial value.
Try this instead, and you'll see that it is changing:
from openmdao.api import Component, Problem, Group, IndepVarComp
class TestObj(Component):
def __init__(self):
Component.__init__(self)
self.add_param('x',5.0)
self.add_output('y',5.0)
def solve_nonlinear(self,params,unknowns,resids):
unknowns['y']=params['x']
top = Problem()
root = Group()
top.root=root
root.add('split',IndepVarComp('x',2.0),promotes=['*'])
root.add('test',TestObj(),promotes=['*'])
top.setup()
top.root.list_connections()
print 'starting'
print top['x']
print top['y']
for k,v in root.unknowns.iteritems():
print k
print top.root.unknowns[k]
top.run()
print 'after first solve'
print top['x']
print top['y']
for k,v in root.unknowns.iteritems():
print k
print top.root.unknowns[k]
top['x']=33.0
print 'after second solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print top.root.unknowns[k]
top.run()
print 'after third solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print top.root.unknowns[k]
Related
I've been trying to code an auto-clicker, but I first needed to learn how to use the Thread library, but I don't know why the code just stop when it runs the first time at the loop.
import time
from threading import Thread
def countdown(n):
while n > 0:
print('T-minus', n)
n -= 1
time.sleep(5)
t = Thread(target = countdown, args =(10, ))
t.start()
The output is only:
>>> T-minus 10
Anyone can help me?
I just understood what was going on! If you trying to run a thread on a Jupyter Cell, it won't show the output, I changed the code a bit to test it.
import time
from threading import Thread
TIMES = 0
def countdown(n):
global TIMES
while n > 0:
TIMES += 1
time.sleep(5)
t = Thread(target = countdown, args =(10, ))
t.start()
And in the next cell I just kept printing the TIMES value and it was changing! but just in the background!
for i in range(10):
time.sleep(3)
print(TIMES)
I'm trying to program the Miller-Rabin primality test in SAGE. Here is my code:
def miller(n,k):
i=1
s=(n-1).valuation(2)
t=(n-1)/(2**s)
while(i>0 and i<=k):
a=randint(3,n-3)
if gcd(a,n)!=1:
i=0
else:
y=a**t%n
if y!=1 and y!=n-1:
j=1
while(j>0 and j<=s-1 and y!=n-1):
y=y**2%n
if y==1:
j=0
else:
j=j+1
if y!=n-1:
i=0
if i>0:
i=i+1
if i==0:
print "n composite"
else:
print "n probably prime"
It works fine for not-too small numbers, however for n=3847982374893247238947238473289472348923784923748723482374832748923748932478239472389478239479273 I get "exponent must be at most 9223372036854775807". I would apreciate any advice :)
I want to print out the elements in the list in reverse order recursively.
def f3(alist):
if alist == []:
print()
else:
print(alist[-1])
f3(alist[:-1])
I know it is working well, but I don't know the difference between
return f3(alist[:-1])
and
f3(alist[:-1])
Actually both are working well.
My inputs are like these.
f3([1,2,3])
f3([])
f3([3,2,1])
There is a difference between the two although it isn't noticeable in this program. Look at the following example where all I am doing is passing a value as an argument and incrementing it thereby making it return the value once it hits 10 or greater:
from sys import exit
a = 0
def func(a):
a += 1
if a >= 10:
return a
exit(1)
else:
# Modifications are made to the following line
return func(a)
g = func(3)
print(g)
Here the output is 10
Now if I re-write the code the second way without the "return" keyword like this :
from sys import exit
a = 0
def func(a):
a += 1
if a >= 10:
return a
exit(1)
else:
# Modifications are made to the following line
func(a)
g = func(3)
print(g)
The output is "None".
This is because we are not returning any value to handle.
In short, return is like internal communication within the function that can be possibly handled versus just running the function again.
the point of my recursion function is to print integers in reverse order.
def rDisp(s):
n=str(s)
if n == "":
return n
else:
return rDisp(n[1:]) + n[0]
def main():
number=(int(input("Enter a number :")))
rDisp(num)
main()
If within the main function I implement print(reverseDisplay(number)), it works, however, for the purpose of this code, I want the reverseDisplay function to do the printing. How would I go about implementing the print function into that block of code.
Thanks!
Untested code:
def reversePrint(s):
if not s:
return
print(s[-1])
reversePrint(s[:-1])
def main():
number=input("Enter a number :")
reversePrint(number)
main()
Just got it
def reverseDisplay(s):
n=str(s)
if n == "":
return n
else:
reverseDisplay(n[1:])
b=n[0]
print(b,end='')
i wonder which hexdump() scapy uses, since i would like to modify it, but i simply cant find anything.
what i DO find is:
def hexdump(self, lfilter=None):
for i in range(len(self.res)):
p = self._elt2pkt(self.res[i])
if lfilter is not None and not lfilter(p):
continue
print "%s %s %s" % (conf.color_theme.id(i,"%04i"),
p.sprintf("%.time%"),
self._elt2sum(self.res[i]))
hexdump(p)
but that simply is an alternative for pkt.hexdump(), which does a pkt.summary() with a following hexdump(pkt)
could anyone tell me where to find the hexdump(pkt) sourcecode?
what i want to have is the hex'ed packet, almost like str(pkt[0]) (where i can check byte by byte via str(pkt[0])[0] ), but with nothing else than hexvalues, just like displayed in hexdump(pkt).
maybe you guys could help me out with this one :)
found it, so, to answer my own question, it is located in utils.py
def hexdump(x):
x=str(x)
l = len(x)
i = 0
while i < l:
print "%04x " % i,
for j in range(16):
if i+j < l:
print "%02X" % ord(x[i+j]),
else:
print " ",
if j%16 == 7:
print "",
print " ",
print sane_color(x[i:i+16])
i += 16