# PLaSK User Manual

#### Page Contents

- Solver Class
  - `Solver`
  - Methods
  - Attributes
  - Descriptions
    - Method Details
      - `Solver.initialize()`
      - `Solver.invalidate()`
      - `Solver.load_xpl()`
    - Attribute Details
      - `Solver.id`
      - `Solver.initialized`

# Solver Class

class plask.Solver(name='')

  Base class for all solvers.

  Parameters:

    name – Solver name for its identification in logs.

  You should inherit this class if you are creating custom Python solvers in Python, which can read its configuration from the XPL file. Then you need to override the `load_xml()` method, which reads the configuration. If you override `on_initialize()` of `on_invalidate()` methods, they will be called once on the solver initialization/invalidation.

  Example

      class MySolver(Solver):

          def __init__(self, name=''):
              super().__init__(name)
              self.param = 0.
              self.geometry = None
              self.mesh = None
              self.workspace = None
              self.bc = plask.mesh.Rectangular2D.BoundaryConditions()

          def load_xpl(self, xpl, manager):
              for tag in xpl:
                  if tag == 'config':
                      self.param = tag.get('param', self.param)
                  elif tag == 'geometry':
                      self.geometry = tag.getitem(manager.geo, 'ref')
                  elif tag == 'mesh':
                      self.mesh = tag.getitem(manager.msh, 'ref')
                  elif tag == 'boundary':
                      self.bc.read_from_xpl(tag, manager)

          def on_initialize(self):
              self.workspace = zeros(1000.)

          def on_invalidate(self):
              self.workspace = None

          def run_computations(self):
              pass

  To make your solver visible in GUI, you must write the `solvers.yml` file and put it in the same directory as your data file.

  Example

      - solver: MySolver
        lib: mymodule
        category: local
        geometry: Cartesian2D
        mesh: Rectangular2D
        tags:
        - tag: config
          label: Solver Configuration
          help: Configuration of the effective model of p-n junction.
          attrs:
          - attr: param
            label: Parameter
            type: float
            unit: V
            help: Some voltage parameter.
        - bcond: boundary
          label: Something

## Methods

`initialize`() | Initialize solver.
`invalidate`() | Set the solver back to uninitialized state.
`load_xpl`(xpl, manager) | Load configuration from XPL reader.

## Attributes

`id` | Id of the solver object.
`initialized` | True if the solver has been initialized.

## Descriptions

### Method Details

Solver.initialize()

  Initialize solver.

  This method manually initialized the solver and sets `initialized` to True. Normally calling it is not necessary, as each solver automatically initializes itself when needed.

  Returns:

    solver `initialized` state prior to this method call.

  Return type:

    bool

Solver.invalidate()

  Set the solver back to uninitialized state.

  This method frees the memory allocated by the solver and sets `initialized` to False.

Solver.load_xpl(xpl, manager)

  Load configuration from XPL reader.

  This method should be overridden in custom Python solvers.

  Example

      def load_xpl(self, xpl, manager):
          for tag in xpl:
              if tag == 'config':
                  self.a = tag['a']
                  self.b = tag.get('b', 0)
                  if 'c' in tag:
                      self.c = tag['c']
              if tag == 'combined':
                  for subtag in tag:
                      if subtag == 'withtext':
                          self.data = subtag.attrs
                          # Text must be read last
                          self.text = subtag.text
              elif tag == 'geometry':
                  self.geometry = tag.getitem(manager.geo, 'ref')

### Attribute Details

Solver.id = <property object>

  Id of the solver object. (read only)

  Example

      >>> mysolver.id
      mysolver:category.type

Solver.initialized = <property object>

  True if the solver has been initialized. (read only)

  Solvers usually get initialized at the beginning of the computations. You can clean the initialization state and free the memory by calling the `invalidate()` method.