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