def test_iterate_regional_gamma_pressure(): problem = make_lv_mechanics_problem("regional") target_gamma = problem.material.activation.vector().get_local() for i in range(len(target_gamma)): target_gamma[i] = 0.01 - i * 0.001 gamma = problem.material.activation target_pressure = 0.1 plv = [p.traction for p in problem.bcs.neumann if p.name == "lv"] pressure = plv[0] iterate(problem, (pressure, gamma), (target_pressure, target_gamma))
def test_iterate_regional_gamma_pressure(): problem = make_lv_mechanics_problem('regional') target_gamma = problem.material.activation.vector().get_local() for i in range(len(target_gamma)): target_gamma[i] = 0.1 - i * 0.001 gamma = problem.material.activation target_pressure = 1.0 plv = [p.traction for p in problem.bcs.neumann if p.name == 'lv'] pressure = plv[0] with pytest.raises(ValueError): iterate(problem, (pressure, gamma), (target_pressure, target_gamma))
def increase_pressure(self): p_lv_next = next(self.lv_pressure_gen) if self.has_rv: p_rv_next = next(self.rv_pressure_gen) target = (p_lv_next, p_rv_next) control = (self.p_lv, self.p_rv) else: target = p_lv_next control = self.p_lv iterate(problem=self.solver, target=target, control=control, continuation=True)
def next_active(self, gamma_current, gamma, assign_prev_state=True, steps=None): old_states, old_gammas = self.load_states() states, gammas = iterate(problem=self.solver, control=gamma, target=gamma_current, continuation=True, old_states=old_states, old_controls=old_gammas) # Store these gammas and states which can be used # as initial guess for the newton solver in a later # iteration self.store_states(states, gammas) if assign_prev_state: # Assign the previous state self.solver.reinit(states[-1]) self.solver.material.activation.assign(gammas[-1]) return self.get_state(False)
def test_iterate_gamma(problem): target_gamma = 0.1 gamma = problem.material.activation if has_dolfin_adjoint: dolfin.parameters["adjoint"]["stop_annotating"] = False dolfin_adjoint.adj_reset() iterate(problem, gamma, target_gamma) assert all(gamma.vector().get_local() == target_gamma) assert dolfin.norm(problem.state.vector()) > 0 if has_dolfin_adjoint: assert dolfin_adjoint.replay_dolfin(tol=1e-12)
def iteration(self, control): """ FIXME TODO: Make this more elegant """ logger.info(f"Try control value {constant2float(control)}") for count in range(self.data_points): # Stop the recording annotate = annotation.annotate bcs = [bc.bc.traction for bc in self.bcs] targets = [bc.data[count] for bc in self.bcs] # Iterate bc iterate(self.problem, bcs, targets) annotation.annotate = False # Iterate control prev_states, prev_controls = iterate(self.problem, self.control, control) self.problem.state.assign(prev_states[-1]) self.control.assign(prev_controls[-1]) # Continue recording annotation.annotate = annotate for b in self.bcs: b._count = count b.assign_bc() self.problem.solve() self.control.assign(control, annotate=annotate) self.problem.solve() u, p = dolfin.split(self.problem.state) for t in self.targets: t._count = count t.assign(u, annotate=annotate) yield count
def test_iterate_pressure(problem): target_pressure = 1.0 plv = [p.traction for p in problem.bcs.neumann if p.name == 'lv'] pressure = plv[0] if has_dolfin_adjoint: dolfin.parameters["adjoint"]["stop_annotating"] = False dolfin_adjoint.adj_reset() iterate(problem, pressure, target_pressure) if has_dolfin_adjoint: # Check the recording assert dolfin_adjoint.replay_dolfin(tol=1e-12) # Check that the pressure is correct assert float(plv[0]) == target_pressure # Check that the state is nonzero assert dolfin.norm(problem.state.vector()) > 0
def test_iterate_gamma_pressure(problem): target_pressure = 1.0 plv = [p.traction for p in problem.bcs.neumann if p.name == 'lv'] pressure = plv[0] target_gamma = 0.1 gamma = problem.material.activation if has_dolfin_adjoint: dolfin.parameters["adjoint"]["stop_annotating"] = False dolfin_adjoint.adj_reset() iterate(problem, (pressure, gamma), (target_pressure, target_gamma)) assert all(gamma.vector().get_local() == target_gamma) assert float(plv[0]) == target_pressure assert dolfin.norm(problem.state.vector()) > 0 if has_dolfin_adjoint: assert dolfin_adjoint.replay_dolfin(tol=1e-12)
def test_iterate_gamma_cg1(continuation): problem = make_lv_mechanics_problem('CG_1') V = problem.material.activation.function_space() target_gamma \ = dolfin.interpolate(dolfin.Expression('0.1 * x[0]', degree=1), V) gamma = problem.material.activation if has_dolfin_adjoint: dolfin.parameters["adjoint"]["stop_annotating"] = False dolfin_adjoint.adj_reset() iterate(problem, gamma, target_gamma, continuation=continuation) assert np.all( gamma.vector().get_local() - target_gamma.vector().get_local() < 1e-12) assert dolfin.norm(problem.state.vector()) > 0 if has_dolfin_adjoint: assert dolfin_adjoint.replay_dolfin(tol=1e-12)
def test_iterate_gamma_regional(): problem = make_lv_mechanics_problem('regional') target_gamma = problem.material.activation.vector().get_local() for i in range(len(target_gamma)): target_gamma[i] = 0.1 - i * 0.001 print(target_gamma) gamma = problem.material.activation if has_dolfin_adjoint: dolfin.parameters["adjoint"]["stop_annotating"] = False dolfin_adjoint.adj_reset() iterate(problem, gamma, target_gamma) print(gamma.vector().get_local()) assert np.all(gamma.vector().get_local() - target_gamma < 1e-12) assert dolfin.norm(problem.state.vector()) > 0 if has_dolfin_adjoint: assert dolfin_adjoint.replay_dolfin(tol=1e-12)