|
311 | 311 | "avg_X = [X.initial_condition]\n", |
312 | 312 | "avg_B = [B.initial_condition]\n", |
313 | 313 | "# Set loglevel to warning in order not to pollute notebook output\n", |
314 | | - "logger.setLevel(logging.DEBUG)\n", |
| 314 | + "logger.setLevel(logging.WARNING)\n", |
315 | 315 | "\n", |
316 | 316 | "while True:\n", |
317 | 317 | " # Solve the system\n", |
|
333 | 333 | " break" |
334 | 334 | ] |
335 | 335 | }, |
336 | | - { |
337 | | - "cell_type": "code", |
338 | | - "execution_count": null, |
339 | | - "id": "72884c41", |
340 | | - "metadata": {}, |
341 | | - "outputs": [], |
342 | | - "source": [ |
343 | | - "import petsc4py.PETSc as PETSc\n", |
344 | | - "Adof = model_cur.sc[\"A\"].dof_map\n", |
345 | | - "Avec = model_cur.sc[\"A\"].sol.vector()[Adof]\n", |
346 | | - "Bdof = model_cur.sc[\"B\"].dof_map\n", |
347 | | - "Bvec = model_cur.sc[\"B\"].sol.vector()[Bdof]\n", |
348 | | - "Xdof = model_cur.sc[\"X\"].dof_map\n", |
349 | | - "Xvec = model_cur.sc[\"X\"].sol.vector()[Xdof]\n", |
350 | | - "data_dict = dict()\n", |
351 | | - "data_dict[\"A\"] = Avec\n", |
352 | | - "data_dict[\"B\"] = Bvec\n", |
353 | | - "data_dict[\"X\"] = Xvec\n", |
354 | | - "\n", |
355 | | - "def weak_residual(model_cur, data_dict):\n", |
356 | | - " for s in model_cur.sc:\n", |
357 | | - " if s.name not in data_dict.keys():\n", |
358 | | - " raise ValueError(\"Must provide vector for all species\")\n", |
359 | | - " else:\n", |
360 | | - " cur_data = data_dict[s.name]\n", |
361 | | - " cur_vec = s.u[\"u\"].vector()\n", |
362 | | - " cur_dof = s.dof_map\n", |
363 | | - " cur_vec[cur_dof] = cur_data\n", |
364 | | - " s.u[\"u\"].vector().set_local(cur_vec)\n", |
365 | | - " s.u[\"u\"].vector().apply(\"insert\")\n", |
366 | | - " if len(model_cur.problem.global_sizes) == 1:\n", |
367 | | - " model_cur._ubackend = model_cur.u[\"u\"]._functions[0].vector().vec().copy()\n", |
368 | | - " else:\n", |
369 | | - " model_cur._ubackend = PETSc.Vec().createNest(\n", |
370 | | - " [usub.vector().vec().copy() for usub in model_cur.u[\"u\"]._functions],\n", |
371 | | - " comm=model_cur.mpi_comm_world,\n", |
372 | | - " )\n", |
373 | | - " return model_cur.solver.getFunctionNorm()\n", |
374 | | - "\n", |
375 | | - "print(f\"Residual is {weak_residual(model_cur, data_dict)}\")\n", |
376 | | - "data_dict[\"A\"] += 1.0\n", |
377 | | - "print(f\"Perturbed residual is {weak_residual(model_cur, data_dict)}\")" |
378 | | - ] |
379 | | - }, |
380 | 336 | { |
381 | 337 | "attachments": {}, |
382 | 338 | "cell_type": "markdown", |
|
0 commit comments