Sun, 04 Oct 2026 13:02:26 -0500
Various revisions:
* Proper line integrals,
* Boundary condition implementation fixes
* Higher simulation resolution for experiments
* Related parameter adjustments
* Updates related to updated pointsource_algs
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | # Laser and mirrors convection-diffusion experiment with unknown diffusivity and convectivity; |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | # Includes `generic_setup` for the known case as well. |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | # |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | import numpy as np |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | from dolfinx import fem |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
8 | from measures import DiscreteMeasure_2_f64 |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
9 | from numpy.linalg import norm |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
10 | from pointsource_pde import Problem, RegTerm |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
11 | from pointsource_pde.compose import ( |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
12 | ComposeFnWithOperator, |
| 12 | 13 | ScaleAuxStepHack, |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
14 | SumOfSeparableFunctions, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
15 | ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
16 | from pointsource_pde.convection_diffusion import ( |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
17 | BoxedQuadraticRegularisation, |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
18 | ConvectionDiffusion, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
19 | PlotFactory, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
20 | XBound, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
21 | ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | from pointsource_pde.laser_sampling import LaserSampling |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | from pointsource_pde.quadratic_dataterm import QuadraticDataTerm |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | |
| 12 | 25 | from dolfinx_access import eval_Function_f64 |
| 26 | ||
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | # Give name to the problem |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
28 | name = "laser_and_mirrors_aux" |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
29 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
30 | # Fine-tune algorithms |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | alg_finetune = { |
| 12 | 32 | "tau0": 0.02, |
| 33 | "sigmap0": 0.0001, | |
| 34 | "tautheta": 0.07, | |
| 35 | "gradv_lipest_mult": 1, | |
| 36 | "transport_tolerance": 1e5, | |
| 37 | "inner_method": "Exact", | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
38 | "merge": True, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
39 | "fitness_merging": True, |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
40 | "merge_radius": 0.1, |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
41 | "merge_interp": False, |
| 12 | 42 | "scaling_heuristic": None, |
| 43 | "extra_weight_optimisation_steps": None, | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
44 | } |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
45 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | algorithm_overrides = { |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | "RadonFB": alg_finetune, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
48 | "RadonSlidingFB": alg_finetune, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
49 | "RadonPDPS": alg_finetune, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
50 | "RadonSlidingPDPS": alg_finetune, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
51 | } |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
52 | |
| 12 | 53 | reg_params = { |
| 54 | "alpha": 2e-3, | |
| 55 | "alpha_k": 60, | |
| 56 | "alpha_c": 0.06, | |
| 57 | "c_noise": 0.02, | |
| 58 | "k_noise": 0.02, | |
| 59 | "seed": 2393, | |
| 60 | "true_init": False, | |
| 61 | "true_target": False, | |
| 62 | "noise": 0.01, | |
| 63 | "sim_oversample": 2, | |
| 64 | "sim_oversample_time": 2, | |
| 65 | "scale_cstep": 300, | |
| 66 | "do_non_sliding": True, | |
| 67 | } | |
| 68 | ||
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
69 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
70 | # Setup shared between experiment with known/unknown conductivity and diffusivity |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
71 | def generic_setup( |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
72 | prefix, |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
73 | simple=True, |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
74 | rng=None, |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
75 | μ_true=DiscreteMeasure_2_f64( |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
76 | [([0.1, 0.3], 0.08), ([0.4, 0.25], 0.05), ([0.25, 0.13], 0.06)] |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
77 | ), |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
78 | k=0.01, |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
79 | r=0.5, |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
80 | θ=30 * np.pi / 180, |
| 12 | 81 | c_noise=reg_params["c_noise"], |
| 82 | k_noise=reg_params["k_noise"], | |
| 83 | noise=reg_params["noise"], | |
| 84 | sim_oversample=reg_params["sim_oversample"], | |
| 85 | sim_oversample_time=reg_params["sim_oversample_time"], | |
| 86 | scale_cstep=reg_params["scale_cstep"], | |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
87 | ): |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
88 | |
| 12 | 89 | u0 = lambda x: 0.0 * x[1] |
| 90 | g = lambda x: 0.0 * x[1] | |
| 91 | domain_dims = [[0, 0.5], [0, 0.5]] | |
| 92 | num_steps = 50 | |
| 93 | res = 32 | |
| 94 | ||
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
95 | # Create convection-diffusion PDE |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
96 | pde = ConvectionDiffusion( |
| 12 | 97 | u0=u0, |
| 98 | g=g, | |
| 99 | domain_dims=domain_dims, | |
| 100 | num_steps=num_steps, | |
| 101 | nx=res, | |
| 102 | ny=res, | |
| 103 | ) | |
| 104 | ||
| 105 | # Create convection-diffusion PDE for simulation, to avoid inverse crimes | |
| 106 | pde_sim = ConvectionDiffusion( | |
| 107 | u0=u0, | |
| 108 | g=g, | |
| 109 | domain_dims=domain_dims, | |
| 110 | # num_steps=(sim_oversample-1) * (num_steps + 1) + num_steps, | |
| 111 | num_steps=sim_oversample_time * (num_steps - 1) + 1, | |
| 112 | nx=res * sim_oversample, | |
| 113 | ny=res * sim_oversample, | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
114 | ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
115 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
116 | # Set up lasers and mirrors |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
117 | lasers = [ |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
118 | np.array([0.1, 0.1]), |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
119 | np.array([0.1, 0.4]), |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
120 | np.array([0.4, 0.1]), |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
121 | np.array([0.4, 0.1]), |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
122 | ] |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
123 | mirrors_per_side = 10 |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
124 | x0, x1, y0, y1 = ( |
| 12 | 125 | pde.domain_dims[0][0], |
| 126 | pde.domain_dims[0][1], | |
| 127 | pde.domain_dims[1][0], | |
| 128 | pde.domain_dims[1][1], | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
129 | ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
130 | beams = [ |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
131 | (src, np.array(dst)) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
132 | for src in lasers |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
133 | for i in range(0, mirrors_per_side) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
134 | for z in [(i + 1) / (mirrors_per_side + 1)] |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
135 | for dst in [ |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
136 | [z * (x1 - x0) + x0, y0], |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
137 | [z * (x1 - x0) + x0, y1], |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
138 | [x0, z * (y1 - y0) + y0], |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
139 | [x1, z * (y1 - y0) + y0], |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
140 | ] |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
141 | ] |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
142 | L_i = LaserSampling(pde.V, pde.domain, pde.nx, pde.ny, beams) |
| 12 | 143 | L_i_sim = LaserSampling(pde_sim.V, pde_sim.domain, pde_sim.nx, pde_sim.ny, beams) |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
144 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
145 | v0 = 1.0 |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
146 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
147 | if simple: |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
148 | # Scalar convectivity and diffusivity |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
149 | c1 = r * np.cos(θ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
150 | c2 = r * np.sin(θ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
151 | # k = 0.001 # high wind |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
152 | print("True aux variables: k = %f, c1 = %f, c2 = %f" % (k, c1, c2)) |
| 12 | 153 | k_sim = k |
| 154 | c1_sim = c1 | |
| 155 | c2_sim = c2 | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
156 | else: |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
157 | # Convectivity and diffusivity fields |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
158 | def velocity_x(x): |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
159 | r = np.sqrt(x[0] ** 2 + x[1] ** 2 + 1e-10) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
160 | return v0 * x[0] / r |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
161 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
162 | def velocity_y(x): |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
163 | r = np.sqrt(x[0] ** 2 + x[1] ** 2 + 1e-10) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
164 | return v0 * x[1] / r |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
165 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
166 | c1 = fem.Function(pde.V) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
167 | c1.interpolate(velocity_x) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
168 | c2 = fem.Function(pde.V) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
169 | c2.interpolate(velocity_y) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
170 | k = fem.Function(pde.V) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
171 | k0 = 1.0 |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
172 | k.interpolate(lambda x: k0 + 0.2 * np.sin(5 * x[0]) * np.cos(5 * x[1])) # k is |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
173 | |
| 12 | 174 | c1_sim = fem.Function(pde_sim.V) |
| 175 | c1_sim.interpolate(velocity_x) | |
| 176 | c2_sim = fem.Function(pde_sim.V) | |
| 177 | c2_sim.interpolate(velocity_y) | |
| 178 | k_sim = fem.Function(pde_sim.V) | |
| 179 | k0 = 1.0 | |
| 180 | k_sim.interpolate( | |
| 181 | lambda x: k0 + 0.2 * np.sin(5 * x[0]) * np.cos(5 * x[1]) | |
| 182 | ) # k is | |
| 183 | ||
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
184 | auxtrue = (k, (c1, c2)) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
185 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
186 | # Simulate measurements |
| 12 | 187 | u_n_list_sim = pde_sim.apply((μ_true, (k_sim, (c1_sim, c2_sim)))) |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
188 | u_n_list = pde.apply((μ_true, (k, (c1, c2)))) |
| 12 | 189 | |
| 190 | b_true = [L_i_sim.apply(u) for u in u_n_list_sim[0::sim_oversample_time]] | |
| 191 | ||
| 192 | # Add noise | |
| 193 | level_k = abs(k) | |
| 194 | level_c = r | |
| 195 | k_noisy = max(0.001, relnoise(k, k_noise, rng, level=level_k)) | |
| 196 | c1_noisy, c2_noisy = relnoise((c1, c2), c_noise, rng, level=level_c) | |
| 197 | aux_noisy = (k_noisy, (c1_noisy, c2_noisy)) | |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
198 | b = [ |
| 12 | 199 | b_i + L_i_sim.noise(noise * norm(b_i) / np.sqrt(np.size(b_i)), rng) |
| 200 | for b_i in b_true | |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
201 | ] |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
202 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
203 | # Estimate bounds on domains and values |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
204 | dat_bound = 2 * max([norm(b_i) for b_i in b]) |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
205 | μ_bound = 1.3 * sum([alpha for _point, alpha in μ_true.iter_padded()]) |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
206 | pde.xbound = XBound(mu_dual=μ_bound, k_min=k * 0.9, k_max=k * 1.1, c_Linf=r * 0.9) |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
207 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
208 | # Create data term |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
209 | dat = ComposeFnWithOperator( |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
210 | SumOfSeparableFunctions( |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
211 | [ |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
212 | QuadraticDataTerm( |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
213 | L_i, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
214 | data, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
215 | xbound=dat_bound, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
216 | b_norm=norm(b), |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
217 | λ=pde.dt / len(beams), |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
218 | ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
219 | for data in b |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
220 | ], |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
221 | ), |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
222 | pde, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
223 | ) |
| 12 | 224 | if scale_cstep is not None: |
| 225 | dat = ComposeFnWithOperator(dat, ScaleAuxStepHack((1.0, scale_cstep))) | |
| 226 | ||
|
10
0c9e19020217
Update to pointsource_algs 3-part curvature_bound_components
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
227 | dat.curvature_bound_components = lambda: (None, None, None) |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
228 | |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
229 | # Save true and initial data |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
230 | plot_factory = PlotFactory(pde) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
231 | plotter = plot_factory.produce(prefix) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
232 | ω, _ = dat.diff((DiscreteMeasure_2_f64([]), auxtrue)) |
| 12 | 233 | ω_true, (dk, (dc1, dc2)) = dat.diff((μ_true, auxtrue)) |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
234 | np.savez_compressed( |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
235 | "%s/omega_0.npz" % prefix, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
236 | true_mu=[[point[0], point[1], alpha] for point, alpha in μ_true.iter_padded()], |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
237 | omega0=ω.x.array, |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
238 | true_omega=ω_true.x.array, |
| 12 | 239 | k_true=k, |
| 240 | c1_true=c1, | |
| 241 | c2_true=c2, | |
| 242 | k_noisy=k_noisy, | |
| 243 | c1_noisy=c1_noisy, | |
| 244 | c2_noisy=c2_noisy, | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
245 | true_u_n_list_array=[u_n_list[i].x.array for i in plotter.plot_frames], |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
246 | ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
247 | |
| 12 | 248 | delta = 0.001 * k |
| 249 | dk_est = ( | |
| 250 | dat.apply((μ_true, (k + delta, (c1, c2)))) - dat.apply((μ_true, auxtrue)) | |
| 251 | ) / delta | |
| 252 | print("k-diff-test:", dk, ", ", dk_est) | |
| 253 | delta = 0.001 * c1 | |
| 254 | dc1_est = ( | |
| 255 | dat.apply((μ_true, (k, (c1 + delta, c2)))) - dat.apply((μ_true, auxtrue)) | |
| 256 | ) / delta | |
| 257 | print("c1-diff-test:", dc1, ", ", dc1_est) | |
| 258 | delta = 0.001 | |
| 259 | μ_perturb = DiscreteMeasure_2_f64( | |
| 260 | [([0.1, 0.3], 0.08 + delta), ([0.4, 0.25], 0.05), ([0.25, 0.13], 0.06)] | |
| 261 | ) | |
| 262 | dcμ_est = (dat.apply((μ_perturb, auxtrue)) - dat.apply((μ_true, auxtrue))) / delta | |
| 263 | print( | |
| 264 | "μ-diff-test:", | |
| 265 | eval_Function_f64(ω_true._cpp_object, [0.1, 0.3, 0.0]), | |
| 266 | ", ", | |
| 267 | dcμ_est, | |
| 268 | ) | |
| 269 | ||
| 270 | return dat, auxtrue, aux_noisy, μ_bound, μ_true, plot_factory, pde | |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
271 | |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
272 | |
|
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
273 | def relnoise(v, σ, rng, level=None): |
| 12 | 274 | if isinstance(v, float): |
| 275 | return float(rng.normal(v, σ * (abs(v) if level is None else level))) | |
| 276 | else: | |
| 277 | return map( | |
| 278 | float, rng.normal(v, σ * (np.linalg.norm(v) if level is None else level)) | |
| 279 | ) | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
280 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
281 | |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
282 | # Setup the problem |
| 12 | 283 | def setup( |
| 284 | prefix, | |
| 285 | alpha=reg_params["alpha"], | |
| 286 | alpha_k=reg_params["alpha_k"], | |
| 287 | alpha_c=reg_params["alpha_c"], | |
| 288 | c_noise=reg_params["c_noise"], | |
| 289 | k_noise=reg_params["k_noise"], | |
| 290 | noise=reg_params["noise"], | |
| 291 | seed=reg_params["seed"], | |
| 292 | sim_oversample=reg_params["sim_oversample"], | |
| 293 | sim_oversample_time=reg_params["sim_oversample_time"], | |
| 294 | true_init=False, | |
| 295 | true_target=False, | |
| 296 | scale_cstep=reg_params["scale_cstep"], | |
| 297 | ): | |
| 298 | rng = np.random.default_rng(seed=seed) | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
299 | |
| 12 | 300 | dat, auxtrue, aux0, _μ_bound, μ_true, plot_factory, pde = generic_setup( |
| 301 | prefix, | |
| 302 | rng=rng, | |
| 303 | c_noise=c_noise, | |
| 304 | k_noise=k_noise, | |
| 305 | noise=noise, | |
| 306 | sim_oversample=sim_oversample, | |
| 307 | sim_oversample_time=sim_oversample_time, | |
| 308 | scale_cstep=scale_cstep, | |
| 309 | ) | |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
310 | |
| 12 | 311 | # Override Lipschitz parameter to use direct step length choice |
| 312 | dat.override_lipschitz = 1.0 | |
| 313 | dat.override_lipschitz_pair = (0.0, 0.0) | |
| 314 | ||
| 315 | reg = RegTerm.NonnegRadon(alpha) | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
316 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
317 | μ0 = DiscreteMeasure_2_f64([]) |
| 12 | 318 | |
| 319 | print("aux init ", aux0) | |
| 320 | aux = BoxedQuadraticRegularisation( | |
| 321 | (0.005, -1.0), | |
| 322 | (1.0, 1.0), | |
| 323 | (alpha_k, alpha_c), | |
| 324 | auxtrue if true_target else aux0, | |
| 325 | (1, scale_cstep) if scale_cstep is not None else None, | |
|
3
c3a4f4bb87f7
Dolfin update, fixes, additional experiment, build instructions.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
326 | ) |
| 12 | 327 | print("Initial value:", dat.apply((μ0, aux0)) + aux.apply(aux0)) |
| 328 | print("Value at true μ:", dat.apply((μ_true, auxtrue)) + aux.apply(auxtrue)) | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
329 | |
| 12 | 330 | return Problem( |
| 331 | dat, | |
| 332 | reg, | |
| 333 | aux, | |
| 334 | auxtrue if true_init else aux0, | |
| 335 | μ0, | |
| 336 | plot_factory=plot_factory, | |
| 337 | domain=pde.domain_dims, | |
| 338 | ) |