├── .circleci └── config.yml ├── .github └── workflows │ ├── docs.yml │ └── pypi.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── ChangeLog.rst ├── LICENSE ├── README.md ├── docker └── firedrake │ └── README ├── docs ├── Makefile ├── bin │ └── pylit.py ├── requirements.txt └── source │ ├── _static │ ├── adjoint.html │ ├── burgers_timesteps.png │ ├── burgers_timesteps_expanded.png │ ├── dolfin_adjoint.pdf │ ├── forward.html │ ├── gst.pdf │ ├── libadjoint.pdf │ ├── logo.pdf │ ├── more.png │ ├── optimal_control.py │ ├── optimisation.pdf │ ├── overloading │ │ ├── normalise.py │ │ ├── normalise_overloaded.py │ │ ├── tutorial9.py │ │ └── tutorial9_overloading.py │ ├── poster_funke_2015.pdf │ ├── poster_small_funke_2015.pdf │ ├── pygments.css │ ├── simplified_burgers │ ├── simplified_burgers.png │ ├── simplified_burgers_named │ ├── simplified_burgers_named.png │ ├── slider │ │ ├── klein.png │ │ └── poisson-topology.png │ ├── time-dependent-wave.py │ ├── tutorial1.py │ ├── tutorial2.py │ ├── tutorial3.py │ ├── tutorial4.py │ ├── tutorial7.py │ ├── tutorial7_name_scope.py │ ├── tutorial7_named.py │ └── tutorial8.py │ ├── _themes │ └── dolfin-adjoint │ │ ├── README │ │ ├── layout.html │ │ ├── static │ │ ├── banner.png │ │ ├── banner.svg │ │ ├── dialog-note.png │ │ ├── dialog-seealso.png │ │ ├── dialog-topic.png │ │ ├── dialog-warning.png │ │ ├── epub.css │ │ ├── feature-item-1.png │ │ ├── featured.css │ │ ├── feed-icon-14x14.gif │ │ ├── fenics-book-icon.png │ │ ├── fenics-web.png │ │ ├── fenics.css_t │ │ ├── flexslider │ │ │ ├── README.mdown │ │ │ ├── changelog.txt │ │ │ ├── flexslider.css │ │ │ ├── fonts │ │ │ │ ├── flexslider-icon.eot │ │ │ │ ├── flexslider-icon.svg │ │ │ │ ├── flexslider-icon.ttf │ │ │ │ └── flexslider-icon.woff │ │ │ ├── images │ │ │ │ └── bg_play_pause.png │ │ │ ├── jquery.flexslider-min.js │ │ │ └── jquery.flexslider.js │ │ ├── footerbg.png │ │ ├── headerbg.png │ │ ├── highlight │ │ │ ├── CHANGES.md │ │ │ ├── LICENSE │ │ │ ├── README.md │ │ │ ├── README.ru.md │ │ │ ├── highlight.pack.js │ │ │ └── styles │ │ │ │ ├── arta.css │ │ │ │ ├── ascetic.css │ │ │ │ ├── atelier-dune.dark.css │ │ │ │ ├── atelier-dune.light.css │ │ │ │ ├── atelier-forest.dark.css │ │ │ │ ├── atelier-forest.light.css │ │ │ │ ├── atelier-heath.dark.css │ │ │ │ ├── atelier-heath.light.css │ │ │ │ ├── atelier-lakeside.dark.css │ │ │ │ ├── atelier-lakeside.light.css │ │ │ │ ├── atelier-seaside.dark.css │ │ │ │ ├── atelier-seaside.light.css │ │ │ │ ├── brown_paper.css │ │ │ │ ├── brown_papersq.png │ │ │ │ ├── dark.css │ │ │ │ ├── default.css │ │ │ │ ├── docco.css │ │ │ │ ├── far.css │ │ │ │ ├── foundation.css │ │ │ │ ├── github.css │ │ │ │ ├── googlecode.css │ │ │ │ ├── idea.css │ │ │ │ ├── ir_black.css │ │ │ │ ├── magula.css │ │ │ │ ├── mono-blue.css │ │ │ │ ├── monokai.css │ │ │ │ ├── monokai_sublime.css │ │ │ │ ├── obsidian.css │ │ │ │ ├── paraiso.dark.css │ │ │ │ ├── paraiso.light.css │ │ │ │ ├── pojoaque.css │ │ │ │ ├── pojoaque.jpg │ │ │ │ ├── railscasts.css │ │ │ │ ├── rainbow.css │ │ │ │ ├── school_book.css │ │ │ │ ├── school_book.png │ │ │ │ ├── solarized_dark.css │ │ │ │ ├── solarized_light.css │ │ │ │ ├── sunburst.css │ │ │ │ ├── tomorrow-night-blue.css │ │ │ │ ├── tomorrow-night-bright.css │ │ │ │ ├── tomorrow-night-eighties.css │ │ │ │ ├── tomorrow-night.css │ │ │ │ ├── tomorrow.css │ │ │ │ ├── vs.css │ │ │ │ ├── xcode.css │ │ │ │ └── zenburn.css │ │ ├── icon.ico │ │ ├── icon.png │ │ ├── ie6.css │ │ ├── middlebg.png │ │ ├── neuton-fontfacekit │ │ │ ├── Apache License Version 2.txt │ │ │ ├── Neuton-webfont.eot │ │ │ ├── Neuton-webfont.svg │ │ │ ├── Neuton-webfont.ttf │ │ │ ├── Neuton-webfont.woff │ │ │ ├── demo.html │ │ │ └── stylesheet.css │ │ ├── nobile-fontfacekit │ │ │ ├── SIL Open Font License 1.1.txt │ │ │ ├── demo.html │ │ │ ├── nobile-webfont.eot │ │ │ ├── nobile-webfont.svg │ │ │ ├── nobile-webfont.ttf │ │ │ ├── nobile-webfont.woff │ │ │ ├── nobile_bold-webfont.eot │ │ │ ├── nobile_bold-webfont.svg │ │ │ ├── nobile_bold-webfont.ttf │ │ │ ├── nobile_bold-webfont.woff │ │ │ ├── nobile_bold_italic-webfont.eot │ │ │ ├── nobile_bold_italic-webfont.svg │ │ │ ├── nobile_bold_italic-webfont.ttf │ │ │ ├── nobile_bold_italic-webfont.woff │ │ │ ├── nobile_italic-webfont.eot │ │ │ ├── nobile_italic-webfont.svg │ │ │ ├── nobile_italic-webfont.ttf │ │ │ ├── nobile_italic-webfont.woff │ │ │ └── stylesheet.css │ │ ├── sample-news-image.png │ │ ├── slides.min.jquery.js │ │ ├── social-buttons.html │ │ ├── transparent.gif │ │ └── unknown.png │ │ └── theme.conf │ ├── about │ ├── difference.rst │ └── index.rst │ ├── citing │ └── index.rst │ ├── conf.py │ ├── documentation │ ├── custom_functions.rst │ ├── debugging.rst │ ├── faq.rst │ ├── functionals.rst │ ├── index.rst │ ├── manual.rst │ ├── maths │ │ ├── 1-foreword.bib │ │ ├── 1-foreword.rst │ │ ├── 2-problem.bib │ │ ├── 2-problem.rst │ │ ├── 3-gradients.bib │ │ ├── 3-gradients.rst │ │ ├── 4-adjoint.bib │ │ ├── 4-adjoint.rst │ │ ├── 5-applications.bib │ │ ├── 5-applications.rst │ │ ├── A-gst.bib │ │ ├── A-gst.rst │ │ └── index.rst │ ├── misc.rst │ ├── optimisation.rst │ ├── parallel.rst │ ├── pyadjoint_api.rst │ ├── pyadjoint_docs.rst │ └── verification.rst │ ├── download │ └── index.rst │ ├── frontpage.rst │ ├── index.rst │ └── support │ └── index.rst ├── examples ├── burgers │ └── burgers.py ├── heat-equation │ └── heat-equation.py ├── klein │ ├── klein.h5 │ ├── klein.py │ ├── klein.xdmf │ └── make-mesh.py ├── mpec │ └── mpec.py ├── poisson-mother │ ├── poisson-mother-ipopt.py │ ├── poisson-mother-rol.py │ ├── poisson-mother-scipy.py │ └── poisson-mother.py ├── poisson-topology │ ├── poisson-topology-3d.py │ └── poisson-topology.py ├── reaction-rate-learning │ ├── concentrations.png │ ├── learned_reaction_rates.png │ ├── navier_stokes_cylinder │ │ ├── compute-flow-field.py │ │ ├── cylinder.xml.gz │ │ └── velocity_series.h5 │ ├── neural_network.py │ └── reaction-rate-learning.py ├── shape-optimization-pipe │ ├── mesh │ │ ├── pipe.geo │ │ ├── pipe.xml │ │ ├── pipe_facet_region.xml │ │ └── pipe_physical_region.xml │ └── shape-optimization-pipe.py ├── stokes-bc-control │ ├── make-mesh.py │ ├── rectangle-less-circle.h5 │ ├── rectangle-less-circle.xdmf │ ├── stokes-bc-control.py │ └── stokes-bc-control_nitsche.py ├── stokes-shape-opt │ ├── create_mesh.py │ ├── mesh.h5 │ ├── mesh.xdmf │ ├── mf.h5 │ ├── mf.xdmf │ └── stokes_problem.py ├── stokes-topology │ ├── stokes-topology-rol-firedrake.py │ ├── stokes-topology-rol.py │ └── stokes-topology.py ├── time-dependent-wave │ └── time-dependent-wave.py ├── time-distributed-control │ └── time-distributed-control.py └── tube-shape-derivative │ ├── mesh │ ├── cable.geo │ ├── cable1.xml │ ├── cable1_facet_region.xml │ ├── cable2.xml │ └── cable2_facet_region.xml │ └── tube-shape-derivative.py ├── firedrake_adjoint └── __init__.py ├── joss ├── paper.md └── references.bib ├── numpy_adjoint ├── __init__.py └── array.py ├── pyadjoint ├── __init__.py ├── adjfloat.py ├── block.py ├── block_variable.py ├── checkpointing.py ├── control.py ├── drivers.py ├── enlisting.py ├── ipopt.py ├── optimization │ ├── __init__.py │ ├── constraints.py │ ├── ipopt_solver.py │ ├── moola_problem.py │ ├── optimization.py │ ├── optimization_problem.py │ ├── optimization_solver.py │ ├── rol_solver.py │ └── tao_solver.py ├── ordered_set.py ├── overloaded_function.py ├── overloaded_type.py ├── placeholder.py ├── reduced_functional.py ├── reduced_functional_numpy.py ├── tape.py └── verification.py ├── pyproject.toml ├── setup.cfg └── tests ├── __init__.py ├── conftest.py ├── migration ├── README.md ├── assembled_action │ ├── assembled_action.py │ └── test_assembled_action.py ├── burgers_newton │ ├── burgers_newton.py │ └── test_burgers_newton.py ├── burgers_newton_time_functional │ ├── burgers_newton_time_functional.py │ └── test_burgers_newton_time_functional.py ├── burgers_oo │ ├── burgers_oo.py │ └── test_burgers_oo.py ├── burgers_picard │ ├── burgers_picard.py │ ├── manual_hessian.py │ └── test_burgers_picard.py ├── cahn_hilliard │ ├── cahn_hilliard.py │ ├── paper_adjoint.py │ ├── paper_tlm.py │ ├── test_cahn_hilliard.py │ └── timings │ │ ├── adjoint-2.log │ │ ├── adjoint.log │ │ ├── adjoint.py │ │ ├── annotated-2.log │ │ ├── annotated.log │ │ ├── annotated.py │ │ ├── run_timings.sh │ │ ├── unannotated-2.log │ │ ├── unannotated.log │ │ └── unannotated.py ├── changing_vector │ ├── changing_vector.py │ └── test_changing_vector.py ├── checkpoint_burgers │ ├── checkpoint_burgers.py │ └── test_checkpoint_burgers.py ├── checkpoint_burgers_newton │ ├── checkpoint_burgers_newton.py │ └── test_checkpoint_burgers_newton.py ├── checkpoint_online │ ├── checkpoint_online.py │ └── test_checkpoint_online.py ├── checkpoint_stokes │ ├── checkpoint_stokes.py │ └── test_checkpoint_stokes.py ├── compute_tlm_list_parameter │ ├── compute_tlm_list_parameter.py │ └── test_compute_tlm_list_parameter.py ├── curl-curl │ ├── curl-curl.py │ └── test_curl-curl.py ├── default_parameters │ ├── default_parameters.py │ └── test_default_parameters.py ├── differentiability-dg-upwind │ ├── differentiability-dg-upwind.py │ ├── mesh.xml.gz │ ├── temperature.xml.gz │ ├── test_differentiability-dg-upwind.py │ └── velocity.xml.gz ├── differentiability-stokes │ ├── differentiability-stokes.py │ ├── mesh.xml.gz │ ├── temperature.xml.gz │ ├── test_differentiability-stokes.py │ └── velocity.xml.gz ├── distributed_control │ └── test_distributed_control.py ├── dolfin_adjoint_variable │ ├── dolfin_adjoint_variable.py │ └── test_dolfin_adjoint_variable.py ├── expression_derivative │ ├── expression_derivative.py │ └── test_expression_derivative.py ├── expression_derivative_cpp │ ├── expression_derivative_cpp.py │ └── test_expression_derivative_cpp.py ├── expression_derivative_inline_cpp │ ├── expression_derivative_inline_cpp.py │ └── test_expression_derivative_inline_cpp.py ├── expression_updates │ ├── expression_updates.py │ └── test_expression_updates.py ├── facet_integrals │ └── test_facet_integrals.py ├── fg_constant │ ├── fg_constant.py │ └── test_fg_constant.py ├── fg_constants │ ├── fg_constants.py │ └── test_fg_constants.py ├── function_assign_lincom │ ├── function_assign_lincom.py │ └── test_function_assign_lincom.py ├── function_assigner │ ├── function_assigner.py │ └── test_function_assigner.py ├── function_assigner_givsub │ ├── function_assigner_givsub.py │ └── test_function_assigner_givsub.py ├── functional_dependencies │ ├── functional_dependencies.py │ └── test_functional_dependencies.py ├── functional_form │ ├── functional_form.py │ └── test_functional_form.py ├── functional_operations │ ├── functional_operations.py │ └── test_functional_operations.py ├── functional_value │ ├── functional_value.py │ └── test_functional_value.py ├── gst_mass │ ├── gst_mass.py │ └── test_gst_mass.py ├── heat │ ├── heat.py │ └── test_heat.py ├── hessian_callback │ ├── hessian_callback.py │ └── test_hessian_callback.py ├── hessian_eps │ ├── hessian_eps.py │ └── test_hessian_eps.py ├── hessian_identity │ ├── graph.png │ ├── hessian_identity.py │ ├── manual_hessian.py │ └── test_hessian_identity.py ├── hessian_identity_list │ ├── hessian_identity_list.py │ └── test_hessian_identity_list.py ├── hessian_identity_list2 │ ├── hessian_identity_list2.py │ └── test_hessian_identity_list2.py ├── interpolate │ ├── interpolate.py │ ├── test_interpolate.py │ └── test_interpolate_unit.py ├── krylov_reevaluate │ ├── krylov_reevaluate.py │ └── test_krylov_reevaluate.py ├── linear_solver │ ├── linear_solver.py │ └── test_linear_solver.py ├── list_parameter │ ├── list_parameter.py │ └── test_list_parameter.py ├── localsolver │ ├── localsolver.py │ └── test_localsolver.py ├── localsolver_factorize │ ├── localsolver_factorize.py │ └── test_localsolver_factorize.py ├── lusolver_changing │ ├── lusolver_changing.py │ └── test_lusolver_changing.py ├── lusolver_reassemble │ ├── lusolver_reassemble.py │ └── test_lusolver_reassemble.py ├── lvs │ ├── lvs.py │ └── test_lvs.py ├── manual_hessian │ ├── manual_hessian.py │ └── test_manual_hessian.py ├── matrix_free_burgers │ ├── matrix_free_burgers.py │ └── test_matrix_free_burgers.py ├── matrix_free_heat │ ├── matrix_free_heat.py │ └── test_matrix_free_heat.py ├── matrix_free_simple │ ├── matrix_free_simple.py │ └── test_matrix_free_simple.py ├── matrix_summation │ ├── matrix_summation.py │ └── test_matrix_summation.py ├── mpec │ ├── mpec.py │ ├── test_mpec.py │ └── timing.py ├── multimesh_poisson │ ├── mesh_poisson.py │ ├── multimesh_poisson.py │ └── test_multimesh_poisson.py ├── navier_stokes │ ├── lshape.xml.gz │ ├── navier_stokes.py │ ├── picard.py │ └── test_navier_stokes.py ├── newton_constant │ ├── newton_constant.py │ └── test_newton_constant.py ├── noannotations │ ├── noannotations.py │ └── test_noannotations.py ├── nullspace │ ├── nullspace.py │ └── test_nullspace.py ├── nullspace_linear_solver │ ├── nullspace_linear_solver.py │ └── test_nullspace_linear_solver.py ├── nvs │ ├── nvs.py │ └── test_nvs.py ├── ode_fitzhughnagumo │ ├── basicsinglecellsolver_fitzhughnagumo.py │ ├── fitzhughnagumo.py │ ├── ode_fitzhughnagumo.py │ └── test_ode_fitzhughnagumo.py ├── ode_solver │ ├── ode_solver.py │ └── test_ode_solver.py ├── ode_tentusscher │ ├── ode_tentusscher.py │ ├── tentusscher_2004_mcell.py │ └── test_ode_tentusscher.py ├── ode_tentusscher_quick │ ├── ode_tentusscher_quick.py │ ├── tentusscher_2004_mcell.py │ └── test_ode_tentusscher_quick.py ├── ode_vector │ ├── ode_vector.py │ └── test_ode_vector.py ├── optimal_control_bfgs │ ├── mesh.geo │ ├── mesh.xml │ ├── mesh_physical_region.xml │ ├── optimal_control_bfgs.py │ └── test_optimal_control_bfgs.py ├── optimal_control_mms │ ├── optimal_control_mms.py │ └── test_optimal_control_mms.py ├── optimization_checkpointing │ ├── optimization_checkpointing.py │ └── test_optimization_checkpointing.py ├── optimization_moola │ ├── optimization_moola.py │ └── test_optimization_moola.py ├── optimization_optizelle │ ├── optimization_optizelle.py │ ├── optimization_optizelle_2d.py │ └── test_optimization_optizelle.py ├── optimization_optizelle_algebra │ ├── optimization_optizelle_algebra.py │ └── test_optimization_optizelle_algebra.py ├── optimization_pyipopt │ ├── optimization_pyipopt.py │ └── test_optimization_pyipopt.py ├── optimization_pyopt │ ├── optimization_pyopt.py │ └── test_optimization_pyopt.py ├── optimization_scalar │ ├── optimization_scalar.py │ └── test_optimization_scalar.py ├── optimization_scipy │ ├── optimization_scipy.py │ └── test_optimization_scipy.py ├── optimization_tao │ ├── optimization_tao.py │ └── test_optimization_tao.py ├── periodic │ ├── periodic.py │ └── test_periodic.py ├── petsckrylov_reevaluate │ ├── petsckrylov_reevaluate.py │ └── test_petsckrylov_reevaluate.py ├── picard-linearisation │ ├── picard-linearisation.py │ └── test_picard-linearisation.py ├── pointwise_functional │ ├── pointwise_functional.py │ └── test_pointwise_functional.py ├── pointwise_functional_one_point │ ├── .gitignore │ ├── pointwise_functional_one_point.py │ └── test_pointwise_functional_one_point.py ├── pointwise_functional_regularisation │ ├── .gitignore │ ├── pointwise_functional_regularisation.py │ └── test_pointwise_functional_regularisation.py ├── pointwise_functional_two_points │ ├── .gitignore │ ├── pointwise_functional_two_points.py │ └── test_pointwise_functional_two_points.py ├── pointwise_functional_vector │ ├── pointwise_functional_vector.py │ └── test_pointwise_functional_vector.py ├── preassembly_efficiency │ ├── preassembly_efficiency.py │ └── test_preassembly_efficiency.py ├── projection │ ├── projection.py │ └── test_projection.py ├── reduced_functional │ ├── reduced_functional.py │ └── test_reduced_functional.py ├── reduced_functional_cache │ ├── reduced_functional_cache.py │ └── test_reduced_functional_cache.py ├── reduced_functional_constants │ ├── reduced_functional_constants.py │ └── test_reduced_functional_constants.py ├── reduced_functional_evaluation │ ├── reduced_functional_evaluation.py │ └── test_reduced_functional_evaluation.py ├── rush_larsen │ ├── rush_larsen.py │ └── test_rush_larsen.py ├── shallow_water │ ├── basin.geo │ ├── basin.msh │ ├── basin.xml │ ├── kelvin_new.py │ ├── shallow_water.py │ ├── sw_lib.py │ └── test_shallow_water.py ├── shallow_water_time_functional │ ├── Makefile │ ├── basin.geo │ ├── basin.msh │ ├── basin.xml │ ├── divett.py │ ├── shallow_water_time_functional.py │ ├── sw_lib.py │ └── test_shallow_water_time_functional.py ├── split │ ├── split.py │ └── test_split.py ├── stokes │ ├── stokes.py │ └── test_stokes.py ├── stokes_krylov │ ├── stokes_krylov.py │ └── test_stokes_krylov.py ├── stokes_maday │ ├── stokes_maday.py │ └── test_stokes_maday.py ├── stokes_petsckrylov │ ├── stokes_petsckrylov.py │ └── test_stokes_petsckrylov.py ├── supg │ ├── mesh.xml.gz │ ├── subdomains.xml.gz │ ├── supg.py │ ├── test_supg.py │ └── velocity.xml.gz ├── svd_bc_simple │ ├── svd_bc_simple.py │ └── test_svd_bc_simple.py ├── svd_burgers │ ├── svd_burgers.py │ └── test_svd_burgers.py ├── svd_burgers_perturb │ ├── svd_burgers_perturb.py │ └── test_svd_burgers_perturb.py ├── svd_simple │ ├── svd_simple.py │ └── test_svd_simple.py ├── taylor_hessian │ ├── taylor_hessian.py │ └── test_taylor_hessian.py ├── time_dependent_data │ ├── test_time_dependent_data.py │ └── time_dependent_data.py ├── time_functionals │ ├── test_time_functionals.py │ └── time_functionals.py ├── time_functionals_no_annotation │ ├── test_time_functionals_no_annotation.py │ └── time_functionals_no_annotation.py ├── timeforms │ └── test_timeforms.py ├── tlm_burgers │ ├── test_tlm_burgers.py │ └── tlm_burgers.py ├── tlm_list_parameter │ ├── test_tlm_list_parameter.py │ └── tlm_list_parameter.py ├── tlm_scalar_parameter │ ├── test_tlm_scalar_parameter.py │ └── tlm_scalar_parameter.py ├── tlm_simple │ ├── test_tlm_simple.py │ └── tlm_simple.py ├── upwind │ ├── mesh.xml.gz │ ├── test_upwind.py │ ├── upwind.py │ └── velocity.xml.gz ├── viscoelasticity │ ├── extract_results.py │ ├── mesh_edgelength2.xml.gz │ ├── mesh_edgelength4.xml.gz │ ├── paper.py │ ├── test_viscoelasticity.py │ ├── timings │ │ ├── adjoint.log │ │ ├── adjoint.py │ │ ├── annotated.log │ │ ├── annotated.py │ │ ├── run_timings.sh │ │ ├── unannotated.log │ │ └── unannotated.py │ └── viscoelasticity.py └── zero_derivative │ ├── test_zero_derivative.py │ └── zero_derivative.py └── pyadjoint ├── test_enlisting.py ├── test_floats.py ├── test_lcoe.py ├── test_numpy.py ├── test_overload_function.py └── test_placeholder.py /.github/workflows/pypi.yml: -------------------------------------------------------------------------------- 1 | name: Upload release to pypi 2 | on: 3 | workflow_dispatch: 4 | push: 5 | tags: "v*" 6 | 7 | jobs: 8 | pypi-publish: 9 | name: Upload release to PyPI 10 | runs-on: ubuntu-latest 11 | environment: 12 | name: pypi 13 | url: https://pypi.org/p/pyadjoint-ad 14 | permissions: 15 | id-token: write 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | 20 | - name: Setup Python 21 | uses: actions/setup-python@v4 22 | with: 23 | python-version: "3.10" 24 | 25 | - name: Install dependencies 26 | run: python3 -m pip install build twine 27 | 28 | - name: Build a binary wheel and a source tarball 29 | run: python3 -m build --sdist --wheel --outdir dist/ . 30 | 31 | - name: Publish package distributions to PyPI 32 | uses: pypa/gh-action-pypi-publish@release/v1 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .cache/* 3 | *.swp 4 | docs/_build/* 5 | docs/build/* 6 | *.egg-info 7 | *.py~ 8 | dist/* 9 | 10 | # Generated files 11 | *.pvd 12 | *.vtu 13 | *.dot 14 | .idea/* 15 | *.xml 16 | *.msh 17 | 18 | dist/ 19 | build/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The algorithmic differentation tool pyadjoint 2 | 3 | The full documentation is available [here](https://pyadjoint.org) 4 | 5 | Circle CI status: [![CircleCI](https://circleci.com/gh/dolfin-adjoint/pyadjoint.svg?style=svg)](https://circleci.com/gh/dolfin-adjoint/pyadjoint) 6 | 7 | 8 | # Contributing 9 | 10 | We love pull requests from everyone. 11 | 12 | Fork, then clone the repository at https://github.com/dolfin-adjoint/pyadjoint 13 | 14 | Make sure the tests pass: 15 | 16 | py.test tests/pyadjoint 17 | 18 | Make your change. Add tests for your change. Make the tests pass: 19 | 20 | py.test tests/pyadjoint 21 | 22 | Push to your fork and [submit a pull request][pr]. 23 | 24 | [pr]: https://github.com/dolfin-adjoint/pyadjoint/pulls 25 | 26 | At this point you're waiting on us. We may suggest 27 | some changes or improvements or alternatives. 28 | 29 | Some things that will increase the chance that your pull request is accepted: 30 | 31 | * Write tests. 32 | * Add Python docstrings that follow the [Google Style][style]. 33 | * Write good commit and pull request message. 34 | 35 | [style]: http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html 36 | 37 | # Licence 38 | This software is licensed under the [GNU LGPL v3][license]. 39 | 40 | [license]: https://github.com/dolfin-adjoint/pyadjoint/raw/master/LICENSE 41 | -------------------------------------------------------------------------------- /docker/firedrake/README: -------------------------------------------------------------------------------- 1 | Please use the Firedrake docker image as it already contains dolfin-adjoint. 2 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | scipy 2 | sphinx 3 | sphinxcontrib-bibtex 4 | git+https://github.com/shomah4a/sphinxcontrib.youtube.git 5 | git+https://github.com/funsim/moola.git@master 6 | -------------------------------------------------------------------------------- /docs/source/_static/burgers_timesteps.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/burgers_timesteps.png -------------------------------------------------------------------------------- /docs/source/_static/burgers_timesteps_expanded.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/burgers_timesteps_expanded.png -------------------------------------------------------------------------------- /docs/source/_static/dolfin_adjoint.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/dolfin_adjoint.pdf -------------------------------------------------------------------------------- /docs/source/_static/gst.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/gst.pdf -------------------------------------------------------------------------------- /docs/source/_static/libadjoint.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/libadjoint.pdf -------------------------------------------------------------------------------- /docs/source/_static/logo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/logo.pdf -------------------------------------------------------------------------------- /docs/source/_static/more.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/more.png -------------------------------------------------------------------------------- /docs/source/_static/optimal_control.py: -------------------------------------------------------------------------------- 1 | """ Solves the optimal control problem for the heat equation """ 2 | from dolfin import * 3 | from dolfin_adjoint import * 4 | 5 | # Setup 6 | n = 200 7 | mesh = RectangleMesh(Point(-1, -1), Point(1, 1), n, n) 8 | V = FunctionSpace(mesh, "CG", 1) 9 | u = Function(V, name="State") 10 | m = Function(V, name="Control") 11 | v = TestFunction(V) 12 | 13 | # Run the forward model once to create the simulation record 14 | F = (inner(grad(u), grad(v)) - m*v)*dx 15 | bc = DirichletBC(V, 0.0, "on_boundary") 16 | solve(F == 0, u, bc) 17 | 18 | # The functional of interest is the normed difference between desired 19 | # and simulated temperature profile 20 | x = SpatialCoordinate(mesh) 21 | u_desired = exp(-1/(1-x[0]*x[0])-1/(1-x[1]*x[1])) 22 | J = assemble((0.5*inner(u-u_desired, u-u_desired))*dx) 23 | 24 | # Run the optimisation 25 | reduced_functional = ReducedFunctional(J, Control(m)) 26 | # Make sure you have scipy >= 0.11 installed 27 | m_opt = minimize(reduced_functional, method = "L-BFGS-B", 28 | tol=2e-08, bounds = (-1, 1), options = {"disp": True}) 29 | -------------------------------------------------------------------------------- /docs/source/_static/optimisation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/optimisation.pdf -------------------------------------------------------------------------------- /docs/source/_static/overloading/normalise.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | 5 | def normalise(func): 6 | vec = func.vector() 7 | normalised_vec = vec / vec.norm('l2') 8 | return Function(func.function_space(), normalised_vec) 9 | -------------------------------------------------------------------------------- /docs/source/_static/overloading/normalise_overloaded.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from pyadjoint import Block 5 | from pyadjoint.overloaded_function import overload_function 6 | 7 | from normalise import normalise 8 | 9 | 10 | backend_normalise = normalise 11 | 12 | 13 | class NormaliseBlock(Block): 14 | def __init__(self, func, **kwargs): 15 | super(NormaliseBlock, self).__init__() 16 | self.kwargs = kwargs 17 | self.add_dependency(func) 18 | 19 | def __str__(self): 20 | return 'NormaliseBlock' 21 | 22 | def evaluate_adj_component(self, inputs, adj_inputs, block_variable, idx, prepared=None): 23 | adj_input = adj_inputs[0] 24 | x = inputs[idx].vector() 25 | inv_xnorm = 1.0 / x.norm('l2') 26 | return inv_xnorm * adj_input - inv_xnorm ** 3 * x.inner(adj_input) * x 27 | 28 | def recompute_component(self, inputs, block_variable, idx, prepared): 29 | return backend_normalise(inputs[0]) 30 | 31 | 32 | normalise = overload_function(normalise, NormaliseBlock) 33 | -------------------------------------------------------------------------------- /docs/source/_static/overloading/tutorial9.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | from normalise import normalise 4 | 5 | mesh = UnitSquareMesh(10, 10) 6 | V = FunctionSpace(mesh, 'CG', 1) 7 | 8 | f = project(Expression('x[0]*x[1]', degree=1), V) 9 | 10 | g = normalise(f) 11 | 12 | J = assemble(g*dx) 13 | -------------------------------------------------------------------------------- /docs/source/_static/overloading/tutorial9_overloading.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | from normalise_overloaded import normalise 4 | 5 | from numpy.random import rand 6 | 7 | mesh = UnitSquareMesh(10, 10) 8 | V = FunctionSpace(mesh, 'CG', 1) 9 | 10 | f = project(Expression('x[0]*x[1]', degree=1), V) 11 | 12 | g = normalise(f) 13 | 14 | J = assemble(g*dx) 15 | 16 | h = Function(V) 17 | h.vector()[:] = rand(h.vector().local_size()) 18 | 19 | taylor_test(ReducedFunctional(J, Control(f)), f, h) 20 | -------------------------------------------------------------------------------- /docs/source/_static/poster_funke_2015.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/poster_funke_2015.pdf -------------------------------------------------------------------------------- /docs/source/_static/poster_small_funke_2015.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/poster_small_funke_2015.pdf -------------------------------------------------------------------------------- /docs/source/_static/simplified_burgers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/simplified_burgers.png -------------------------------------------------------------------------------- /docs/source/_static/simplified_burgers_named.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/simplified_burgers_named.png -------------------------------------------------------------------------------- /docs/source/_static/slider/klein.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/slider/klein.png -------------------------------------------------------------------------------- /docs/source/_static/slider/poisson-topology.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_static/slider/poisson-topology.png -------------------------------------------------------------------------------- /docs/source/_static/tutorial1.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | 3 | n = 30 4 | mesh = UnitSquareMesh(n, n) 5 | V = VectorFunctionSpace(mesh, "CG", 2) 6 | 7 | u = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V) 8 | 9 | u_next = Function(V) 10 | v = TestFunction(V) 11 | 12 | nu = Constant(0.0001) 13 | 14 | timestep = Constant(0.01) 15 | 16 | F = (inner((u_next - u)/timestep, v) 17 | + inner(grad(u_next)*u_next, v) 18 | + nu*inner(grad(u_next), grad(v)))*dx 19 | 20 | bc = DirichletBC(V, (0.0, 0.0), "on_boundary") 21 | 22 | t = 0.0 23 | end = 0.1 24 | while (t <= end): 25 | solve(F == 0, u_next, bc) 26 | u.assign(u_next) 27 | t += float(timestep) 28 | -------------------------------------------------------------------------------- /docs/source/_static/tutorial2.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | n = 30 5 | mesh = UnitSquareMesh(n, n) 6 | V = VectorFunctionSpace(mesh, "CG", 2) 7 | 8 | u = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V) 9 | control = Control(u) 10 | 11 | u_next = Function(V) 12 | v = TestFunction(V) 13 | 14 | nu = Constant(0.0001) 15 | 16 | timestep = Constant(0.01) 17 | 18 | F = (inner((u_next - u)/timestep, v) 19 | + inner(grad(u_next)*u_next, v) 20 | + nu*inner(grad(u_next), grad(v)))*dx 21 | 22 | bc = DirichletBC(V, (0.0, 0.0), "on_boundary") 23 | 24 | t = 0.0 25 | end = 0.1 26 | while (t <= end): 27 | solve(F == 0, u_next, bc) 28 | u.assign(u_next) 29 | t += float(timestep) 30 | 31 | J = assemble(inner(u, u)*dx) 32 | dJdu, dJdnu = compute_gradient(J, [control, Control(nu)]) 33 | -------------------------------------------------------------------------------- /docs/source/_static/tutorial3.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | n = 30 5 | mesh = UnitSquareMesh(n, n) 6 | V = VectorFunctionSpace(mesh, "CG", 2) 7 | 8 | u = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V) 9 | 10 | u_next = Function(V) 11 | v = TestFunction(V) 12 | 13 | nu = Constant(0.0001) 14 | 15 | timestep = Constant(0.01) 16 | 17 | F = (inner((u_next - u)/timestep, v) 18 | + inner(grad(u_next)*u_next, v) 19 | + nu*inner(grad(u_next), grad(v)))*dx 20 | 21 | bc = DirichletBC(V, (0.0, 0.0), "on_boundary") 22 | 23 | t = 0.0 24 | end = 0.1 25 | while (t <= end): 26 | solve(F == 0, u_next, bc) 27 | u.assign(u_next) 28 | t += float(timestep) 29 | 30 | J = assemble(inner(u, u)*dx) 31 | dJdnu = compute_gradient(J, Control(nu)) 32 | -------------------------------------------------------------------------------- /docs/source/_static/tutorial4.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | n = 30 5 | mesh = UnitSquareMesh(n, n) 6 | V = VectorFunctionSpace(mesh, "CG", 2) 7 | 8 | u = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V) 9 | 10 | u_next = Function(V) 11 | v = TestFunction(V) 12 | 13 | nu = Constant(0.0001) 14 | 15 | timestep = Constant(0.01) 16 | 17 | F = (inner((u_next - u)/timestep, v) 18 | + inner(grad(u_next)*u_next, v) 19 | + nu*inner(grad(u_next), grad(v)))*dx 20 | 21 | bc = DirichletBC(V, (0.0, 0.0), "on_boundary") 22 | 23 | t = 0.0 24 | end = 0.1 25 | while (t <= end): 26 | solve(F == 0, u_next, bc) 27 | u.assign(u_next) 28 | t += float(timestep) 29 | 30 | J = assemble(inner(u, u)*dx) 31 | dJdnu = compute_gradient(J, nu) 32 | 33 | h = Constant(0.0001) # the direction of the perturbation 34 | Jhat = ReducedFunctional(J, Control(nu)) # the functional as a pure function of nu 35 | conv_rate = taylor_test(Jhat, nu, h) 36 | -------------------------------------------------------------------------------- /docs/source/_static/tutorial7.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from fenics_adjoint import * 3 | 4 | n = 30 5 | mesh = UnitSquareMesh(n, n) 6 | V = VectorFunctionSpace(mesh, "CG", 2) 7 | 8 | u = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V) 9 | u_next = Function(V) 10 | v = TestFunction(V) 11 | 12 | nu = Constant(0.0001) 13 | 14 | timestep = Constant(0.01) 15 | 16 | F = (inner((u_next - u)/timestep, v) 17 | + inner(grad(u_next)*u_next, v) 18 | + nu*inner(grad(u_next), grad(v)))*dx 19 | 20 | bc = DirichletBC(V, (0.0, 0.0), "on_boundary") 21 | 22 | solve(F == 0, u_next, bc) 23 | 24 | tape = get_working_tape() 25 | tape.visualise() 26 | -------------------------------------------------------------------------------- /docs/source/_static/tutorial7_name_scope.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from fenics_adjoint import * 3 | 4 | n = 30 5 | mesh = UnitSquareMesh(n, n) 6 | V = VectorFunctionSpace(mesh, "CG", 2) 7 | 8 | u = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V) 9 | u_next = Function(V, name="u_next") 10 | v = TestFunction(V) 11 | 12 | nu = Constant(0.0001, name="nu") 13 | 14 | timestep = Constant(0.01, name="dt") 15 | 16 | F = (inner((u_next - u)/timestep, v) 17 | + inner(grad(u_next)*u_next, v) 18 | + nu*inner(grad(u_next), grad(v)))*dx 19 | 20 | bc = DirichletBC(V, (0.0, 0.0), "on_boundary") 21 | 22 | tape = get_working_tape() 23 | 24 | t = 0.0 25 | end = 0.05 26 | while (t <= end): 27 | with tape.name_scope("Timestep"): 28 | solve(F == 0, u_next, bc) 29 | u.assign(u_next) 30 | t += float(timestep) 31 | 32 | tape.visualise() 33 | -------------------------------------------------------------------------------- /docs/source/_static/tutorial7_named.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from fenics_adjoint import * 3 | 4 | n = 30 5 | mesh = UnitSquareMesh(n, n) 6 | V = VectorFunctionSpace(mesh, "CG", 2) 7 | 8 | u = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V) 9 | u_next = Function(V, name="u_next") 10 | v = TestFunction(V) 11 | 12 | nu = Constant(0.0001, name="nu") 13 | 14 | timestep = Constant(0.01, name="dt") 15 | 16 | F = (inner((u_next - u)/timestep, v) 17 | + inner(grad(u_next)*u_next, v) 18 | + nu*inner(grad(u_next), grad(v)))*dx 19 | 20 | bc = DirichletBC(V, (0.0, 0.0), "on_boundary") 21 | 22 | solve(F == 0, u_next, bc) 23 | 24 | tape = get_working_tape() 25 | tape.visualise() 26 | -------------------------------------------------------------------------------- /docs/source/_static/tutorial8.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | n = 30 5 | mesh = UnitSquareMesh(n, n) 6 | V = VectorFunctionSpace(mesh, "CG", 2) 7 | 8 | u = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V) 9 | 10 | u_next = Function(V) 11 | v = TestFunction(V) 12 | 13 | nu = Constant(0.0001) 14 | 15 | timestep = Constant(0.01) 16 | 17 | F = (inner((u_next - u)/timestep, v) 18 | + inner(grad(u_next)*u_next, v) 19 | + nu*inner(grad(u_next), grad(v)))*dx 20 | 21 | bc = DirichletBC(V, (0.0, 0.0), "on_boundary") 22 | 23 | 24 | t = 0.0 25 | end = 0.1 26 | Jtemp = assemble(inner(u, u)*dx) 27 | Jlist = [Jtemp] 28 | while (t <= end): 29 | solve(F == 0, u_next, bc) 30 | u.assign(u_next) 31 | t += float(timestep) 32 | 33 | Jtemp = assemble(inner(u, u)*dx) 34 | Jlist.append(Jtemp) 35 | 36 | 37 | J = 0 38 | for i in range(1, len(Jlist)): 39 | J += 0.5*(Jlist[i-1] + Jlist[i])*float(timestep) 40 | 41 | h = Constant(nu) 42 | taylor_test(ReducedFunctional(J, Control(nu)), nu, h) 43 | -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/README: -------------------------------------------------------------------------------- 1 | This is the Sphinx theme for the dolfin-adjoint web page. 2 | It was originally based on the FEniCS project theme. 3 | -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/banner.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/dialog-note.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/dialog-note.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/dialog-seealso.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/dialog-seealso.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/dialog-topic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/dialog-topic.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/dialog-warning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/dialog-warning.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/feature-item-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/feature-item-1.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/featured.css: -------------------------------------------------------------------------------- 1 | #products_example { 2 | width:400px; 3 | height:282px; 4 | position:relative; 5 | float:left; 6 | } 7 | 8 | /* 9 | Slideshow 10 | */ 11 | 12 | #products { 13 | } 14 | 15 | /* 16 | Slides container 17 | Important: 18 | Set the width of your slides container 19 | Set to display none, prevents content flash 20 | */ 21 | 22 | #products .slides_container { 23 | width:366px; 24 | overflow:hidden; 25 | float:left; 26 | position:relative; 27 | display:none; 28 | } 29 | 30 | /* 31 | Each slide 32 | Important: 33 | Set the width of your slides 34 | If height not specified height will be set by the slide content 35 | Set to display block 36 | */ 37 | 38 | .slides_container a { 39 | width:366px; 40 | height:274px; 41 | display:block; 42 | } 43 | 44 | /* 45 | Caption 46 | */ 47 | 48 | .caption { 49 | letter-spacing:0; 50 | position:relative; 51 | text-shadow:0 1px 0 rgba(255,255,255,.8); 52 | -webkit-font-smoothing: subpixel-antialiased; 53 | z-index:500; 54 | position:absolute; 55 | bottom:-35px; 56 | left:0; 57 | height:30px; 58 | padding-left: 10px; 59 | padding-bottom: 20px; 60 | background:#000; 61 | background:rgba(0,0,0,.5); 62 | width:400px; 63 | font-size:1.0em; 64 | line-height:1.33; 65 | color:#fff; 66 | border-top:1px solid #000; 67 | text-shadow:none; 68 | } 69 | 70 | .slides_container div.slide { 71 | width:400px; 72 | height:282px; 73 | display:block; 74 | } -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/feed-icon-14x14.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/feed-icon-14x14.gif -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/fenics-book-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/fenics-book-icon.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/fenics-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/fenics-web.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/flexslider/changelog.txt: -------------------------------------------------------------------------------- 1 | FLEXSLIDER CHANGELOG 2 | 3 | 2013.02.15 - Version 2.0 4 | * Added changelog.txt -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/flexslider/fonts/flexslider-icon.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/flexslider/fonts/flexslider-icon.eot -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/flexslider/fonts/flexslider-icon.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/flexslider/fonts/flexslider-icon.ttf -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/flexslider/fonts/flexslider-icon.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/flexslider/fonts/flexslider-icon.woff -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/flexslider/images/bg_play_pause.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/flexslider/images/bg_play_pause.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/footerbg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/footerbg.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/headerbg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/headerbg.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/highlight/styles/ascetic.css: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Original style from softwaremaniacs.org (c) Ivan Sagalaev 4 | 5 | */ 6 | 7 | .hljs { 8 | display: block; padding: 0.5em; 9 | background: white; color: black; 10 | } 11 | 12 | .hljs-string, 13 | .hljs-tag .hljs-value, 14 | .hljs-filter .hljs-argument, 15 | .hljs-addition, 16 | .hljs-change, 17 | .apache .hljs-tag, 18 | .apache .hljs-cbracket, 19 | .nginx .hljs-built_in, 20 | .tex .hljs-formula { 21 | color: #888; 22 | } 23 | 24 | .hljs-comment, 25 | .hljs-template_comment, 26 | .hljs-shebang, 27 | .hljs-doctype, 28 | .hljs-pi, 29 | .hljs-javadoc, 30 | .hljs-deletion, 31 | .apache .hljs-sqbracket { 32 | color: #CCC; 33 | } 34 | 35 | .hljs-keyword, 36 | .hljs-tag .hljs-title, 37 | .ini .hljs-title, 38 | .lisp .hljs-title, 39 | .clojure .hljs-title, 40 | .http .hljs-title, 41 | .nginx .hljs-title, 42 | .css .hljs-tag, 43 | .hljs-winutils, 44 | .hljs-flow, 45 | .apache .hljs-tag, 46 | .tex .hljs-command, 47 | .hljs-request, 48 | .hljs-status { 49 | font-weight: bold; 50 | } 51 | -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/highlight/styles/brown_papersq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/highlight/styles/brown_papersq.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/highlight/styles/mono-blue.css: -------------------------------------------------------------------------------- 1 | /* 2 | Five-color theme from a single blue hue. 3 | */ 4 | .hljs { 5 | display: block; padding: 0.5em; 6 | background: #EAEEF3; color: #00193A; 7 | } 8 | 9 | .hljs-keyword, 10 | .hljs-title, 11 | .hljs-important, 12 | .hljs-request, 13 | .hljs-header, 14 | .hljs-javadoctag { 15 | font-weight: bold; 16 | } 17 | 18 | .hljs-comment, 19 | .hljs-chunk, 20 | .hljs-template_comment { 21 | color: #738191; 22 | } 23 | 24 | .hljs-string, 25 | .hljs-title, 26 | .hljs-parent, 27 | .hljs-built_in, 28 | .hljs-literal, 29 | .hljs-filename, 30 | .hljs-value, 31 | .hljs-addition, 32 | .hljs-tag, 33 | .hljs-argument, 34 | .hljs-link_label, 35 | .hljs-blockquote, 36 | .hljs-header { 37 | color: #0048AB; 38 | } 39 | 40 | .hljs-decorator, 41 | .hljs-prompt, 42 | .hljs-yardoctag, 43 | .hljs-subst, 44 | .hljs-symbol, 45 | .hljs-doctype, 46 | .hljs-regexp, 47 | .hljs-preprocessor, 48 | .hljs-pragma, 49 | .hljs-pi, 50 | .hljs-attribute, 51 | .hljs-attr_selector, 52 | .hljs-javadoc, 53 | .hljs-xmlDocTag, 54 | .hljs-deletion, 55 | .hljs-shebang, 56 | .hljs-string .hljs-variable, 57 | .hljs-link_url, 58 | .hljs-bullet, 59 | .hljs-sqbracket, 60 | .hljs-phony { 61 | color: #4C81C9; 62 | } 63 | -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/highlight/styles/pojoaque.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/highlight/styles/pojoaque.jpg -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/highlight/styles/school_book.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/highlight/styles/school_book.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/icon.ico -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/icon.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/ie6.css: -------------------------------------------------------------------------------- 1 | * html img, 2 | * html .png{position:relative;behavior:expression((this.runtimeStyle.behavior="none")&&(this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none", 3 | this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "',sizingMethod='image')", 4 | this.src = "_static/transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''), 5 | this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "',sizingMethod='crop')", 6 | this.runtimeStyle.backgroundImage = "none")),this.pngSet=true) 7 | );} 8 | -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/middlebg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/middlebg.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/neuton-fontfacekit/Neuton-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/neuton-fontfacekit/Neuton-webfont.eot -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/neuton-fontfacekit/Neuton-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/neuton-fontfacekit/Neuton-webfont.ttf -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/neuton-fontfacekit/Neuton-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/neuton-fontfacekit/Neuton-webfont.woff -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/neuton-fontfacekit/demo.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | Font Face Demo 9 | 10 | 21 | 22 | 23 | 24 |
25 |

Font-face Demo for the Neuton Font

26 | 27 | 28 | 29 |

Neuton Regular - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

30 | 31 |
32 | 33 | 34 | -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/neuton-fontfacekit/stylesheet.css: -------------------------------------------------------------------------------- 1 | /* Generated by Font Squirrel (http://www.fontsquirrel.com) on June 12, 2011 05:33:46 AM America/New_York */ 2 | 3 | 4 | 5 | @font-face { 6 | font-family: 'NeutonRegular'; 7 | src: url('Neuton-webfont.eot'); 8 | src: url('Neuton-webfont.eot?#iefix') format('embedded-opentype'), 9 | url('Neuton-webfont.woff') format('woff'), 10 | url('Neuton-webfont.ttf') format('truetype'), 11 | url('Neuton-webfont.svg#NeutonRegular') format('svg'); 12 | font-weight: normal; 13 | font-style: normal; 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile-webfont.eot -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile-webfont.ttf -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile-webfont.woff -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold-webfont.eot -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold-webfont.ttf -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold-webfont.woff -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold_italic-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold_italic-webfont.eot -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold_italic-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold_italic-webfont.ttf -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold_italic-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_bold_italic-webfont.woff -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_italic-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_italic-webfont.eot -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_italic-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_italic-webfont.ttf -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_italic-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/nobile-fontfacekit/nobile_italic-webfont.woff -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/sample-news-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/sample-news-image.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/transparent.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/transparent.gif -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/static/unknown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/docs/source/_themes/dolfin-adjoint/static/unknown.png -------------------------------------------------------------------------------- /docs/source/_themes/dolfin-adjoint/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = basic 3 | stylesheet = fenics.css 4 | pygments_style = fenics_theme_support.FenicsStyle 5 | 6 | [options] 7 | highlight_language = guess 8 | short_title = Home 9 | logo = icon.png 10 | favicon = icon.ico 11 | -------------------------------------------------------------------------------- /docs/source/citing/index.rst: -------------------------------------------------------------------------------- 1 | :orphan: 2 | 3 | .. _citing: 4 | 5 | ############## 6 | How to cite us 7 | ############## 8 | 9 | If you use pyadjoint in your research, the developers would be 10 | grateful if you would cite the relevant publications. 11 | 12 | For pyadjoint generally, please cite: 13 | 14 | * **Sebastian K. Mitusch, Simon W. Funke, and Jørgen S. Dokken (2019)**. 15 | *dolfin-adjoint 2018.1: automated adjoints for FEniCSand Firedrake*, 16 | Journal of Open Source Software, 4(38), 1292, `doi:10.21105/joss.01292 `__. 17 | 18 | -------------------------------------------------------------------------------- /docs/source/documentation/faq.rst: -------------------------------------------------------------------------------- 1 | .. _dolfin-adjoint-faq: 2 | 3 | .. py:currentmodule:: dolfin_adjoint 4 | 5 | ========================== 6 | Frequently asked questions 7 | ========================== 8 | 9 | 1. *Q*: I have a time-dependent control in my PDE-constrained 10 | optimization problem: how do I define this? 11 | 12 | *A*: Take a look the `time-dependent wave example `_ for how to 13 | specify time-dependent controls and functionals. 14 | -------------------------------------------------------------------------------- /docs/source/documentation/manual.rst: -------------------------------------------------------------------------------- 1 | :orphan: 2 | 3 | .. _dolfin-adjoint-manual: 4 | 5 | ************************* 6 | How to use dolfin-adjoint 7 | ************************* 8 | 9 | .. sectionauthor:: Patrick E. Farrell , 10 | Simon W. Funke 11 | 12 | .. toctree:: 13 | 14 | tutorial 15 | verification 16 | debugging 17 | parallel 18 | functionals 19 | optimisation 20 | custom_functions 21 | -------------------------------------------------------------------------------- /docs/source/documentation/maths/2-problem.bib: -------------------------------------------------------------------------------- 1 | @mastersthesis{karush1939, 2 | address = {Chicago, IL, USA}, 3 | school = {University of Chicago}, 4 | author = {W. Karush}, 5 | title = {Minima of functions of several variables with inequalities as side constraints}, 6 | year = {1939}, 7 | } 8 | 9 | @inproceedings{kuhn1951, 10 | booktitle = {Proceedings of 2nd Berkeley Symposium}, 11 | publisher = {University of California Press}, 12 | author = {Kuhn, H. W. and Tucker, A. W.}, 13 | title = {Nonlinear programming}, 14 | pages = {481--492}, 15 | year = {1951}, 16 | } 17 | 18 | -------------------------------------------------------------------------------- /docs/source/documentation/maths/3-gradients.bib: -------------------------------------------------------------------------------- 1 | @article{byrd1995, 2 | author = {R. H. Byrd and P. Lu and J. Nocedal and C. Zhu}, 3 | title = {A limited memory algorithm for bound constrained optimization}, 4 | journal = {SIAM Journal on Scientific and Statistical Computing}, 5 | volume = {16}, 6 | number = {5}, 7 | pages = {1190-1208}, 8 | year = {1995}, 9 | } 10 | 11 | @article{zhu1997b, 12 | author = {Zhu, C. and Byrd, R. H. and Lu, P. and Nocedal, J.}, 13 | title = {Algorithm 778: {L-BFGS-B:} {F}ortran subroutines for large-scale bound-constrained optimization}, 14 | journal = {ACM Transactions on Mathematical Software}, 15 | volume = {23}, 16 | number = {4}, 17 | pages = {550--560}, 18 | year = {1997}, 19 | doi = {10.1145/279232.279236}, 20 | } 21 | 22 | -------------------------------------------------------------------------------- /docs/source/documentation/maths/index.rst: -------------------------------------------------------------------------------- 1 | :orphan: 2 | 3 | .. _dolfin-adjoint-mathematical-background: 4 | 5 | ******************************************************** 6 | Mathematical background: adjoints and their applications 7 | ******************************************************** 8 | 9 | .. sectionauthor:: Patrick E. Farrell 10 | 11 | Contacting the author 12 | ===================== 13 | 14 | If you find this discussion useful, or have any comments on it, please 15 | `send me an email `_! 16 | 17 | Sections 18 | ======== 19 | 20 | .. toctree:: 21 | 22 | 1-foreword 23 | 2-problem 24 | 3-gradients 25 | 4-adjoint 26 | 5-applications 27 | 28 | Appendices 29 | ========== 30 | 31 | .. toctree:: 32 | 33 | A-gst 34 | -------------------------------------------------------------------------------- /docs/source/documentation/misc.rst: -------------------------------------------------------------------------------- 1 | :orphan: 2 | 3 | .. py:currentmodule:: dolfin_adjoint 4 | 5 | =================== 6 | Miscellaneous notes 7 | =================== 8 | 9 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | .. include:: about/index.rst 2 | -------------------------------------------------------------------------------- /docs/source/support/index.rst: -------------------------------------------------------------------------------- 1 | :orphan: 2 | 3 | .. _support: 4 | 5 | ####### 6 | Support 7 | ####### 8 | 9 | .. _help_bugs: 10 | 11 | Having issues? 12 | ============== 13 | 14 | Do you wonder about how something works, whether some feature is 15 | supported or why you are getting an error? Feel free to ask on the 16 | `github issuetracker 17 | `_. 18 | 19 | .. _getting_answers_checklist: 20 | 21 | Checklist when asking a question 22 | ================================ 23 | 24 | We want to help you; by making sure that you provide us with enough 25 | information, you are more likely to get the answer you are looking 26 | for. If you have encountered an error or a problem that you can't figure 27 | out, please make sure to include the following in your description of 28 | the problem: 29 | 30 | #. A *minimal, running code example* that reproduces the error. 31 | 32 | #. The error message. 33 | 34 | Contacting the authors 35 | ====================== 36 | 37 | Use our `Slack channel `_ to get in contact with us directly (or send us an email)! 38 | 39 | 40 | 41 | 42 | 43 | .. toctree:: 44 | :hidden: 45 | -------------------------------------------------------------------------------- /examples/burgers/burgers.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | mesh = UnitIntervalMesh(30) 5 | V = FunctionSpace(mesh, "CG", 2) 6 | 7 | 8 | def Dt(u, u_, timestep): 9 | return (u - u_) / timestep 10 | 11 | 12 | pr = project(Expression("sin(2*pi*x[0])", degree=1), V) 13 | u_ = Function(V) 14 | u_.vector()[:] = pr.vector()[:] 15 | control = Control(u_) 16 | u = Function(V) 17 | v = TestFunction(V) 18 | nu = Constant(0.0001) 19 | a = Constant(0.4) 20 | timestep = AdjFloat(0.05) 21 | bc = DirichletBC(V, 0.0, "on_boundary") 22 | t = 0.0 23 | 24 | F = (Dt(u, u_, timestep) * v 25 | + a * u * u.dx(0) * v + nu * u.dx(0) * v.dx(0)) * dx 26 | 27 | end = 0.3 28 | J = 0 29 | while (t <= end): 30 | solve(F == 0, u, bc) 31 | u_.assign(u) 32 | t += float(timestep) 33 | J += timestep * assemble(u_ * u_ * dx) 34 | 35 | h = Function(V) 36 | h.vector()[:] = 1 37 | Jhat = ReducedFunctional(J, control) 38 | print(taylor_test(Jhat, pr.copy(deepcopy=True), h)) 39 | -------------------------------------------------------------------------------- /examples/klein/klein.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/examples/klein/klein.h5 -------------------------------------------------------------------------------- /examples/klein/klein.xdmf: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | klein.h5:/Mesh/mesh/topology 7 | 8 | 9 | klein.h5:/Mesh/mesh/geometry 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /examples/reaction-rate-learning/concentrations.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/examples/reaction-rate-learning/concentrations.png -------------------------------------------------------------------------------- /examples/reaction-rate-learning/learned_reaction_rates.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/examples/reaction-rate-learning/learned_reaction_rates.png -------------------------------------------------------------------------------- /examples/reaction-rate-learning/navier_stokes_cylinder/cylinder.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/examples/reaction-rate-learning/navier_stokes_cylinder/cylinder.xml.gz -------------------------------------------------------------------------------- /examples/reaction-rate-learning/navier_stokes_cylinder/velocity_series.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/examples/reaction-rate-learning/navier_stokes_cylinder/velocity_series.h5 -------------------------------------------------------------------------------- /examples/shape-optimization-pipe/mesh/pipe.geo: -------------------------------------------------------------------------------- 1 | Point(1) = {0,0, 0, 1.}; 2 | Point(2) = {1.5,0, 0, 1.}; 3 | Point(3) = {3,0, 0, 1.}; 4 | Point(4) = {4,0, 0, 1.}; 5 | Point(5) = {4.75,0.5, 0, 1.}; 6 | Point(6) = {8.50,5, 0, 1.}; 7 | Point(7) = {7.5,5, 0, 1.}; 8 | Point(8) = {12,5, 0, 1.}; 9 | Point(9) = {15,5, 0, 1.}; 10 | Point(10) = {15,6, 0, 1.}; 11 | Point(11) = {12,6, 0, 1.}; 12 | Point(12) = {8.5,6, 0, 1.}; 13 | Point(13) = {7.5,6, 0, 1.}; 14 | Point(14) = {6.75,5.5, 0, 1.}; 15 | Point(15) = {4,1, 0, 1.}; 16 | Point(16) = {3,1, 0, 1.}; 17 | Point(17)= {1.5,1, 0, 1.}; 18 | Point(18)= {0,1, 0, 1.}; 19 | Line(1) = {1, 2}; 20 | Line(2) = {2, 3}; 21 | BSpline(3) = {3, 4, 5, 7, 6}; 22 | Line(4) = {6, 8}; 23 | Line(5) = {8, 9}; 24 | Line(6) = {9, 10}; 25 | Line(7) = {10, 11}; 26 | Line(8) = {11, 12}; 27 | BSpline(9) = {12, 13, 14, 15, 16}; 28 | Line(10) = {16, 17}; 29 | Line(11) = {17, 18}; 30 | Line(12) = {18, 1}; 31 | Line Loop(1) = {11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 32 | Physical Line("Inflow") = {12}; 33 | Physical Line("Outflow") = {6}; 34 | Physical Line("WallFixed") = {1, 5, 7, 11}; 35 | Physical Line("WallFree") = {2, 3, 4, 8, 9, 10}; 36 | Plane Surface(1) = {1}; 37 | Physical Surface("Pipe") = {1}; -------------------------------------------------------------------------------- /examples/stokes-bc-control/make-mesh.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from mshr import * 3 | 4 | rect = Rectangle(Point(0, 0), Point(30, 10)) 5 | circ = Circle(Point(10, 5), 2.5) 6 | domain = rect - circ 7 | N = 50 8 | 9 | mesh = generate_mesh(domain, N) 10 | 11 | filename_xdmf = XDMFFile(MPI.comm_world, "rectangle-less-circle.xdmf") 12 | filename_xdmf.write(mesh) 13 | -------------------------------------------------------------------------------- /examples/stokes-bc-control/rectangle-less-circle.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/examples/stokes-bc-control/rectangle-less-circle.h5 -------------------------------------------------------------------------------- /examples/stokes-bc-control/rectangle-less-circle.xdmf: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | rectangle-less-circle.h5:/Mesh/mesh/topology 8 | 9 | 10 | rectangle-less-circle.h5:/Mesh/mesh/geometry 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /examples/stokes-shape-opt/mesh.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/examples/stokes-shape-opt/mesh.h5 -------------------------------------------------------------------------------- /examples/stokes-shape-opt/mesh.xdmf: -------------------------------------------------------------------------------- 1 | mesh.h5:/data0mesh.h5:/data1mesh.h5:/data2 -------------------------------------------------------------------------------- /examples/stokes-shape-opt/mf.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/examples/stokes-shape-opt/mf.h5 -------------------------------------------------------------------------------- /examples/stokes-shape-opt/mf.xdmf: -------------------------------------------------------------------------------- 1 | mf.h5:/data0mf.h5:/data1mf.h5:/data2 -------------------------------------------------------------------------------- /examples/tube-shape-derivative/mesh/cable.geo: -------------------------------------------------------------------------------- 1 | r1 = 1.0; 2 | r2 = 0.1; 3 | cl1 = 0.5; 4 | cl2 = 1.0; 5 | 6 | x0_off = -0.3; 7 | x1_off = 0.3; 8 | 9 | Point(0) = {0, 0, 0, cl1}; 10 | Point(1) = {-r1, 0, 0, cl1}; 11 | Point(2) = {r1, 0, 0, cl1}; 12 | Point(3) = {0, r1, 0, cl1}; 13 | Point(4) = {0, -r1, 0, cl1}; 14 | 15 | Point(5) = {x0_off, x1_off, 0, cl2}; 16 | Point(6) = {-r2+x0_off, x1_off, 0, cl2}; 17 | Point(7) = {r2+x0_off, x1_off, 0, cl2}; 18 | Point(8) = {x0_off, r2+x1_off, 0, cl2}; 19 | Point(9) = {x0_off, -r2+x1_off, 0, cl2}; 20 | 21 | Circle(1) = {2, 0, 3}; 22 | Circle(2) = {3, 0, 1}; 23 | Circle(3) = {1, 0, 4}; 24 | Circle(4) = {4, 0, 2}; 25 | Circle(5) = {8, 5, 6}; 26 | Circle(6) = {6, 5, 9}; 27 | Circle(7) = {9, 5, 7}; 28 | Circle(8) = {7, 5, 8}; 29 | Line Loop(9) = {2, 3, 4, 1}; 30 | Line Loop(10) = {8, 5, 6, 7}; 31 | Plane Surface(11) = {9, 10}; 32 | Physical Line(1) = {3, 4, 1, 2}; 33 | Physical Line(2) = {8, 5, 6, 7}; 34 | Physical Surface(0) = {11}; 35 | -------------------------------------------------------------------------------- /firedrake_adjoint/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | 3 | import warnings 4 | 5 | from firedrake.adjoint import * 6 | 7 | continue_annotation() 8 | 9 | warnings.warn("""The firedrake_adjoint module is deprecated. 10 | 11 | Instead, use the firedrake.adjoint module and explicitly start taping 12 | by calling continue_annotation().""", FutureWarning) 13 | -------------------------------------------------------------------------------- /numpy_adjoint/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | 3 | import pyadjoint 4 | __version__ = pyadjoint.__version__ 5 | __author__ = 'Sebastian Kenji Mitusch' 6 | __credits__ = [] 7 | __license__ = 'LGPL-3' 8 | __maintainer__ = 'Sebastian Kenji Mitusch' 9 | __email__ = 'sebastkm@simula.no' 10 | 11 | 12 | from .array import ndarray 13 | 14 | # Use pyadjoint AdjFloat for numpy.float64. 15 | import numpy 16 | from pyadjoint.overloaded_type import register_overloaded_type 17 | from pyadjoint.adjfloat import AdjFloat 18 | register_overloaded_type(AdjFloat, numpy.float64) 19 | -------------------------------------------------------------------------------- /pyadjoint/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | 3 | __version__ = '2019.1.2' 4 | __author__ = 'Sebastian Kenji Mitusch' 5 | __credits__ = [] 6 | __license__ = 'LGPL-3' 7 | __maintainer__ = 'Sebastian Kenji Mitusch' 8 | __email__ = 'sebastkm@math.uio.no' 9 | 10 | from .block import Block 11 | from .tape import (Tape, 12 | set_working_tape, get_working_tape, no_annotations, 13 | annotate_tape, stop_annotating, pause_annotation, continue_annotation) 14 | from .adjfloat import AdjFloat, exp, log 15 | from .reduced_functional import ReducedFunctional 16 | from .checkpointing import disk_checkpointing_callback 17 | from .drivers import compute_gradient, compute_hessian, solve_adjoint 18 | from .verification import taylor_test, taylor_to_dict 19 | from .overloaded_type import OverloadedType, create_overloaded_object 20 | from .control import Control 21 | from .optimization.optimization import minimize, maximize, print_optimization_methods 22 | from .optimization.optimization_problem import MinimizationProblem 23 | from .optimization.ipopt_solver import IPOPTSolver 24 | from .optimization.rol_solver import ROLSolver 25 | from .optimization.tao_solver import TAOSolver 26 | from .optimization.constraints import InequalityConstraint, EqualityConstraint 27 | from .optimization.moola_problem import MoolaOptimizationProblem 28 | -------------------------------------------------------------------------------- /pyadjoint/enlisting.py: -------------------------------------------------------------------------------- 1 | class Enlist(list): 2 | def __init__(self, x): 3 | self.listed = isinstance(x, (list, tuple)) 4 | super(Enlist, self).__init__(x if self.listed else [x]) 5 | 6 | def delist(self, y=None): 7 | y = self if y is None else y 8 | if self.listed: 9 | return y 10 | else: 11 | assert len(y) == 1 12 | return y[0] 13 | -------------------------------------------------------------------------------- /pyadjoint/ipopt.py: -------------------------------------------------------------------------------- 1 | """ 2 | Compatibility layer for ipopt wrappers 3 | Currently only cyipopt is supported 4 | 5 | Repos: 6 | 7 | - https://github.com/matthias-k/cyipopt 8 | """ 9 | 10 | try: 11 | import cyipopt # noqa: F401 12 | except ImportError: 13 | raise ImportError("You need to install cyipopt. It is recommended to install IPOPT with HSL support!") 14 | -------------------------------------------------------------------------------- /pyadjoint/optimization/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | The optimization module is automatically imported by dolfin-adjoint 3 | """ 4 | 5 | __author__ = 'Simon Funke and Patrick Farrell' 6 | __credits__ = ['Patrick Farrell', 'Simon Funke', 'David Ham', 'Marie Rognes'] 7 | __license__ = 'LGPL-3' 8 | __maintainer__ = 'Simon Funke' 9 | __email__ = 'simon@simula.no' 10 | -------------------------------------------------------------------------------- /pyadjoint/optimization/optimization_solver.py: -------------------------------------------------------------------------------- 1 | from . import optimization_problem 2 | 3 | 4 | class OptimizationSolver(object): 5 | """An abstract base class that represents an optimization solver.""" 6 | def __init__(self, problem, parameters=None): 7 | self.__check_arguments(problem, parameters) 8 | 9 | #: problem: an OptimizationProblem instance. 10 | self.problem = problem 11 | 12 | #: parameters: a dictionary of parameters. 13 | self.parameters = parameters 14 | 15 | def __check_arguments(self, problem, parameters): 16 | if not isinstance(problem, optimization_problem.OptimizationProblem): 17 | raise TypeError("problem should be an OptimizationProblem.") 18 | 19 | def solve(self): 20 | raise NotImplementedError("This class is abstract.") 21 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools"] 3 | build-backend = "setuptools.build_meta" 4 | 5 | [project] 6 | name = "pyadjoint-ad" 7 | version = "2025.04.0" 8 | dependencies = [ 9 | "checkpoint_schedules", 10 | "scipy>=1.0", 11 | ] 12 | authors = [ 13 | {name = "Jørgen S. Dokken", email = "dokken@simula.no"}, 14 | ] 15 | description = "High-level automatic differentiation library" 16 | readme = "README.md" 17 | license = {file = "LICENSE"} 18 | classifiers = [ 19 | "Programming Language :: Python", 20 | ] 21 | 22 | [project.urls] 23 | Repository = "https://github.com/dolfin-adjoint/pyadjoint.git" 24 | 25 | [project.optional-dependencies] 26 | all = [ 27 | "coverage", 28 | "flake8", 29 | "meshio", 30 | "moola>=0.1.6", 31 | "networkx", 32 | "protobuf", 33 | "pygmsh", 34 | "pygraphviz", 35 | "pytest>=3.10", 36 | "sphinx", 37 | "sphinx-autobuild", 38 | "sphinxcontrib-bibtex", 39 | "tensorflow", 40 | ] 41 | doc = ["sphinx", "sphinx-autobuild", "sphinxcontrib-bibtex"] 42 | meshing = ["pygmsh", "meshio"] 43 | moola = ["moola>=0.1.6"] 44 | test = ["pytest>=3.10", "flake8", "coverage"] 45 | visualisation = ["tensorflow", "protobuf", "networkx", "pygraphviz"] 46 | 47 | 48 | [tool.setuptools] 49 | packages = ["firedrake_adjoint", "numpy_adjoint", "pyadjoint", "pyadjoint.optimization"] 50 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | exclude = .git,__pycache__,docs/,examples/,tests/,scripts/ 3 | max-line-length = 119 4 | ignore=E731,W503,F405 5 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/__init__.py -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import importlib 3 | import numpy.random 4 | from pyadjoint import set_working_tape, Tape, continue_annotation, \ 5 | annotate_tape 6 | 7 | 8 | @pytest.fixture(autouse=True) 9 | def skip_by_missing_module(request): 10 | marker = request.node.get_closest_marker("skipif_module_is_missing") 11 | if marker: 12 | to_import = marker.args[0] 13 | try: 14 | importlib.import_module(to_import) 15 | except ImportError: 16 | pytest.skip('skipped because module {} is missing'.format(to_import)) 17 | 18 | 19 | def pytest_runtest_setup(item): 20 | """ Hook function which is called before every test """ 21 | set_working_tape(Tape()) 22 | 23 | # Fix the seed to avoid random test failures due to slight tolerance variations 24 | numpy.random.seed(21) 25 | 26 | # Ensure taping is on. 27 | if not annotate_tape(): 28 | continue_annotation() -------------------------------------------------------------------------------- /tests/migration/assembled_action/assembled_action.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | 6 | mesh = UnitIntervalMesh(10) 7 | V = FunctionSpace(mesh, "CG", 1) 8 | 9 | def main(data): 10 | u = TrialFunction(V) 11 | v = TestFunction(V) 12 | mass = inner(u, v)*dx 13 | M = assemble(mass) 14 | 15 | rhs = M*data.vector() 16 | soln = Function(V) 17 | 18 | solve(M, soln.vector(), rhs) 19 | return soln 20 | 21 | if __name__ == "__main__": 22 | data = Function(V, name="Data") 23 | data.vector()[0] = 1.0 24 | 25 | soln = main(data) 26 | 27 | J = assemble(inner(soln, soln)*dx) 28 | c = Control(data) 29 | dJdic = compute_gradient(J, c) 30 | 31 | def Jhat(data): 32 | soln = main(data) 33 | return assemble(soln*soln*dx) 34 | 35 | h = Function(V) 36 | h.vector()[:] = rand(V.dim()) 37 | dJdic = h._ad_dot(dJdic) 38 | 39 | minconv = taylor_test(Jhat, data, h, dJdic) 40 | assert minconv > 1.9 41 | -------------------------------------------------------------------------------- /tests/migration/assembled_action/test_assembled_action.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/burgers_newton/burgers_newton.py: -------------------------------------------------------------------------------- 1 | """ 2 | Implementation of Burger's equation with nonlinear solve in each 3 | timestep 4 | """ 5 | 6 | from fenics import * 7 | from fenics_adjoint import * 8 | 9 | from numpy.random import rand, seed 10 | seed(21) 11 | 12 | n = 30 13 | mesh = UnitIntervalMesh(n) 14 | V = FunctionSpace(mesh, "CG", 1) 15 | 16 | def Dt(u, u_, timestep): 17 | return (u - u_)/timestep 18 | 19 | def main(ic, annotate=False): 20 | 21 | u_ = ic.copy(deepcopy=True) 22 | u = Function(V) 23 | v = TestFunction(V) 24 | 25 | nu = Constant(0.0001) 26 | 27 | timestep = Constant(1.0/n) 28 | 29 | F = (Dt(u, u_, timestep)*v 30 | + u*u.dx(0)*v + nu*u.dx(0)*v.dx(0))*dx 31 | bc = DirichletBC(V, 0.0, "on_boundary") 32 | 33 | t = 0.0 34 | end = 0.2 35 | while (t <= end): 36 | solve(F == 0, u, bc, annotate=annotate) 37 | u_.assign(u, annotate=annotate) 38 | 39 | t += float(timestep) 40 | 41 | return u_ 42 | 43 | if __name__ == "__main__": 44 | 45 | x = SpatialCoordinate(mesh) 46 | ic = project(sin(2*pi*x[0]), V) 47 | forward = main(ic, annotate=True) 48 | 49 | J = assemble(forward*forward*dx + ic*ic*dx) 50 | 51 | Jhat = ReducedFunctional(J, Control(ic)) 52 | 53 | h = Function(V) 54 | h.vector()[:] = 0.1*rand(V.dim()) 55 | 56 | minconv = taylor_to_dict(Jhat, ic, h) 57 | assert(min(minconv["R0"]["Rate"]) > 0.9) 58 | assert(min(minconv["R1"]["Rate"]) > 1.9) 59 | assert(min(minconv["R2"]["Rate"]) > 2.9) 60 | -------------------------------------------------------------------------------- /tests/migration/burgers_newton/test_burgers_newton.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/burgers_newton_time_functional/test_burgers_newton_time_functional.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/burgers_oo/test_burgers_oo.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import subprocess 3 | import sys 4 | from os import path 5 | 6 | fenics = pytest.importorskip("fenics") 7 | 8 | 9 | @pytest.mark.skipif(not hasattr(fenics, "HDF5File"), reason="requires hdf5 support") 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/burgers_picard/test_burgers_picard.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | fenics = pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.skipif(not hasattr(fenics, "HDF5File"), 11 | reason="requires hdf5 support") 12 | def test(request): 13 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 14 | test_dir = path.split(str(request.fspath))[0] 15 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 16 | 17 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 18 | assert handle.wait() == 0 19 | -------------------------------------------------------------------------------- /tests/migration/cahn_hilliard/test_cahn_hilliard.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.slow 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/cahn_hilliard/timings/run_timings.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export PYTHONPATH=../../../.. 4 | 5 | for i in $(seq 1 5) 6 | do 7 | /usr/bin/time python $1 2>&1 8 | done 9 | -------------------------------------------------------------------------------- /tests/migration/changing_vector/changing_vector.py: -------------------------------------------------------------------------------- 1 | """ 2 | Implementation of Burger's equation with nonlinear solve in each 3 | timestep 4 | """ 5 | 6 | 7 | import sys 8 | 9 | from dolfin import * 10 | from dolfin_adjoint import * 11 | 12 | from numpy import array 13 | 14 | dolfin.parameters["adjoint"]["record_all"] = True 15 | dolfin.parameters["adjoint"]["fussy_replay"] = False 16 | 17 | n = 30 18 | mesh = UnitIntervalMesh(n) 19 | V = FunctionSpace(mesh, "CG", 2) 20 | 21 | def Dt(u, u_, timestep): 22 | return (u - u_)/timestep 23 | 24 | def main(ic, annotate=False): 25 | 26 | u_ = ic.copy(deepcopy=True, name="Velocity") 27 | u = Function(V, name="VelocityNext") 28 | v = TestFunction(V) 29 | 30 | nu = Function(V) 31 | nu.vector()[:] = 0.0001 32 | 33 | timestep = Constant(1.0/n) 34 | 35 | F = (Dt(u, u_, timestep)*v 36 | + u*u.dx(0)*v + nu*u.dx(0)*v.dx(0))*dx 37 | bc = DirichletBC(V, 0.0, "on_boundary") 38 | 39 | t = 0.0 40 | end = 0.2 41 | while (t <= end): 42 | nu.vector()[:] += array([0.0001] * V.dim()) # <--------- change nu here by hand 43 | solve(F == 0, u, bc, annotate=annotate) 44 | u_.assign(u, annotate=annotate) 45 | 46 | t += float(timestep) 47 | 48 | return u_ 49 | 50 | if __name__ == "__main__": 51 | ic = project(Expression("sin(2*pi*x[0])", degree=1), V) 52 | main(ic, annotate=True) 53 | 54 | success = replay_dolfin(forget=False) # <------ should catch it here 55 | assert not success 56 | -------------------------------------------------------------------------------- /tests/migration/changing_vector/test_changing_vector.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.xfail(reason='How pyadjoint should behave in this problem has currently not been decided.') 7 | @pytest.mark.xfail(reason="replay_dolfin is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/checkpoint_burgers/test_checkpoint_burgers.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from os import path 3 | import pytest 4 | import sys 5 | 6 | fenics = pytest.importorskip("fenics") 7 | 8 | 9 | @pytest.mark.skipif(not hasattr(fenics, "HDF5File"), 10 | reason="requires hdf5 support") 11 | @pytest.mark.xfail(reason='Checkpointing is not implemented yet.') 12 | @pytest.mark.xfail(reason="checkpointing is not implemented") 13 | def test(request): 14 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 15 | test_dir = path.split(str(request.fspath))[0] 16 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 17 | 18 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 19 | assert handle.wait() == 0 20 | -------------------------------------------------------------------------------- /tests/migration/checkpoint_burgers_newton/test_checkpoint_burgers_newton.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | fenics = pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.skipif(not hasattr(fenics, "HDF5File"), reason="requires hdf5 support") 11 | @pytest.mark.xfail(reason="checkpointing is not implemented") 12 | def test(request): 13 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 14 | test_dir = path.split(str(request.fspath))[0] 15 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 16 | 17 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 18 | assert handle.wait() == 0 19 | -------------------------------------------------------------------------------- /tests/migration/checkpoint_online/test_checkpoint_online.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | 8 | @pytest.mark.skip(reason="Not supported yet") 9 | @pytest.mark.xfail(reason="checkpointing is not implemented") 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/checkpoint_stokes/test_checkpoint_stokes.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from os import path 3 | import pytest 4 | import sys 5 | 6 | pytest.importorskip("fenics") 7 | 8 | 9 | @pytest.mark.xfail(reason="checkpointing is not implemented") 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/compute_tlm_list_parameter/compute_tlm_list_parameter.py: -------------------------------------------------------------------------------- 1 | 2 | import random 3 | 4 | from dolfin import * 5 | from dolfin_adjoint import * 6 | 7 | mesh = UnitSquareMesh(4, 4) 8 | V = FunctionSpace(mesh, "CG", 3) 9 | 10 | def main(ic, a, b, annotate=False): 11 | u = TrialFunction(V) 12 | v = TestFunction(V) 13 | 14 | bc = DirichletBC(V, "-1.0", "on_boundary") 15 | 16 | mass = inner(u, v)*dx 17 | soln = Function(V) 18 | 19 | solve(b*mass == a*action(mass, ic), soln, bc, annotate=annotate) 20 | return soln 21 | 22 | if __name__ == "__main__": 23 | 24 | ic = project(Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)", degree=4), V) 25 | a = Constant(1.0, name="a") 26 | b = Constant(1.0, name="b") 27 | soln = main(ic, a, b, annotate=True) 28 | 29 | p = [Constant(2.0), Constant(3.0)] 30 | m = [Control(a), Control(b)] 31 | 32 | for (dudm, tlm_var) in compute_tlm(m, forget=False): 33 | # just keep iterating until we get the last dudm 34 | pass 35 | 36 | frm = inner(soln, soln)*dx 37 | dJdm_tlm = assemble(derivative(frm, soln)).inner(dudm.vector()) 38 | 39 | J = Functional(frm) 40 | m = [ConstantControl("a"), ConstantControl("b")] # get rid of the perturbation direction \delta m 41 | 42 | dJdm_adm = compute_gradient(J, m, forget=False) 43 | dJdm_adm = float(dJdm_adm[0])*float(p[0]) + float(dJdm_adm[1])*float(p[1]) 44 | 45 | print("dJdm_tlm: ", dJdm_tlm) 46 | print("dJdm_adm: ", dJdm_adm) 47 | 48 | assert abs(dJdm_tlm - dJdm_adm) < 1.0e-12 49 | -------------------------------------------------------------------------------- /tests/migration/compute_tlm_list_parameter/test_compute_tlm_list_parameter.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="compute_tlm is not implemented. It is possible to get tlm values, but it involves interacting with tape and block outputs directly") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/curl-curl/test_curl-curl.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/default_parameters/default_parameters.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | # This test checks that passing in solver parameters as 5 | # a dolfin.Parameters object is handled in dolfin-adjoint 6 | params = NonlinearVariationalSolver.default_parameters() 7 | 8 | # Setup 9 | n = 200 10 | mesh = RectangleMesh(MPI.comm_world, Point(-1, -1), Point(1, 1), n, n) 11 | V = FunctionSpace(mesh, "CG", 1) 12 | u = Function(V, name="State") 13 | m = Function(V, name="Control") 14 | v = TestFunction(V) 15 | 16 | m.interpolate(Constant(0.05)) 17 | 18 | # Run the forward model once to create the simulation record 19 | F = (inner(grad(u), grad(v)) - m*v)*dx 20 | bc = DirichletBC(V, 0.0, "on_boundary") 21 | solve(F == 0, u, bc, solver_parameters=params) 22 | -------------------------------------------------------------------------------- /tests/migration/default_parameters/test_default_parameters.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/differentiability-dg-upwind/mesh.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/differentiability-dg-upwind/mesh.xml.gz -------------------------------------------------------------------------------- /tests/migration/differentiability-dg-upwind/temperature.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/differentiability-dg-upwind/temperature.xml.gz -------------------------------------------------------------------------------- /tests/migration/differentiability-dg-upwind/test_differentiability-dg-upwind.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/differentiability-dg-upwind/velocity.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/differentiability-dg-upwind/velocity.xml.gz -------------------------------------------------------------------------------- /tests/migration/differentiability-stokes/mesh.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/differentiability-stokes/mesh.xml.gz -------------------------------------------------------------------------------- /tests/migration/differentiability-stokes/temperature.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/differentiability-stokes/temperature.xml.gz -------------------------------------------------------------------------------- /tests/migration/differentiability-stokes/test_differentiability-stokes.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | 8 | @pytest.mark.skip("does not test dolfin-adjoint") 9 | def test(request): 10 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 11 | test_dir = path.split(str(request.fspath))[0] 12 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 13 | 14 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 15 | assert handle.wait() == 0 16 | -------------------------------------------------------------------------------- /tests/migration/differentiability-stokes/velocity.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/differentiability-stokes/velocity.xml.gz -------------------------------------------------------------------------------- /tests/migration/dolfin_adjoint_variable/dolfin_adjoint_variable.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | mesh = UnitSquareMesh(2, 2) 5 | V = FunctionSpace(mesh, "CG", 1) 6 | 7 | v = TestFunction(V) 8 | u_new = Function(V) 9 | u_old = Function(V) 10 | 11 | a = inner(u_new - u_old - Constant(1), v)*dx 12 | 13 | adjointer.time.start(0) 14 | for t in range(5): 15 | solve(a == 0, u_new) 16 | adj_inc_timestep(time=t+1, finished=(t+1==5)) 17 | u_old.assign(u_new) 18 | 19 | adj_html("forward.html", "forward") 20 | 21 | # Get the tape value of the last timestep 22 | var = DolfinAdjointVariable(u_new) 23 | assert var.timestep == 4 24 | assert var.iteration == 0 25 | assert max(abs(var.tape_value().vector().array() - 5)) < 1e-12 26 | 27 | assert var.known_timesteps() == list(range(5)) 28 | 29 | # Alternatively we can use negative indices to get the latest Variable 30 | var = DolfinAdjointVariable(u_new, timestep=-2, iteration=-1) 31 | assert var.timestep == 4 32 | assert var.iteration == 0 33 | assert max(abs(var.tape_value().vector().array() - 5)) < 1e-12 34 | 35 | # Another way is to explicitly set timestep and iteration to retrieve 36 | for t in range(5): 37 | var = DolfinAdjointVariable(u_new, timestep=t, iteration=-1) 38 | expected_value = t + 1 39 | assert max(abs(var.tape_value().vector().array() - expected_value)) < 1e-12 40 | -------------------------------------------------------------------------------- /tests/migration/dolfin_adjoint_variable/test_dolfin_adjoint_variable.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="pyadjoint does not have DolfinAdjointVariable") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/expression_derivative/test_expression_derivative.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="The design of the expression breaks how pyadjoint stores the expression checkpoints - this issue needs to be discussed further") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/expression_derivative_cpp/test_expression_derivative_cpp.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/expression_derivative_inline_cpp/expression_derivative_inline_cpp.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | 5 | if __name__ == "__main__": 6 | mesh = UnitSquareMesh(4, 4) 7 | V = FunctionSpace(mesh, "CG", 1) 8 | 9 | a = Constant(0.5) 10 | b = Constant(0.25) 11 | 12 | f = Expression("(a-x[0])*(a-x[0])*b*b", a=a, b=b, degree=2) 13 | f.dependencies = [a, b] 14 | 15 | dfda = Expression("2*(a-x[0])*b", a=a, b=b, degree=1) 16 | dfdb = Expression("2*b*(a-x[0])*(a-x[0])", a=a, b=b, degree=2) 17 | 18 | f.user_defined_derivatives = {a: dfda, b: dfdb} 19 | 20 | J = assemble(f**2*dx(domain=mesh)) 21 | rf1 = ReducedFunctional(J, Control(a)) 22 | rf2 = ReducedFunctional(J, Control(b)) 23 | 24 | h = Constant(1.0) 25 | assert taylor_test(rf1, a, h) > 1.9 26 | assert taylor_test(rf2, b, h) > 1.9 27 | 28 | rf3 = ReducedFunctional(J, [Control(a), Control(b)]) 29 | hs = [Constant(1.0), Constant(1.0)] 30 | assert taylor_test(rf3, [a, b], hs) > 1.9 31 | -------------------------------------------------------------------------------- /tests/migration/expression_derivative_inline_cpp/test_expression_derivative_inline_cpp.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/expression_updates/expression_updates.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | mesh = UnitSquareMesh(4,4) 5 | V = FunctionSpace(mesh, "CG", 1) 6 | 7 | u = TrialFunction(V) 8 | v = TestFunction(V) 9 | 10 | a = inner(grad(u), grad(v))*dx 11 | u = Function(V, name = "solution") 12 | 13 | bc = DirichletBC(V, 0, "on_boundary") 14 | 15 | source = Expression("t*sin(x[0])*sin(x[1])", t = 0.0, element=V.ufl_element()) 16 | 17 | f = source*v*dx 18 | 19 | t = 0.0 20 | dt = 0.1 21 | 22 | A = assemble(a) 23 | for i in range(2): 24 | t += dt 25 | source.t = t 26 | F = assemble(f) 27 | bc.apply(A) 28 | bc.apply(F) 29 | solve(A, u.vector(), F, "cg", "ilu") 30 | 31 | assert replay_dolfin() 32 | -------------------------------------------------------------------------------- /tests/migration/expression_updates/test_expression_updates.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from os import path 3 | import pytest 4 | pytest.importorskip("fenics") 5 | 6 | 7 | @pytest.mark.xfail(reason="replay_dolfin is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/facet_integrals/test_facet_integrals.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | pytest.importorskip("fenics") 3 | pytest.importorskip("fenics_adjoint") 4 | 5 | from fenics import * 6 | from fenics_adjoint import * 7 | 8 | from numpy.random import rand 9 | 10 | 11 | def test_facet_integrals(): 12 | mesh = UnitIntervalMesh(2) 13 | W = FunctionSpace(mesh, "CG", 1) 14 | 15 | u = Function(W) 16 | g = project(Constant(1), W) 17 | u_ = TrialFunction(W) 18 | v = TestFunction(W) 19 | 20 | F = u_*v * dx - g*v*dx 21 | solve(lhs(F) == rhs(F), u) 22 | 23 | J = assemble(0.5 * inner(u("+"), u("+")) * dS) 24 | 25 | # Reduced functional with single control 26 | m = Control(g) 27 | 28 | Jhat = ReducedFunctional(J, m) 29 | h = Function(W) 30 | h.vector()[:] = rand(W.dim()) 31 | assert taylor_test(Jhat, g, h) > 1.9 32 | -------------------------------------------------------------------------------- /tests/migration/fg_constant/fg_constant.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | 5 | mesh = UnitSquareMesh(4, 4) 6 | V = FunctionSpace(mesh, "CG", 3) 7 | a = Constant(2.0, name="a") 8 | 9 | def main(ic, a, annotate=False): 10 | u = TrialFunction(V) 11 | v = TestFunction(V) 12 | 13 | bc = DirichletBC(V, "-1.0", "on_boundary") 14 | 15 | mass = inner(u, v)*dx 16 | rhs = a*action(mass, ic) 17 | soln = Function(V) 18 | da = Function(V) 19 | 20 | solve(mass == rhs, soln, bc, annotate=annotate) 21 | return soln 22 | 23 | if __name__ == "__main__": 24 | 25 | ic = project(Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)", degree=1), V) 26 | soln = main(ic, a, annotate=True) 27 | 28 | J = assemble(soln*soln*dx) 29 | dJda = compute_gradient(J, Control(a)) 30 | 31 | h = Constant(1.0) 32 | dJda = h._ad_dot(dJda) 33 | 34 | def J(a): 35 | soln = main(ic, a, annotate=False) 36 | return assemble(soln*soln*dx) 37 | 38 | Ja = assemble(soln**2*dx) 39 | assert taylor_test(J, a, h, dJda) > 1.9 40 | -------------------------------------------------------------------------------- /tests/migration/fg_constant/test_fg_constant.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/fg_constants/fg_constants.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | mesh = UnitSquareMesh(4, 4) 5 | V = FunctionSpace(mesh, "CG", 3) 6 | a = Constant(2.0) 7 | b = Constant(3.0) 8 | 9 | def main(ic, params, annotate=False): 10 | u = TrialFunction(V) 11 | v = TestFunction(V) 12 | (a, b) = params 13 | 14 | bc = DirichletBC(V, "-1.0", "on_boundary") 15 | 16 | mass = inner(u, v)*dx 17 | rhs = a*b*action(mass, ic) 18 | soln = Function(V) 19 | da = Function(V) 20 | 21 | solve(mass == rhs, soln, bc, annotate=annotate) 22 | return soln 23 | 24 | if __name__ == "__main__": 25 | 26 | ic = project(Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)", degree=1), V) 27 | soln = main(ic, (a, b), annotate=True) 28 | 29 | J = assemble(soln*soln*dx) 30 | dJda = compute_gradient(J, [Control(a), Control(b)]) 31 | hs = [Constant(1.0), Constant(1.0)] 32 | dJda = sum([hs[i]._ad_dot(dJda[i]) for i in range(len(hs))]) 33 | 34 | def J(params): 35 | soln = main(ic, params, annotate=False) 36 | return assemble(soln*soln*dx) 37 | 38 | minconv = taylor_test(J, [a, b], hs, dJda) 39 | assert minconv > 1.9 40 | -------------------------------------------------------------------------------- /tests/migration/fg_constants/test_fg_constants.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/function_assign_lincom/function_assign_lincom.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | 6 | 7 | def main(m): 8 | a = interpolate(Constant(1), m.function_space()) 9 | z = Function(m.function_space(), name="z") 10 | z.assign(0.5 * a + 2.0 * m) 11 | 12 | return z 13 | 14 | if __name__ == "__main__": 15 | mesh = UnitSquareMesh(4, 4) 16 | V = FunctionSpace(mesh, "CG", 1) 17 | 18 | m = interpolate(Expression("x[0]", degree=1), V) 19 | z = main(m) 20 | 21 | c = Control(m) 22 | J = assemble(inner(z, z)*dx) 23 | 24 | dJ = compute_gradient(J, c) 25 | h = Function(V) 26 | h.vector()[:] = rand(V.dim()) 27 | dJ = h._ad_dot(dJ) 28 | 29 | def Jhat(m): 30 | z = main(m) 31 | return assemble(inner(z, z)*dx) 32 | 33 | minconv = taylor_test(Jhat, c, h, dJ) 34 | -------------------------------------------------------------------------------- /tests/migration/function_assign_lincom/test_function_assign_lincom.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.xfail(reason="Not supported yet") 7 | @pytest.mark.xfail(reason="Assign linear combination not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/function_assigner/test_function_assigner.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | # @pytest.mark.skip("Should probably work, but not tested") 10 | @pytest.mark.xfail(reason="FunctionAssigner not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/function_assigner_givsub/test_function_assigner_givsub.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="FunctionAssigner not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/functional_dependencies/test_functional_dependencies.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="Invalid - adjointer/Functional not present. Might be valid if rewritten") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/functional_form/functional_form.py: -------------------------------------------------------------------------------- 1 | "Test expected failures" 2 | 3 | from dolfin import * 4 | from dolfin_adjoint import * 5 | 6 | # Define discrete Functionspace 7 | mesh = UnitSquareMesh(20, 20) 8 | V = FunctionSpace(mesh, "CG", 1) 9 | 10 | # Define Functions 11 | u = TrialFunction(V) 12 | v = TestFunction(V) 13 | s = Function(V) # PDE solution 14 | lmbd = Function(V) # Adjoint PDE solution 15 | f = Function(V) # Parameter 16 | alpha = Constant(1e-6) # Regularisation parameter 17 | ud = Expression("sin(pi*x[0])*sin(pi*x[1])", degree=1) # Desired temperature profile 18 | 19 | form_valid = 0.5*inner(s-ud, s-ud)*dx + alpha*f*f*dx 20 | J = Functional(form_valid) 21 | 22 | # Form has no consistent rank 23 | form_invalid = inner(u-ud, s)*dx 24 | try: 25 | J = Functional(form_invalid) 26 | except Exception: 27 | pass 28 | 29 | # Form has not rank 0 30 | form_invalid = inner(u, s)*dx 31 | try: 32 | J = Functional(form_invalid) 33 | except Exception: 34 | pass 35 | 36 | # Form has not rank 0 37 | form_invalid = inner(u, v)*dx 38 | try: 39 | J = Functional(form_invalid) 40 | except Exception: 41 | pass 42 | -------------------------------------------------------------------------------- /tests/migration/functional_form/test_functional_form.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="invalid - pyadjoint doesnt have Functional") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/functional_operations/functional_operations.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | from fenics import * 4 | from fenics_adjoint import * 5 | 6 | mesh = UnitSquareMesh(4, 4) 7 | V = FunctionSpace(mesh, "CG", 1) 8 | f = project(Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)", degree=4), V) 9 | 10 | def main(f, annotate=True): 11 | u = TrialFunction(V) 12 | v = TestFunction(V) 13 | 14 | u = Function(V, name="Solution") 15 | F = (inner(grad(u), grad(v)) + f*v)*dx 16 | bc = DirichletBC(V, 1.0, "on_boundary") 17 | solve(F == 0, u, bc, annotate=annotate) 18 | return u 19 | 20 | if __name__ == "__main__": 21 | 22 | u = main(f) 23 | m = Control(f) 24 | 25 | J1 = assemble(u**2 * dx) 26 | dJ1dm = compute_gradient(J1, m) 27 | HJ1m = Hessian(J1, m) 28 | 29 | 30 | J2 = assemble(f**2 * dx) 31 | dJ2dm = compute_gradient(J2, m) 32 | HJ2m = Hessian(J2, m) 33 | 34 | J = 0.5 * J1 + (-1) * (-J2) / 2 35 | dJdm = compute_gradient(J, m) 36 | HJm = Hessian(J, m) 37 | 38 | assert (dJdm.vector() - 0.5 * dJ1dm.vector() - 0.5 * dJ2dm.vector()).norm("l2") < 1e-14 39 | assert (HJm(f).vector() - 0.5 * HJ1m(f).vector() - 0.5 * HJ2m(f).vector()).norm("l2") < 1e-14 40 | -------------------------------------------------------------------------------- /tests/migration/functional_operations/test_functional_operations.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="certain AdjFloat operations doesnt have TLM/Hessian support") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/functional_value/test_functional_value.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/gst_mass/gst_mass.py: -------------------------------------------------------------------------------- 1 | 2 | import random 3 | 4 | from dolfin import * 5 | from dolfin_adjoint import * 6 | import sys 7 | import libadjoint.exceptions 8 | 9 | mesh = UnitSquareMesh(100, 100) 10 | V = FunctionSpace(mesh, "CG", 1) 11 | dolfin.parameters["adjoint"]["record_all"] = True 12 | 13 | def main(ic, annotate=False): 14 | u = TrialFunction(V) 15 | v = TestFunction(V) 16 | 17 | mass = inner(u, v)*dx 18 | soln = Function(V) 19 | 20 | solve(mass == action(mass, ic), soln, annotate=annotate, 21 | solver_parameters={"linear_solver": "cg"}) 22 | return soln 23 | 24 | if __name__ == "__main__": 25 | 26 | ic = project(Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)", degree=1), V) 27 | soln = main(ic, annotate=True) 28 | 29 | parameters["adjoint"]["cache_factorizations"] = True 30 | 31 | try: 32 | svd = compute_gst(ic, soln, 5, ic_norm="mass", final_norm="mass") 33 | except libadjoint.exceptions.LibadjointErrorSlepcError: 34 | info_red("Not testing since SLEPc unavailable.") 35 | import sys; sys.exit(0) 36 | 37 | (sigma, u, v, error) = svd.get_gst(0, return_vectors=True, return_residual=True) 38 | 39 | print("Maximal singular value: ", (sigma, error)) 40 | 41 | u_l2 = assemble(inner(u, u)*dx) 42 | print("L2 norm of u: %.16e" % u_l2) 43 | assert abs(u_l2 - 1.0) < 1.0e-13 44 | 45 | v_l2 = assemble(inner(v, v)*dx) 46 | print("L2 norm of v: %.16e" % v_l2) 47 | assert abs(v_l2 - 1.0) < 1.0e-13 48 | 49 | assert abs(sigma - 1.0) < 1.0e-13 50 | -------------------------------------------------------------------------------- /tests/migration/gst_mass/test_gst_mass.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="compute_gst not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = ["mpirun", "-n", "2", sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/heat/test_heat.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/hessian_callback/hessian_callback.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | import numpy as np 4 | 5 | mesh = UnitSquareMesh(2,2) 6 | V = FunctionSpace(mesh, "CG", 1) 7 | u = TrialFunction(V) 8 | v = TestFunction(V) 9 | R = FunctionSpace(mesh, "R", 0) 10 | 11 | a = interpolate(Constant(1.0), R, name="a") 12 | b = interpolate(Constant(2.0), R, name="b") 13 | 14 | f = a*u*v*dx 15 | l = b*v*dx 16 | 17 | u = Function(V) 18 | 19 | F = assemble(f) 20 | L = assemble(l) 21 | solve(F, u.vector(), L) 22 | 23 | J = Functional(u*dx) 24 | 25 | #Test the hessian array 26 | test = 0 27 | 28 | def hessian_cb(j, m, mdot, h): 29 | global test 30 | test = 1 31 | 32 | J_hat = ReducedFunctional(J, Control(a), hessian_cb = hessian_cb) 33 | J_hat(a) 34 | J_hat.hessian(a) 35 | assert test == 1, "Hessian callback was not called." 36 | -------------------------------------------------------------------------------- /tests/migration/hessian_callback/test_hessian_callback.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="Hessian callback not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/hessian_eps/hessian_eps.py: -------------------------------------------------------------------------------- 1 | 2 | from dolfin import * 3 | from dolfin_adjoint import * 4 | import libadjoint 5 | 6 | parameters["adjoint"]["cache_factorizations"] = True 7 | 8 | mesh = UnitSquareMesh(3, 3) 9 | V = FunctionSpace(mesh, "CG", 1) 10 | 11 | test = TestFunction(V) 12 | trial = TrialFunction(V) 13 | 14 | def main(m): 15 | u = interpolate(Constant(0.1), V, name="Solution") 16 | 17 | F = inner(u*u, test)*dx - inner(m, test)*dx 18 | solve(F == 0, u) 19 | F = inner(sin(u)*u*u*trial, test)*dx - inner(u**4, test)*dx 20 | solve(lhs(F) == rhs(F), u) 21 | 22 | return u 23 | 24 | if __name__ == "__main__": 25 | m = interpolate(Constant(1), V, name="Parameter") 26 | u = main(m) 27 | 28 | parameters["adjoint"]["stop_annotating"] = True 29 | 30 | J = Functional((inner(u, u))**6*dx, name="NormSquared") 31 | HJm = hessian(J, Control(m), warn=False) 32 | 33 | try: 34 | eps = HJm.eigendecomposition(n=3, solver='krylovschur') 35 | except libadjoint.exceptions.LibadjointErrorSlepcError: 36 | info_red("Not testing since SLEPc unavailable.") 37 | import sys; sys.exit(0) 38 | 39 | for i in range(len(eps)): 40 | (lamda, m) = eps[i] 41 | output = HJm(m) 42 | residual = assemble(inner(lamda*m - output, lamda*m - output)*dx) 43 | print("(%02d) eigenvector: " % i, m.vector().array()) 44 | print("(%02d) lambda: " % i, lamda) 45 | print("(%02d) residual^2: " % i, residual) 46 | 47 | assert residual < 1.0e-10 48 | -------------------------------------------------------------------------------- /tests/migration/hessian_eps/test_hessian_eps.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.skip("Crashes with segfault - Needs fixing") 7 | @pytest.mark.xfail(reason="Eigendecomposition of Hessian not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/hessian_identity/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/hessian_identity/graph.png -------------------------------------------------------------------------------- /tests/migration/hessian_identity/hessian_identity.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | 6 | mesh = UnitSquareMesh(3, 3) 7 | V = FunctionSpace(mesh, "R", 0) 8 | 9 | test = TestFunction(V) 10 | trial = TrialFunction(V) 11 | 12 | def main(m): 13 | u = interpolate(Constant(0.1), V) 14 | u = Function(V, u.vector()) 15 | 16 | F = inner(u*u, test)*dx - inner(m, test)*dx 17 | solve(F == 0, u) 18 | F = inner(sin(u)*u*u*trial, test)*dx - inner(u**4, test)*dx 19 | solve(lhs(F) == rhs(F), u) 20 | 21 | return u 22 | 23 | if __name__ == "__main__": 24 | m = interpolate(Constant(2.13), V) 25 | m = Function(V, m.vector()) 26 | u = main(m) 27 | 28 | J = assemble(inner(u, u)**3*dx + inner(m, m)*dx) 29 | dJdm = compute_gradient(J, Control(m)) 30 | h = Function(V) 31 | h.vector()[:] = rand(V.dim()) 32 | dJdm = h._ad_dot(dJdm) 33 | HJm = compute_hessian(J, Control(m), h) 34 | HJm = h._ad_dot(HJm) 35 | 36 | def Jhat(m): 37 | u = main(m) 38 | return assemble(inner(u, u)**3*dx + inner(m, m)*dx) 39 | 40 | minconv = taylor_test(Jhat, m, h, dJdm, Hm=HJm) 41 | assert minconv > 2.9 42 | -------------------------------------------------------------------------------- /tests/migration/hessian_identity/test_hessian_identity.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/hessian_identity_list/hessian_identity_list.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | 6 | mesh = UnitSquareMesh(3, 3) 7 | V = FunctionSpace(mesh, "R", 0) 8 | 9 | test = TestFunction(V) 10 | trial = TrialFunction(V) 11 | 12 | def main(m): 13 | u = interpolate(Constant(0.1), V, name="Solution") 14 | 15 | F = inner(u*u, test)*dx - inner(m, test)*dx 16 | solve(F == 0, u) 17 | F = inner(sin(u)*u*u*trial, test)*dx - inner(u**4, test)*dx 18 | solve(lhs(F) == rhs(F), u) 19 | 20 | return u 21 | 22 | if __name__ == "__main__": 23 | m = interpolate(Constant(2.13), V, name="Parameter1") 24 | u = main(m) 25 | 26 | J = assemble(inner(u, u)**3*dx + inner(m, m)*dx) 27 | 28 | controls = [Control(m)] 29 | 30 | dJdm = compute_gradient(J, controls) 31 | HJm = Hessian(J, controls) 32 | 33 | def Jhat(m): 34 | m = m[0] # the control is a list of length one, so Jhat will have to 35 | # except a list as well 36 | u = main(m) 37 | return assemble(inner(u, u)**3*dx + inner(m, m)*dx) 38 | 39 | direction = [interpolate(Constant(0.1), V)] 40 | minconv = taylor_test(Jhat, controls, Jm, dJdm, HJm=HJm, 41 | perturbation_direction=direction) 42 | assert minconv > 2.9 43 | -------------------------------------------------------------------------------- /tests/migration/hessian_identity_list/test_hessian_identity_list.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="Should work but testing interface is lacking for hessian with multiple controls") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/hessian_identity_list2/hessian_identity_list2.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | parameters["adjoint"]["cache_factorizations"] = True 5 | 6 | mesh = UnitSquareMesh(3, 3) 7 | V = FunctionSpace(mesh, "R", 0) 8 | 9 | test = TestFunction(V) 10 | trial = TrialFunction(V) 11 | 12 | def main(m, n): 13 | u = interpolate(Constant(0.1), V, name="Solution") 14 | 15 | F = inner(u*u, test)*dx - inner(m+n, test)*dx 16 | solve(F == 0, u) 17 | F = inner(sin(u)*u*u*trial, test)*dx - inner(u**4, test)*dx 18 | solve(lhs(F) == rhs(F), u) 19 | 20 | return u 21 | 22 | if __name__ == "__main__": 23 | m = interpolate(Constant(2.13), V, name="Parameter1") 24 | n = interpolate(Constant(1.5), V, name="Parameter2") 25 | u = main(m,n) 26 | 27 | parameters["adjoint"]["stop_annotating"] = True 28 | 29 | J = Functional((inner(u, u))**3*dx + inner(m+n, m+n)*dx, name="NormSquared") 30 | Jm = assemble(inner(u, u)**3*dx + inner(m+n, m+n)*dx) 31 | 32 | controls = [Control(m), Control(n)] 33 | 34 | dJdm = compute_gradient(J, controls, forget=None) 35 | HJm = hessian(J, controls, warn=False) 36 | 37 | def Jhat(mn): 38 | m, n = mn 39 | u = main(m, n) 40 | return assemble(inner(u, u)**3*dx + inner(m+n, m+n)*dx) 41 | 42 | direction = [interpolate(Constant(0.1), V), interpolate(Constant(0.1), V)] 43 | minconv = taylor_test(Jhat, controls, Jm, dJdm, HJm=HJm, 44 | perturbation_direction=direction) 45 | assert minconv > 2.9 46 | -------------------------------------------------------------------------------- /tests/migration/hessian_identity_list2/test_hessian_identity_list2.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/interpolate/interpolate.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | dolfin.parameters["adjoint"]["record_all"] = True 5 | 6 | import random 7 | import sys 8 | 9 | mesh = UnitIntervalMesh(5) 10 | V2 = FunctionSpace(mesh, "CG", 2) 11 | V1 = FunctionSpace(mesh, "CG", 1) 12 | 13 | def main(ic, annotate=False): 14 | 15 | ic_proj = Function(V2, name="Projection") 16 | u = TrialFunction(V2) 17 | v = TestFunction(V2) 18 | solve(inner(u, v)*dx == inner(ic, v)*dx, ic_proj, annotate=annotate) 19 | 20 | out = interpolate(ic_proj, V1, annotate=annotate) 21 | return out 22 | 23 | if __name__ == "__main__": 24 | ic = Function(V2, name="InitialCondition") 25 | vec = ic.vector() 26 | for i in range(len(vec)): 27 | vec[i] = random.random() 28 | 29 | out = main(ic, annotate=True) 30 | 31 | success = replay_dolfin() 32 | 33 | # J = Functional(out*out*dx*dt[FINISH_TIME]) 34 | # icparam = FunctionControl("InitialCondition") 35 | # dJdic = compute_gradient(J, icparam) 36 | # 37 | # def J(ic): 38 | # out = main(ic, annotate=False) 39 | # return assemble(out*out*dx) 40 | # 41 | # minconv = utils.test_initial_condition_adjoint(J, ic, dJdic) 42 | # if minconv < 1.9: 43 | # sys.exit(1) 44 | 45 | if not success: 46 | sys.exit(1) 47 | -------------------------------------------------------------------------------- /tests/migration/interpolate/test_interpolate.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="Interpolate is not implemented yet") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/interpolate/test_interpolate_unit.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | pytest.importorskip("dolfin") 3 | pytest.importorskip("dolfin_adjoint") 4 | 5 | from dolfin import * 6 | from dolfin_adjoint import * 7 | 8 | @pytest.mark.xfail 9 | def test_register_interpolate(): 10 | mesh = UnitIntervalMesh(10) 11 | V = FunctionSpace(mesh, "CG", 1) 12 | f = interpolate(Constant(1), V, annotate=False) 13 | assert adjointer.adjointer.nequations == 0 14 | 15 | f = interpolate(Constant(1), V, annotate=True) 16 | assert adjointer.adjointer.nequations == 1 17 | adj_html("forward.html", "forward") 18 | -------------------------------------------------------------------------------- /tests/migration/krylov_reevaluate/krylov_reevaluate.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | if __name__ == "__main__": 5 | 6 | mesh = UnitIntervalMesh(4) 7 | W = FunctionSpace(mesh, "CG", 1) 8 | 9 | # Create control and solution field(s) 10 | p = Function(W) 11 | 12 | Ks = Function(W) 13 | Ks.vector()[:] = 1.0 14 | 15 | ps = TrialFunction(W) 16 | qs = TestFunction(W) 17 | 18 | F = (Ks*ps + 1)*qs*dx 19 | 20 | # Assemble the stiffness matrix 21 | A = assemble(lhs(F)) 22 | rhs = assemble(rhs(F)) 23 | 24 | # Apply boundary conditions to the matrix 25 | bc = DirichletBC(W, Constant(0.0), "on_boundary") 26 | bc.apply(A) 27 | bc.apply(rhs) 28 | 29 | # Solve the system 30 | sol = KrylovSolver("gmres", "amg") 31 | sol.set_operator(A) 32 | sol.solve(p.vector(), rhs) 33 | 34 | J = assemble(p*dx) 35 | m = Control(Ks) 36 | Jr = ReducedFunctional(J, m) 37 | 38 | # Check that perturbing the control changes the output 39 | val1 = Jr(Ks) 40 | Ks.vector()[:] += 1 41 | val2 = Jr(Ks) 42 | 43 | assert abs(val1 - val2) > 1e-10 44 | 45 | r = Function(W) 46 | from numpy.random import rand 47 | r.vector()[:] = rand(W.dim()) 48 | 49 | # Perform Taylor test 50 | results = taylor_to_dict(Jr, Ks, r) 51 | print(results) 52 | 53 | -------------------------------------------------------------------------------- /tests/migration/krylov_reevaluate/test_krylov_reevaluate.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/linear_solver/test_linear_solver.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="LinearSolver is not part of the python interface in dolfin 2018") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/list_parameter/test_list_parameter.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/localsolver/test_localsolver.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="LocalSolver not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/localsolver_factorize/test_localsolver_factorize.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="LocalSolver not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/lusolver_changing/lusolver_changing.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | set_log_level(WARNING) 4 | 5 | parameters["adjoint"]["debug_cache"] = True 6 | 7 | class BidomainSolver(object): 8 | def __init__(self, dt): 9 | mesh = UnitIntervalMesh(2) 10 | V = FunctionSpace(mesh, "CG", 1) 11 | 12 | self.v = TrialFunction(V) 13 | self.w = TestFunction(V) 14 | self.s = Function(V) 15 | 16 | self._lhs = dt*self.v*self.w*dx 17 | self._rhs = self.w*dx 18 | 19 | def step(self): 20 | 21 | mat = assemble(self._lhs) 22 | solver = LUSolver(mat) 23 | solver.parameters["reuse_factorization"] = False 24 | 25 | rhs = assemble(self._rhs) 26 | 27 | solver.solve(self.s.vector(), rhs) 28 | 29 | 30 | dt = Constant(0.1) 31 | solver = BidomainSolver(dt) 32 | 33 | solver.step() 34 | dt.assign(0.5) 35 | solver.step() 36 | 37 | success = replay_dolfin(tol=0.0, stop=True) 38 | assert success 39 | -------------------------------------------------------------------------------- /tests/migration/lusolver_changing/test_lusolver_changing.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="replay_dolfin not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/lusolver_reassemble/lusolver_reassemble.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | set_log_level(WARNING) 5 | mesh = UnitIntervalMesh(5) 6 | 7 | 8 | class Solver(object): 9 | 10 | def __init__(self): 11 | R = FunctionSpace(mesh, "R", 0) 12 | self.timestep = Function(R) 13 | self.r = TestFunction(R) 14 | 15 | def step(self, dt): 16 | V = FunctionSpace(mesh, "CG", 1) 17 | v = TrialFunction(V) 18 | w = TestFunction(V) 19 | s = Function(V) 20 | r = self.r 21 | 22 | timestep_update = (self.timestep - Constant(dt))*r*dx 23 | solve(timestep_update == 0, self.timestep) 24 | 25 | G = self.timestep*v*w*dx - w*dx 26 | 27 | lhs, rhs = system(G) 28 | lhs_matrix = assemble(lhs) 29 | solver = LUSolver(lhs_matrix) 30 | solver.parameters["reuse_factorization"] = True 31 | 32 | rhs = assemble(rhs) 33 | solver.solve(s.vector(), rhs) 34 | 35 | 36 | solver = Solver() 37 | 38 | solver.step(0.1) 39 | solver.step(0.2) 40 | 41 | assert replay_dolfin(tol=0.0, stop=True) 42 | -------------------------------------------------------------------------------- /tests/migration/lusolver_reassemble/test_lusolver_reassemble.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | dolfin = pytest.importorskip("dolfin") 8 | 9 | 10 | @pytest.mark.skipif(not hasattr(dolfin, "HDF5File"), 11 | reason="requires hdf5 support") 12 | @pytest.mark.xfail(reason="replay_dolfin not implemented") 13 | def test(request): 14 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 15 | test_dir = path.split(str(request.fspath))[0] 16 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 17 | 18 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 19 | assert handle.wait() == 0 20 | -------------------------------------------------------------------------------- /tests/migration/lvs/lvs.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | 6 | mesh = UnitSquareMesh(4, 4) 7 | V = FunctionSpace(mesh, "CG", 1) 8 | v = TestFunction(V) 9 | f = interpolate(Expression("sin(pi*x[0])", degree=1), V) 10 | # TODO: Workaround until interpolate is overloaded. 11 | f = Function(V, f.vector()) 12 | 13 | def main(f, annotate=False): 14 | u = Function(V, name = "system state") 15 | w = TrialFunction(V) 16 | a = w*v*dx 17 | L = f*v*dx 18 | F = a - L 19 | 20 | bcs = None 21 | 22 | problem = LinearVariationalProblem(a, L, u, bcs) 23 | problem = LinearVariationalProblem(a, L, u) 24 | solver = LinearVariationalSolver(problem) 25 | solver.solve(annotate=annotate) 26 | 27 | return u 28 | 29 | u = main(f, annotate=True) 30 | if False: 31 | # TODO: Not implemented yet. 32 | assert replay_dolfin() 33 | 34 | grad = compute_gradient(assemble(u*u*dx), Control(f)) 35 | h = Function(V) 36 | h.vector()[:] = rand(V.dim()) 37 | grad = h._ad_dot(grad) 38 | 39 | def J(f): 40 | u = main(f, annotate=False) 41 | return assemble(u*u*dx) 42 | 43 | minconv = taylor_test(J, f, h, grad) 44 | assert minconv > 1.9 45 | -------------------------------------------------------------------------------- /tests/migration/lvs/test_lvs.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/manual_hessian/test_manual_hessian.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="Might not be relevant as Hessian is already implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/matrix_free_burgers/test_matrix_free_burgers.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.skip("Matrix free not supported anymore") 7 | @pytest.mark.xfail(reason="KrylovSolver is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/matrix_free_heat/test_matrix_free_heat.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.skip("Matrix free not supported anymore") 7 | @pytest.mark.xfail(reason="KrylovSolver is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/matrix_free_simple/matrix_free_simple.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | import sys 5 | 6 | dolfin.parameters["adjoint"]["record_all"] = True 7 | 8 | mesh = UnitSquareMesh(4, 4) 9 | V = FunctionSpace(mesh, 'CG', 1) 10 | bc = DirichletBC(V, Constant(0.0), lambda x, on_boundary: on_boundary) 11 | u = TrialFunction(V); v = TestFunction(V); 12 | 13 | def main(ic): 14 | b = assemble(ic * v* dx) 15 | bc.apply(b) 16 | 17 | x = Function(V) 18 | KrylovSolver = AdjointPETScKrylovSolver("cg","none") 19 | mat = AdjointKrylovMatrix(inner(grad(u), grad(v))*dx, bcs=bc) 20 | KrylovSolver.solve(mat, down_cast(x.vector()), down_cast(b)) 21 | 22 | return x 23 | 24 | if __name__ == "__main__": 25 | 26 | # There must be a better way of doing this ... 27 | import random 28 | ic = Function(V) 29 | icvec = ic.vector() 30 | for i in range(len(icvec)): 31 | icvec[i] = random.random() 32 | 33 | iccopy = Function(ic) 34 | final = main(ic) 35 | 36 | adj_html("forward.html", "forward") 37 | replay_dolfin() 38 | 39 | J = Functional(inner(final, final)*dx*dt[FINISH_TIME]) 40 | for (adjoint, var) in compute_adjoint(J, forget=False): 41 | pass 42 | 43 | def J(ic): 44 | soln = main(ic) 45 | return assemble(inner(soln, soln)*dx) 46 | 47 | minconv = utils.test_initial_condition_adjoint(J, ic, adjoint, seed=1.0e-3) 48 | if minconv < 1.9: 49 | sys.exit(1) 50 | -------------------------------------------------------------------------------- /tests/migration/matrix_free_simple/test_matrix_free_simple.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.skip("Matrix free not supported anymore") 7 | @pytest.mark.xfail(reason="KrylovSolver is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/matrix_summation/matrix_summation.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | from fenics import * 4 | from fenics_adjoint import * 5 | 6 | from numpy.random import rand 7 | 8 | n = 3 9 | mesh = UnitIntervalMesh(n) 10 | V = FunctionSpace(mesh, "CG", 1) 11 | 12 | def Dt(u, u_, timestep): 13 | return (u - u_)/timestep 14 | 15 | def main(ic, annotate=False): 16 | 17 | u_ = ic.copy(deepcopy=True) 18 | u = TrialFunction(V) 19 | v = TestFunction(V) 20 | 21 | mass = assemble(inner(u, v) * dx) 22 | if annotate: assert hasattr(mass, 'form') 23 | 24 | advec = assemble(u_*u.dx(0)*v * dx) 25 | if annotate: assert hasattr(advec, 'form') 26 | 27 | rhs = assemble(inner(u_, v) * dx) 28 | if annotate: assert hasattr(rhs, 'form') 29 | 30 | L = mass + advec 31 | 32 | if annotate: assert hasattr(L, 'form') 33 | solve(L, u_.vector(), rhs, 'lu', annotate=annotate) 34 | 35 | return u_ 36 | 37 | if __name__ == "__main__": 38 | 39 | ic = project(Expression("sin(2*pi*x[0])", degree=1), V) 40 | forward = main(ic, annotate=True) 41 | 42 | m = Control(ic) 43 | J = assemble(forward*forward*dx) 44 | dJdm = compute_gradient(J, m) 45 | h = Function(V) 46 | h.vector()[:] = rand(V.dim()) 47 | dJdm = h._ad_dot(dJdm) 48 | 49 | def Jfunc(ic): 50 | forward = main(ic, annotate=False) 51 | return assemble(forward*forward*dx) 52 | 53 | minconv = taylor_test(Jfunc, ic, h, dJdm) 54 | assert minconv > 1.8 55 | -------------------------------------------------------------------------------- /tests/migration/matrix_summation/test_matrix_summation.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="Matrix summation is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/mpec/mpec.py: -------------------------------------------------------------------------------- 1 | """Verify the tangent linear model of example 5.2 in 10.1007/s10589-009-9307-9""" 2 | 3 | from fenics import * 4 | from fenics_adjoint import * 5 | 6 | parameters["form_compiler"]["representation"] = "uflacs" 7 | 8 | n = 32 9 | mesh = UnitSquareMesh(n, n) 10 | 11 | V = FunctionSpace(mesh, "CG", 1) 12 | gamma = Constant(1e3) # 1.0 / alpha in the paper 13 | 14 | def main(): 15 | y = Function(V, name="Solution") 16 | u = Function(V, name="Control") 17 | w = TestFunction(V) 18 | 19 | eps = 1e-4 20 | 21 | def smoothmax(r): 22 | return conditional(gt(r, eps), r - eps/2, conditional(lt(r, 0), 0, r**2 / (2*eps))) 23 | def uflmax(a, b): 24 | return conditional(gt(a, b), a, b) 25 | 26 | f = interpolate(Expression("-std::abs(x[0]*x[1] - 0.5) + 0.25", degree=1), V) 27 | F = inner(grad(y), grad(w))*dx - gamma * inner(smoothmax(-y), w)*dx - inner(f + u, w)*dx 28 | 29 | bc = DirichletBC(V, 0.0, "on_boundary") 30 | 31 | solve(F == 0, y, bcs = bc, solver_parameters={"newton_solver": {"maximum_iterations": 30}}) 32 | return y 33 | 34 | if __name__ == "__main__": 35 | def J(g): 36 | gamma.assign(g) 37 | y = main() 38 | return assemble(inner(y, y)*dx) 39 | 40 | y = main() 41 | m = Control(gamma) 42 | 43 | Jy = inner(y, y)*dx 44 | dJdu = assemble(derivative(Jy, y)) 45 | Jm = assemble(Jy) 46 | for (soln, var) in compute_tlm(m): 47 | pass 48 | dJdm = soln.vector().inner(dJdu) 49 | 50 | minconv = taylor_test(J, m, Jm, dJdm) 51 | assert minconv > 1.89 52 | -------------------------------------------------------------------------------- /tests/migration/mpec/test_mpec.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="compute_tlm is not implemented. It is possible to get tlm values, but it involves interacting with tape and block outputs directly") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/mpec/timing.py: -------------------------------------------------------------------------------- 1 | """Verify the tangent linear model of example 5.2 in 10.1007/s10589-009-9307-9""" 2 | 3 | 4 | from dolfin import * 5 | from dolfin_adjoint import * 6 | 7 | n = 512 8 | mesh = UnitSquareMesh(n, n) 9 | 10 | V = FunctionSpace(mesh, "CG", 1) 11 | gamma = Constant(1e3) # 1.0 / alpha in the paper 12 | 13 | f = interpolate(Expression("-std::abs(x[0]*x[1] - 0.5) + 0.25"), V) 14 | yd = Function(f) 15 | alpha = 1e-2 16 | u = Function(V, name="Control") 17 | 18 | def main(): 19 | y = Function(V, name="Solution") 20 | w = TestFunction(V) 21 | 22 | eps = 1e-4 23 | 24 | def smoothmax(r): 25 | return conditional(gt(r, eps), r - eps/2, conditional(lt(r, 0), 0, r**2 / (2*eps))) 26 | def uflmax(a, b): 27 | return conditional(gt(a, b), a, b) 28 | 29 | F = inner(grad(y), grad(w))*dx - gamma * inner(smoothmax(-y), w)*dx - inner(f + u, w)*dx 30 | 31 | bc = DirichletBC(V, 0.0, "on_boundary") 32 | 33 | solve(F == 0, y, bcs = bc, solver_parameters={"newton_solver": {"maximum_iterations": 30}}) 34 | return y 35 | 36 | if __name__ == "__main__": 37 | fwd = Timer("forward") 38 | y = main() 39 | fwd_time = fwd.stop() 40 | 41 | print("fwd_time: ", fwd_time) 42 | 43 | J = Functional(0.5*inner(y - yd, y - yd)*dx*dt[FINISH_TIME] + alpha/2*inner(u, u)*dx*dt[START_TIME]) 44 | m = TimeConstantParameter(u) 45 | 46 | adj = Timer("adjoint") 47 | dJ = compute_gradient(J, m) 48 | adj_time = adj.stop() 49 | 50 | print("adj_time: ", adj_time) 51 | print("ratio: ", (fwd_time + adj_time) / (fwd_time)) 52 | -------------------------------------------------------------------------------- /tests/migration/multimesh_poisson/mesh_poisson.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | class Noslip(SubDomain): 5 | def inside(self, x, on_boundary): 6 | return on_boundary 7 | 8 | def solve_poisson(): 9 | mesh = RectangleMesh(Point(0,0), Point(1,1), 40, 20) 10 | V = FunctionSpace(mesh, 'Lagrange', 1) 11 | u = TrialFunction(V) 12 | v = TestFunction(V) 13 | 14 | a = dot(grad(u), grad(v))*dx 15 | L = Constant(1)*v*dx 16 | 17 | A = assemble(a) 18 | b = assemble(L) 19 | 20 | noslip=Noslip() 21 | bc0 = DirichletBC(V, Constant(0), noslip) 22 | bc0.apply(A,b) 23 | 24 | u = Function(V) 25 | solve(A, u.vector(), b) 26 | 27 | adj_html("forward.html", "forward") 28 | J = Functional(u*u*u*dx) 29 | c = Control(u) 30 | q= compute_gradient(J, c) 31 | plot(u) 32 | plot(q) 33 | interactive() 34 | 35 | 36 | if __name__=='__main__': 37 | solve_poisson() 38 | -------------------------------------------------------------------------------- /tests/migration/multimesh_poisson/test_multimesh_poisson.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.skip("Not supported by dolfin master yet") 7 | @pytest.mark.xfail(reason="multimesh is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/navier_stokes/lshape.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/navier_stokes/lshape.xml.gz -------------------------------------------------------------------------------- /tests/migration/navier_stokes/test_navier_stokes.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/newton_constant/newton_constant.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | from distutils.version import LooseVersion 4 | import sys 5 | 6 | n = 30 7 | mesh = UnitIntervalMesh(n) 8 | import dolfin 9 | if LooseVersion(dolfin.__version__) > LooseVersion('1.3.0'): 10 | dx = dx(mesh) 11 | 12 | V = FunctionSpace(mesh, "CG", 2) 13 | 14 | def main(ic, nu): 15 | u = ic.copy(deepcopy=True) 16 | u_next = Function(V) 17 | v = TestFunction(V) 18 | 19 | 20 | timestep = Constant(1.0/n) 21 | 22 | F = ((u_next - u)/timestep*v 23 | + u_next*u_next.dx(0)*v + nu*u_next.dx(0)*v.dx(0))*dx 24 | bc = DirichletBC(V, 0.0, "on_boundary") 25 | 26 | t = 0.0 27 | end = 0.2 28 | while (t <= end): 29 | solve(F == 0, u_next, bc) 30 | u.assign(u_next) 31 | t += float(timestep) 32 | 33 | return u 34 | 35 | if __name__ == "__main__": 36 | ic = project(Expression("sin(2*pi*x[0])", degree=1), V) 37 | nu = Constant(0.0001, name="Nu") 38 | 39 | u = main(ic, nu) 40 | 41 | J = assemble(inner(u, u)*dx + inner(nu, nu)*dx) 42 | dJdnu = compute_gradient(J, Control(nu)) 43 | h = Constant(0.0001) 44 | dJdnu = h._ad_dot(dJdnu) 45 | 46 | def Jhat(nu): 47 | u = main(ic, nu) 48 | return assemble(inner(u, u)*dx + inner(nu, nu)*dx) 49 | 50 | minconv = taylor_test(Jhat, nu, h, dJdnu) 51 | assert minconv > 1.9 52 | -------------------------------------------------------------------------------- /tests/migration/newton_constant/test_newton_constant.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/noannotations/noannotations.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | mesh = UnitSquareMesh(4, 4) 5 | V0 = FunctionSpace(mesh, "CG", 1) 6 | 7 | u0 = project(Constant(1), V0, annotate=False) 8 | u0.assign(10*u0, annotate=False) 9 | 10 | tape = get_working_tape() 11 | assert len(tape.get_blocks()) == 0 12 | -------------------------------------------------------------------------------- /tests/migration/noannotations/test_noannotations.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/nullspace/test_nullspace.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="KrylovSolver is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/nullspace_linear_solver/test_nullspace_linear_solver.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from os import path 3 | import pytest 4 | import sys 5 | 6 | pytest.importorskip("fenics") 7 | 8 | 9 | @pytest.mark.xfail(reason="LinearSolver is not implemented") 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/nvs/nvs.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | 6 | mesh = UnitSquareMesh(4, 4) 7 | V = FunctionSpace(mesh, "CG", 1) 8 | v = TestFunction(V) 9 | f = interpolate(Expression("sin(pi*x[0])", degree=1), V) 10 | f = Function(V, f.vector(), name="ic") 11 | 12 | def main(f, annotate=False): 13 | u = Function(V) 14 | a = u*v*dx 15 | L = f*v*dx 16 | F = a - L 17 | 18 | bcs = None 19 | 20 | problem = NonlinearVariationalProblem(F, u, bcs, J=derivative(F, u)) 21 | solver = NonlinearVariationalSolver(problem) 22 | solver.solve(annotate=annotate) 23 | return u 24 | 25 | u = main(f, annotate=True) 26 | if False: 27 | # TODO: Not implemented. 28 | assert replay_dolfin() 29 | 30 | grad = compute_gradient(assemble(u*u*dx + 31 | f*f*dx), Control(f)) 32 | h = Function(V) 33 | h.vector()[:] = rand(V.dim()) 34 | grad = h._ad_dot(grad) 35 | 36 | def J(f): 37 | u = main(f, annotate=False) 38 | return assemble(u*u*dx + f*f*dx) 39 | 40 | minconv = taylor_test(J, f, h, grad) 41 | assert minconv > 1.9 42 | -------------------------------------------------------------------------------- /tests/migration/nvs/test_nvs.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/ode_fitzhughnagumo/test_ode_fitzhughnagumo.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from os import path 3 | import pytest 4 | import sys 5 | 6 | fenics = pytest.importorskip("fenics") 7 | 8 | 9 | @pytest.mark.skipif(not hasattr(fenics, "HDF5File"), 10 | reason="requires hdf5 support") 11 | @pytest.mark.xfail(reason="PointIntegralSolver is not implemented") 12 | def test(request): 13 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 14 | test_dir = path.split(str(request.fspath))[0] 15 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 16 | 17 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 18 | assert handle.wait() == 0 19 | -------------------------------------------------------------------------------- /tests/migration/ode_solver/test_ode_solver.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PointIntegralSolver is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/ode_tentusscher/test_ode_tentusscher.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PointIntegralSolver is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/ode_tentusscher_quick/test_ode_tentusscher_quick.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PointIntegralSolver is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/ode_vector/test_ode_vector.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PointIntegralSolver is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/optimal_control_bfgs/mesh.geo: -------------------------------------------------------------------------------- 1 | n = 0.04; 2 | Point(1) = {-1, -1, 0, n}; 3 | Point(2) = {1, -1, 0, n}; 4 | Point(3) = {1, 1, 0, n}; 5 | Point(4) = {-1, 1, 0, n}; 6 | Line(1) = {1, 2}; 7 | Line(2) = {2, 3}; 8 | Line(3) = {3, 4}; 9 | Line(4) = {4, 1}; 10 | Line Loop(5) = {3, 4, 1, 2}; 11 | Plane Surface(6) = {5}; 12 | Physical Surface(7) = {6}; 13 | -------------------------------------------------------------------------------- /tests/migration/optimal_control_bfgs/optimal_control_bfgs.py: -------------------------------------------------------------------------------- 1 | """ Solves the optimal control problem for the heat equation """ 2 | from fenics import * 3 | from fenics_adjoint import * 4 | from ufl_legacy.log import info_red, info_green 5 | 6 | # Setup 7 | mesh = Mesh("mesh.xml") 8 | V = FunctionSpace(mesh, "CG", 1) 9 | u = Function(V, name="State") 10 | m = project(Constant(-5), V) 11 | v = TestFunction(V) 12 | 13 | # Run the forward model once to create the simulation record 14 | F = (inner(grad(u), grad(v)) - m * v) * dx 15 | bc = DirichletBC(V, 0.0, "on_boundary") 16 | solve(F == 0, u, bc) 17 | 18 | # The functional of interest is the normed difference between desired 19 | # and simulated temperature profile 20 | x = SpatialCoordinate(mesh) 21 | u_desired = exp(-1 / (1 - x[0] * x[0]) - 1 / (1 - x[1] * x[1])) 22 | J = assemble(0.5 * inner(u - u_desired, u - u_desired) * dx) 23 | 24 | # Run the optimisation 25 | rf = ReducedFunctional(J, Control(m)) 26 | ub = 0.5 27 | lb = interpolate(Constant(-1), V) # Test 2 different ways of imposing bounds 28 | 29 | m_opt = minimize(rf, method="L-BFGS-B", 30 | tol=2e-08, bounds=(lb, ub), options={"disp": True, "maxiter": 5}) 31 | 32 | assert min(m_opt.vector().get_local()) > lb((0, 0)) - 0.05 33 | info_red("Skipping bound check in L-BFGS-B test") 34 | # Skipping this test for now until I have figured out what is going wrong 35 | # assert max(m_opt.vector().array()) < ub + 0.05 36 | assert abs(rf(m_opt)) < 1e-3 37 | 38 | info_green("Test passed") 39 | -------------------------------------------------------------------------------- /tests/migration/optimal_control_bfgs/test_optimal_control_bfgs.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/optimal_control_mms/test_optimal_control_mms.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = ["mpirun", "-n", "2", sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/optimization_checkpointing/test_optimization_checkpointing.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | dolfin = pytest.importorskip("dolfin") 8 | 9 | 10 | @pytest.mark.skipif(not hasattr(dolfin, "HDF5File"), reason="requires hdf5 support") 11 | @pytest.mark.xfail(reason="optimization is not implemented") 12 | def test(request): 13 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 14 | test_dir = path.split(str(request.fspath))[0] 15 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 16 | 17 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 18 | assert handle.wait() == 0 19 | -------------------------------------------------------------------------------- /tests/migration/optimization_moola/test_optimization_moola.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import sys 4 | import pytest 5 | 6 | pytest.importorskip("fenics") 7 | pytest.importorskip("moola") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/optimization_optizelle/test_optimization_optizelle.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="optimization is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = ["mpirun", "-n", "2", sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/optimization_optizelle_algebra/test_optimization_optizelle_algebra.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from os import path 3 | import pytest 4 | pytest.importorskip("fenics") 5 | 6 | 7 | @pytest.mark.xfail(reason="optimization is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/optimization_pyipopt/test_optimization_pyipopt.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import sys 3 | import subprocess 4 | import pytest 5 | 6 | pytest.importorskip("fenics") 7 | 8 | 9 | def test(request): 10 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 11 | test_dir = path.split(str(request.fspath))[0] 12 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 13 | 14 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 15 | assert handle.wait() == 0 16 | -------------------------------------------------------------------------------- /tests/migration/optimization_pyopt/test_optimization_pyopt.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="optimization is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/optimization_scalar/test_optimization_scalar.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/optimization_scipy/test_optimization_scipy.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/optimization_tao/test_optimization_tao.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="TaoSolver is not implemented, maybe it should live in fenics_adjoint?") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/periodic/test_periodic.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PeriodicBC is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/petsckrylov_reevaluate/petsckrylov_reevaluate.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | if __name__ == "__main__": 5 | 6 | mesh = UnitIntervalMesh(4) 7 | W = FunctionSpace(mesh, "CG", 1) 8 | 9 | # Create control and solution field(s) 10 | p = Function(W) 11 | 12 | Ks = Function(W) 13 | Ks.vector()[:] = 1.0 14 | 15 | ps = TrialFunction(W) 16 | qs = TestFunction(W) 17 | 18 | F = (Ks*ps + 1)*qs*dx 19 | 20 | # Assemble the stiffness matrix 21 | A = assemble(lhs(F)) 22 | rhs = assemble(rhs(F)) 23 | 24 | # Apply boundary conditions to the matrix 25 | bc = DirichletBC(W, Constant(0.0), "on_boundary") 26 | bc.apply(A) 27 | bc.apply(rhs) 28 | 29 | # Solve the system 30 | sol = PETScKrylovSolver("gmres", "amg") 31 | sol.set_operator(A) 32 | sol.solve(p.vector(), rhs) 33 | 34 | 35 | J = assemble(p*dx) 36 | m = Control(Ks) 37 | Jr = ReducedFunctional(J, m) 38 | 39 | # Check that perturbing the control changes the output 40 | val1 = Jr(Ks) 41 | Ks.vector()[:] += 1 42 | val2 = Jr(Ks) 43 | assert abs(val1 - val2) > 1e-10 44 | 45 | # Perform Taylor test 46 | Jr.taylor_test(Ks) 47 | -------------------------------------------------------------------------------- /tests/migration/petsckrylov_reevaluate/test_petsckrylov_reevaluate.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.skipif_module_is_missing("petsc4py") 11 | @pytest.mark.xfail(reason="PETScKrylovSolver is not implemented") 12 | def test(request): 13 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 14 | test_dir = path.split(str(request.fspath))[0] 15 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 16 | 17 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 18 | assert handle.wait() == 0 19 | -------------------------------------------------------------------------------- /tests/migration/picard-linearisation/test_picard-linearisation.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional/pointwise_functional.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | from numpy.testing import assert_approx_equal 6 | 7 | # Create mesh and define function space 8 | mesh = UnitSquareMesh(64, 64) 9 | V = FunctionSpace(mesh, "Lagrange", 1) 10 | 11 | # Define Dirichlet boundary (x = 0 or x = 1) 12 | def boundary(x): 13 | return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS 14 | 15 | def center_func(x): 16 | return (0.45 <= x[0] and x[0] <= 0.55 and near(x[1], 0.5)) or \ 17 | 0.45 <= x[1] and x[1] <= 0.55 and near(x[0], 0.5) 18 | 19 | # Define domain for point integral 20 | center_domain = MeshFunction("size_t", mesh, mesh.geometric_dimension()-2, 0) 21 | center = AutoSubDomain(center_func) 22 | center.mark(center_domain, 1) 23 | dPP = dP(subdomain_data=center_domain) 24 | 25 | # Define boundary condition 26 | bc = DirichletBC(V, Constant(0), boundary) 27 | 28 | # Define variational problem 29 | u = TrialFunction(V) 30 | v = TestFunction(V) 31 | f = project(Constant(0.4), V) 32 | a = inner(grad(u), grad(v))*dx 33 | L = f*v*dx 34 | 35 | # Compute solution 36 | u_ = Function(V) 37 | solve(a == L, u_, bc) 38 | 39 | # Compute the gradient of a functional 40 | J = assemble(u_**2*dPP(1) + u_*dx) 41 | Jhat = ReducedFunctional(J, Control(f)) 42 | 43 | assert_approx_equal(Jhat(f), J) 44 | 45 | h = Function(V) 46 | h.vector()[:] = rand(V.dim()) 47 | assert taylor_test(Jhat, f, h) > 1.9 48 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional/test_pointwise_functional.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_one_point/.gitignore: -------------------------------------------------------------------------------- 1 | *.txt 2 | *.html 3 | *.dat 4 | *~ 5 | *.pvd 6 | *.vtu 7 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_one_point/pointwise_functional_one_point.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | import numpy as np 4 | 5 | # Define mesh 6 | mesh = UnitSquareMesh(10, 10) 7 | n = FacetNormal(mesh) 8 | U = FunctionSpace(mesh, "CG", 1) 9 | 10 | adj_start_timestep() 11 | 12 | def forward(c): 13 | u = Function(U) 14 | u0 = Function(U) 15 | v = TestFunction(U) 16 | F = inner(u - u0 - c, v)*dx 17 | for t in range(1, 5): 18 | solve(F == 0, u) 19 | u0.assign(u) 20 | adj_inc_timestep(t, t == 4) 21 | return u0 22 | 23 | c = Constant(3.) 24 | u = forward(c) 25 | 26 | J = PointwiseFunctional(u, [0, 0, 0, 0], Point(np.array([0.4, 0.4])), [1, 2, 3, 4], u_ind=[None]) 27 | Jr = ReducedFunctional(J, Control(c)) 28 | 29 | Jr3 = Jr(Constant(3)) 30 | 31 | assert Jr.taylor_test(Constant(5)) > 1.9 32 | assert abs(Jr3 - 270.0) < 1e-12 33 | info_green("Test passed") 34 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_one_point/test_pointwise_functional_one_point.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PointwiseFunctional does not exist in pyadjoint") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_regularisation/.gitignore: -------------------------------------------------------------------------------- 1 | *.txt 2 | *.html 3 | *.dat 4 | *~ 5 | *.pvd 6 | *.vtu 7 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_regularisation/pointwise_functional_regularisation.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | import numpy as np 5 | 6 | # Define mesh 7 | mesh = UnitSquareMesh(10, 10) 8 | n = FacetNormal(mesh) 9 | U = FunctionSpace(mesh, "CG", 1) 10 | 11 | adj_start_timestep() 12 | 13 | def forward(c): 14 | u = Function(U, name="state") 15 | u0 = Function(U, name="oldstate") 16 | v = TestFunction(U) 17 | F = inner(u - u0 - c, v)*dx 18 | for t in range(1, 5): 19 | solve(F == 0, u) 20 | u0.assign(u) 21 | adj_inc_timestep(t, t == 4) 22 | return u0 23 | 24 | c = Constant(3.) 25 | c = project(c, U, name="Control") 26 | u = forward(c) 27 | 28 | # check for one coord 29 | Regform = Constant(0.01)*inner(c, c)*inner(c, c)*dx 30 | J = PointwiseFunctional(u, [0, 0, 0, 0], Point(np.array([0.4, 0.4])), [1, 2, 3, 4], u_ind=[None], regularisation=Regform, verbose=False) 31 | Jr = ReducedFunctional(J, Control(c)) 32 | 33 | Jr3 = Jr(c) 34 | #G = compute_gradient(J, Control(c)) 35 | #print assemble(G) 36 | #assert abs(Jr3 - 310.5) < 1e-4 37 | assert Jr.taylor_test(project(Constant(5.), U)) > 1.9 38 | 39 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_regularisation/test_pointwise_functional_regularisation.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.skip("Pointwise functional with regularisation is currently not implemented (see documentation in PointwiseFunctional class)") 7 | @pytest.mark.xfail(reason="PointwiseFunctional does not exist in pyadjoint") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_two_points/.gitignore: -------------------------------------------------------------------------------- 1 | *.txt 2 | *.html 3 | *.dat 4 | *~ 5 | *.pvd 6 | *.vtu 7 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_two_points/pointwise_functional_two_points.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | import numpy as np 4 | np.random.seed(seed=21) 5 | 6 | 7 | # Define mesh 8 | mesh = UnitSquareMesh(10, 10) 9 | n = FacetNormal(mesh) 10 | U = FunctionSpace(mesh, "CG", 1) 11 | 12 | adj_start_timestep() 13 | 14 | def forward(c): 15 | u = Function(U) 16 | u0 = Function(U) 17 | v = TestFunction(U) 18 | F = inner(u - u0 - c, v)*dx 19 | for t in range(1, 5): 20 | solve(F == 0, u) 21 | u0.assign(u) 22 | adj_inc_timestep(t, t == 4) 23 | return u0 24 | 25 | c = Constant(3.) 26 | u = forward(c) 27 | 28 | mJ = PointwiseFunctional(u, [[0, 0, 0, 0], [1, 1, 1, 1]], [Point(np.array([0.2, 0.2])), Point(np.array([0.8, 0.8]))], [1, 2, 3, 4], u_ind=[None, None]) 29 | mJr = ReducedFunctional(mJ, Control(c)) 30 | 31 | mJr3 = mJr(Constant(3)) 32 | 33 | assert mJr.taylor_test(Constant(5), seed=1e2) > 1.9 34 | assert abs(mJr3 - 484.0) < 1e-11, abs(mJr3 - 484.0) 35 | info_green("Test passed") 36 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_two_points/test_pointwise_functional_two_points.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PointwiseFunctional does not exist in pyadjoint") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_vector/pointwise_functional_vector.py: -------------------------------------------------------------------------------- 1 | from dolfin import * 2 | from dolfin_adjoint import * 3 | 4 | import numpy as np 5 | 6 | # Define mesh 7 | mesh = UnitSquareMesh(10, 10) 8 | n = FacetNormal(mesh) 9 | U = VectorFunctionSpace(mesh, "CG", 1) 10 | 11 | adj_start_timestep() 12 | 13 | def forward(c): 14 | u = Function(U, name="u") 15 | u0 = Function(U, name="u0") 16 | v = TestFunction(U) 17 | F = inner(u - u0 - c*Constant((1., 2)), v)*dx 18 | for t in range(1, 5): 19 | solve(F == 0, u) 20 | u0.assign(u) 21 | adj_inc_timestep(t, t == 4) 22 | return u0 23 | 24 | c = Constant(3.) 25 | u = forward(c) 26 | 27 | J0 = PointwiseFunctional(u, [0, 0, 0, 0], Point(np.array([0.4, 0.4])), [1, 2, 3, 4], u_ind=[0]) 28 | Jr0 = ReducedFunctional(J0, Control(c)) 29 | J1 = PointwiseFunctional(u, [0, 0, 0, 0], Point(np.array([0.4, 0.4])), [1, 2, 3, 4], u_ind=[1], alpha=0.5) 30 | Jr1 = ReducedFunctional(J1, Control(c)) 31 | 32 | Jr3 = Jr0(Constant(3.)) 33 | Jr4 = Jr1(Constant(2.)) 34 | 35 | assert abs(Jr3 - 270.0) < 1e-12 36 | assert abs(Jr4 - 240.0) < 1e-11 37 | assert Jr0.taylor_test(Constant(5.)) > 1.9 38 | 39 | -------------------------------------------------------------------------------- /tests/migration/pointwise_functional_vector/test_pointwise_functional_vector.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PointwiseFunctional does not exist in pyadjoint") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/preassembly_efficiency/test_preassembly_efficiency.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="assemble kwarg \"cache\" is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | 15 | test_cmd = [sys.executable, path.join(test_dir, test_file), "--ignore"] 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | 19 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 20 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 21 | assert handle.wait() == 0 22 | -------------------------------------------------------------------------------- /tests/migration/projection/projection.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | 6 | mesh = UnitSquareMesh(4, 4) 7 | V3 = FunctionSpace(mesh, "CG", 3) 8 | V2 = FunctionSpace(mesh, "CG", 2) 9 | 10 | 11 | def main(ic, annotate=False): 12 | bc = DirichletBC(V2, "-1.0", "on_boundary") 13 | soln = project(ic, V2, bcs=bc, solver_type='lu', annotate=annotate) 14 | return soln 15 | 16 | if __name__ == "__main__": 17 | 18 | ic = project(Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)", degree=4), V3) 19 | soln = main(ic, annotate=True) 20 | 21 | 22 | if False: 23 | # TODO: Not implemented. 24 | replay_dolfin() 25 | 26 | J = assemble(soln*soln*dx) 27 | dJdic = compute_gradient(J, Control(ic)) 28 | h = Function(V3) 29 | h.vector()[:] = rand(V3.dim()) 30 | dJdic = h._ad_dot(dJdic) 31 | 32 | def J(ic): 33 | soln = main(ic, annotate=False) 34 | return assemble(soln*soln*dx) 35 | 36 | minconv = taylor_test(J, ic, h, dJdic) 37 | assert minconv > 1.9 38 | -------------------------------------------------------------------------------- /tests/migration/projection/test_projection.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/reduced_functional/reduced_functional.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | from numpy.random import rand 5 | 6 | mesh = UnitIntervalMesh(MPI.comm_world, 2) 7 | 8 | W = FunctionSpace(mesh, "CG", 1) 9 | rho = project(Constant(1), W) 10 | g = project(Constant(1), W) 11 | 12 | u = Function(W, name="State") 13 | u_ = TrialFunction(W) 14 | v = TestFunction(W) 15 | 16 | F = rho*u_*v * dx - Constant(1)*g*v*dx 17 | solve(lhs(F) == rhs(F), u) 18 | 19 | J = assemble(0.5 * inner(u, u) * dx + g**3*dx) 20 | 21 | # Reduced functional with single control 22 | m = Control(rho) 23 | 24 | Jhat = ReducedFunctional(J, m) 25 | Jhat.derivative() 26 | Jhat(rho) 27 | 28 | if False: 29 | # TODO: This is not implemented. And exactly how the interface should be has not been decided. 30 | Jhat.hessian(rho) 31 | 32 | h = Function(W) 33 | h.vector()[:] = rand(W.dim()) 34 | assert taylor_test(Jhat, rho, h) > 1.9 35 | 36 | 37 | # Reduced functional with multiple controls 38 | m2 = Control(g) 39 | 40 | Jhat = ReducedFunctional(J, [m, m2]) 41 | Jhat.derivative() 42 | Jhat([rho, g]) 43 | if False: 44 | # TODO: This is not implemented. And exactly how the interface should be has not been decided. 45 | Jhat.hessian([rho, g]) 46 | 47 | h2 = Function(W) 48 | h2.vector()[:] = rand(W.dim()) 49 | assert taylor_test(Jhat, [rho, g], [h, h2]) > 1.9 50 | -------------------------------------------------------------------------------- /tests/migration/reduced_functional/test_reduced_functional.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/reduced_functional_cache/test_reduced_functional_cache.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="ReducedFunctional kwarg cache is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/reduced_functional_constants/reduced_functional_constants.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | mesh = UnitIntervalMesh(MPI.comm_world, 2) 5 | 6 | W = FunctionSpace(mesh, "CG", 1) 7 | rho = Constant(1) 8 | g = Constant(1) 9 | 10 | 11 | u = Function(W, name="State") 12 | u_ = TrialFunction(W) 13 | v = TestFunction(W) 14 | 15 | F = rho*u_*v * dx - g*v*dx(domain=mesh) 16 | solve(lhs(F) == rhs(F), u) 17 | 18 | J = assemble(0.5 * inner(u, u) * dx + g**3*dx(domain=mesh)) 19 | 20 | # Reduced functional with single control 21 | m = Control(rho) 22 | 23 | Jhat = ReducedFunctional(J, m) 24 | Jhat.derivative() 25 | Jhat(rho) 26 | 27 | h = Constant(1.0) 28 | assert taylor_test(Jhat, rho, h) > 1.9 29 | 30 | # Reduced functional with multiple controls 31 | m2 = Control(g) 32 | 33 | Jhat = ReducedFunctional(J, [m, m2]) 34 | Jhat.derivative() 35 | Jhat([rho, g]) 36 | 37 | direction = [Constant(1), Constant(1)] 38 | 39 | assert taylor_test(Jhat, [rho, g], direction) > 1.9 40 | -------------------------------------------------------------------------------- /tests/migration/reduced_functional_constants/test_reduced_functional_constants.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/reduced_functional_evaluation/reduced_functional_evaluation.py: -------------------------------------------------------------------------------- 1 | ''' A simple test that compares the functional value computed manually and with libadjoints functional_evaluation. 2 | Writting this test was motivated by the bug described on https://bugs.launchpad.net/dolfin-adjoint/+bug/1032291 ''' 3 | 4 | from fenics import * 5 | from fenics_adjoint import * 6 | import sys 7 | 8 | # Global settings 9 | set_log_level(LogLevel.ERROR) 10 | 11 | mesh = UnitSquareMesh(10, 10) 12 | V = FunctionSpace(mesh, "DG", 1) 13 | 14 | u_new = Function(V, name = "u_new") 15 | u_old = Function(V, name = "u_old") 16 | m = Control(u_old) 17 | u_test = TestFunction(V) 18 | 19 | T = 2. 20 | t = 0. 21 | dlt = 1. 22 | F1 = ( inner((u_new - u_old)/dlt, u_test)*dx - inner(Constant(1.), u_test)*dx ) 23 | #solve(inner(u_new, u_test)*dx == 0, u_new) 24 | 25 | man_func_value = 0. 26 | print("+++++++++++++ INITIAL RUN +++++++++") 27 | man_func_value_contr = 0.5*assemble(inner(u_new, u_new)*dx) 28 | while t < T: 29 | 30 | solve(F1 == 0, u_new) 31 | u_old.assign(u_new) 32 | 33 | t += dlt 34 | man_func_value_contr = assemble(inner(u_new, u_new)*dx) 35 | if t>=T: 36 | man_func_value += 0.5*man_func_value_contr 37 | else: 38 | man_func_value += man_func_value_contr 39 | 40 | print() 41 | print("+++++++++++++ REPLAY +++++++++") 42 | u_new.vector()[:] = 0. 43 | u_old.vector()[:] = 0. 44 | J = man_func_value 45 | reduced_functional = ReducedFunctional(J, m) 46 | reduced_functional_value = reduced_functional(u_old) 47 | 48 | assert abs(reduced_functional_value - man_func_value) < 1e-13 49 | -------------------------------------------------------------------------------- /tests/migration/reduced_functional_evaluation/test_reduced_functional_evaluation.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/rush_larsen/test_rush_larsen.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="PointIntegralSolver is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/shallow_water/basin.geo: -------------------------------------------------------------------------------- 1 | Point(1) = {250000.0,0,0,10000.0}; 2 | Point(2) = {0,0,0,10000.0}; 3 | Point(3) = {-250000.0,0,0,10000.0}; 4 | Circle(1) = {1,2,3}; 5 | Circle(2) = {3,2,1}; 6 | Physical Line(3) = {1,2}; 7 | Line Loop(4) = {1,2}; 8 | Plane Surface(5) = {4}; 9 | Physical Surface(6) = {5}; 10 | -------------------------------------------------------------------------------- /tests/migration/shallow_water/kelvin_new.py: -------------------------------------------------------------------------------- 1 | from dolfin import Mesh, UserExpression 2 | from math import exp, sqrt, pi 3 | 4 | import sw_lib 5 | 6 | params=sw_lib.parameters({ 7 | 'depth' : 5., 8 | 'g' : 10., 9 | 'f' : 1.0313e-4, 10 | 'dump_period' : 10 11 | }) 12 | 13 | # Basin radius. 14 | r0=250000 15 | # Long wave celerity. 16 | c=sqrt(params["g"]*params["depth"]) 17 | 18 | 19 | params["finish_time"]=4*2*pi*r0/c 20 | params["dt"]=params["finish_time"]/4000. 21 | 22 | # Rossby radius. 23 | LR=c/params["f"] 24 | 25 | class InitialConditions(UserExpression): 26 | def eval(self, values, X): 27 | r=(X[0]**2+X[1]**2)**0.5 28 | if r>0.0001: 29 | values[0]=-0.05*c*exp((r-r0)/LR)*X[0]/r*X[1]/r 30 | values[1]= 0.05*c*exp((r-r0)/LR)*X[0]/r*X[0]/r 31 | values[2]= 0.05*exp((r-r0)/LR)*X[0]/r 32 | else: 33 | values[0]=0. 34 | values[1]=0. 35 | values[2]=0. 36 | def value_shape(self): 37 | return (3,) 38 | 39 | try: 40 | mesh=Mesh("basin.xml") 41 | except RuntimeError: 42 | import sys 43 | import os.path 44 | 45 | mesh=Mesh(os.path.dirname(sys.argv[0]) + os.path.sep + "basin.xml") 46 | 47 | mesh.order() 48 | mesh.init() 49 | -------------------------------------------------------------------------------- /tests/migration/shallow_water/shallow_water.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import kelvin_new as kelvin 4 | import sw_lib 5 | 6 | from fenics import * 7 | from fenics_adjoint import * 8 | 9 | from numpy.random import rand, seed 10 | 11 | mesh = UnitSquareMesh(6, 6) 12 | W=sw_lib.p1dgp2(mesh) 13 | 14 | state=Function(W) 15 | 16 | state.interpolate(kelvin.InitialConditions(degree=1)) 17 | m = Control(state) 18 | 19 | kelvin.params["basename"] = "p1dgp2" 20 | kelvin.params["dt"] = 2 21 | kelvin.params["finish_time"] = kelvin.params["dt"]*2 22 | kelvin.params["dump_period"] = 1 23 | 24 | M, G=sw_lib.construct_shallow_water(W, kelvin.params) 25 | 26 | state = sw_lib.timeloop_theta(M, G, state, kelvin.params) 27 | 28 | if False: 29 | # TODO: Not implemented. 30 | replay_dolfin() 31 | 32 | J = assemble(dot(state, state)*dx) 33 | ic = Function(W) 34 | ic.interpolate(kelvin.InitialConditions(degree=1)) 35 | def compute_J(ic): 36 | state = sw_lib.timeloop_theta(M, G, ic.copy(deepcopy=True), kelvin.params, annotate=False) 37 | return assemble(dot(state, state)*dx) 38 | 39 | h = Function(W) 40 | h.vector()[:] = rand(W.dim())*0.01 41 | dJdm = compute_gradient(J, m) 42 | dJdm = h._ad_dot(dJdm) 43 | 44 | minconv = taylor_test(compute_J, ic, h, dJdm) 45 | assert minconv > 1.9 46 | -------------------------------------------------------------------------------- /tests/migration/shallow_water/test_shallow_water.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/shallow_water_time_functional/Makefile: -------------------------------------------------------------------------------- 1 | run: clean 2 | python sw_flather.py 3 | clean: 4 | rm -f *vtu 5 | rm -f *pvd 6 | 7 | 8 | -------------------------------------------------------------------------------- /tests/migration/shallow_water_time_functional/basin.geo: -------------------------------------------------------------------------------- 1 | Point(1) = {250000.0,0,0,10000.0}; 2 | Point(2) = {0,0,0,10000.0}; 3 | Point(3) = {-250000.0,0,0,10000.0}; 4 | Circle(1) = {1,2,3}; 5 | Circle(2) = {3,2,1}; 6 | Physical Line(3) = {1,2}; 7 | Line Loop(4) = {1,2}; 8 | Plane Surface(5) = {4}; 9 | Physical Surface(6) = {5}; 10 | -------------------------------------------------------------------------------- /tests/migration/shallow_water_time_functional/shallow_water_time_functional.py: -------------------------------------------------------------------------------- 1 | import divett 2 | import sw_lib 3 | from fenics import * 4 | from fenics_adjoint import * 5 | 6 | from numpy.random import rand 7 | 8 | W=sw_lib.p1dgp2(divett.mesh) 9 | 10 | state=Function(W) 11 | 12 | state.interpolate(divett.InitialConditions(degree=1)) 13 | m = Control(state) 14 | 15 | divett.params["basename"]="p1dgp2" 16 | divett.params["finish_time"]=2*pi/(sqrt(divett.params["g"]*divett.params["depth"])*pi/3000) 17 | divett.params["dt"]=divett.params["finish_time"]/5 18 | divett.params["period"]=60*60*1.24 19 | divett.params["dump_period"]=1 20 | 21 | M, G, rhs_contr, ufl,ufr=sw_lib.construct_shallow_water(W, divett.ds, divett.params) 22 | 23 | j, state = sw_lib.timeloop_theta(M, G, rhs_contr, ufl, ufr, state, divett.params) 24 | 25 | if False: 26 | # TODO: Not implemented. 27 | replay_dolfin() 28 | 29 | (u,p) = split(state) 30 | J = j + assemble(3.14*dot(state, state)*dx) 31 | 32 | def compute_J(ic): 33 | j, state = sw_lib.timeloop_theta(M, G, rhs_contr, ufl, ufr, ic.copy(deepcopy=True), divett.params, annotate=False) 34 | return j + assemble(3.14*dot(state, state)*dx) 35 | 36 | ic = Function(W) 37 | ic.interpolate(divett.InitialConditions(degree=1)) 38 | 39 | h = Function(W) 40 | h.vector()[:] = rand(W.dim()) 41 | dJdm = compute_gradient(J, m) 42 | dJdm = h._ad_dot(dJdm) 43 | 44 | minconv = taylor_test(compute_J, ic, h, dJdm) 45 | assert minconv > 1.9 46 | -------------------------------------------------------------------------------- /tests/migration/shallow_water_time_functional/test_shallow_water_time_functional.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/split/split.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | import random 5 | import sys 6 | 7 | mesh = UnitSquareMesh(2, 2) 8 | cg2 = FiniteElement("CG", triangle, 2) 9 | cg1 = FiniteElement("CG", triangle, 1) 10 | ele = MixedElement([cg2, cg1]) 11 | Z = FunctionSpace(mesh, ele) 12 | V2 = FunctionSpace(mesh, cg2) 13 | 14 | def main(ic, fnsplit=True, annotate=False): 15 | u = Function(V2) 16 | w = TrialFunction(V2) 17 | v = TestFunction(V2) 18 | 19 | if fnsplit: 20 | ic_u = ic.split()[0] 21 | else: 22 | ic_u = split(ic)[0] 23 | 24 | mass = inner(w, v)*dx 25 | rhs = inner(ic_u, v)*dx 26 | 27 | solve(mass == rhs, u, annotate=annotate) 28 | 29 | return u 30 | 31 | if __name__ == "__main__": 32 | ic = Function(Z) 33 | vec = ic.vector() 34 | for i in range(len(vec)): 35 | vec[i] = random.random() 36 | 37 | raised_exception = False 38 | try: 39 | u = main(ic, fnsplit=True, annotate=True) 40 | except: 41 | raised_exception = True 42 | 43 | if raised_exception: 44 | sys.exit(1) 45 | 46 | raised_exception = False 47 | try: 48 | u = main(ic, fnsplit=False, annotate=True) 49 | except: 50 | raised_exception = True 51 | 52 | if raised_exception: 53 | sys.exit(1) 54 | -------------------------------------------------------------------------------- /tests/migration/split/test_split.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/stokes/test_stokes.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/stokes_krylov/test_stokes_krylov.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/stokes_maday/test_stokes_maday.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="Optimization is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/stokes_petsckrylov/test_stokes_petsckrylov.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.skipif_module_is_missing("petsc4py") 11 | @pytest.mark.xfail(reason="PETScKrylovSolver is not implemented") 12 | def test(request): 13 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 14 | test_dir = path.split(str(request.fspath))[0] 15 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 16 | 17 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 18 | assert handle.wait() == 0 19 | -------------------------------------------------------------------------------- /tests/migration/supg/mesh.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/supg/mesh.xml.gz -------------------------------------------------------------------------------- /tests/migration/supg/subdomains.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/supg/subdomains.xml.gz -------------------------------------------------------------------------------- /tests/migration/supg/test_supg.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.slow 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/supg/velocity.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/supg/velocity.xml.gz -------------------------------------------------------------------------------- /tests/migration/svd_bc_simple/test_svd_bc_simple.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="compute_gst is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/svd_burgers/test_svd_burgers.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | import subprocess 3 | import pytest 4 | 5 | 6 | @pytest.mark.skip("Crashes with LibadjointErrorSlepcError: SLEPc returned a negative number as a growth rate (error in SLEPc)?") 7 | @pytest.mark.xfail(reason="compute_gst is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/svd_burgers_perturb/test_svd_burgers_perturb.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.slow 11 | @pytest.mark.xfail(reason="compute_gst is not implemented") 12 | def test(request): 13 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 14 | test_dir = path.split(str(request.fspath))[0] 15 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 16 | 17 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 18 | assert handle.wait() == 0 19 | -------------------------------------------------------------------------------- /tests/migration/svd_simple/svd_simple.py: -------------------------------------------------------------------------------- 1 | 2 | import random 3 | 4 | from dolfin import * 5 | from dolfin_adjoint import * 6 | import sys 7 | import libadjoint.exceptions 8 | 9 | mesh = UnitSquareMesh(4, 4) 10 | V = FunctionSpace(mesh, "CG", 1) 11 | dolfin.parameters["adjoint"]["record_all"] = True 12 | 13 | def main(ic, annotate=False): 14 | u = TrialFunction(V) 15 | v = TestFunction(V) 16 | 17 | mass = inner(u, v)*dx 18 | soln = Function(V) 19 | 20 | solve(mass == action(mass, ic), soln, annotate=annotate) 21 | return soln 22 | 23 | if __name__ == "__main__": 24 | 25 | ic = project(Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)", degree=1), V) 26 | soln = main(ic, annotate=True) 27 | 28 | try: 29 | svd = compute_gst(ic, soln, 1, ic_norm=None, final_norm=None) 30 | except libadjoint.exceptions.LibadjointErrorSlepcError: 31 | info_red("Not testing since SLEPc unavailable.") 32 | import sys; sys.exit(0) 33 | 34 | (sigma, error) = svd.get_gst(0, return_residual=True) 35 | 36 | print("Maximal singular value: ", (sigma, error)) 37 | 38 | if (abs(sigma - 1.0) > 1.0e-15): 39 | sys.exit(1) 40 | -------------------------------------------------------------------------------- /tests/migration/svd_simple/test_svd_simple.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="compute_gst is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = ["mpirun", "-n", "2", sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/taylor_hessian/taylor_hessian.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | # A dummy test to check the Hessian testing in the Taylor test. 5 | # Tests that check tests that check tests! 6 | 7 | a = Constant(3.0, name="MyConstant") 8 | af = float(a) 9 | 10 | def J(a): 11 | af = float(a) 12 | return af**3 13 | 14 | dJda = 3*af**2 15 | 16 | def HJa(adot): 17 | return 6*af*adot 18 | 19 | Ja = J(a) 20 | 21 | h = Constant(1.0) 22 | minconv = taylor_test(J, a, h, dJda, HJa(float(h))) 23 | assert minconv > 2.9 24 | -------------------------------------------------------------------------------- /tests/migration/taylor_hessian/test_taylor_hessian.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/time_dependent_data/test_time_dependent_data.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/time_dependent_data/time_dependent_data.py: -------------------------------------------------------------------------------- 1 | '''Contributed by Martin Alnaes, launchpad question 228870''' 2 | 3 | from fenics import * 4 | from fenics_adjoint import * 5 | 6 | mesh = UnitSquareMesh(5,5) 7 | V = FunctionSpace(mesh, "CG", 1) 8 | R = FunctionSpace(mesh, "R", 0) 9 | 10 | z = Function(R, name="z") 11 | 12 | ut = TrialFunction(V) 13 | v = TestFunction(V) 14 | m = Function(V, name="m") 15 | u = Function(V, name="u") 16 | 17 | nt = 3 18 | J = 0 19 | for t in range(nt): 20 | tmp = interpolate(Expression("t*t", t=t, degree=2), R) # ... to make sure this is recorded 21 | tmp = Function(R, tmp.vector()) 22 | z.assign(tmp) 23 | solve(ut*v*dx == m*v*dx, u, []) # ... even though it's not used in the computation 24 | if t == nt-1: 25 | quad_weight = AdjFloat(0.5) 26 | else: 27 | quad_weight = AdjFloat(1.0) 28 | J += quad_weight * assemble((u-z)**2*dx) 29 | 30 | # ... so it can be replayed by the functional at the end 31 | J = ReducedFunctional(J, Control(m)) 32 | assert abs(float(J(m)) - 9.0) < 1.0e-14 33 | -------------------------------------------------------------------------------- /tests/migration/time_functionals/test_time_functionals.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="invalid - pyadjoint doesn't have functional") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/time_functionals_no_annotation/test_time_functionals_no_annotation.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/time_functionals_no_annotation/time_functionals_no_annotation.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | mesh = UnitSquareMesh(4, 4) 5 | V = FunctionSpace(mesh, "R", 0) 6 | f = Constant(1.0) 7 | 8 | # Solve the 'PDE' du/dt = 1 9 | # with initial condition u(0) = c 10 | # this gives the solution u(t) = c + t. 11 | def run_forward(annotate=True): 12 | u = TrialFunction(V) 13 | v = TestFunction(V) 14 | 15 | u_0 = Function(V, name="Value") 16 | u_0.vector()[0] = 1.0 17 | 18 | dt = 0.5 19 | T = 1.0 20 | 21 | F = ( (u - u_0)/dt*v - f*v)*dx 22 | a, L = lhs(F), rhs(F) 23 | 24 | t = float(dt) 25 | 26 | while t <= T: 27 | 28 | solve(a == L, u_0, annotate=annotate) 29 | 30 | t += float(dt) 31 | 32 | return u_0 33 | 34 | if __name__ == "__main__": 35 | 36 | u = run_forward() 37 | 38 | # Pointwise evaluation (at the end of time, symbolically) 39 | J = assemble(inner(u,u)*dx) 40 | dJdic = compute_gradient(J, Control(u)) 41 | # Work out the solution by hand -- it's 4 42 | assert dJdic.vector().get_local()[0] == 4.0 43 | -------------------------------------------------------------------------------- /tests/migration/timeforms/test_timeforms.py: -------------------------------------------------------------------------------- 1 | from dolfin_adjoint import * 2 | from dolfin import * 3 | import pytest 4 | 5 | pytest.importorskip("dolfin") 6 | 7 | 8 | @pytest.mark.xfail 9 | def test_evaluation(): 10 | mesh = UnitSquareMesh(4, 4) 11 | V = FunctionSpace(mesh, "CG", 1) 12 | 13 | u = TrialFunction(V) 14 | v = TestFunction(V) 15 | 16 | adjointer.time.start(0) 17 | u1 = project(Constant(1), V, annotate=True, name="u1") 18 | 19 | adj_inc_timestep(time=1, finished=False) 20 | u2 = project(Constant(2), V, annotate=True, name="u2") 21 | 22 | adj_inc_timestep(time=2, finished=False) 23 | u3 = project(Constant(3), V, annotate=True, name="u3") 24 | 25 | adj_inc_timestep(time=3, finished=True) 26 | 27 | m = Control(u1) 28 | 29 | J = Functional(u1 * dx * dt[START_TIME]) 30 | rf = ReducedFunctional(J, m) 31 | assert (1.0 - rf(u1)) < 1e-10 32 | 33 | J = Functional(u3 * dx * dt[FINISH_TIME]) 34 | rf = ReducedFunctional(J, m) 35 | assert (3.0 - rf(u1)) < 1e-10 36 | 37 | 38 | @pytest.mark.xfail 39 | def test_wrong_time_type(): 40 | dt["x"] 41 | 42 | 43 | @pytest.mark.xfail 44 | def test_valid_time_index(): 45 | dt[START_TIME] 46 | dt[FINISH_TIME] 47 | dt[0] 48 | dt[0.4] 49 | dt[0.4:0.8] 50 | -------------------------------------------------------------------------------- /tests/migration/tlm_burgers/test_tlm_burgers.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="compute_gradient_tlm is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/tlm_list_parameter/test_tlm_list_parameter.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="compute_gradient_tlm is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/tlm_scalar_parameter/test_tlm_scalar_parameter.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="compute_gradient_tlm is not implemented") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/tlm_simple/test_tlm_simple.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import sys 3 | pytest.importorskip("fenics") 4 | from os import path 5 | import subprocess 6 | 7 | @pytest.mark.xfail(reason="compute_gradient_tlm is not implemented") 8 | def test(request): 9 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 10 | test_dir = path.split(str(request.fspath))[0] 11 | test_cmd = ["mpirun", "-n", "2", "python", path.join(test_dir, test_file)] 12 | 13 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 14 | assert handle.wait() == 0 15 | -------------------------------------------------------------------------------- /tests/migration/tlm_simple/tlm_simple.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from numpy.random import random 3 | from dolfin import * 4 | from dolfin_adjoint import * 5 | 6 | mesh = UnitSquareMesh(4, 4) 7 | V = FunctionSpace(mesh, "CG", 3) 8 | dolfin.parameters["adjoint"]["record_all"] = True 9 | 10 | def main(ic, annotate=False): 11 | u = TrialFunction(V) 12 | v = TestFunction(V) 13 | 14 | bc = DirichletBC(V, "-1.0", "on_boundary") 15 | 16 | mass = inner(u, v)*dx 17 | soln = Function(V) 18 | 19 | solve(mass == action(mass, ic), soln, bc, annotate=annotate) 20 | return soln 21 | 22 | if __name__ == "__main__": 23 | 24 | ic = project(Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)", degree=4), V, name="InitialCondition") 25 | soln = main(ic, annotate=True) 26 | parameters["adjoint"]["stop_annotating"] = True 27 | 28 | perturbation_direction = Function(V) 29 | vec = perturbation_direction.vector() 30 | vec_size = vec.local_size() 31 | vec.set_local(random(vec_size)) 32 | vec.apply("") 33 | 34 | m = Control(ic, perturbation=perturbation_direction, value=ic) 35 | Jm = assemble(soln*soln*dx) 36 | J = Functional(soln*soln*dx) 37 | dJdm = compute_gradient_tlm(J, m, forget=False) 38 | 39 | def Jhat(ic): 40 | soln = main(ic, annotate=False) 41 | return assemble(soln*soln*dx) 42 | 43 | minconv = taylor_test(Jhat, m, Jm, dJdm) 44 | assert minconv > 1.9 45 | 46 | adj_reset() 47 | -------------------------------------------------------------------------------- /tests/migration/upwind/mesh.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/upwind/mesh.xml.gz -------------------------------------------------------------------------------- /tests/migration/upwind/test_upwind.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | def test(request): 11 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 12 | test_dir = path.split(str(request.fspath))[0] 13 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 14 | 15 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 16 | assert handle.wait() == 0 17 | -------------------------------------------------------------------------------- /tests/migration/upwind/velocity.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/upwind/velocity.xml.gz -------------------------------------------------------------------------------- /tests/migration/viscoelasticity/mesh_edgelength2.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/viscoelasticity/mesh_edgelength2.xml.gz -------------------------------------------------------------------------------- /tests/migration/viscoelasticity/mesh_edgelength4.xml.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dolfin-adjoint/pyadjoint/a4f940a7899809a65d43d9fb9e6049a70755a30a/tests/migration/viscoelasticity/mesh_edgelength4.xml.gz -------------------------------------------------------------------------------- /tests/migration/viscoelasticity/test_viscoelasticity.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.slow 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/viscoelasticity/timings/run_timings.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export PYTHONPATH=../../../.. 4 | 5 | for i in $(seq 1 5) 6 | do 7 | /usr/bin/time python $1 2>&1 8 | done 9 | -------------------------------------------------------------------------------- /tests/migration/zero_derivative/test_zero_derivative.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | from os import path 4 | 5 | import pytest 6 | 7 | pytest.importorskip("fenics") 8 | 9 | 10 | @pytest.mark.xfail(reason="How pyadjoint should handle zero derivatives is yet to be decided") 11 | def test(request): 12 | test_file = path.split(path.dirname(str(request.fspath)))[1] + ".py" 13 | test_dir = path.split(str(request.fspath))[0] 14 | test_cmd = [sys.executable, path.join(test_dir, test_file)] 15 | 16 | handle = subprocess.Popen(test_cmd, cwd=test_dir) 17 | assert handle.wait() == 0 18 | -------------------------------------------------------------------------------- /tests/migration/zero_derivative/zero_derivative.py: -------------------------------------------------------------------------------- 1 | from fenics import * 2 | from fenics_adjoint import * 3 | 4 | mesh = UnitIntervalMesh(10) 5 | V = FunctionSpace(mesh, "CG", 1) 6 | 7 | # Set up controls 8 | a = Constant(0) 9 | b = Constant(1) 10 | ctrls = [Control(a), Control(b)] 11 | 12 | # Evaluate forward model and functional. Note that only b is being used - and c 13 | # never show up 14 | x = project(b, V, annotate=True) 15 | J = assemble(x**2*dx) 16 | 17 | # Compute gradient 18 | dJ = compute_gradient(J, ctrls) 19 | assert dJ[0] is None 20 | assert dJ[1] is not None 21 | 22 | # For zero derivatives, return a dolfin object rather None 23 | # TODO: Is this something to consider for pyadjoint? 24 | parameters["adjoint"]["allow_zero_derivatives"] = True 25 | dJ = compute_gradient(J, ctrls) 26 | assert dJ[0] is not None 27 | assert dJ[1] is not None 28 | assert float(dJ[0]) == 0.0 29 | 30 | -------------------------------------------------------------------------------- /tests/pyadjoint/test_enlisting.py: -------------------------------------------------------------------------------- 1 | from pyadjoint import * 2 | 3 | def test_compute_gradient(): 4 | a = AdjFloat(1.0) 5 | b = AdjFloat(2.0) 6 | 7 | J = a*b 8 | c = Control(a) 9 | assert isinstance(compute_gradient(J, c), AdjFloat) 10 | assert isinstance(compute_gradient(J, [c]), list) 11 | 12 | 13 | def test_reduced_functional(): 14 | a = AdjFloat(1.0) 15 | b = AdjFloat(2.0) 16 | 17 | J = a*b 18 | c = Control(a) 19 | Jhat = ReducedFunctional(J, c) 20 | assert isinstance(Jhat.derivative(), AdjFloat) 21 | 22 | Jhat = ReducedFunctional(J, [c]) 23 | assert isinstance(Jhat.derivative(), list) 24 | 25 | -------------------------------------------------------------------------------- /tests/pyadjoint/test_numpy.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from pyadjoint import * 3 | from numpy_adjoint import * 4 | 5 | 6 | def test_ndarray_getitem_single(): 7 | a = create_overloaded_object(np.array([-2.0])) 8 | J = ReducedFunctional(a[0], Control(a)) 9 | dJ = J.derivative() 10 | assert dJ == 1.0 11 | -------------------------------------------------------------------------------- /tests/pyadjoint/test_placeholder.py: -------------------------------------------------------------------------------- 1 | from pyadjoint import * 2 | from pyadjoint.placeholder import Placeholder 3 | 4 | 5 | def test_simple(): 6 | a = AdjFloat(2.0) 7 | b = AdjFloat(3.0) 8 | 9 | c = a*b 10 | d = AdjFloat(5.0) 11 | p = Placeholder(d) 12 | e = c*d 13 | 14 | Jhat = ReducedFunctional(e, Control(a)) 15 | assert Jhat(2.0) == e 16 | p.set_value(e) 17 | res = Jhat(2.0) 18 | assert res != e 19 | assert res == e*6 20 | assert Jhat(2.0) == e * 6 ** 2 21 | assert Jhat(2.0) == e * 6 ** 3 22 | assert Jhat(2.0) == e * 6 ** 4 23 | # The functional changed after last evaluation so the derivative is with the new placeholder value: 24 | assert Jhat.derivative() == 0.5 * e * 6 ** 5 25 | 26 | Jhat.optimize_tape() 27 | assert Jhat(2.0) == e * 6 ** 5 28 | p.set_value(a) 29 | assert Jhat(2.0) == 2.0 * 6 30 | assert Jhat.derivative() == 0.5 * 2.0 * 6 31 | assert Jhat(5.0) == 5.0 * 15 32 | assert Jhat.derivative() == 15 33 | --------------------------------------------------------------------------------