python调用comsol (3)

python调用comsol (3)

和Sentaurus TCAD一样,Comsol也可以录制在GUI里进行的操作,然后导出这些操作对应的Java代码,比如这里随便建立了一个方块,求解稳态传热问题,保存为Java格式。

用Java Comsol API和这些Java代码,就可以复现我们在GUI里的操作了,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Untitled.java
*/

import com.comsol.model.*;
import com.comsol.model.util.*;

/** Model exported on Oct 12 2022, 10:27 by COMSOL 5.6.0.280. */
public class Untitled {

public static Model run() {
Model model = ModelUtil.create("Model");

model.modelPath("E:\\DeskTop");

model.component().create("comp1", true);

model.component("comp1").geom().create("geom1", 2);

model.component("comp1").mesh().create("mesh1");

model.component("comp1").physics().create("ht", "HeatTransfer", "geom1");

model.study().create("std1");
model.study("std1").create("stat", "Stationary");
model.study("std1").feature("stat").activate("ht", true);

model.component("comp1").geom("geom1").create("sq1", "Square");
model.component("comp1").geom("geom1").run();

model.component("comp1").physics("ht").create("temp1", "TemperatureBoundary", 1);
model.component("comp1").physics("ht").feature("temp1").selection().set(2);

model.component("comp1").material().create("mat1", "Common");
model.component("comp1").material("mat1").propertyGroup("def").set("thermalconductivity", new String[]{"1"});
model.component("comp1").material("mat1").propertyGroup("def").set("density", new String[]{"1"});
model.component("comp1").material("mat1").propertyGroup("def").set("heatcapacity", new String[]{"1"});

model.sol().create("sol1");
model.sol("sol1").study("std1");

model.study("std1").feature("stat").set("notlistsolnum", 1);
model.study("std1").feature("stat").set("notsolnum", "1");
model.study("std1").feature("stat").set("listsolnum", 1);
model.study("std1").feature("stat").set("solnum", "1");

model.sol("sol1").create("st1", "StudyStep");
model.sol("sol1").feature("st1").set("study", "std1");
model.sol("sol1").feature("st1").set("studystep", "stat");
model.sol("sol1").create("v1", "Variables");
model.sol("sol1").feature("v1").set("control", "stat");
model.sol("sol1").create("s1", "Stationary");
model.sol("sol1").feature("s1").create("fc1", "FullyCoupled");
model.sol("sol1").feature("s1").feature("fc1").set("dtech", "auto");
model.sol("sol1").feature("s1").feature("fc1").set("initstep", 0.01);
model.sol("sol1").feature("s1").feature("fc1").set("minstep", 1.0E-6);
model.sol("sol1").feature("s1").feature("fc1").set("maxiter", 50);
model.sol("sol1").feature("s1").feature("fc1").set("termonres", "off");
model.sol("sol1").feature("s1").create("d1", "Direct");
model.sol("sol1").feature("s1").feature("d1").set("linsolver", "pardiso");
model.sol("sol1").feature("s1").feature("d1").set("pivotperturb", 1.0E-13);
model.sol("sol1").feature("s1").feature("d1").label("Direct, temperature (ht)");
model.sol("sol1").feature("s1").create("i1", "Iterative");
model.sol("sol1").feature("s1").feature("i1").set("linsolver", "gmres");
model.sol("sol1").feature("s1").feature("i1").set("prefuntype", "left");
model.sol("sol1").feature("s1").feature("i1").set("itrestart", 50);
model.sol("sol1").feature("s1").feature("i1").set("rhob", 20);
model.sol("sol1").feature("s1").feature("i1").set("maxlinit", 10000);
model.sol("sol1").feature("s1").feature("i1").set("nlinnormuse", "on");
model.sol("sol1").feature("s1").feature("i1").label("AMG, temperature (ht)");
model.sol("sol1").feature("s1").feature("i1").create("mg1", "Multigrid");
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("prefun", "saamg");
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("mgcycle", "v");
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("maxcoarsedof", 50000);
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("strconn", 0.01);
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("nullspace", "constant");
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("usesmooth", false);
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("saamgcompwise", true);
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("loweramg", true);
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("pr").create("so1", "SOR");
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("po").create("so1", "SOR");
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("cs").create("d1", "Direct");
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("cs").feature("d1")
.set("linsolver", "pardiso");
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("cs").feature("d1")
.set("pivotperturb", 1.0E-13);
model.sol("sol1").feature("s1").feature("fc1").set("linsolver", "d1");
model.sol("sol1").feature("s1").feature("fc1").set("dtech", "auto");
model.sol("sol1").feature("s1").feature("fc1").set("initstep", 0.01);
model.sol("sol1").feature("s1").feature("fc1").set("minstep", 1.0E-6);
model.sol("sol1").feature("s1").feature("fc1").set("maxiter", 50);
model.sol("sol1").feature("s1").feature("fc1").set("termonres", "off");
model.sol("sol1").feature("s1").feature().remove("fcDef");
model.sol("sol1").attach("std1");

model.result().create("pg1", "PlotGroup2D");
model.result("pg1").label("Temperature (ht)");
model.result("pg1").set("data", "dset1");
model.result("pg1").feature().create("surf1", "Surface");
model.result("pg1").feature("surf1").label("Surface");
model.result("pg1").feature("surf1").set("colortable", "ThermalLight");
model.result("pg1").feature("surf1").set("data", "parent");
model.result().create("pg2", "PlotGroup2D");
model.result("pg2").label("Isothermal Contours (ht)");
model.result("pg2").set("data", "dset1");
model.result("pg2").feature().create("con1", "Contour");
model.result("pg2").feature("con1").label("Contour");
model.result("pg2").feature("con1").set("levelrounding", false);
model.result("pg2").feature("con1").set("colortable", "ThermalLight");
model.result("pg2").feature("con1").set("smooth", "internal");
model.result("pg2").feature("con1").set("data", "parent");

model.sol("sol1").runAll();

model.result("pg1").run();

return model;
}

public static void main(String[] args) {
run();
}

}

Python调用Comsol的mph库,实际上是Comsol Java API的wrapper,把上面的东西稍微改一改,比如把数字都改成字符串类型,就可以在Python里运行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import mph

client = mph.start(cores=1)
py_model = client.create('test')
model = py_model.java

model.modelPath("E:\\DeskTop")

model.component().create("comp1", True)

model.component("comp1").geom().create("geom1", 2)

model.component("comp1").mesh().create("mesh1")

model.component("comp1").physics().create("ht", "HeatTransfer", "geom1")

model.study().create("std1")
model.study("std1").create("stat", "Stationary")
model.study("std1").feature("stat").activate("ht", True)

model.component("comp1").geom("geom1").create("sq1", "Square")
model.component("comp1").geom("geom1").run()

model.component("comp1").physics("ht").create("temp1", "TemperatureBoundary", 1)
model.component("comp1").physics("ht").feature("temp1").selection().set(2)

model.component("comp1").material().create("mat1", "Common")
model.component("comp1").material("mat1").propertyGroup("def").set("thermalconductivity", 1)
model.component("comp1").material("mat1").propertyGroup("def").set("density", 1)
model.component("comp1").material("mat1").propertyGroup("def").set("heatcapacity", 1)

model.sol().create("sol1")
model.sol("sol1").study("std1")

model.study("std1").feature("stat").set("notlistsolnum", "1")
model.study("std1").feature("stat").set("notsolnum", "1")
model.study("std1").feature("stat").set("listsolnum", "1")
model.study("std1").feature("stat").set("solnum", "1")

model.sol("sol1").create("st1", "StudyStep")
model.sol("sol1").feature("st1").set("study", "std1")
model.sol("sol1").feature("st1").set("studystep", "stat")
model.sol("sol1").create("v1", "Variables")
model.sol("sol1").feature("v1").set("control", "stat")
model.sol("sol1").create("s1", "Stationary")
model.sol("sol1").feature("s1").create("fc1", "FullyCoupled")
model.sol("sol1").feature("s1").feature("fc1").set("dtech", "auto")
model.sol("sol1").feature("s1").feature("fc1").set("initstep", 0.01)
model.sol("sol1").feature("s1").feature("fc1").set("minstep", 1.0E-6)
model.sol("sol1").feature("s1").feature("fc1").set("maxiter", "50")
model.sol("sol1").feature("s1").feature("fc1").set("termonres", "off")
model.sol("sol1").feature("s1").create("d1", "Direct")
model.sol("sol1").feature("s1").feature("d1").set("linsolver", "pardiso")
model.sol("sol1").feature("s1").feature("d1").set("pivotperturb", 1.0E-13)
model.sol("sol1").feature("s1").feature("d1").label("Direct, temperature (ht)")
model.sol("sol1").feature("s1").create("i1", "Iterative")
model.sol("sol1").feature("s1").feature("i1").set("linsolver", "gmres")
model.sol("sol1").feature("s1").feature("i1").set("prefuntype", "left")
model.sol("sol1").feature("s1").feature("i1").set("itrestart", "50")
model.sol("sol1").feature("s1").feature("i1").set("rhob", "20")
model.sol("sol1").feature("s1").feature("i1").set("maxlinit", "10000")
model.sol("sol1").feature("s1").feature("i1").set("nlinnormuse", "on")
model.sol("sol1").feature("s1").feature("i1").label("AMG, temperature (ht)")
model.sol("sol1").feature("s1").feature("i1").create("mg1", "Multigrid")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("prefun", "saamg")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("mgcycle", "v")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("maxcoarsedof", "50000")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("strconn", "0.01")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("nullspace", "constant")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("usesmooth", "off")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("saamgcompwise", "on")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").set("loweramg", "on")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("pr").create("so1", "SOR")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("po").create("so1", "SOR")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("cs").create("d1", "Direct")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("cs").feature("d1").set("linsolver", "pardiso")
model.sol("sol1").feature("s1").feature("i1").feature("mg1").feature("cs").feature("d1").set("pivotperturb", 1.0E-13)
model.sol("sol1").feature("s1").feature("fc1").set("linsolver", "d1")
model.sol("sol1").feature("s1").feature("fc1").set("dtech", "auto")
model.sol("sol1").feature("s1").feature("fc1").set("initstep", "0.01")
model.sol("sol1").feature("s1").feature("fc1").set("minstep", 1.0E-6)
model.sol("sol1").feature("s1").feature("fc1").set("maxiter", "50")
model.sol("sol1").feature("s1").feature("fc1").set("termonres", "off")
model.sol("sol1").feature("s1").feature().remove("fcDef")
model.sol("sol1").attach("std1")

model.result().create("pg1", "PlotGroup2D")
model.result("pg1").label("Temperature (ht)")
model.result("pg1").set("data", "dset1")
model.result("pg1").feature().create("surf1", "Surface")
model.result("pg1").feature("surf1").label("Surface")
model.result("pg1").feature("surf1").set("colortable", "ThermalLight")
model.result("pg1").feature("surf1").set("data", "parent")
model.result().create("pg2", "PlotGroup2D")
model.result("pg2").label("Isothermal Contours (ht)")
model.result("pg2").set("data", "dset1")
model.result("pg2").feature().create("con1", "Contour")
model.result("pg2").feature("con1").label("Contour")
model.result("pg2").feature("con1").set("levelrounding", "off")
model.result("pg2").feature("con1").set("colortable", "ThermalLight")
model.result("pg2").feature("con1").set("smooth", "internal")
model.result("pg2").feature("con1").set("data", "parent")

model.sol("sol1").runAll()

model.result("pg1").run()