2017-07-14 45 views
0

我可以設置components'solve_nonlinear函數中的未知數和殘差。我也可以設置參數的值嗎?爲什麼或者爲什麼不?我可以在openmdao的solve_nonlinear中更改參數值嗎?

編輯

這是我在 「純Python」 讀/寫器組件的嘗試。我的問題是我無法從頂層讀取/寫入參數。

$ cat test.py 
from openmdao.api import Component, Group, Problem 

class reader(): 
    def __init__(self): 
     self.file_to_read = 'test.in' 
     self.file_data = 0 
    def execute(self): 
     dat = open(self.file_to_read, 'r') 
     self.file_data = dat.read() 

class writer(): 
    def __init__(self): 
     self.file_to_write = 'test.out' 
     self.data = -99 
    def execute(self): 
     dat = open(self.file_to_write, 'w') 
     dat.write(str(self.data)) 

class ReadWriteComp(Component): 
    def __init__(self): 
     super(ReadWriteComp, self).__init__() 
     self.reader = reader() 
     self.writer = writer() 
     self.reader.execute() 

    def solve_nonlinear(self, params, unknowns, resids): 
     self.writer.data = self.reader.file_data 
     self.writer.execute() 

root = Group() 
root.add('testio', ReadWriteComp()) 
prob = Problem(root) 
prob.setup() 
prob['testio.writer.file_to_write'] = 'newname' # "Variable 'testio.writer.file_to_write' not found." 
prob.run() 


$ cat test.in 
8 
+0

嗯我從來沒有嘗試過。我懷疑你最終會通過這樣做破壞衍生信息。 這聽起來像是你問一個關於解決更一般問題的具體方法的問題。你究竟想要做什麼? –

+0

@RobFalck我正在構建一個包含讀,寫和封裝組件的模擬代碼的包裝。我正在盡力模仿1.X中的vartrees。 https://github.com/WISDEM/AeroelasticSE/blob/turbsim_connection/src/AeroelasticSE/FAST_mdao/FST8_group.py#L78我的問題是,在這個「vartree」fst_vt對象中,未知數是參數:它們必須是由讀者設置並由作者使用。 – kilojoules

回答

1

參數是傳入組件的值。它們是外部提供的信息。由於這種外部性,你不能/不應該改變它們。

表示另一種方式: 如果您有傳入連接,那麼您的參數值由該上游組件的輸出(源)定義。改變你的參數就像改變上游組件的輸出一樣。

+0

我的問題是我的未知數是參數。我希望一個組件在模板文件中讀取並相應地設置參數,另一個組件使用這些參數來寫入輸入文件。有很多參數,我想使這個模塊化,因爲只有一組東西必須進行編輯,以將項目添加到讀/寫過程中...... – kilojoules

0

我不能發表評論,因爲我的代表太低,但這更多的是對讀/寫組件的評論,而不是對問題的回答。我建議使讀/寫和包裝類爲純python,然後在頂層使用openmdao,並使用一個執行所有三個的組件,如果需要並行化案例,可能還會包含一個組。

+0

我更新我的問題以顯示我的問題。我無法從頂層設置寫入參數。我習慣於讀取輸出的vartree並用相同的vartree編寫輸出。看起來這不再是一個選項,並且需要一個更復雜的方案來設置頂層的寫入參數。 – kilojoules

+0

到'testio'實例的路徑是'prob.root.testio.writer.file_to_write'。我猜一些要編寫的輸入將取決於上游組件計算的輸出,爲此,我只是明確地將這些參數作爲參數添加到「ReadWriteComp」中。 – frza

相關問題