Design of an optimum control solution for inverter based generation integrated to a weak power grid