-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample 5.edp
149 lines (125 loc) · 4.86 KB
/
Example 5.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
/*********************************************************************
* Optimal control linear elasticity/Example 5.edp (2022/06/12)
*
* Copyright (C) 2022 Q. H. Nguyen, T. T. M. Ta
*
* Example 5: Deflected material by boundary load in 3D
* Objective function: L2 norm
* Constraint function: No constraints
*********************************************************************/
/* Libraries */
load "ff-IpOpt"
load "medit"
load "msh3"
include "cube.idp"
/* Parameter */
real lambda = 1e-6; // Tikhonov regularization paremeter
real coef = 1000.; // Scale factor
/* Operators */
// Strain rate tensor of a vector field
macro ep(u, v, s) [dx(u), dy(v), dz(s), (dy(u) + dx(v))/sqrt(2.), (dz(u) + dx(s))/sqrt(2.), (dy(s) + dz(v))/sqrt(2.)] //EOM
// divergence of a vector field
macro div(u, v, s) (dx(u) + dy(v) + dz(s)) //EOM
/* Problem configuration */
real E = 21e5; // Young modulus of a material
real nu = 0.3; // Poisson ratio of a material
/* Build mesh */
int[int] Nxyz = [20, 10, 5];
real [int,int] Bxyz = [[-6., 6.], [-2., 2.], [0., 2.]];
int [int,int] Lxyz = [[3, 1], [2, 2], [2, 2]];
/* Create and plot the mesh */
mesh3 Th = Cube(Nxyz, Bxyz, Lxyz);
savemesh(Th, "vd5bandau.mesh");
plot(Th);
/* Declare FE space */
fespace Vh(Th, P1);
/* Declare FE varialbes */
Vh gini1, gini2, gini3, gini, gdes1, gdes2, gdes3, gdes, udes1, udes2, udes3, udes, usol1, usol2, usol3, usol, p;
/* Set initial values */
real mu = E/(2*(1 + nu)); // Lamé coefficients
real kappa = E*nu/((1 + nu)*(1 - 2*nu));
gini1[] = 10; // Initial load
gini2[] = -50;
gini3[] = 70;
gini[] = [gini1[], gini2[], gini3[]];
/* Set desired load */
gdes1[] = 0;
gdes2[] = 0;
gdes3[] = -10;
gdes[] = [gdes1[], gdes2[], gdes3[]];
/* Declare variational problems */
func real[int] SolveState0(real[int] ffsol){
Vh us1, us2, us3, vs1, vs2, vs3;
Vh f1, f2, f3, y1;
[f1[], f2[], f3[]] = ffsol;
solve Poisson([us1, us2, us3], [vs1, vs2, vs3]) =
int3d(Th)(kappa*div(us1, us2, us3)*div(vs1, vs2, vs3) + 2.*mu*(ep(us1, us2, us3)'*ep(vs1, vs2, vs3)))
- int2d(Th, 1)((f1*vs1 + f2*vs2 + f3*vs3))
+ on(3, us1 = 0, us2 = 0, us3 = 0);
y1[] = [us1[], us2[], us3[]];
return y1[];
}
func real[int] SolveState(real[int] ffsol){
Vh us1, us2, us3, vs1, vs2, vs3;
Vh f1, f2, f3, y1;
[f1[], f2[], f3[]] = ffsol;
solve Poisson([us1, us2, us3], [vs1, vs2, vs3]) =
int3d(Th)(kappa*div(us1, us2, us3)*div(vs1, vs2, vs3) + 2.*mu*(ep(us1, us2, us3)'*ep(vs1, vs2, vs3)))
- int2d(Th, 1)((f1*vs1 + f2*vs2 + f3*vs3))
+ on(3, us1 = 0, us2 = 0, us3 = 0);
y1[] = [us1[], us2[], us3[]];
return y1[];
}
func real[int] SolveAdjoint(real[int] ffsol){
Vh p;
Vh f1, f2, f3;
[f1[], f2[], f3[]] = ffsol;
Vh u1, u2, u3, v1, v2, v3;
solve Elasticity([u1, u2, u3], [v1, v2, v3]) =
int3d(Th)(kappa*div(u1, u2, u3)*div(v1, v2, v3) + 2.*mu*(ep(u1, u2, u3)'*ep(v1, v2, v3)))
- int3d(Th)((f1 - udes1)*v1 + (f2 - udes2)*v2 + (f3 - udes3)*v3)
+ on(3, u1 = 0, u2 = 0, u3 = 0);
p[] = [u1[], u2[], u3[]];
return p[];
}
/* Declare the objective function */
func real J(real[int] ffsol){
Vh uu1, uu2, uu3;
[uu1[], uu2[], uu3[]] = ffsol;
Vh yy1, yy2, yy3;
[yy1[], yy2[], yy3[]] = SolveState(ffsol);
real res;
res = 1./2*int3d(Th)((yy1 - udes1)^2 + (yy2 - udes2)^2 + (yy3 - udes3)^2)
+ lambda/2*int2d(Th, 1)(uu1^2 + uu2^2 + uu3^2);
return res;
}
/* Compute the gradient of the objective function */
func real[int] gradJ(real[int] ffsol){
Vh pp1, pp2, pp3, qq1, qq2, qq3;
[pp1[], pp2[], pp3[]] = SolveAdjoint(SolveState(ffsol));
[qq1[], qq2[], qq3[]] = ffsol;
Vh res1 = pp1 + lambda*qq1;
Vh res2 = pp2 + lambda*qq2;
Vh res3 = pp3 + lambda*qq3;
Vh res;
res[] = [res1[], res2[], res3[]];
return res[];
}
/* Main block */
[udes1[], udes2[], udes3[]] = SolveState0(gdes[]); // Compute the desired state
udes[] = [udes1[], udes2[], udes3[]];
IPOPT(J, gradJ, gini[]); // Compute the optimal load
[usol1[], usol2[], usol3[]] = SolveState(gini[]); // Compute the optimal state
usol[] = [usol1[], usol2[], usol3[]];
/* Final results */
savesol("vd5bandau.mesh", Th, [usol1, usol2, usol3]);
int[int] ref2 = [1, 0, 2, 0];
mesh3 Th2 = movemesh3(Th, transfo = [x + coef*usol1, y + coef*usol2, z + coef*usol3], label = ref2);
Th2 = change(Th2, label = ref2);
savemesh(Th2, "vd5giai.mesh");
int[int] ref1 = [1, 0, 5, 0];
mesh3 Th1 = movemesh3(Th, transfo = [x + coef*udes1, y + coef*udes2, z + coef*udes3], label = ref1);
Th1 = change(Th1, label = ref1);
savemesh(Th1, "vd5sao.mesh");
plot(Th1, Th2);
medit("Th", Th1, Th2);