CONOPT
Loading...
Searching...
No Matches
mp_lincns.f90
Go to the documentation of this file.
1!> @file mp_lincns.f90
2!! @ingroup FORTOPENMP_EXAMPLES
3!!
4!!
5!! Large Linear Dense CNS model.
6!! Used to test the inversion routine for a very dense model.
7!!
8!!
9!! For more information about the individual callbacks, please have a look at the source code.
10
11!> Main program. A simple setup and call of CONOPT
12!!
13Program lincns
14 Use proginfop
15 Use coidef
16 Use omp_lib
17 Implicit none
18!
19! Declare the user callback routines as Integer, External:
20!
21 Integer, External :: lincns_readmatrix ! Mandatory Matrix definition routine defined below
22 Integer, External :: std_status ! Standard callback for displaying solution status
23 Integer, External :: lincns_solution ! callback for displaying and testing solution values
24 Integer, External :: std_message ! Standard callback for managing messages
25 Integer, External :: std_errmsg ! Standard callback for managing error messages
26 Integer, External :: std_triord ! Standard callback for triangular order
27#if defined(itl)
28!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lincns_ReadMatrix
29!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
30!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lincns_Solution
31!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
32!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
33!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
34#endif
35!
36! Control vector
37!
38 INTEGER, Dimension(:), Pointer :: cntvect
39 INTEGER :: coi_error
40!
41! Locals
42!
43 Integer :: n
44!
45! Create and initialize a Control Vector
46!
47 real*8 time0, time1, time2
48
49 call startup
50 coi_error = coi_createfort( cntvect )
51!
52! Define the number of variables and constraints
53!
54 n = 1000
55!
56! Tell CONOPT about the size of the model by populating the Control Vector:
57!
58! Number of variables = N
59!
60 coi_error = max( coi_error, coidef_numvar( cntvect, n ) )
61!
62! Number of equations = M
63!
64 coi_error = max( coi_error, coidef_numcon( cntvect, n ) )
65!
66! Number of nonzeros in the Jacobian: N*N
67!
68 coi_error = max( coi_error, coidef_numnz( cntvect, n*n ) )
69!
70! Number of nonlinear nonzeros. 0 -- the model is linear
71!
72 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) )
73!
74! Square system ( No objective is defined)
75!
76 coi_error = max( coi_error, coidef_square( cntvect, 1 ) )
77!
78! Options file is test.opt (if available)
79!
80 coi_error = max( coi_error, coidef_optfile( cntvect, 'mp_lincns.opt' ) )
81!
82! Tell CONOPT about the callback routines:
83!
84 coi_error = max( coi_error, coidef_readmatrix( cntvect, lincns_readmatrix ) )
85 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
86 coi_error = max( coi_error, coidef_solution( cntvect, lincns_solution ) )
87 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
88 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
89 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
90
91#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
92 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
93#endif
94
95 If ( coi_error .ne. 0 ) THEN
96 write(*,*)
97 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
98 write(*,*)
99 call flog( "Skipping Solve due to setup errors", 1 )
100 ENDIF
101!
102! Start CONOPT -- first with the default single thread:
103!
104 time0 = omp_get_wtime()
105 coi_error = coi_solve( cntvect )
106 time1 = omp_get_wtime() - time0
107
108 write(*,*)
109 write(*,*) 'Single Thread: End of LinCns. Return code=',coi_error
110
111 If ( coi_error /= 0 ) then
112 call flog( "Single Thread: Errors encountered during solution", 1 )
113 elseif ( stacalls == 0 .or. solcalls == 0 ) then
114 call flog( "Single Thread: Status or Solution routine was not called", 1 )
115 elseif ( mstat /= 15 .or. sstat /= 1 ) then ! 15 since the model is linear
116 call flog( "Single Thread: The model or solver status was not (15,1) as expected", 1 )
117 elseif ( miter /= 1 ) then ! we expect one iteration for a linear CNS
118 call flog( "Single Thread: The iteration count was not 1 as expected", 1 )
119 endif
120!
121! Start CONOPT again using multiple threads everywhere:
122!
123 coi_error = max( coi_error, coidef_threads( cntvect, 0 ) ) ! 0 means use as many threads as you can
124 time0 = omp_get_wtime()
125 coi_error = coi_solve( cntvect )
126 time2 = omp_get_wtime() - time0
127
128 write(*,*)
129 write(*,*) 'Multiple Threads: End of LinCns. Return code=',coi_error
130
131 If ( coi_error /= 0 ) then
132 call flog( "Multiple Threads: Errors encountered during solution", 1 )
133 elseif ( stacalls == 0 .or. solcalls == 0 ) then
134 call flog( "Multiple Threads: Status or Solution routine was not called", 1 )
135 elseif ( mstat /= 15 .or. sstat /= 1 ) then ! 15 since the model is linear
136 call flog( "Multiple Threads: The model or solver status was not (15,1) as expected", 1 )
137 elseif ( miter /= 1 ) then ! we expect one iteration for a linear CNS
138 call flog( "Multiple Threads: The iteration count was not 1 as expected", 1 )
139 endif
140
141 if ( coi_free( cntvect ) /= 0 ) call flog( "Error while freeing control vector", 1 )
142
143 write(*,*)
144 write(*,"('Time for single thread',f10.3)") time1
145 write(*,"('Time for multi thread',f10.3)") time2
146 write(*,"('Speedup ',f10.3)") time1/time2
147 write(*,"('Efficiency ',f10.3)") time1/time2/omp_get_max_threads()
148
149 call flog( "Successful Solve", 0 )
150
151end Program lincns
152!
153! =====================================================================
154! Define information about the model structure
155!
156
157!> Define information about the model
158!!
159!! @include{doc} readMatrix_params.dox
160Integer Function lincns_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
161 colsta, rowno, value, nlflag, n, m, nz, usrmem )
162#if defined(itl)
163!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lincns_ReadMatrix
164#endif
165 implicit none
166 integer, intent (in) :: n ! number of variables
167 integer, intent (in) :: m ! number of constraints
168 integer, intent (in) :: nz ! number of nonzeros
169 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
170 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
171 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
172 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
173 ! (not defined here)
174 integer, intent (out), dimension(m) :: type ! vector of equation types
175 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
176 ! (not defined here)
177 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
178 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
179 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
180 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
181 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
182 real*8 usrmem(*) ! optional user memory
183
184 Integer :: i, j, k
185!
186! Define the information for the columns. All are unbounded with initial value I
187!
188 do i = 1, n
189 curr(i) = i
190 enddo
191!
192! All are equalities:
193!
194 do i = 1, m
195 type(i) = 0
196 enddo
197!
198! Right hand sides: Initialize to 0 and revise so the solution becomes
199! x(i) = 1.0
200!
201 do i = 1, m
202 rhs(i) = 0.d0
203 enddo
204!
205! Define the structure and content of the Jacobian:
206! We have a pattern with the largest values on the cross-diagonal from
207! (1,N) to (N,1) with gradually smaller value away from this diagonal.
208! The off-diagonal values are bounded away from zero to force a
209! completely dense model even if small values are filtered out.
210!
211 k = 1
212 do i = 1, n
213 colsta(i) = k
214 do j = 1, n
215 rowno(k) = j
216 value(k) = (-1)**(i+j)*max(1.d-4, 2.0d0**(-abs(i+j-(n+1))))
217 rhs(j) = rhs(j) + value(k)
218 k = k + 1
219 enddo
220 enddo
221 colsta(n+1) = k
222
224
225end Function lincns_readmatrix
226
227Integer Function lincns_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
228#if defined(itl)
229!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lincns_Solution
230#endif
231!
232! We write the solution values to the 'Documentation file' on unit 10
233! and test that the solution values are 1.0
234!
235 Use proginfop
236 IMPLICIT NONE
237 INTEGER, Intent(IN) :: n, m
238 INTEGER, Intent(IN), Dimension(N) :: xbas, xsta
239 INTEGER, Intent(IN), Dimension(M) :: ybas, ysta
240 real*8, Intent(IN), Dimension(N) :: xval, xmar
241 real*8, Intent(IN), Dimension(M) :: yval, ymar
242 real*8, Intent(IN OUT) :: usrmem(*)
243
244 INTEGER i
245 CHARACTER*5, Parameter, Dimension(4) :: stat = (/ 'Lower','Upper','Basic','Super' /)
246
247 Logical error
248
249 error = .false.
251 WRITE(10,"(/' Variable Solution value Reduced cost B-stat')")
252 DO i = 1, n
253 WRITE(10,"(1P,I7,E20.6,E16.6,4X,A5 )") i, xval(i), xmar(i), stat(1+xbas(i))
254 If ( abs(xval(i)-1.d0) > 1.d-7 ) then
255 write(10,*) '**** Bad solution value'
256 error = .true.
258 Endif
259 ENDDO
260
261 WRITE(10,"(/' Constrnt Activity level Marginal cost B-stat')")
262 DO i = 1, m
263 WRITE(10,"(1P,I7,E20.6,E16.6,4X,A5 )") i, yval(i), ymar(i), stat(1+ybas(i))
264 ENDDO
265
266 solcalls = solcalls + 1
267
268END Function lincns_solution
269
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:291
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
integer function coidef_threads(cntvect, threads)
number of threads allowed internally in CONOPT.
integer function coidef_square(cntvect, square)
square models.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
integer function lincns_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
integer function lincns_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
program lincns
Main program. A simple setup and call of CONOPT.
Definition mp_lincns.f90:13
subroutine flog(msg, code)
Definition comdeclp.f90:42
integer miter
Definition comdeclp.f90:16
subroutine startup
Definition comdeclp.f90:25