How to use python to call ads interface

Posted by satyac46 on Tue, 11 Jan 2022 17:17:29 +0100

background

Recently, I was doing the reverse design of RF devices. I needed to use python to call ads to generate data, so I asked my senior brother for advice. After stepping on many pits, I finally succeeded. Record the whole process

Environment introduction

ads 2017 (elder martial brother uses 2021. I think 2017 and later versions can be used)
python3.6 (mainly using os)

Specific operation process

ads help documentation


In the official document of ads, this article completely describes how to use the ads interface (i.e. perform layout simulation in the form of command line). If you encounter any problems, please find here.

em generate simulation initialization file


The generated folder looks like this

The path to this folder is in the simulation folder of the workspace
For example: D: / < your ads workspace > / simulation / ****** / ***** / layout

Configuration environment and testing

Environment variable configuration under windows

set HPEESOF_DIR=<path_to_ADS_installation_directory> # for example: set HPEESOF_DIR=C:\Progam Files\Keysight\ADS2014_11
set ADS_LICENSE_FILE=<path_to_ADS_license_file_or_server> # for example: set ADS_LICENSE_FILE=C:\Program Files\Keysight\EEsof_License_Tools\licenses\license.lic
set PATH=%HPEESOF_DIR%\bin;%PATH% 

Note: do not use these commands directly in the cmd console. They are only temporary variables and need to be configured in the environment variables themselves;

There is a small hole here. If you don't have a lincense file when cracking, you don't need to configure the certificate. You only need to configure the first three to use it (that's what I do. There is no lincense when cracking)
Remember to restart the computer after configuration (very important!!!)

Test after configuration

Start simulation

This part is nothing to say, as long as cd to the specified target folder, and then call the command can be.
But there are two kinds of commands

# Command format: adsMomWrapper -T proj proj 
Substrate file pre-processing (e.g. expanding substrate for thick conductors)
-T

Substrate database generation (Green function calculation)
-DB

Layout pre-processing and mesh generation
-M

S-parameter model generation
-3D

Far Field calculation
-FF

# Another is this, that is, continuous execution, from - T execution to - 3D- 3D is our commonly used s-parameter simulation
adsMomWrapper -O -3D proj proj 

Because I recently upgraded win11, an error will be reported if I use continuously executed commands.. Can only be written separately. As shown in the following figure (using os package and ads interface command in python)

The simulated files become like this (some files)

The file of s parameters after simulation is saved in In the cti file.
Note: different type s of simulation generate different s parameter files;
The adaptive pattern generates afs file, others have not been measured.

The above situation is a single simulation. If it is necessary to change the device parameters (such as microstrip line length / width) for continuous simulation, it needs to be modified
proj_a and proj Pin file
proj_a inside is the position of the device. Take a single microstrip line as an example, one line is the coordinates of four rectangular points of the microstrip line, which needs to be changed according to the actual needs
proj.pin saves the coordinates of the p needle, which should be consistent with proj_a corresponds to the port of the device
For other requirements, look at the documents according to examples. I found them one by one.

Complete process and supplement

Sort out the whole process and add a pit
1. Configure environment variables and restart the computer.
2. Open python, open the file, and modify the internal device parameters
3. Use OS System, cd to the simulation folder of the target, and then use the ads command to simulate.
4. If ads cannot be simulated continuously, that is, after the first simulation, there is no simulation after executing the command for the next few times, but it is the same as the simulation data for the first time, indicating that continuous simulation cannot be performed. At this time, the redundant files in the file can be deleted, so that it is normal for the next simulation.
(for specific file comparison, please write it yourself. Different simulation files generate different files)

As for how to save data, it depends on everyone's needs
The complete code is shown below (as an example)

import os
import math
import numpy as np
import pandas as pd


# liner correspondence Corresponding to cti adapt afs extracts S parameters and reads S parameters
def read_cti(path: str):
    print(path)
    read_c = open(path + "/proj.cti")
    data = read_c.read()
    read_c.close()
    data = data.split("BEGIN\n")
    s_str = data[2].split("END")
    s11_str = s_str[0].replace("\t", "")
    s11_str = s11_str.split("\n")
    s_str = data[4].split("END")
    s21_str = s_str[0].replace("\t", "")
    s21_str = s21_str.split("\n")

    s11_r = []
    s11_i = []
    s21_r = []
    s21_i = []
    s21_phase = []
    for i in range(len(s11_str) - 1):
        s11_ri = s11_str[i].split(",")
        s21_ri = s21_str[i].split(",")
        s11_r.append(float(s11_ri[0]))
        s11_i.append(float(s11_ri[1]))
        s21_r.append(float(s21_ri[0]))
        s21_i.append(float(s21_ri[1]))
        s21_phase.append(math.atan2(s21_i[i], s21_r[i]) * 180 / math.pi)
    return s21_phase

# Modify the pre simulation configuration file, that is, modify the device design parameters
def generate_s21(proj_path: str, w1):
    i_hang = 0
    with open(proj_path + r"\proj_a") as read_f, open(proj_path + r"\.proj_a", 'w') as write_f:
        for line in read_f:
            if i_hang == 2:
                print(line)
                line = "ADD P1 :W0.000000  -0.9500,-0.2750 %.4f,-0.2750 %.4f,0.4350 -0.9500,0.4350;\r" % (w1, w1)
                write_f.write(line)
            else:
                write_f.write(line)
            i_hang = i_hang + 1

    os.remove(proj_path + r"\proj_a")
    os.rename(proj_path + r"\.proj_a", proj_path + r"\proj_a")

    i_hang = 0
    w1_p = w1 * math.pow(10, -3)
    with open(proj_path + r"\proj.pin") as read_f_1, open(proj_path + r"\.proj.swap.pin", 'w') as write_f_1:
        for line in read_f_1:
            if i_hang == 25:
                print(line)
                line = "          <x>%.5f</x>\r" % w1_p
                write_f_1.write(line)
            else:
                write_f_1.write(line)
            i_hang = i_hang + 1

    os.remove(proj_path + r"\proj.pin")
    os.rename(proj_path + r"\.proj.swap.pin", proj_path + r"\proj.pin")

    # os.system("cd %s && adsMomWrapper -O -3D proj proj" % proj_path)
    os.chdir(proj_path)
    print(os.getcwd())
    # Simulation command
    os.system("adsMomWrapper -T proj proj")
    os.system("adsMomWrapper -DB proj proj")
    os.system("adsMomWrapper -M proj proj")
    os.system("adsMomWrapper -3D proj proj")


# Delete the last simulation file before the next one (the function here is to copy the code of a blog elder brother and put the link at the end)
def Delete_S21(proj_path: str, fileList):
    for parent, dirnames, filenames in os.walk(proj_path):
        goodfilelist = []
        fullfilelist = []
        for x in fileList:
            goodfile = proj_path + x
            goodfilelist.append(goodfile)

        for filename in filenames:
            fullpath = parent + '/' + filename
            fullfilelist.append(fullpath)

        for xlist in fullfilelist:
            if xlist not in goodfilelist:
                os.remove(xlist)
    pass


if __name__ == '__main__':
    # proj_path = "D:/project_all/ADS/Myfirstlayout_wrk/simulation/Myfirstlayout_lib/uniform_line/layout/emSetup_MoM"
    proj_path = "D:/em"
    w1 = 4.0500
    s21_csv = pd.DataFrame()
    S21_Phase = np.zeros([1, 402])
    w1_list = np.arange(0.65, 139.05, 0.01)
    fileList = [r'/emStateFile.xml', r'/momentum.cfg', r'/proj.cfg', r'/proj.lcf', r'/proj.ltd', r'/proj.opt',
                r'/proj.params', r'/proj.pin', r'/proj.plan', r'/proj.prt', r'/proj.sti', r'/proj_a']

    for i in range(len(w1_list)):
        generate_s21(proj_path, w1_list[i])
        S21_Phase_hang = read_cti(proj_path)
        S21_Freq = np.array(w1_list[i])
        S21_Freq = np.reshape(np.append(S21_Freq, np.reshape(np.array(S21_Phase_hang), [1, 401])), [1, 402])
        S21_Phase = np.append(S21_Phase, S21_Freq, axis=0)
        Delete_S21(proj_path, fileList)

    S21_data = pd.DataFrame(S21_Phase)
    S21_data.to_csv("D:/project_all/python/phase/0.65-139.05mm-phase.csv")
    print("save complete")

Last suggestion

The whole simulation file can be copied and can run normally in a folder alone;
For example, after generating the simulation input file, copy all the files and put them under the D:/em (self created) file path. After cd to the target path, use the ads command to simulate. The final simulation data will also be saved in the em folder to reduce the folder depth, which is a little optimization.

python retains the ability to specify files / delete directories other files:
https://www.cnblogs.com/chub/p/4509921.html

Topics: Python Back-end