.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery_examples/pervious_surround_optimization/make_pervious_surround.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_examples_pervious_surround_optimization_make_pervious_surround.py: 3D pervious surround ==================== .. GENERATED FROM PYTHON SOURCE LINES 5-57 .. code-block:: Python import numpy as np from HydrOpTop.Functions import p_Weighted_Head_Gradient, Volume_Percentage from HydrOpTop.Materials import Log_SIMP from HydrOpTop.Filters import Density_Filter from HydrOpTop.Crafter import Steady_State_Crafter from HydrOpTop.Solvers import PFLOTRAN if __name__ == "__main__": #1. Create a timer to time the optimization t = time.time() #2. create PFLOTRAN simulation object pflotranin = "pflotran.in" sim = PFLOTRAN(pflotranin) #3. Parametrize hydraulic conductivity #get cell ids in the region to optimize and parametrize permeability #same name than in pflotran input file pit_ids = sim.get_region_ids("Pit") perm = Log_SIMP(cell_ids_to_parametrize=pit_ids, property_name="PERMEABILITY", bounds=[5e-14,1e-10], power=3) #4. Define the cost function and constraint #define cost function as sum of the head in the pit cf = p_Weighted_Head_Gradient(pit_ids, invert_weighting=True) #define maximum volume constrains max_vol = Volume_Percentage(pit_ids) max_vol.constraint_tol = 0.2 #max volume percentage of 20% #5. Define filter filter = Density_Filter(6) #search neighbors in a 6 m radius #6. Craft optimization problem #i.e. create function to optimize, initiate IO array in classes... crafted_problem = Steady_State_Crafter(cf, sim, [perm], [max_vol], [filter]) #7. Output #Define the output behavior (output density parameter every 2 iterations in vtu format) crafted_problem.IO.output_every_iteration(5) crafted_problem.IO.define_output_format("vtu") #8. Degine initial guess (homogeneous) and optimize p_ini = np.zeros(crafted_problem.get_problem_size(),dtype='f8') + 0.12 out = crafted_problem.optimize(optimizer="nlopt-mma", action="minimize", max_it=200, ftol=1e-8, initial_guess=p_ini) #9. Output the final optimized and filtered density parameter in a out.vtu file crafted_problem.IO.write_fields_to_file([out.p_opt_filtered], "./out.vtu", ["Filtered_density"], at_ids=pit_ids-1) print(f"Elapsed time: {time.time()-t} seconds") crafted_problem.IO.plot_convergence_history() .. _sphx_glr_download_gallery_examples_pervious_surround_optimization_make_pervious_surround.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: make_pervious_surround.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: make_pervious_surround.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_