Re: [firedrake] how to set up random initial conditions?
Hi Francis, You can set random initial conditions by setting the value of the data array on the function. E.g. f.dat.data[:] = np.random.randn(*f.dat.shape) Note that np.random.randn produces normally distributed values. Check out the contents of np.random to find other possibilities. Regards, David On Fri, 22 Apr 2016 at 19:45 Francis Poulin <fpoulin@uwaterloo.ca> wrote:
Hello,
I was thinking of taking my Quasi-Geostrophic demo that I recently submitted and change the initial conditions to be a random field. Then we can see the evolution of freely evolving 2D turbulence. Unfortunately, I am not sure how to pick random initial conditions in firedrake.
I found issue 514 that suggested doing the following:
import numpy as np
perm_array = np.random.randn(Nx, Ny, Nz)
I guess this works if you have a regular mesh. But if I have an irregular mesh then is there a way to define this for the mesh?
Cheers, Francis
Hi Francis and David,
From experience in using Firedrake and Uncertainty Quantification, I have found that using spatially correlated random initial condition for a field is better for some cases.
You can do this by solving a simple Helmholtz equation once with the forcing f as a random field, Np.random.normal(0,1,len(F.dat.data)) Many Thanks, Alastair Gregory Sent from my iPhone On 23 Apr 2016, at 11:32, David Ham <David.Ham@imperial.ac.uk<mailto:David.Ham@imperial.ac.uk>> wrote: Hi Francis, You can set random initial conditions by setting the value of the data array on the function. E.g. f.dat.data[:] = np.random.randn(*f.dat.shape) Note that np.random.randn produces normally distributed values. Check out the contents of np.random to find other possibilities. Regards, David On Fri, 22 Apr 2016 at 19:45 Francis Poulin <fpoulin@uwaterloo.ca<mailto:fpoulin@uwaterloo.ca>> wrote: Hello, I was thinking of taking my Quasi-Geostrophic demo that I recently submitted and change the initial conditions to be a random field. Then we can see the evolution of freely evolving 2D turbulence. Unfortunately, I am not sure how to pick random initial conditions in firedrake. I found issue 514 that suggested doing the following: import numpy as np perm_array = np.random.randn(Nx, Ny, Nz) I guess this works if you have a regular mesh. But if I have an irregular mesh then is there a way to define this for the mesh? Cheers, Francis _______________________________________________ firedrake mailing list firedrake@imperial.ac.uk<mailto:firedrake@imperial.ac.uk> https://mailman.ic.ac.uk/mailman/listinfo/firedrake
Hello, Thanks David and Alastair for the advice. I am sorry that I should have been more precise in terms of my confusion. If you wanted to see the same code that I put together (with lots of help from Colin I should add) you can go to the following, https://github.com/francispoulin/firedrake/blob/qg_demo/demos/quasigeostroph... Below are the lines where we define the function space and then define the initial conditions. Vdg = FunctionSpace(mesh,"DG",1) # DG elements for Potential Vorticity (PV) # Intial Conditions for PV q0 = Function(Vdg).interpolate(Expression("0.1*sin(x[0])*sin(x[1])")) I am really confused as to where the f.dat comes in. Sorry. I thought that if I wanted to define the initial conditions I should do it in terms of an expression but I don't imagine that even if I import random from numpy that random will be understood in expression. I guess my more specific question is how do I use random in an expression? Cheers, Francis ------------------ Francis Poulin Associate Professor Department of Applied Mathematics University of Waterloo email: fpoulin@uwaterloo.ca Web: https://uwaterloo.ca/poulin-research-group/ Telephone: +1 519 888 4567 x32637 ________________________________ From: firedrake-bounces@imperial.ac.uk [firedrake-bounces@imperial.ac.uk] on behalf of Gregory, Alastair C A [a.gregory14@imperial.ac.uk] Sent: Saturday, April 23, 2016 6:53 AM To: firedrake Subject: Re: [firedrake] how to set up random initial conditions? Hi Francis and David,
From experience in using Firedrake and Uncertainty Quantification, I have found that using spatially correlated random initial condition for a field is better for some cases.
You can do this by solving a simple Helmholtz equation once with the forcing f as a random field, Np.random.normal(0,1,len(F.dat.data)) Many Thanks, Alastair Gregory Sent from my iPhone On 23 Apr 2016, at 11:32, David Ham <David.Ham@imperial.ac.uk<mailto:David.Ham@imperial.ac.uk>> wrote: Hi Francis, You can set random initial conditions by setting the value of the data array on the function. E.g. f.dat.data[:] = np.random.randn(*f.dat.shape) Note that np.random.randn produces normally distributed values. Check out the contents of np.random to find other possibilities. Regards, David On Fri, 22 Apr 2016 at 19:45 Francis Poulin <fpoulin@uwaterloo.ca<mailto:fpoulin@uwaterloo.ca>> wrote: Hello, I was thinking of taking my Quasi-Geostrophic demo that I recently submitted and change the initial conditions to be a random field. Then we can see the evolution of freely evolving 2D turbulence. Unfortunately, I am not sure how to pick random initial conditions in firedrake. I found issue 514 that suggested doing the following: import numpy as np perm_array = np.random.randn(Nx, Ny, Nz) I guess this works if you have a regular mesh. But if I have an irregular mesh then is there a way to define this for the mesh? Cheers, Francis _______________________________________________ firedrake mailing list firedrake@imperial.ac.uk<mailto:firedrake@imperial.ac.uk> https://mailman.ic.ac.uk/mailman/listinfo/firedrake
Hi Francis, Thanks for the clarification. The f.dat.data just comes from accessing the basis coefficients in a different way to interpolating an expression. If you want to input the randomness via the expression instance then you can manipulate the code in the following way depending on what you want to. For example if you wanted to shift the initial condition in the X-axis by the normally distributed variable 'C' you do the following : q0=Function(Vdg).interpolate(Expression("0.1*(sin(x[0])+C)*sin(x[1])",C=np.random.normal(0,1,1))) I hope this is what you're looking for. Many Thanks, Ali Sent from my iPhone On 23 Apr 2016, at 23:23, Francis Poulin <fpoulin@uwaterloo.ca<mailto:fpoulin@uwaterloo.ca>> wrote: Hello, Thanks David and Alastair for the advice. I am sorry that I should have been more precise in terms of my confusion. If you wanted to see the same code that I put together (with lots of help from Colin I should add) you can go to the following, https://github.com/francispoulin/firedrake/blob/qg_demo/demos/quasigeostroph... Below are the lines where we define the function space and then define the initial conditions. Vdg = FunctionSpace(mesh,"DG",1) # DG elements for Potential Vorticity (PV) # Intial Conditions for PV q0 = Function(Vdg).interpolate(Expression("0.1*sin(x[0])*sin(x[1])")) I am really confused as to where the f.dat comes in. Sorry. I thought that if I wanted to define the initial conditions I should do it in terms of an expression but I don't imagine that even if I import random from numpy that random will be understood in expression. I guess my more specific question is how do I use random in an expression? Cheers, Francis ------------------ Francis Poulin Associate Professor Department of Applied Mathematics University of Waterloo email: fpoulin@uwaterloo.ca<mailto:fpoulin@uwaterloo.ca> Web: https://uwaterloo.ca/poulin-research-group/ Telephone: +1 519 888 4567 x32637 ________________________________ From: firedrake-bounces@imperial.ac.uk<mailto:firedrake-bounces@imperial.ac.uk> [firedrake-bounces@imperial.ac.uk<mailto:firedrake-bounces@imperial.ac.uk>] on behalf of Gregory, Alastair C A [a.gregory14@imperial.ac.uk<mailto:a.gregory14@imperial.ac.uk>] Sent: Saturday, April 23, 2016 6:53 AM To: firedrake Subject: Re: [firedrake] how to set up random initial conditions? Hi Francis and David,
From experience in using Firedrake and Uncertainty Quantification, I have found that using spatially correlated random initial condition for a field is better for some cases.
You can do this by solving a simple Helmholtz equation once with the forcing f as a random field, Np.random.normal(0,1,len(F.dat.data)) Many Thanks, Alastair Gregory Sent from my iPhone On 23 Apr 2016, at 11:32, David Ham <David.Ham@imperial.ac.uk<mailto:David.Ham@imperial.ac.uk>> wrote: Hi Francis, You can set random initial conditions by setting the value of the data array on the function. E.g. f.dat.data[:] = np.random.randn(*f.dat.shape) Note that np.random.randn produces normally distributed values. Check out the contents of np.random to find other possibilities. Regards, David On Fri, 22 Apr 2016 at 19:45 Francis Poulin <fpoulin@uwaterloo.ca<mailto:fpoulin@uwaterloo.ca>> wrote: Hello, I was thinking of taking my Quasi-Geostrophic demo that I recently submitted and change the initial conditions to be a random field. Then we can see the evolution of freely evolving 2D turbulence. Unfortunately, I am not sure how to pick random initial conditions in firedrake. I found issue 514 that suggested doing the following: import numpy as np perm_array = np.random.randn(Nx, Ny, Nz) I guess this works if you have a regular mesh. But if I have an irregular mesh then is there a way to define this for the mesh? Cheers, Francis _______________________________________________ firedrake mailing list firedrake@imperial.ac.uk<mailto:firedrake@imperial.ac.uk> https://mailman.ic.ac.uk/mailman/listinfo/firedrake _______________________________________________ firedrake mailing list firedrake@imperial.ac.uk<mailto:firedrake@imperial.ac.uk> https://mailman.ic.ac.uk/mailman/listinfo/firedrake
Hello Ali, Thanks a lot. That is very helpful. It is very nice how you can access numpy functions through this syntax. Very clean. I tired what you suggested and I believe that the example you suggested shifted the whole solution by a random constant. If I wanted to have a different random variable at every position, then i presume I need C=np.random.normal(0,1,len(something)) where something is the size of the mesh. In a previous email David suggested using *f.dat.shape I presume that f.dat is based on the mesh but I don't know how to extract that information from the mesh. Again, sorry for not being able to figure this out on my own but your help is greatly appreciated. Francis ------------------ Francis Poulin Associate Professor Department of Applied Mathematics University of Waterloo email: fpoulin@uwaterloo.ca Web: https://uwaterloo.ca/poulin-research-group/ Telephone: +1 519 888 4567 x32637 ________________________________ From: firedrake-bounces@imperial.ac.uk [firedrake-bounces@imperial.ac.uk] on behalf of Gregory, Alastair C A [a.gregory14@imperial.ac.uk] Sent: Saturday, April 23, 2016 6:42 PM To: firedrake Subject: Re: [firedrake] how to set up random initial conditions? Hi Francis, Thanks for the clarification. The f.dat.data just comes from accessing the basis coefficients in a different way to interpolating an expression. If you want to input the randomness via the expression instance then you can manipulate the code in the following way depending on what you want to. For example if you wanted to shift the initial condition in the X-axis by the normally distributed variable 'C' you do the following : q0=Function(Vdg).interpolate(Expression("0.1*(sin(x[0])+C)*sin(x[1])",C=np.random.normal(0,1,1))) I hope this is what you're looking for. Many Thanks, Ali Sent from my iPhone On 23 Apr 2016, at 23:23, Francis Poulin <fpoulin@uwaterloo.ca<mailto:fpoulin@uwaterloo.ca>> wrote: Hello, Thanks David and Alastair for the advice. I am sorry that I should have been more precise in terms of my confusion. If you wanted to see the same code that I put together (with lots of help from Colin I should add) you can go to the following, https://github.com/francispoulin/firedrake/blob/qg_demo/demos/quasigeostroph... Below are the lines where we define the function space and then define the initial conditions. Vdg = FunctionSpace(mesh,"DG",1) # DG elements for Potential Vorticity (PV) # Intial Conditions for PV q0 = Function(Vdg).interpolate(Expression("0.1*sin(x[0])*sin(x[1])")) I am really confused as to where the f.dat comes in. Sorry. I thought that if I wanted to define the initial conditions I should do it in terms of an expression but I don't imagine that even if I import random from numpy that random will be understood in expression. I guess my more specific question is how do I use random in an expression? Cheers, Francis ------------------ Francis Poulin Associate Professor Department of Applied Mathematics University of Waterloo email: fpoulin@uwaterloo.ca<mailto:fpoulin@uwaterloo.ca> Web: https://uwaterloo.ca/poulin-research-group/ Telephone: +1 519 888 4567 x32637 ________________________________ From: firedrake-bounces@imperial.ac.uk<mailto:firedrake-bounces@imperial.ac.uk> [firedrake-bounces@imperial.ac.uk<mailto:firedrake-bounces@imperial.ac.uk>] on behalf of Gregory, Alastair C A [a.gregory14@imperial.ac.uk<mailto:a.gregory14@imperial.ac.uk>] Sent: Saturday, April 23, 2016 6:53 AM To: firedrake Subject: Re: [firedrake] how to set up random initial conditions? Hi Francis and David,
From experience in using Firedrake and Uncertainty Quantification, I have found that using spatially correlated random initial condition for a field is better for some cases.
You can do this by solving a simple Helmholtz equation once with the forcing f as a random field, Np.random.normal(0,1,len(F.dat.data)) Many Thanks, Alastair Gregory Sent from my iPhone On 23 Apr 2016, at 11:32, David Ham <David.Ham@imperial.ac.uk<mailto:David.Ham@imperial.ac.uk>> wrote: Hi Francis, You can set random initial conditions by setting the value of the data array on the function. E.g. f.dat.data[:] = np.random.randn(*f.dat.shape) Note that np.random.randn produces normally distributed values. Check out the contents of np.random to find other possibilities. Regards, David On Fri, 22 Apr 2016 at 19:45 Francis Poulin <fpoulin@uwaterloo.ca<mailto:fpoulin@uwaterloo.ca>> wrote: Hello, I was thinking of taking my Quasi-Geostrophic demo that I recently submitted and change the initial conditions to be a random field. Then we can see the evolution of freely evolving 2D turbulence. Unfortunately, I am not sure how to pick random initial conditions in firedrake. I found issue 514 that suggested doing the following: import numpy as np perm_array = np.random.randn(Nx, Ny, Nz) I guess this works if you have a regular mesh. But if I have an irregular mesh then is there a way to define this for the mesh? Cheers, Francis _______________________________________________ firedrake mailing list firedrake@imperial.ac.uk<mailto:firedrake@imperial.ac.uk> https://mailman.ic.ac.uk/mailman/listinfo/firedrake _______________________________________________ firedrake mailing list firedrake@imperial.ac.uk<mailto:firedrake@imperial.ac.uk> https://mailman.ic.ac.uk/mailman/listinfo/firedrake
participants (3)
-
David Ham
-
Francis Poulin
-
Gregory, Alastair C A