# Diff of /branches/symbolic_from_3470/escript/py_src/nonlinearPDE.py

revision 3810 by caltinay, Wed Feb 8 00:31:12 2012 UTC revision 3811 by gross, Wed Feb 8 01:13:28 2012 UTC
# Line 63  class NonlinearPDE(object): Line 63  class NonlinearPDE(object):
63      Constraints for the solution prescribe the value of the solution at certain      Constraints for the solution prescribe the value of the solution at certain
64      locations in the domain. They have the form      locations in the domain. They have the form
65
66      *u=r* where *q>0*      *u=u0* where *q>0*
67
68      *r* and *q* are each scalar where *q* is the characteristic function      *u0* and *q* are each scalar where *q* is the characteristic function
69      defining where the constraint is applied. The constraints override any      defining where the constraint is applied. The constraints override any
70      other condition set by the PDE or the boundary condition.      other condition set by the PDE or the boundary condition.
71
# Line 80  class NonlinearPDE(object): Line 80  class NonlinearPDE(object):
80
81          u = Symbol('u', dim=dom.getDim())          u = Symbol('u', dim=dom.getDim())
82          p = NonlinearPDE(dom, u)          p = NonlinearPDE(dom, u)
84          v = p.getSolution(u=0.)          v = p.getSolution(u=0.)
85      """      """
86
# Line 94  class NonlinearPDE(object): Line 94  class NonlinearPDE(object):
94          :type u: `Symbol`          :type u: `Symbol`
95          :param debug: if True debug information is printed          :param debug: if True debug information is printed
96          """          """
97            self.__COEFFICIENTS = [ "X", "X_reduced", "Y", "Y_reduced", "y", "y_reduced", "y_contact", "y_contact_reduced", "q" ]
98          self.coeffs={}          self.coeffs={}
99          self._u=u          self._u=u
100          self._debug=debug          self._debug=debug
101            self.dim = domain.getDim()
102          if u.getRank()==0:          if u.getRank()==0:
103              numEquations=1              numEquations=1
104          else:          else:
# Line 112  class NonlinearPDE(object): Line 114  class NonlinearPDE(object):
114          :rtype: ``str``          :rtype: ``str``
115          """          """
116          return "<NonlinearPDE %d>"%id(self)          return "<NonlinearPDE %d>"%id(self)
117
118        def getSolutionSymbol(self):
119            """
120            Returns the symbol of the PDE solution
121
122            :return: returns the symbol of the PDE solution
123            :rtype: `Symbol`
124            """
125            return self._u
126
127      def trace(self, text):      def trace(self, text):
128          """          """
129          Prints the text message if debug mode is switched on.          Prints the text message if debug mode is switched on.
# Line 192  class NonlinearPDE(object): Line 203  class NonlinearPDE(object):
203
204          self.trace("Final error after %d iterations: %g"%(n,util.Lsup(u_new)))          self.trace("Final error after %d iterations: %g"%(n,util.Lsup(u_new)))
205          return u_new          return u_new
206
207        def getNumSolutions(self):
208            """
209            Returns the number of the solution components
210            :rtype: ``int``
211            """
212            s=self._u.getShape()
213            if len(s) > 0:
214              return s[0]
215            else:
216              return 1
217
218        def getShapeOfCoefficient(self,name):
219            """
220            Returns the shape of the coefficient ``name``.
221
222      def getCoefficient(self, name):          :param name: name of the coefficient enquired
223            :type name: ``string``
224            :return: the shape of the coefficient ``name``
225            :rtype: ``tuple`` of ``int``
226            :raise IllegalCoefficient: if ``name`` is not a coefficient of the PDE
227            """
228            numSol=self.getNumSolutions()
229            dim = self.dim
230            if name=="X" or name=="X_reduced":
231               if numSol > 1:
232                  return (numSol,dim)
233               else:
234              return (dim,)
235            elif name=="q" :
236               if numSol > 1:
237                  return (numSol,)
238               else:
239              return ()
240            elif name=="Y" or name=="Y_reduced":
241               if numSol > 1:
242                  return (numSol,)
243               else:
244                  return ()
245            elif name=="y" or name=="y_reduced":
246                if numSol > 1:
247                   return (numSol,)
248                else:
249               return ()
250            elif name=="y_contact" or name=="y_contact_reduced":
251                if numSol > 1:
252                   return (numSol,)
253                else:
254               return ()
255            else:
256                raise IllegalCoefficient("Attempt to request unknown coefficient %s"%name)
257
258        def createCoefficient(self, name):
259          """          """
260          Returns the value of the coefficient ``name``.          create a new coefficient ``name`` as Symbol
261
262          :param name: name of the coefficient requested          :param name: name of the coefficient requested
263          :type name: ``string``          :type name: ``string``
# Line 203  class NonlinearPDE(object): Line 265  class NonlinearPDE(object):
265          :rtype: `Symbol`          :rtype: `Symbol`
266          :raise IllegalCoefficient: if ``name`` is not a coefficient of the PDE          :raise IllegalCoefficient: if ``name`` is not a coefficient of the PDE
267          """          """
268          if self.hasCoefficient(name):          s=self.getShapeOfCoefficient(name)
269              return self.coeffs[name]          return Symbol(name, s)
270          raise IllegalCoefficient("Illegal coefficient %s requested."%name)
271        def getCoefficient(self, name):
def hasCoefficient(self, name):
272          """          """
273          Returns True if ``name`` is the name of a coefficient.          Returns the value of the coefficient ``name`` as Symbol
274
275          :param name: name of the coefficient enquired          :param name: name of the coefficient requested
276          :type name: ``string``          :type name: ``string``
277          :return: True if ``name`` is the name of a coefficient of the PDE,          :return: the value of the coefficient
278                   False otherwise          :rtype: `Symbol`
279          :rtype: ``bool``          :raise IllegalCoefficient: if ``name`` is not a coefficient of the PDE

280          """          """
281          return self.coeffs.has_key(name)          if self.coeffs.has_key(name):
282             return self.coeffs[name]
283        else:
284             return IllegalCoefficient("Attempt to request undefined coefficient %s"%name)
285
286
287      def setValue(self,**coefficients):      def setValue(self,**coefficients):
288          """          """
289          Sets new values to one or more coefficients.          Sets new values to one or more coefficients.
# Line 234  class NonlinearPDE(object): Line 298  class NonlinearPDE(object):
298          :keyword y_contact: value for coefficient ``y_contact``          :keyword y_contact: value for coefficient ``y_contact``
299          :type y_contact: `Symbol` or any type that can be cast to a `Data`          :type y_contact: `Symbol` or any type that can be cast to a `Data`
300                           object                           object
:keyword r: values prescribed to the solution at the locations of
constraints
:type r: `Symbol` or any type that can be cast to a `Data` object
301          :keyword q: mask for location of constraints          :keyword q: mask for location of constraints
302          :type q: `Symbol` or any type that can be cast to a `Data` object          :type q: `Symbol` or any type that can be cast to a `Data` object
303
# Line 244  class NonlinearPDE(object): Line 305  class NonlinearPDE(object):
305          :raise IllegalCoefficientValue: if a supplied coefficient value has an          :raise IllegalCoefficientValue: if a supplied coefficient value has an
306                                          invalid shape                                          invalid shape
307          """          """
308
309          u=self._u          u=self._u
310          for name,val in coefficients.iteritems():          for name,val in coefficients.iteritems():
311              shape=util.getShape(val)              shape=util.getShape(val)
312                if not shape == self.getShapeOfCoefficient(name):
313                IllegalCoefficientValue("%s has shape %s but must have shape %d"%(name, self.getShapeOfCoefficient(name), shape ) )
314              rank=len(shape)              rank=len(shape)
315              if name=="X" or name=="X_reduced":              if name=="X" or name=="X_reduced":
# Line 405  class NonlinearPDE(object): Line 469  class NonlinearPDE(object):
469                  if rank != u.getRank():                  if rank != u.getRank():
470                      raise IllegalCoefficientValue("q must have rank %d"%u.getRank())                      raise IllegalCoefficientValue("q must have rank %d"%u.getRank())
471                  self.coeffs['q']=val                  self.coeffs['q']=val
elif name=="r":
if rank != u.getRank():
raise IllegalCoefficientValue("r must have rank %d"%u.getRank())
self.coeffs['r']=val
472              else:              else:
473                  raise IllegalCoefficient("Attempt to set unknown coefficient %s"%name)                  raise IllegalCoefficient("Attempt to set unknown coefficient %s"%name)
474

Legend:
 Removed from v.3810 changed lines Added in v.3811