Re: How to represent strange terms in FiPy
Hi, Thanks for the previous help. Regarding your previous answer on those expressions, I checked the three different methods: my original version, and both of your suggestions, and I see that they match. Nevertheless, I am taking your suggestion. I have a couple of follow-ups regarding a newly-discovered issues. You suggested that I look at my original system of equations, and it turns out there was an error in what I originally presented. I want critique on my implementation. As a reminder, the cell variables are n, T, and Z, with D taking a couple of forms, as a function of Z and (possibly) the first derivative of Z. The numerical constants are a, b, c, c_n, c_T, alpha, epsilon, mu, zeta, and Z_S. Here's the system in LaTeX: \begin{align} \frac{\partial n}{\partial t} \,&=\, \frac{\partial}{\partial x}\left(D \, \frac{\partial n}{\partial x}\right) \\ \frac{\partial}{\partial t}(n\,T) \,&=\, \frac{\partial}{\partial x}\left(\frac{D\,n}{\zeta} \, \frac{\partial T}{\partial x}\right) + \frac{\partial}{\partial x}\left(D\,T \, \frac{\partial n}{\partial x}\right) \\ \epsilon\,\frac{\partial Z}{\partial t} \,&=\, \mu\,\frac{\partial Z}{\partial x^2} + \frac{c_n\,T}{n^2}\,\frac{\partial n}{\partial x} + \frac{c_T}{n}\,\frac{\partial T}{\partial x} + a + b(Z - Z_S) + c(Z - Z_S)^3 \\ D(Z^\prime) \,&=\, 1 + \frac{1}{1 + (Z^\prime)^2} \end{align} And here's the way I've set the code, although it gives me strange behavior at the edge of the mesh: # - PDE Declarations -- # Diffusivity D Diffusivity = 1 + 1 / (1.0 + alpha*numerix.dot(Z.grad, Z.grad)) # Density Equation n density_equation = TransientTerm(var=density) == DiffusionTerm(coeff=Diffusivity, var=density) # Temperature Equation T temp_equation = TransientTerm(coeff=density, var=temperature) == DiffusionTerm(coeff=((Diffusivity*density) / zeta), var=temperature) + DiffusionTerm(coeff=Diffusivity*temperature, var=density) # Z Equation G = a + b*(Z - Z_S) + c*(Z - Z_S)**3 S_Z = (c_T / density)*temperature.grad[0] + (c_n*temperature / density**2)*density.grad[0] + G Z_equation = TransientTerm(coeff=epsilon, var=Z) == DiffusionTerm(coeff=mu, var=Z) + S_Z # Fully-Coupled Equation full_equation = density_equation & temp_equation & Z_equation # - As it turns out, rewriting the temperature equation with a simpler transient term is possible, but it gives another strange term: \begin{equation} \frac{D\,T}{n} \, \frac{\partial^2 n}{\partial x^2} \end{equation} Is there any way of reliably writing this, or should I stick to using the forms of the equations stated above? My final question regards to a boundary condition with a second derivative. What is the proper way to implement the following? $\frac{\partial^2 Z}{\partial x^2} \,=\, 0$ Currently, I have it in as: Z.faceGrad.divergence.constrain(0.0, mesh.facesLeft) Once again, thank you for the help, Kevin On 3 January 2018 at 20:04, Guyer, Jonathan E. Dr. (Fed) < jonathan.gu...@nist.gov> wrote: > Sorry for the slow reply. > > Expressions like [(1.0/density),] and [(temperature / density**2),] > unfortunately don't work reliably in FiPy. They might work, but I wouldn't > count on it. > > The issue is that FiPy sees a Python list that holds the instantaneous > value of a rank-0 CelVariable, rather than seeing a rank-1 CellVariable > that can change. This is down to Python syntax that FiPy doesn't get a > chance reinterpret. > > I believe you'll have better luck with numerix.dot((1.0/density) * [[1.]], > temperature.grad) and numerix.dot((temperature / density**2) * [[1]], > density.grad). > > You could as readily write (1.0/density) * temperature.grad[0] and > (temperature / density**2) * density.grad[0]. It depends on what you really > mean, which usually is clearer in higher dimensions. > > > On Dec 14, 2017, at 12:26 PM, Kevin Blondino> wrote: > > > > Hi, > > > > I've adjusted the system a little bit, and I wanted some verification > and more criticism. > > > > I took your suggestions about (Z.grad)**2 and changed it to > numerix.dot(Z.grad, Z.grad) to give me a scalar. > > > > In addition, the terms that could not be easily written as convection > terms: > > \begin{align} > > \frac{1}{n} \frac{\partial T}{\partial x} + \frac{T}{n^2} > \frac{\partial n}{\partial x} > > \end{align} > > > > I wrote them by making the coefficients a vector and then dotting with > the gradient: > > numerix.dot([(1.0/density),], temperature.grad) + > numerix.dot([(temperature / density**2), ], density.grad) > > > > Are these things a fair / reasonable thing to do? I have looked over my > system in the most original form and it seems there is no way to show them > in a ConvectionTerm or ImplicitSourceTerm. > > > > I apologize if the code is a little difficult to read. > > > > Thanks, > > Kevin > > > > On 12 December 2017 at 16:37, Guyer, Jonathan E. Dr. (Fed) < >
Re: How to represent strange terms in FiPy
Sorry for the slow reply. Expressions like [(1.0/density),] and [(temperature / density**2),] unfortunately don't work reliably in FiPy. They might work, but I wouldn't count on it. The issue is that FiPy sees a Python list that holds the instantaneous value of a rank-0 CelVariable, rather than seeing a rank-1 CellVariable that can change. This is down to Python syntax that FiPy doesn't get a chance reinterpret. I believe you'll have better luck with numerix.dot((1.0/density) * [[1.]], temperature.grad) and numerix.dot((temperature / density**2) * [[1]], density.grad). You could as readily write (1.0/density) * temperature.grad[0] and (temperature / density**2) * density.grad[0]. It depends on what you really mean, which usually is clearer in higher dimensions. > On Dec 14, 2017, at 12:26 PM, Kevin Blondinowrote: > > Hi, > > I've adjusted the system a little bit, and I wanted some verification and > more criticism. > > I took your suggestions about (Z.grad)**2 and changed it to > numerix.dot(Z.grad, Z.grad) to give me a scalar. > > In addition, the terms that could not be easily written as convection terms: > \begin{align} > \frac{1}{n} \frac{\partial T}{\partial x} + \frac{T}{n^2} > \frac{\partial n}{\partial x} > \end{align} > > I wrote them by making the coefficients a vector and then dotting with the > gradient: > numerix.dot([(1.0/density),], temperature.grad) + > numerix.dot([(temperature / density**2), ], density.grad) > > Are these things a fair / reasonable thing to do? I have looked over my > system in the most original form and it seems there is no way to show them in > a ConvectionTerm or ImplicitSourceTerm. > > I apologize if the code is a little difficult to read. > > Thanks, > Kevin > > On 12 December 2017 at 16:37, Guyer, Jonathan E. Dr. (Fed) > wrote: > Kevin - > > - Should I model the last term in the temperature (T) equations as a > convection term or explicit source term? > > The best coupling would be obtained by treating this term as a convection > term on n with a velocity proportional to \partial T/\partial x. > Unfortunately, (D/n) sits outside the derivatives and so FiPy has no implicit > way to represent it. I think you're stuck with the source. > > - What about the "S_Z" term in the Z equation? > > Again, as written, these are just sources. > > - Is it required that I declare diffusivity (D) as a cell variable, as it is > now? > > In order to write an equation to solve for D, it must be a CellVariable, > however, there's no advantage in adding this as a solution equation. I would > just define D as a function of Z. As such, it's better to define it as a > FaceVariable for use in the DiffusionTerms. > > Note: > > The expressions Z*(Z.grad) and (Z.grad)**2 amount to multiplying a scalar by > a vector and a vector by a vector, but D needs to be scalar. I recommend > working out what your equations would be using either divergence and gradient > operators or Einstein notation, as premature introduction of d/dx in 1D > equations leads to equations that are difficult to debug and render properly. > > You may find that when you go back to the source equations in general > vectorial form that the sources in the T and Z equations are amenable to > being written as convection terms or implicit sources, which would be good. > > - Jon > > > > On Dec 11, 2017, at 2:42 PM, Kevin Blondino wrote: > > > > Hello, > > > > I have a somewhat complicated system of highly nonlinear, 1D PDE's which I > > am having trouble modeling. The issue is that I am not sure how to > > represent certain terms. > > > > Here is a the following LaTeX code for the system, as to make sure there is > > no ambiguity: > > \begin{align} > > \frac{\partial n}{\partial t} \,&=\, \frac{\partial}{\partial > > x}\left(D \frac{\partial n}{\partial x}\right) \\ > > \frac{\partial T}{\partial t} \,&=\, \frac{\partial}{\partial > > x}\left(D \frac{\partial T}{\partial x}\right) + > > \frac{D}{n}\,\frac{\partial n}{\partial x}\,\frac{\partial T}{\partial x} \\ > > \frac{\partial Z}{\partial t} \,&=\, \frac{\partial}{\partial > > x}\left(D \frac{\partial Z}{\partial x}\right) + > > \frac{T}{n^2}\frac{\partial n}{\partial x} + \frac{1}{n}\frac{\partial > > T}{\partial x} + G(Z) \\ > > G(Z) \,&=\, a + b(Z - Z_S) + c(Z - Z_S)^3 \\ > > D \,&=\, 1 + \frac{1}{1 + Z^2 + Z\cdot\frac{\partial Z}{\partial x} + > > \left(\frac{\partial Z}{\partial x}\right)^2} > > \end{align} > > > > I have purposefully squelched many coefficients as possible to make simple. > > > > The main code I have been using to model them is as follows: > > > > # - Variable Declarations - > > density = CellVariable(name=r"$n$", mesh=mesh, hasOld=True) > > > > temperature = CellVariable(name=r"$T$", mesh=mesh, hasOld=True) > > > >
Re: How to represent strange terms in FiPy
Hi, I've adjusted the system a little bit, and I wanted some verification and more criticism. I took your suggestions about (Z.grad)**2 and changed it to numerix.dot(Z.grad, Z.grad) to give me a scalar. In addition, the terms that could not be easily written as convection terms: \begin{align} \frac{1}{n} \frac{\partial T}{\partial x} + \frac{T}{n^2} \frac{\partial n}{\partial x} \end{align} I wrote them by making the coefficients a vector and then dotting with the gradient: numerix.dot([(1.0/density),], temperature.grad) + numerix.dot([(temperature / density**2), ], density.grad) Are these things a fair / reasonable thing to do? I have looked over my system in the most original form and it seems there is no way to show them in a ConvectionTerm or ImplicitSourceTerm. I apologize if the code is a little difficult to read. Thanks, Kevin On 12 December 2017 at 16:37, Guyer, Jonathan E. Dr. (Fed) < jonathan.gu...@nist.gov> wrote: > Kevin - > > - Should I model the last term in the temperature (T) equations as a > convection term or explicit source term? > > The best coupling would be obtained by treating this term as a convection > term on n with a velocity proportional to \partial T/\partial x. > Unfortunately, (D/n) sits outside the derivatives and so FiPy has no > implicit way to represent it. I think you're stuck with the source. > > - What about the "S_Z" term in the Z equation? > > Again, as written, these are just sources. > > - Is it required that I declare diffusivity (D) as a cell variable, as it > is now? > > In order to write an equation to solve for D, it must be a CellVariable, > however, there's no advantage in adding this as a solution equation. I > would just define D as a function of Z. As such, it's better to define it > as a FaceVariable for use in the DiffusionTerms. > > Note: > > The expressions Z*(Z.grad) and (Z.grad)**2 amount to multiplying a scalar > by a vector and a vector by a vector, but D needs to be scalar. I recommend > working out what your equations would be using either divergence and > gradient operators or Einstein notation, as premature introduction of d/dx > in 1D equations leads to equations that are difficult to debug and render > properly. > > You may find that when you go back to the source equations in general > vectorial form that the sources in the T and Z equations are amenable to > being written as convection terms or implicit sources, which would be good. > > - Jon > > > > On Dec 11, 2017, at 2:42 PM, Kevin Blondino> wrote: > > > > Hello, > > > > I have a somewhat complicated system of highly nonlinear, 1D PDE's which > I am having trouble modeling. The issue is that I am not sure how to > represent certain terms. > > > > Here is a the following LaTeX code for the system, as to make sure there > is no ambiguity: > > \begin{align} > > \frac{\partial n}{\partial t} \,&=\, \frac{\partial}{\partial > x}\left(D \frac{\partial n}{\partial x}\right) \\ > > \frac{\partial T}{\partial t} \,&=\, \frac{\partial}{\partial > x}\left(D \frac{\partial T}{\partial x}\right) + > \frac{D}{n}\,\frac{\partial n}{\partial x}\,\frac{\partial T}{\partial x} \\ > > \frac{\partial Z}{\partial t} \,&=\, \frac{\partial}{\partial > x}\left(D \frac{\partial Z}{\partial x}\right) + > \frac{T}{n^2}\frac{\partial n}{\partial x} + \frac{1}{n}\frac{\partial > T}{\partial x} + G(Z) \\ > > G(Z) \,&=\, a + b(Z - Z_S) + c(Z - Z_S)^3 \\ > > D \,&=\, 1 + \frac{1}{1 + Z^2 + Z\cdot\frac{\partial Z}{\partial > x} + \left(\frac{\partial Z}{\partial x}\right)^2} > > \end{align} > > > > I have purposefully squelched many coefficients as possible to make > simple. > > > > The main code I have been using to model them is as follows: > > > > # - Variable Declarations - > > density = CellVariable(name=r"$n$", mesh=mesh, hasOld=True) > > > > temperature = CellVariable(name=r"$T$", mesh=mesh, hasOld=True) > > > > Z = CellVariable(name=r"$Z$", mesh=mesh, hasOld=True) > > > > ## Diffusivity > > D = CellVariable(name=r"$D$", mesh=mesh, hasOld=True) > > > > ... (initial and boundary conditions are here) ... > > > > # - PDE Declarations -- > > # Diffusivity Equation (D) > > diffusivity_equation = ImplicitSourceTerm(coeff=1.0, var=D) == 1.0 > + 1.0 / (1 + Z**2 + Z*(Z.grad) + (Z.grad)**2) > > > > > > # Density Equation (n) > > density_equation = TransientTerm(var=density) == > DiffusionTerm(coeff=D, var=density) > > > > > > # Temperature Equation (T) > > S_T = (D/density) * numerix.dot(density.grad,temperature.grad) # > ??? > > S_T_conv = ConvectionTerm(coeff=(D/density)*density.grad, > var=temperature) # ?? > > > > temp_equation = TransientTerm(var=temperature) == > DiffusionTerm(coeff=D, var=temperature) + S_T > > > > > > # Z Equation > > G = a + b*(Z - Z_S) +
Re: How to represent strange terms in FiPy
Kevin - - Should I model the last term in the temperature (T) equations as a convection term or explicit source term? The best coupling would be obtained by treating this term as a convection term on n with a velocity proportional to \partial T/\partial x. Unfortunately, (D/n) sits outside the derivatives and so FiPy has no implicit way to represent it. I think you're stuck with the source. - What about the "S_Z" term in the Z equation? Again, as written, these are just sources. - Is it required that I declare diffusivity (D) as a cell variable, as it is now? In order to write an equation to solve for D, it must be a CellVariable, however, there's no advantage in adding this as a solution equation. I would just define D as a function of Z. As such, it's better to define it as a FaceVariable for use in the DiffusionTerms. Note: The expressions Z*(Z.grad) and (Z.grad)**2 amount to multiplying a scalar by a vector and a vector by a vector, but D needs to be scalar. I recommend working out what your equations would be using either divergence and gradient operators or Einstein notation, as premature introduction of d/dx in 1D equations leads to equations that are difficult to debug and render properly. You may find that when you go back to the source equations in general vectorial form that the sources in the T and Z equations are amenable to being written as convection terms or implicit sources, which would be good. - Jon > On Dec 11, 2017, at 2:42 PM, Kevin Blondinowrote: > > Hello, > > I have a somewhat complicated system of highly nonlinear, 1D PDE's which I am > having trouble modeling. The issue is that I am not sure how to represent > certain terms. > > Here is a the following LaTeX code for the system, as to make sure there is > no ambiguity: > \begin{align} > \frac{\partial n}{\partial t} \,&=\, \frac{\partial}{\partial x}\left(D > \frac{\partial n}{\partial x}\right) \\ > \frac{\partial T}{\partial t} \,&=\, \frac{\partial}{\partial x}\left(D > \frac{\partial T}{\partial x}\right) + \frac{D}{n}\,\frac{\partial > n}{\partial x}\,\frac{\partial T}{\partial x} \\ > \frac{\partial Z}{\partial t} \,&=\, \frac{\partial}{\partial x}\left(D > \frac{\partial Z}{\partial x}\right) + \frac{T}{n^2}\frac{\partial > n}{\partial x} + \frac{1}{n}\frac{\partial T}{\partial x} + G(Z) \\ > G(Z) \,&=\, a + b(Z - Z_S) + c(Z - Z_S)^3 \\ > D \,&=\, 1 + \frac{1}{1 + Z^2 + Z\cdot\frac{\partial Z}{\partial x} + > \left(\frac{\partial Z}{\partial x}\right)^2} > \end{align} > > I have purposefully squelched many coefficients as possible to make simple. > > The main code I have been using to model them is as follows: > > # - Variable Declarations - > density = CellVariable(name=r"$n$", mesh=mesh, hasOld=True) > > temperature = CellVariable(name=r"$T$", mesh=mesh, hasOld=True) > > Z = CellVariable(name=r"$Z$", mesh=mesh, hasOld=True) > > ## Diffusivity > D = CellVariable(name=r"$D$", mesh=mesh, hasOld=True) > > ... (initial and boundary conditions are here) ... > > # - PDE Declarations -- > # Diffusivity Equation (D) > diffusivity_equation = ImplicitSourceTerm(coeff=1.0, var=D) == 1.0 + > 1.0 / (1 + Z**2 + Z*(Z.grad) + (Z.grad)**2) > > > # Density Equation (n) > density_equation = TransientTerm(var=density) == DiffusionTerm(coeff=D, > var=density) > > > # Temperature Equation (T) > S_T = (D/density) * numerix.dot(density.grad,temperature.grad) # ??? > S_T_conv = ConvectionTerm(coeff=(D/density)*density.grad, > var=temperature) # ?? > > temp_equation = TransientTerm(var=temperature) == > DiffusionTerm(coeff=D, var=temperature) + S_T > > > # Z Equation > G = a + b*(Z - Z_S) + c*(Z - Z_S)**3 > S_Z = G + (1 / density)*temperature.grad.mag + (temperature / > density**2)*density.grad.mag # ??? > Z_equation = TransientTerm(coeff=1.0, var=Z) == DiffusionTerm(coeff=D, > var=Z) + S_Z > > # Fully-Coupled Equation > full_equation = density_equation & temp_equation & Z_equation & > diffusivity_equation > > Should I model the last term in the temperature (T) equations as a convection > term or explicit source term? > What about the "S_Z" term in the Z equation? > Is it required that I declare diffusivity (D) as a cell variable, as it is > now? > > I appreciate any help that can be given. > > Thank you, > Kevin > ___ > fipy mailing list > fipy@nist.gov > http://www.ctcms.nist.gov/fipy > [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ] ___ fipy mailing list fipy@nist.gov http://www.ctcms.nist.gov/fipy [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]
How to represent strange terms in FiPy
Hello, I have a somewhat complicated system of highly nonlinear, 1D PDE's which I am having trouble modeling. The issue is that I am not sure how to represent certain terms. Here is a the following LaTeX code for the system, as to make sure there is no ambiguity: \begin{align} \frac{\partial n}{\partial t} \,&=\, \frac{\partial}{\partial x}\left(D \frac{\partial n}{\partial x}\right) \\ \frac{\partial T}{\partial t} \,&=\, \frac{\partial}{\partial x}\left(D \frac{\partial T}{\partial x}\right) + \frac{D}{n}\,\frac{\partial n}{\partial x}\,\frac{\partial T}{\partial x} \\ \frac{\partial Z}{\partial t} \,&=\, \frac{\partial}{\partial x}\left(D \frac{\partial Z}{\partial x}\right) + \frac{T}{n^2}\frac{\partial n}{\partial x} + \frac{1}{n}\frac{\partial T}{\partial x} + G(Z) \\ G(Z) \,&=\, a + b(Z - Z_S) + c(Z - Z_S)^3 \\ D \,&=\, 1 + \frac{1}{1 + Z^2 + Z\cdot\frac{\partial Z}{\partial x} + \left(\frac{\partial Z}{\partial x}\right)^2} \end{align} I have purposefully squelched many coefficients as possible to make simple. The main code I have been using to model them is as follows: # - Variable Declarations - density = CellVariable(name=r"$n$", mesh=mesh, hasOld=True) temperature = CellVariable(name=r"$T$", mesh=mesh, hasOld=True) Z = CellVariable(name=r"$Z$", mesh=mesh, hasOld=True) ## Diffusivity D = CellVariable(name=r"$D$", mesh=mesh, hasOld=True) ... (initial and boundary conditions are here) ... # - PDE Declarations -- # Diffusivity Equation (D) diffusivity_equation = ImplicitSourceTerm(coeff=1.0, var=D) == 1.0 + 1.0 / (1 + Z**2 + Z*(Z.grad) + (Z.grad)**2) # Density Equation (n) density_equation = TransientTerm(var=density) == DiffusionTerm(coeff=D, var=density) # Temperature Equation (T) S_T = (D/density) * numerix.dot(density.grad,temperature.grad) # ??? S_T_conv = ConvectionTerm(coeff=(D/density)*density.grad, var=temperature) # ?? temp_equation = TransientTerm(var=temperature) == DiffusionTerm(coeff=D, var=temperature) + S_T # Z Equation G = a + b*(Z - Z_S) + c*(Z - Z_S)**3 S_Z = G + (1 / density)*temperature.grad.mag + (temperature / density**2)*density.grad.mag # ??? Z_equation = TransientTerm(coeff=1.0, var=Z) == DiffusionTerm(coeff=D, var=Z) + S_Z # Fully-Coupled Equation full_equation = density_equation & temp_equation & Z_equation & diffusivity_equation Should I model the last term in the temperature (T) equations as a convection term or explicit source term? What about the "S_Z" term in the Z equation? Is it required that I declare diffusivity (D) as a cell variable, as it is now? I appreciate any help that can be given. Thank you, Kevin ___ fipy mailing list fipy@nist.gov http://www.ctcms.nist.gov/fipy [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]