The following functions should be defined in U Function and the codes are performed a simulation by the CoLink solver.
• void simInitializes(CSys *CS)
• void simInitialConditions(CSys *CS)
• void simUpdateParameters(CSys *C)
• void simJacobians(CSys *CS)
• void simDerivatives(CSys *CS)
• void simOutputs(CSys *CS)
• void simTerminates(CSys *CS)
CSys class
The CSys class is the opened class in the CoLink solver. You can set or get desired values by the member function of CSys.
U Function Operation Flow
The CoLink solver is divided into three big processes – the initialization process, the simulation loop process, and the termination process. The defined functions by the user are called by the each process.
Initialization |
simInitializes |
Required |
simInitialConditions |
Required | |
Simulation Loop |
simUpdateParameters |
Optional |
simJacobians |
Optional | |
simDerivatives |
Optional (If there are the differential equations, it should certainly exist.) | |
simOutputs |
Required | |
Termination |
CSubTerminate |
Optional |
• Initialization Process
Before the first simulation loop, U Function block is initialized by CoLink solver to call simInitializes and simInitialConditions.
• Works in simInitializes
o SetNumInputPort: Defines the number of input port.
o SetInputPortSize: Defines the size of each input port.
o SetNumOutputPort: Defines the number of output port.
o SetNumOutputPortSize: Defines the size of output port.
o SetNumDiffStates: Defines the number of differential equations.
o SetSampleTime: Defines the sample time.
extern "C" __declspec(dllexport) void simInitializes( CSys *CS ) { CS->SetNumInputPort(1); CS->SetInputPortSize(0,1); CS->SetNumOutputPort(1); CS->SetOutputPortSize(0,1); CS->SetNumDiffStates(3); CS->SetSampleTime(-1.0); |
• Works in simInitialConditions
Define initial conditions of differential states.
o GetNumDiffStates: Get the number of differential states.
o GetDiffStates: Get the data array of differential sates.
o SetJacobianMethod: Define the method to calculate Jacobian of differential states.
o SetNumRWork: Define double data array to use in U Function.
o SetNumIWork: Define integer data array to use in U Function.
o SetPassword: Define a password to lock a DLL file. The user can use this DLL file if the password inputted in U Function block is matched with this password.
extern "C" __declspec(dllexport) void simInitialConditions( CSys *CS ) { double* Beta; int nDiff; nDiff = CS->GetNumDiffStates(); Beta = CS->GetDiffStates(); for( int i=0 ; i<nDiff ; i++ ) { Beta[i] = 0.0; } CS->SetJacobianMethod(2); // 1:FDM 2:Anlaytical CS->SetNumRWork(nDiff*nDiff); CS->SetNumIWork(0); CS->WriteToMessageFile("U Function Sample Code.\n"); } |
• Simulation Loop Process
In simulation loop process, simUpdateParameters, simJacobians, simDerivatives, and simOutputs are called by the CoLink solver.
• Works in simUpdateParameters
o SetParameter: Changes the input parameters in the U Function block.
o SetParameterWithName: Changes the input parameters in the U Function block.
If there are the defined functions, simUpdateParameters is called.
If there are not the defined functions, simUpdateParameters is skipped.
extern "C" __declspec(dllexport) void simUpdateParameters( CSys *CS ) { double simtime; simtime = CS->GetSimTime(); if( simtime > 1.0 ) CS->SetParameter(0,0.2); } |
• Works in simJacobians
o SetJacobian: Sets the values of Jaconbian matrix
o simJacobians is called by the implicit integrator(G-alpha or DDASSL).
If there are not the defined functions, solver calculates the Jacobian matrix using FDM method.
extern "C" __declspec(dllexport) void simJacobians( CSys *CS ) { double Ra,La,Ke,Inertia; double *Jac; int nDiff;
Ke = CS->GetParameter(0); Ra = CS->GetParameter(2); La = CS->GetParameter(3); Inertia = CS->GetParameter(4); Jac = CS->GetRWorkPtr(); Jac[0] = -Ra/La; Jac[1] = 0.0; Jac[2] = -Ke/La; Jac[3] = 0.0; Jac[4] = 0.0; Jac[5] = 1.0; Jac[6] = Ke/Inertia; Jac[7] = 0.0; Jac[8] = 0.0; CS->SetJacobian(Jac); } |
• Works in simDerivatives
Define the derivative values of differential states.
o GetDiffStates: Get the data point of differential states.
o GetDotDiffStates: Get the data point of derivatives of differential states.
If there are the diffential equations in U Function, it should be defined.
extern "C" __declspec(dllexport) void simDerivatives( CSys *CS ) { double Ra,La,Ke,Kt,angle,omega,Ia,Va,E,Te,Tfric,Td; double Inertia,Bm,Tf,TL; double simtime; int npole; double *Uin; double *dBeta,*Beta; // Parameter Ke = CS->GetParameter(0); npole = (int) CS->GetParameter(1); Ra = CS->GetParameter(2); La = CS->GetParameter(3); Inertia = CS->GetParameter(4); Bm = CS->GetParameter(5); Tf = CS->GetParameter(6); // Input Uin = CS->GetInputPortData(0); // Diff eqn. Beta = CS->GetDiffStates(); dBeta = CS->GetDotDiffStates(); TL = 0.0; Va = Uin[0]; Ia = Beta[0]; angle = Beta[1]; omega = Beta[2]; // Motor eqn. E = Ke*omega; Kt = Ke; Te = Kt*Ia; Tfric = (omega>=0?1:-1)*(Bm*fabs(omega)+Tf); Td = Te-Tfric; // Result dBeta[0] = (Va-Ra*Ia-E)/La; dBeta[1] = omega; dBeta[2] = 1/Inertia*(Td - TL); Output_Te = Te; } |
• Works in simOutputs:
Sets the values of output of U Function block.
o GetOutputPortData: Gets the data pointer of output port.
extern "C" __declspec(dllexport) void simOutputs( CSys *CS ) { double *Yout;
Yout = CS->GetOutputPortData(0); Yout[0] = Output_Te; } |
• Works in Termination Process
The CoLink Solver calls simTerminates when the simulation is quitted. You can use the needed code when the simulation is quitted.
extern "C" __declspec(dllexport) void simTerminates( CSys *CS ) { } |