#--------------------------------------------------------------------- #Name: d0007_coefficient_of_electrostatic_capacitance.py #Menu-en: Electrostatic Capacitance Coefficient Calculation #Menu-ja: 静電容量係数の計算 #Type: Python #Create: April 23, 2017 JSOL Corporation #Comment-en: Outputs the capacitance coefficient of a system consisting of multiple conductors in CSV file format. #Comment-ja: 複数の導体からなる系の静電容量係数をCSVファイルに出力する。 #Copyright: (c) JSOL Corporation. All Rights Reserved. #--------------------------------------------------------------------- from __future__ import print_function import sys import designer app = designer.GetApplication() model = app.GetCurrentModel() CrtStudy = app.GetCurrentStudy() num_set = model.GetSetList().NumSet() def main(): if (is_study_type_electric() == False): return if (CheckNumSet() == False): return if (CheckStudy() == False): return else: if (SetCsv() == False): return else: CreateWorkStudy() DeleteExtraConditions() ModifyPerfectConductor() if (ModifyElectricPotentialBC() == False): return SetConditions() RunCalculation() OutputResults() def is_study_type_electric(): str = "Electric Field Static Analysis" str_ja = "電界静解析" study_type = app.GetCurrentStudy().GetType() if str in study_type or str_ja in study_type: result = True else: result = False message = "The study is not an electric field static analysis study." message_jp = "スタディタイプは電界静解析ではありません。" ShowErrorExitMessage(message, message_jp) return result def CheckNumSet(): global target_counter work_array_target_id = [0 for i in range(num_set)] target_counter = 0 target_sfch = 0 max_target_id = 0 min_target_id = num_set for i in range(num_set): setname = model.GetSetList().GetSet(i).GetName() if (setname[0:4] == "cond"): target_id = setname[4:num_set*10] target_id = int(target_id) work_array_target_id[target_counter] = target_id if (max_target_id < target_id): max_target_id = target_id if (min_target_id > target_id): min_target_id = target_id for j in range(0,num_set): setname2 = model.GetSetList().GetSet(j).GetName() if (setname2 == "scharge"+str(target_id)): target_sfch += 1 break target_counter += 1 # arranging target_id array_target_id = [0 for i in range(target_counter)] if (target_counter == 0): message = "Can not find the Set for targets." message_jp = "計算対象となる導体のセット名が見つかりませんでした。" ShowErrorExitMessage(message, message_jp) return False elif (min_target_id != 1): message = "Name of target Set dose not start from cond1." message_jp = "計算対象となる導体のセット名はcond1から始まっていません。" ShowErrorExitMessage(message, message_jp) return False elif (max_target_id != target_counter): message = "The number in the name of target Set is not serial." message_jp = "計算対象となる導体のセット名が連番になっていません。" ShowErrorExitMessage(message, message_jp) return False elif (target_sfch == 0): message = "Can not find the Set for surface charge calculations" message_jp = "計算対象となる導体の表面電荷計算のセット名が見つかりませんでした。" ShowErrorExitMessage(message, message_jp) return False elif (target_counter != target_sfch): message = "Number of Set for targets and surface charge calculations are not the same." message_jp = "計算対象となる導体のセット数と表面電荷計算のセット数が異なるか、互いの番号が異なります。" ShowErrorExitMessage(message, message_jp) return False else: for i in range(target_counter): if (work_array_target_id[i] != i + min_target_id): for j in range(target_counter): if (work_array_target_id[j] == i + min_target_id): array_target_id[i] = work_array_target_id[j] break if (j == target_counter-1): message = "The number in the name of target Set is not serial." message_jp = "計算対象となる導体のセット名が連番になっていません。" ShowErrorExitMessage(message, message_jp) return False else: array_target_id[i] = work_array_target_id[i] ExtructTargetPart() def CheckStudy(): num_study = model.NumStudies() for i in range(num_study): StudyName = model.GetStudy(i).GetName() if (StudyName == "Work"): message = "Study Work already exsist. Please delete it and try again." message_jp = "スタディWorkがすでにあります。削除して再実行してください。" ShowErrorExitMessage(message, message_jp) return False break num_MeshGroup = CrtStudy.GetMeshGroupList().NumMeshGroup() if (num_MeshGroup > 0): message = "Mesh model is not supported. Please load a geometry analysis model." message_jp = "メッシュモデルはサポートしていません。モデルを読み込みの際に形状で読み込んでください。" ShowErrorExitMessage(message, message_jp) return False def ExtructTargetPart(): global target_counter, shift_target_counter, id_target_part, num_set_target_part shift_target_counter = 0 cond_name = ["" for i in range(target_counter)] setname = ["" for i in range(num_set)] num_set_part = [0 for i in range(num_set)] num_set_target_part = [0 for i in range(target_counter)] for i in range(target_counter): cond_name[i] = "cond"+str(i+1) max_set_part = 0 for i in range(num_set): setname[i] = model.GetSetList().GetSet(i).GetName() for j in range(target_counter): if setname[i] == cond_name[j]: num_set_part[i] = model.GetSetList().GetSet(i).NumParts() if max_set_part < num_set_part[i]: max_set_part = num_set_part[i] setID = [0 for j in range(max_set_part)] id_target_part = [[0 for j in range(max_set_part)] for k in range(target_counter)] for i in range(num_set): for j in range(target_counter): if setname[i] == cond_name[j]: num_set_part[i] = model.GetSetList().GetSet(i).NumParts() setID = model.GetSetList().GetSet(i).GetIDs() for k in range(num_set_part[i]): id_target_part[shift_target_counter][k] = setID[k] num_set_target_part[shift_target_counter] = num_set_part[i] shift_target_counter += 1 def CreateWorkStudy(): global CrtStudy StudyName = CrtStudy.GetName() model.DuplicateStudyName(StudyName, "Work") CrtStudy = model.GetStudy("Work") def DeleteExtraConditions(): counter = 0 num_conditions = CrtStudy.NumConditions() for i in range(num_conditions - counter): ConditionName = CrtStudy.GetCondition(i-counter).GetScriptTypeName() CrtCondition = CrtStudy.GetCondition(i-counter) if (ConditionName == "ElectricFieldBoundary") or \ (ConditionName == "ElectricChargeFace") or \ (ConditionName == "ElectricChargeVolume") or \ (ConditionName == "ElectricChargeCalculation"): CrtStudy.DeleteCondition(i-counter) counter += 1 def ModifyPerfectConductor(): global target_counter, shift_target_counter, id_target_part, num_set_target_part num_conditions = CrtStudy.NumConditions() id_pfcond = [0 for i in range(num_conditions)] num_pfcond = 0 for i in range(num_conditions): ConditionName = CrtStudy.GetCondition(i).GetScriptTypeName() CrtCondition = CrtStudy.GetCondition(i) if ConditionName == "Conductor": id_pfcond[num_pfcond] = i num_pfcond += 1 max_part_pfcond = 0 for i in range(num_pfcond): CrtCondition = CrtStudy.GetCondition(id_pfcond[i]) num_part_pfcond = CrtCondition.GetSelection().NumParts() if max_part_pfcond < num_part_pfcond: max_part_pfcond = num_part_pfcond partid_pfcond = [[0 for i in range(max_part_pfcond)] for j in range(num_pfcond)] id_del_pfcond = [0 for i in range(num_pfcond)] id_del_pfcond_part = [[0 for i in range(max_part_pfcond)] for j in range(num_pfcond)] num_del_pfcond_part = [0 for i in range(num_pfcond)] count_for_pfcond = MatchingPart(max_part_pfcond, num_pfcond, id_pfcond, \ num_del_pfcond_part, id_del_pfcond, id_del_pfcond_part) counter = 0 for i in range(count_for_pfcond): CrtCondition = CrtStudy.GetCondition(id_del_pfcond[i]-counter) for j in range(num_del_pfcond_part[i]): if id_del_pfcond_part[i][j] > 0: CrtCondition.RemovePart(id_del_pfcond_part[i][j]) # num_parts = CrtCondition.GetSelection().NumParts() # if num_parts == 0: CrtStudy.DeleteCondition(id_del_pfcond[i]-counter) counter += 1 def ModifyElectricPotentialBC(): global target_counter, shift_target_counter, id_target_part, num_set_target_part num_conditions = CrtStudy.NumConditions() # id_epbc = [0 for i in range(num_conditions)] num_epbc = 0 for i in range(num_conditions): ConditionName = CrtStudy.GetCondition(i).GetScriptTypeName() CrtCondition = CrtStudy.GetCondition(i) if ConditionName == "ElectricPotentialBoundary": CrtCondition.SetValue("Amplitude", 0) id_epbc[num_epbc] = i num_epbc += 1 max_part_epbc = 0 for i in range(num_epbc): ConditionName = CrtStudy.GetCondition(id_epbc[i]).GetType() CrtCondition = CrtStudy.GetCondition(id_epbc[i]) num_part_epbc = CrtCondition.GetSelection().NumParts() if max_part_epbc < num_part_epbc: max_part_epbc = num_part_epbc partid_epbc = [[0 for i in range(max_part_epbc)] for j in range(num_epbc)] id_del_epbc = [0 for i in range(num_epbc)] id_del_epbc_part = [[0 for i in range(max_part_epbc)] for j in range(num_epbc)] num_del_epbc_part = [0 for i in range(num_epbc)] count_for_epbc = MatchingPart(max_part_epbc, num_epbc, id_epbc, \ num_del_epbc_part, id_del_epbc, id_del_epbc_part) counter = 0 for i in range(count_for_epbc): CrtCondition = CrtStudy.GetCondition(id_del_epbc[i]-counter) for j in range(num_del_epbc_part[i]): if id_del_epbc_part[i][j] > 0: CrtCondition.RemovePart(id_del_epbc_part[i][j]) # num_vertices = CrtCondition.GetSelection().NumVertices() num_edges = CrtCondition.GetSelection().NumEdges() num_faces = CrtCondition.GetSelection().NumFaces() num_parts = CrtCondition.GetSelection().NumParts() # if (num_vertices == 0) and \ (num_edges == 0) and \ (num_faces == 0) and (num_parts == 0): CrtStudy.DeleteCondition(id_del_epbc[i]-counter) counter += 1 if (counter == num_epbc): message = "Please specify at least one more GND except target part." message_jp = "容量計算の対象以外に少なくともひとつの基準電位(0V)を指定してください。" ShowErrorExitMessage(message, message_jp) return False else: return True def SetConditions(): global CrtStudy, target_counter for i in range(1,target_counter+1): # Setting the surface charge conditions CrtStudy.CreateCondition("ElectricChargeCalculation", "scharge"+str(i)) CrtStudy.GetCondition("scharge"+str(i)).AddSet(model.GetSetList().GetSet("scharge"+str(i)), 0) # Setting the electric potential boundaries CrtStudy.CreateCondition("ElectricPotentialBoundary", "cond"+str(i)) CrtStudy.GetCondition("cond"+str(i)).AddSet(model.GetSetList().GetSet("cond"+str(i)), 0) # Setting parameters in the case control for i in range(1,target_counter+1): CrtStudy.GetDesignTable().AddParameterVariableName("cond"+str(i)+" (ElectricPotential): Amplitude") # Adding cases CrtStudy.GetDesignTable().AddCases(target_counter-1) # Changing the setting of electric potentials for i in range(target_counter): CrtStudy.GetDesignTable().SetValue(i, i, 1) # Run all cases def RunCalculation(): global CrtStudy CrtStudy.RunAllCases() def MatchingPart(max_part_cond, num_cond, id_cond, \ num_del_cond_part, id_del_cond, id_del_cond_part): global target_counter, shift_target_counter, id_target_part, num_set_target_part count_for_cond = 0 partid_cond = [[0 for i in range(max_part_cond)] for j in range(num_cond)] for i in range(num_cond): CrtCondition = CrtStudy.GetCondition(id_cond[i]) num_part_cond = CrtCondition.GetSelection().NumParts() if num_part_cond > 0: count_for_cond_part = 0 for j in range(num_part_cond): partid_cond[count_for_cond][j] = CrtCondition.GetSelection().PartID(j) flag = 0 for k in range(shift_target_counter): for l in range(num_set_target_part[k]): if id_target_part[k][l] == partid_cond[count_for_cond][j]: id_del_cond_part[count_for_cond][count_for_cond_part] = partid_cond[count_for_cond][j] flag = 1 count_for_cond_part += 1 break if flag == 1: flag = 0 break num_del_cond_part[count_for_cond] = count_for_cond_part id_del_cond[count_for_cond] = id_cond[i] count_for_cond += 1 return count_for_cond def SetCsv(): global FilePath outcsvlbl_jp = "出力用csvファイル:" outcsvlbl = "Csv file for output" savecsvlbl_jp = "CSVファイルパス" savecsvlbl ="CSV file path" dialog = app.CreateDialogBox() dialog.SetTranslation(outcsvlbl, outcsvlbl_jp) dialog.SetTitle(outcsvlbl) dialog.SetTranslation(savecsvlbl, savecsvlbl_jp) dialog.AddSaveFilename("InputFilePath", savecsvlbl, "", "CSV files(*.csv)",0,1) dialog.Show() FilePath = dialog.GetValue("InputFilePath") if (FilePath == ""): message = "Please set the csv file path" message_jp = "Csvファイル出力先を指定してください。" ShowErrorExitMessage(message, message_jp) return False elif (dialog.WasCancelled() == True): return False else: return True def OutputResults(): global CrtStudy, FilePath, target_counter matrix = [[0 for j in range(target_counter)] for i in range(target_counter)] # for i in range(1,target_counter+1): app.View().SetCurrentCase(i) tables = CrtStudy.GetResultTable().GetData("SurfaceCharge") matrix[0][i-1] = tables.GetValue(0,0) for j in range(target_counter): matrix[j][i-1] = tables.GetValue(0,j) # file = open(FilePath.decode('utf-8'), 'w') for i in range(target_counter): C_matrix = str(matrix[i][0]) for j in range(1,target_counter): C_matrix = C_matrix + "," + str(matrix[i][j]) print(C_matrix, file=file) file.close() def ShowErrorExitMessage(message, message_jp): msgdlg = app.CreateDialogBox() title = "Error" title_jp = "エラー" msgdlg.SetTranslation(title, title_jp) msgdlg.SetTranslation(message, message_jp) msgdlg.SetCancelButtonVisible(False) msgdlg.SetTitle(title) msgdlg.AddLabel(message) msgdlg.Show() main()