Skip to content

Commit

Permalink
feat: assure no negative Ti and Td (#872)
Browse files Browse the repository at this point in the history
* feat: assure no negative Ti and Td

* feat: also check for Kp
test: added tests
  • Loading branch information
asmfstatoil authored Dec 4, 2023
1 parent b94fbd7 commit 94d3785
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package neqsim.processSimulation.controllerDevice;

import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import neqsim.processSimulation.measurementDevice.MeasurementDeviceInterface;
import neqsim.util.NamedBaseClass;

Expand All @@ -20,6 +22,7 @@
*/
public class ControllerDeviceBaseClass extends NamedBaseClass implements ControllerDeviceInterface {
private static final long serialVersionUID = 1000;
static Logger logger = LogManager.getLogger(ControllerDeviceBaseClass.class);

/**
* Unique identifier of which solve/run call was last called successfully.
Expand Down Expand Up @@ -48,7 +51,18 @@ public class ControllerDeviceBaseClass extends NamedBaseClass implements Control
* </p>
*/
public ControllerDeviceBaseClass() {
super("controller");
this("controller");
}

/**
* <p>
* Constructor for ControllerDeviceBaseClass.
* </p>
*
* @param name Name of PID controller object
*/
public ControllerDeviceBaseClass(String name) {
super(name);
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -77,8 +91,9 @@ public void runTransient(double initResponse, double dt, UUID id) {
transmitter.getMeasuredPercentValue() - (controllerSetPoint - transmitter.getMinimumValue())
/ (transmitter.getMaximumValue() - transmitter.getMinimumValue()) * 100;

if (Ti > 0)
if (Ti != 0) {
TintValue += Kp / Ti * error * dt;
}
double TderivValue = Kp * Td * (error - oldError) / dt;
response = initResponse + propConstant * (Kp * error + TintValue + TderivValue);
// System.out.println("error " + error + " %");
Expand Down Expand Up @@ -142,7 +157,11 @@ public double getKp() {
* @param Kp Proportional gain of PID controller
*/
public void setKp(double Kp) {
this.Kp = Kp;
if (Kp >= 0) {
this.Kp = Kp;
} else {
logger.warn("Negative Kp is not allowed. Use setReverseActing.");
}
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -172,7 +191,11 @@ public double getTi() {
* @param Ti Integral time in seconds
*/
public void setTi(double Ti) {
this.Ti = Ti;
if (Ti >= 0) {
this.Ti = Ti;
} else {
logger.warn("Negative Ti is not allowed.");
}
}

/**
Expand All @@ -194,6 +217,10 @@ public double getTd() {
* @param Td Derivative time in seconds
*/
public void setTd(double Td) {
this.Td = Td;
if (Td >= 0) {
this.Td = Td;
} else {
logger.warn("Negative Td is not allowed.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package neqsim.processSimulation.controllerDevice;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ControllerDeviceBaseClassTest {

static ControllerDeviceBaseClass c;

@BeforeAll
static void setUp() {
c = new ControllerDeviceBaseClass("testPID");
}

@Test
void testSetControllerParameters() {
double zero = 0;
double positive = 9.9;
double negative = -0.1;

c.setControllerParameters(zero, zero, zero);

Assertions.assertEquals(zero, c.getKp());
Assertions.assertEquals(zero, c.getTd());
Assertions.assertEquals(zero, c.getTi());

c.setControllerParameters(positive, positive, positive);
Assertions.assertEquals(positive, c.getKp());
Assertions.assertEquals(positive, c.getTd());
Assertions.assertEquals(positive, c.getTi());

c.setControllerParameters(negative, positive, positive);
Assertions.assertEquals(positive, c.getKp());
Assertions.assertEquals(positive, c.getTd());
Assertions.assertEquals(positive, c.getTi());

c.setControllerParameters(positive, negative, positive);
Assertions.assertEquals(positive, c.getKp());
Assertions.assertEquals(positive, c.getTd());
Assertions.assertEquals(positive, c.getTi());

c.setControllerParameters(positive, positive, negative);
Assertions.assertEquals(positive, c.getKp());
Assertions.assertEquals(positive, c.getTd());
Assertions.assertEquals(positive, c.getTi());
}

@Test
void testGetKp() {
double kp = c.getKp();
Assertions.assertEquals(c.getKp(), kp);
}

@Test
void testGetTd() {
double td = c.getTd();
Assertions.assertEquals(c.getTd(), td);
}

@Test
void testGetTi() {
double ti = c.getTi();
Assertions.assertEquals(c.getTi(), ti);
}

@Test
void testGetUnit() {
String unit = c.getUnit();
Assertions.assertEquals(c.getUnit(), unit);
}

@Test
void testIsReverseActing() {
boolean isReverse = c.isReverseActing();
Assertions.assertEquals(isReverse, c.isReverseActing());
}

@Test
void testSetReverseActing() {
boolean testValue = true;
boolean oldValue = c.isReverseActing();
c.setReverseActing(testValue);
Assertions.assertEquals(testValue, c.isReverseActing());
c.setReverseActing(oldValue);
Assertions.assertEquals(oldValue, c.isReverseActing());
}

@Test
void testSetUnit() {
String testUnit = "test";
String oldUnit = c.getUnit();
c.setUnit(testUnit);
Assertions.assertEquals(testUnit, c.getUnit());
c.setUnit(oldUnit);
Assertions.assertEquals(oldUnit, c.getUnit());
}
}

0 comments on commit 94d3785

Please sign in to comment.