示例#1
0
    def _build_function_space(self):
        class Exterior(fa.SubDomain):
            def inside(self, x, on_boundary):
                return on_boundary and (fa.near(x[1], 1) or fa.near(x[0], 1) or
                                        fa.near(x[0], 0) or fa.near(x[1], 0))

        class Left(fa.SubDomain):
            def inside(self, x, on_boundary):
                return on_boundary and fa.near(x[0], 0)

        class Right(fa.SubDomain):
            def inside(self, x, on_boundary):
                return on_boundary and fa.near(x[0], 1)

        class Bottom(fa.SubDomain):
            def inside(self, x, on_boundary):
                return on_boundary and fa.near(x[1], 0)

        class Top(fa.SubDomain):
            def inside(self, x, on_boundary):
                return on_boundary and fa.near(x[1], 1)

        class Interior(fa.SubDomain):
            def inside(self, x, on_boundary):
                return on_boundary and (x[0] > 0.1 and x[0] < 0.9
                                        and x[1] > 0.1 and x[1] < 0.9)

        self.exteriors_dic = {
            'left': Left(),
            'right': Right(),
            'bottom': Bottom(),
            'top': Top()
        }
        self.exterior = Exterior()
        self.interior = Interior()

        self.V = fa.FunctionSpace(self.mesh, 'P', 1)

        self.source = da.Expression(("100*sin(2*pi*x[0])"), degree=3)
        # self.source = da.Expression("k*100*exp( (-(x[0]-x0)*(x[0]-x0) -(x[1]-x1)*(x[1]-x1)) / (2*0.01*l) )",
        #                        k=1, l=1, x0=0.9, x1=0.1, degree=3)
        # self.source = da.Constant(10)
        self.source = da.interpolate(self.source, self.V)

        boundary_fn_ext = da.Constant(1.)
        boundary_fn_int = da.Constant(1.)
        boundary_bc_ext = da.DirichletBC(self.V, boundary_fn_ext,
                                         self.exterior)
        boundary_bc_int = da.DirichletBC(self.V, boundary_fn_int,
                                         self.interior)
        self.bcs = [boundary_bc_ext, boundary_bc_int]
示例#2
0
文件: matlab.py 项目: tianjuxue/crack
    def set_bcs_staggered(self):
        self.upper.mark(self.boundaries, 1)

        self.presLoad = da.Expression((0, "t"), t=0.0, degree=1)
        BC_u_lower = da.DirichletBC(self.U, da.Constant((0., 0.)), self.lower)
        BC_u_upper = da.DirichletBC(self.U, self.presLoad, self.upper)

        BC_d_middle = fe.DirichletBC(self.W,
                                     fe.Constant(1.),
                                     self.middle,
                                     method='pointwise')

        self.BC_u = [BC_u_lower, BC_u_upper]
        self.BC_d = [BC_d_middle]
示例#3
0
 def fix_basal_plane(W):
     V = W if W.sub(0).num_sub_spaces() == 0 else W.sub(0)
     bc = dolfin_adjoint.DirichletBC(
         V.sub(0),
         dolfin.Constant(0.0, name="fix_base"),
         geometry.ffun,
         geometry.markers["BASE"][0],
     )
     return bc
示例#4
0
if case_flag == 0:
    g = da.interpolate(
        da.Expression("1/(1+alpha*4*pow(pi, 4))*w", w=w, alpha=alpha,
                      degree=3), W)
    f = da.interpolate(
        da.Expression("1/(1+alpha*4*pow(pi, 4))*w", w=w, alpha=alpha,
                      degree=3), W)
else:
    g = da.interpolate(da.Expression(("sin(2*pi*x[0])"), degree=3), W)
    f = da.interpolate(da.Expression(("sin(2*pi*x[0])"), degree=3), W)

u = da.Function(V, name='State')
v = fa.TestFunction(V)

F = (fa.inner(fa.grad(u), fa.grad(v)) - f * v) * fa.dx
bc = da.DirichletBC(V, 0.0, "on_boundary")
da.solve(F == 0, u, bc)

d = da.Function(V)
d.vector()[:] = u.vector()[:]

J = da.assemble((0.5 * fa.inner(u - d, u - d)) * fa.dx +
                alpha / 2 * f**2 * fa.dx)
control = da.Control(f)
rf = da.ReducedFunctional(J, control)

# set the initial value to be zero for optimization
f.vector()[:] = 0
# N = len(f.vector()[:])
# f.vector()[:] = np.random.rand(N)*2 -1
示例#5
0
 def dirichlet_bc(W):
     V = W if W.sub(0).num_sub_spaces() == 0 else W.sub(0)
     return da.DirichletBC(V, da.Constant((0.0, 0.0, 0.0)), left)