2021年10月29日 星期五

利用desktopproxy中斷.aedt模擬

我們可以用指令執行aedt模擬,比如說

set path=C:\Program Files\AnsysEM\v221\Win64;%path%

start ansysedt.exe -ng -BatchSolve .\package.aedt

上述案例執行時間4分17秒。 

接下來我們用ansysedt指令執行,2分鐘後以desktopproxy -abort中斷模擬。

set path=C:\Program Files\AnsysEM\v221\Win64;%path%

start ansysedt.exe -ng -BatchSolve .\package.aedt

PING localhost -n 120 >NUL

desktopproxy -abort .\package.aedt

中斷之後,再重啟模擬到模擬結束共2分22秒

set path=C:\Program Files\AnsysEM\v221\Win64;%path%

start ansysedt.exe -ng -BatchSolve .\package.aedt

總共花了4分22秒,比一次完成執行僅僅多了約5秒。代表中斷之後重啟模擬是延續之前中斷之前的狀態,而非重新開始。

Steamlit手動排序項目


import time
import streamlit as st


def submit_up(n):
if n > 0:
x = st.session_state.waiting_list[n]
y = st.session_state.waiting_list[n - 1]
st.session_state.waiting_list[n - 1] = x
st.session_state.waiting_list[n] = y


def submit_down(n):
if n < len(st.session_state.waiting_list) - 1:
x = st.session_state.waiting_list[n]
y = st.session_state.waiting_list[n + 1]
st.session_state.waiting_list[n + 1] = x
st.session_state.waiting_list[n] = y


def submit_top(n):
st.session_state.waiting_list = [st.session_state.waiting_list.pop(n)] + st.session_state.waiting_list


def submit_bottom(n):
st.session_state.waiting_list = st.session_state.waiting_list + [st.session_state.waiting_list.pop(n)]


def delete(n):
st.session_state.waiting_list.pop(n)


if 'waiting_list' not in st.session_state:
st.session_state.waiting_list = ['A1', 'B2', 'C3', 'D4']

for n, i in enumerate(st.session_state.waiting_list):
with st.expander(i):
c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 = st.columns(10)
with c1:
st.button('▲', key=f'move up_{i}', on_click=submit_up, args=(n,))
with c2:
st.button('▼', key=f'move down_{i}', on_click=submit_down, args=(n,))
with c3:
st.button('Top', key=f'move up_{i}', on_click=submit_top, args=(n,))
with c4:
st.button('Bottom', key=f'move down_{i}', on_click=submit_bottom, args=(n,))
with c8:
st.button('Delete', key=f'delete_{i}', on_click=delete, args=(n,))
(圖一)手動排序選單


如何用程式碼更改AEDT選項設定

AEDT當中所有的設定都存放在XML檔案當中:C:\Users\<UserName>\Documents\Ansoft\<AnsysProductNameversion>\config\<PC_NAME>_user.XML,比方HPC的設定等等。可以透過指令修改,比方:oDesktop.SetRegistryString('Desktop\Settings\ProjectOptions\HPCLicenseType' , 'Pool')。

處理機碼的指令包含如下:

  • DeleteRegistryEntry
  • DoesRegistryValueExist
  • GetRegistryInt
  • GetRegistryString
  • SetRegistryFromFile
  • SetRegistryInt
  • SetRegistryString

其他像是AEDT Option當中的選項設定也可以透過上述的函式來讀取或修改。

(圖一)AEDT選項設定視窗


2021年10月25日 星期一

不同資料結構對應之表格


import pandas as pd
import streamlit as st
from streamlit_autorefresh import st_autorefresh
import json
count = st_autorefresh(interval=1000)

x = {'name':['a','b','c'], 'Year':[4,5,6]}
st.table(pd.DataFrame(x))

y = [{'name':'a', 'Year':4}, {'name':'b', 'Year':5}, {'name':'c', 'Year':6}]
st.table(pd.DataFrame(y))

z = {'John':{'name':'a', 'Year':'4'}, 'Mark':{'name':'b', 'Year':'5'}, 'Eddy':{'name':'c', 'Year':'6'}}
st.table(pd.DataFrame(z))

w = {'John':{'name':'a', 'Year':4}, 'Mark':{'name':'b', 'Year':5}, 'Eddy':{'name':'c', 'Year':6}}
st.table(pd.DataFrame(w).T)
st.dataframe(pd.DataFrame(w).T)




2021年10月20日 星期三

透過頁面監視檔案夾

 

(圖一)頁面監看檔案夾

import streamlit as st
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from streamlit_autorefresh import st_autorefresh

count = st_autorefresh(interval=1000)

class MyHandler(FileSystemEventHandler):
def on_created(self, event):
with open('state.log', 'a') as f:
f.writelines("on_created: {}\n".format(event.src_path))

def on_deleted(self, event):
with open('state.log', 'a') as f:
f.writelines("on_deleted: {}\n".format(event.src_path))

if 'event_handler' not in st.session_state:
st.session_state.event_handler = MyHandler()
st.session_state.observer = Observer()
st.session_state.observer.schedule(st.session_state.event_handler, path='d:/demo', recursive=True)
st.session_state.observer.start()

try:
with open('state.log') as f:
text = f.readlines()
st.text(''.join(text))
except:
pass

如何在SIwave匯出Pin Group的設定,並在另一個設計匯入

在SIwave匯出Pin Group的設定,並在另一個設計匯入。

(圖一) SIwave Pin Group設定

exportPG_v2.py

import os
import json
import logging

os.chdir(os.path.dirname(__file__))
logging.basicConfig(filename='simulation.log', encoding='utf-8', filemode='w', level=logging.DEBUG)

oDoc = oApp.GetActiveProject()
oDoc.ScrExportComponentFile('temp.cmp')
with open('temp.cmp') as f:
text = f.readlines()

pg = {}
for line in text:
if line.startswith('B_IC') or line.startswith('B_IO'):
_, refdes, part = line.split()

if line.startswith('B_PIN_GROUP '):
group_name = line.split()[1]
key = ','.join([part[1:-1], refdes[1:-1], group_name])
continue

if 'E_PIN_GROUP' in line:
if 'key' in locals():
del (key)
continue

if 'key' in locals():
pg[key] = line.split()

logging.info(pg)

with open('pg_info.json', 'w') as f:
json.dump(pg, f, indent=4)

importPG_v2.py

import os
import json
import logging

os.chdir(os.path.dirname(__file__))
logging.basicConfig(filename='simulation.log', encoding='utf-8', filemode='w', level=logging.DEBUG)

oDoc = oApp.GetActiveProject()
with open('pg_info.json') as f:
data = json.load(f)

logging.info(data)
for key, pins in data.items():
part_name, refdes_name, group_name = key.split(',')
oDoc.ScrCreatePinGroups(part_name, refdes_name, pins, group_name, False)

2021年10月16日 星期六

License Increment簡化

# -*- coding: utf-8 -*-
import streamlit as st

st.header("License Increment輸出工具")
st.markdown("**從自己的license檔案取得與客戶license相同的Increments建立新的license。注意:數量無法改變!**")
large_license = st.file_uploader("上傳自己的ansyslmd.lic")
small_license = st.file_uploader("上傳客戶的ansyslmd.lic")

if large_license and small_license:
header = []
license_items = {}

text = large_license.getvalue().decode("utf-8").splitlines()

for line in text:
if line.startswith('INCREMENT'):
feature = line.split()[1]
license_items[feature] = [line]
else:
try:
license_items[feature].append(line)
except:
header.append(line)

text = small_license.getvalue().decode("utf-8").splitlines()

extracted_features = []
for line in text:
if line.startswith('INCREMENT'):
feature = line.split()[1]
extracted_features.append(feature)

text = '\n'.join(header) + '\n'
for feature in extracted_features:
text += '\n'.join(license_items[feature]) + '\n'
st.subheader('{} Increments 輸出:'.format(len(extracted_features)))
st.write(',\n'.join(extracted_features))

st.download_button('下載檔案', file_name='new_ansyslmd.lic', data=text)

2021年10月9日 星期六

Python虛擬環境安裝及啟動

Python安裝完畢之後,我們只有一個非常基本的環境。除了Python命令視窗,沒有Spyder編輯器,也沒有numpy, scipy, matplotlib, pyqt等模組。這時候我們可以建立一組全新的虛擬環境。虛擬環境是最單純的環境,當中只有最小的Python核心。接著便可以在虛擬環境安裝之後開發需要用的套件模組。

首先到Python目錄底下,執行下面指令在env03建立虛擬環境。底下顯示的是在筆者安裝Python3.8的目錄底下建立虛擬環境。

C:\Users\mlin\AppData\Local\Programs\Python\Python38> .\python -m venv D:\demo_env\env03

接著切換目錄到虛擬環境的Script目錄當中,執行activate,便可以進入虛擬環境的console,此時輸入行前端會出現(env03)。

(圖二) activate進入虛擬環境

接著便可以繼續執行pip install matplotlib, pip install spyder, pip install pyqt5, pip install pyqt5-tools等等指令來安裝必要套件。安裝套件需耗時數分鐘至數十分鐘不等。

(圖三)pip install安裝套件

完成之後便可以在虛擬環境當中執行python, spyder, jupyter notebook等指令來使用工具。也可以為這些工具建立捷徑方便之後使用。虛擬環境不需要時,可直接刪除該目錄即可。

2021年10月7日 星期四

用ansysedt.exe讀取網表做電路模擬

之前介紹過用nexxim.exe讀取網表作電路模擬,輸出.sdf並將.sdf轉成.csv。缺點是只能支援線性模擬及暫態模擬。且線性模擬只能輸出S參數,不能輸出Y/Z參數。除此之外,必須自行編寫程式來輸出Touchstone格式,非常不方便。本篇做法可以處理AEDT的各類電路模擬,並且以.csv輸出任意AEDT本身支援的模擬資料,如頻譜,眼圖等等。

1. 建立AEDT專案test.aedt,加入一個設計CircuitNetlist1,當中只有一行: .INCLUDE "D:\demo\test.cir",如下圖。並在專案當中加入"Z Parameter Table 1"的表格報告。完成之後關閉aedt。

(圖一)test.aedt專案包含CircuitNetlist1設計

2. 文字檔test.cir當中包含完整的網表及模擬設定,例如:

C1 Port1 Port2 1e-3
RPort1 Port1 0 PORTNUM=1 RZ=50 IZ=0
.PORT Port1 0 1 RPort1
RPort2 Port2 0 PORTNUM=2 RZ=50 IZ=0
.PORT Port2 0 2 RPort2

* end toplevel circuit
.LNA
+ LIN 101 0 1000000000
+ FLAG='LNA'

3.建立.py檔,執行模擬及.csv輸出,如下

oProject = oDesktop.OpenProject("d:/demo/test.aedt")
oDesign = oProject.SetActiveDesign("CircuitNetlist1")
oDesign.AnalyzeAll()
oModule = oDesign.GetModule("ReportSetup")

oModule.ExportToFile("Z Parameter Table 1", "D:/demo/Z Parameter Table 1.csv", False)

4.執行ansysedt命令輸出.csv檔

SET PATH=C:\Program Files\AnsysEM\AnsysEM21.1\Win64
ansysedt.exe -features=beta -ng -waitforlicense -RunScriptAndExit .\test.py

自動化程式可以透過修改test.cir當中的網表內容並執行ansysedt命令來取得模擬結果.csv檔來製作報告。

2021年10月1日 星期五

如何在HTML嵌入圖片?

我們可以將圖片的Base64碼直接寫在html當中,就可以不用另外傳送圖片檔了。

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYsAAAB/CAMAAAApSh4CAAAAt1BMVEX///8AAAD8tCbQ0NC2trZYWFgaGhqBgYH8sRT8sAnw8PD91pv+5r7Y2Ng5OTn7+/v19fXj4+PFxcXp6empqakrKytdXV3e3t5jY2Ps7OzLy8uJiYlGRkaPj4+oqKgyMjJxcXGysrKcnJwkJCRLS0t6enoSEhK/v7+YmJhRUVH9x2f+8Nj91ZEnJydtbW1AQED+68v8v0/+3638uj3/+u/90YX9y3P+7dH+9eX8wlr+3qn8vEb8ti/q75xwAAAMe0lEQVR4nO2deWPaOBDFQ4GQ0ALmCOSAcDRAstsk3bZ7dNvv/7k22Bhbnvd02AZ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