Source code for openmdao.test_suite.components.quad_implicit

import openmdao.api as om


[docs]class QuadraticComp(om.ImplicitComponent): """ A Simple Implicit Component representing a Quadratic Equation. R(a, b, c, x) = ax^2 + bx + c Solution via Quadratic Formula: x = (-b + sqrt(b^2 - 4ac)) / 2a """
[docs] def setup(self): self.add_input('a', val=1.) self.add_input('b', val=1.) self.add_input('c', val=1.) self.add_output('x', val=0.)
[docs] def setup_partials(self): self.declare_partials(of='x', wrt='*')
[docs] def apply_nonlinear(self, inputs, outputs, residuals): a = inputs['a'] b = inputs['b'] c = inputs['c'] x = outputs['x'] residuals['x'] = a * x ** 2 + b * x + c
[docs] def solve_nonlinear(self, inputs, outputs): a = inputs['a'] b = inputs['b'] c = inputs['c'] outputs['x'] = (-b + (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
[docs] def linearize(self, inputs, outputs, partials): a = inputs['a'] b = inputs['b'] x = outputs['x'] partials['x', 'a'] = x ** 2 partials['x', 'b'] = x partials['x', 'c'] = 1.0 partials['x', 'x'] = 2 * a * x + b self.inv_jac = 1.0 / (2 * a * x + b)