-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSM_main.edp
231 lines (193 loc) · 9.08 KB
/
CSM_main.edp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* Copyright V.C. Le, T.T.M. Ta (2019) */
include "getARGV.idp"
include "operators.edp"
load "medit";
cout.precision(12);
/* Label of boundaries */
int WALL = 1;
int LOAD = 2;
int FREE = 3;
/*----------------------------------- Problem configurations -----------------------------------*/
real E = getARGV("--E", 1.0); // The Young modulus of material
real nu = getARGV("--nu", 0.3); // The Poisson ratio of material
real cr = getARGV("--cr", 1.) ; // Constraint ratio
/*----------------------------------------------------------------------------------------------*/
/*---------------------------------- Numerical configurations ----------------------------------*/
real b0 = getARGV("--b0", 1e-2); // Initial value of b
real bmax = getARGV("--bmax", 1e1); // Maximal value of b, no longer update if b > bmax
real l0 = getARGV("--l0", 0.0); // Initial value of l
real betamax = getARGV("--betamax", 0.7); // Upper bound of the update coefficient
real betamin = getARGV("--betamin", 0.5); // Lower bound of the update coefficient
real tau0 = getARGV("--tau0", 1e-2); // Initial descent step length
int fixstep = getARGV("--fixstep", 0); // 0 = fixed initial descent step, 1 = fixed initial deformation step
real length = getARGV("--length", 1e-2); // Fixed initial deformation step length
real sm = getARGV("--sm", 1e-4); // Armijo condition's coefficient
real alpha = getARGV("--alpha", 1.05); // Update coefficient of b: b_{n+1} = alpha*b_n
real gm = getARGV("--gm", 0.01); // Regularization parameter
real minsgn = getARGV("--minsgn", 1e-2); // Stop criteria
real minarea = getARGV("--minarea", 1e-4); // Remesh parameter: remesh if any element has an area less than minarea
real mshsize = getARGV("--mshsize", 0.03); // Required edge size when remeshing : hmax = mshsize, hmin = mshsize/sqrt(2)
/*--------------------------------------------------------------------------------------------*/
/*------------------------------------ IO configurations -------------------------------------*/
string mshname = getARGV("--mshname", "shortcantilever"); // File name of the mesh
string folder = getARGV("--resu", "results"); // Folder containing outputs
int steps = getARGV("--steps", 3); // Number of steps to save results once
int regex = getARGV("--regex", 1); // Regularization-extension config
/*--------------------------------------------------------------------------------------------*/
system("mkdir " + folder);
ofstream fout(folder + "/out.dat");
/* Save the command */
ofstream cmd(folder + "/command.sh");
for (int i = 0; i < ARGV.n; i++)
cmd << ARGV[i] << endl;
cmd.flush;
/* Load mesh */
mesh Th = readmesh("meshes/CSM/" + mshname + ".mesh");
plot(Th);
cout << "Done loading mesh!" << endl;
/* Declare FE spaces */
fespace Uh(Th, P2);
fespace Dh(Th, P1);
/* Declare FE variables */
Uh u1, u2, ut1, ut2, fs1, fs2, uload1, uload2;
Dh d1, d2, dt1, dt2;
/*---------------------------- Set initial values for test cases ----------------------------*/
real mu = E/(2 * (1 + nu));
real lambda = E * nu/((1 + nu) * (1 - 2 * nu));
uload1 = 0.0;
uload2 = -1.0;
fs1 = 0.0;
fs2 = 0.0;
/*--------------------------------------------------------------------------------------------*/
/* Variables */
real l = l0;
real b = b0;
real J0, vol0, convol;
int i, j;
int imax = 2000, jmax = 30;
/*-------------------------- Declare functions and their derivatives --------------------------*/
macro J() (int2d(Th)(2.0 * mu * dot(eps2(u1, u2), eps2(u1, u2)) + lambda * div2(u1, u2) * div2(u1, u2))) //EOM
macro dJ() (2.0 * (fs1 * u1 + fs2 * u2) - 2.0 * mu * dot(eps2(u1, u2), eps2(u1, u2)) - lambda * div2(u1, u2) * div2(u1, u2)) //EOM
macro vol(Th) (int2d(Th)(1.)) //EOM
macro dvol() (1.0) //EOM
macro L() (J/J0 - l * (vol(Th) - convol)/vol0 + b/2.0 * ((vol(Th) - convol) ^ 2)/(vol0 ^ 2)) //EOM
macro dL() (dJ/J0 - l * dvol/vol0 + b * dvol * (vol(Th) - convol)/(vol0 ^ 2)) //EOM
/*---------------------------------------------------------------------------------------------*/
/*-------------------------------- Declare variational problems --------------------------------*/
problem elastic2D([u1, u2], [ut1, ut2]) =
int2d(Th)(2.0 * mu * dot(eps2(u1, u2), eps2(ut1, ut2)) + lambda * div2(u1, u2) * div2(ut1, ut2))
- int2d(Th)(fs1 * ut1 + fs2 * ut2)
- int1d(Th, LOAD)(uload1 * ut1 + uload2 * ut2)
+ on(WALL, u1 = 0.0, u2 = 0.0);
problem regex1([d1, d2], [dt1, dt2]) =
int2d(Th)(gm * dot(eps2(d1, d2), eps2(dt1, dt2)) + dot([d1, d2], [dt1, dt2]))
+ int1d(Th, FREE)(dL * dot([dt1, dt2], N2))
+ on(LOAD, WALL, d1 = 0.0, d2 = 0.0);
problem regex2([d1, d2], [dt1, dt2]) =
int2d(Th)(gm * dot(eps2(d1, d2), eps2(dt1, dt2)))
+ int1d(Th, FREE)((1 - gm) * (dot(tgrad2(d1), tgrad2(dt1)) + dot(tgrad2(d2), tgrad2(dt2))))
+ int1d(Th, FREE)(dL * dot([dt1, dt2], N2))
+ on(LOAD, WALL, d1 = 0.0, d2 = 0.0);
macro solveregex()
{
if(regex == 1)
regex1;
else if(regex == 2)
regex2;
} //EOM
/*---------------------------------------- Main block -----------------------------------------*/
/* Initial values */
vol0 = vol(Th);
convol = vol0 * cr;
elastic2D;
J0 = J;
fout << J << " " << L << " " << vol0 << " " << l0 << endl;
fout.flush;
for(i = 0; i < imax; i++)
{
elastic2D;
solveregex;
real dd = int1d(Th, FREE)(dL * dot([d1, d2], N2));
real sgn = sqrt(int1d(Th, FREE)(d1^2 + d2^2));
cout << "Directional derivative: " << dd << endl;
real L0 = L;
real tau;
if (fixstep == 0)
{
tau = tau0;
}
else if(fixstep == 1)
{
Uh nd = sqrt(d1^2 + d2^2);
tau = length / nd[].max;
}
mesh Th1 = Th; // temporary mesh
for (j = 0; j < jmax; j++)
{
cout << "Descent step = "<< tau << endl;
real minarea1 = checkmovemesh(Th, [x + tau * d1, y + tau * d2]);
int remeshcount = 0;
while (minarea1 <= minarea && remeshcount <= 5)
{
Th = Th1;
Th = adaptmesh(Th, hmax = mshsize, hmin = mshsize/sqrt(2), ratio = 1.5);
remeshcount ++;
minarea1 = checkmovemesh(Th, [x + tau * d1, y + tau * d2]);
}
Th = movemesh(Th, [x + tau * d1, y + tau * d2]);
cout << "Done movemesh!" << endl;
Th = adaptmesh(Th, hmax = mshsize, hmin = mshsize/sqrt(2), ratio = 1.5);
cout << "Done adaptmesh!" << endl;
elastic2D;
real L1 = L;
cout << "After deformation: L = " << L1 << " / before L0 = " << L0 << " (variation = " << 100 * (L1 - L0)/L0 << "%)" << endl;
if (L1 <= L0 + sm * tau * dd)
{
break;
}
else
{
real optitau = -dd * tau ^ 2 / (2 * (L1 - L0 - dd * tau));
if (optitau < betamin * tau)
tau *= betamin;
else
{
if (optitau > betamax * tau)
tau *= betamax;
else
tau = optitau;
}
Th = Th1;
}
}
// Update Lagrange multiplier and penalty coefficient
l = l - b * (vol(Th) - convol) / vol0;
if (b < bmax)
b *= alpha;
if (i % steps == 0)
{
savesol(folder + "/mesh." + i/steps + ".sol", Th, [u1, u2]);
savemesh(Th, folder + "/mesh." + i/steps + ".mesh");
plot(Th, cmm = "Iteration i = " + i + "/" + imax);
}
if (sgn < minsgn)
{
cout << "The shape change is insignificant!" << endl;
break;
}
else if (j == jmax)
{
cout << "There is an issue with ADAPTMESH!" << endl;
break;
}
else
{
/* Save post-processing data */
fout << J << " " << L << " " << vol(Th) << " " << l << endl;
}
}
/*----------------------------------------------------------------------------------------------*/
// Final results
plot(Th, [u1,u2], cmm = "Solution");
savesol(folder + "/mesh_final.sol", Th, [u1, u2]);
savemesh(Th, folder + "/mesh_final.mesh");