47 #include "NLPInterfacePack_NLPSerialPreprocessExplJac.hpp"
48 #include "AbstractLinAlgPack_MatrixPermAggr.hpp"
49 #include "AbstractLinAlgPack_BasisSystemFactory.hpp"
50 #include "AbstractLinAlgPack_MatrixComposite.hpp"
51 #include "AbstractLinAlgPack_MatrixSparseCOORSerial.hpp"
52 #include "AbstractLinAlgPack_PermutationSerial.hpp"
53 #include "AbstractLinAlgPack_VectorDenseEncap.hpp"
54 #include "DenseLinAlgPack_DVectorOp.hpp"
55 #include "DenseLinAlgPack_IVector.hpp"
56 #include "DenseLinAlgPack_PermVecMat.hpp"
57 #include "Teuchos_Assert.hpp"
58 #include "Teuchos_dyn_cast.hpp"
59 #include "Teuchos_AbstractFactoryStd.hpp"
60 #include "OptionsFromStreamPack_OptionsFromStream.hpp"
62 namespace NLPInterfacePack {
69 const basis_sys_fcty_ptr_t &basis_sys_fcty
72 :initialized_(false),test_setup_(false)
74 this->set_basis_sys_fcty(basis_sys_fcty);
82 if(factory_Gc_full.
get())
83 factory_Gc_full_ = factory_Gc_full;
105 namespace mmp = MemMngPack;
107 test_setup_ = test_setup;
123 Gc_val_orig_.resize(Gc_nz_orig_);
124 Gc_ivect_orig_.resize(Gc_nz_orig_);
125 Gc_jvect_orig_.resize(Gc_nz_orig_);
127 Gh_val_orig_.resize(Gh_nz_orig_);
128 Gh_ivect_orig_.resize(Gh_nz_orig_);
129 Gh_jvect_orig_.resize(Gh_nz_orig_);
131 Gc_perm_new_basis_updated_ =
false;
154 return fcty.create();
170 Permutation* P_var, Range1D* var_dep
171 ,Permutation* P_equ, Range1D* equ_decomp
175 P_var,var_dep,P_equ,equ_decomp
178 Gc_perm_new_basis_updated_ =
false;
184 const Permutation &P_var,
const Range1D &var_dep
185 ,
const Permutation *P_equ,
const Range1D *equ_decomp
189 P_var,var_dep,P_equ,equ_decomp
191 Gc_perm_new_basis_updated_ =
false;
197 const Vector& x,
bool newx
201 namespace mmp = MemMngPack;
217 n_full = n_orig + mI_orig,
218 m_full = m_orig + mI_orig;
229 &G_aggr =
dyn_cast<MatrixPermAggr>( *first_order_info.
Gc );
232 G_full = Teuchos::rcp_const_cast<MatrixOp>( G_aggr.mat_orig() );
234 P_row = Teuchos::rcp_dynamic_cast<PermutationSerial>(
235 Teuchos::rcp_const_cast<Permutation>( G_aggr.row_perm() ) );
237 P_col = Teuchos::rcp_dynamic_cast<PermutationSerial>(
238 Teuchos::rcp_const_cast<Permutation>( G_aggr.col_perm() ) );
240 G_perm = G_aggr.mat_perm();
242 G_aggr.set_uninitialized();
244 if( G_full.get() == NULL || G_full.total_count() > 1 )
245 G_full = factory_Gc_full_->create();
247 MatrixLoadSparseElements
248 &G_lse =
dyn_cast<MatrixLoadSparseElements>(*G_full);
262 = Gc_nz_orig_ + Gh_nz_orig_ + mI_orig;
265 const bool load_struct = (G_lse.nz() == 0);
269 G_lse.reinitialize(n,num_cols,nz_full);
272 G_nz_previous = G_lse.nz();
273 G_lse.reset_to_load_values();
281 value_type *val = NULL;
282 index_type *ivect = NULL,
284 G_lse.get_load_nonzeros_buffers(
287 ,load_struct ? &ivect : NULL
288 ,load_struct ? &jvect : NULL
291 const value_type *val_orig = NULL;
292 const value_type *val_orig_end = NULL;
293 const index_type *ivect_orig = NULL;
294 const index_type *jvect_orig = NULL;
299 val_orig = &Gc_val_orig_[0];
300 val_orig_end = val_orig + Gc_nz_orig_;
301 ivect_orig = &Gc_ivect_orig_[0];
302 jvect_orig = &Gc_jvect_orig_[0];
303 imp_fill_jacobian_entries(
304 n, n_full, load_struct
306 ,val_orig, val_orig_end, ivect_orig, jvect_orig
313 val_orig = &Gh_val_orig_[0];
314 val_orig_end = val_orig + Gh_nz_orig_;
315 ivect_orig = &Gh_ivect_orig_[0];
316 jvect_orig = &Gh_jvect_orig_[0];
317 imp_fill_jacobian_entries(
318 n, n_full, load_struct
320 ,val_orig, val_orig_end, ivect_orig, jvect_orig
322 ,val + nz, ivect + nz, jvect + nz
325 value_type *val_itr = val + nz;
326 index_type *ivect_itr = ivect + nz;
327 index_type *jvect_itr = jvect + nz;
331 for( index_type k = 1; k <= mI_orig; ++k ) {
339 *ivect_itr++ = var_idx;
340 *jvect_itr++ = m_orig + k;
347 for( index_type k = 1; k <= mI_orig; ++k ) {
364 G_nz_previous != nz, std::runtime_error
365 ,
"NLPSerialPreprocessExplJac::imp_calc_Gc(...): Error, "
366 "The number of added nonzeros does not match the number of nonzeros "
367 "in the previous matrix load!." );
371 G_lse.commit_load_nonzeros_buffers(
374 ,load_struct ? &ivect : NULL
375 ,load_struct ? &jvect : NULL
377 G_lse.finish_construction(test_setup_);
384 if( P_row.get() == NULL || P_col.
total_count() > 1 )
387 if( P_row->perm().get() == NULL ) var_perm =
Teuchos::rcp(
new IVector(n_full));
388 else var_perm = Teuchos::rcp_const_cast<IVector>(P_row->perm());
390 P_row->initialize(var_perm,Teuchos::null);
395 if( P_col->perm().
get() == NULL ) con_perm =
Teuchos::rcp(
new IVector(m_full));
396 else con_perm = Teuchos::rcp_const_cast<IVector>(P_col->perm());
398 P_col->initialize(con_perm,Teuchos::null);
400 int num_row_part, num_col_part;
401 index_type row_part[3], col_part[3];
405 row_part[1] = (var_dep.lbound() == 1 ? var_dep.ubound()+1 : var_dep.lbound());
417 col_part[2] = m_full+1;
422 col_part[1] = m_full+1;
424 if( G_perm.
get() == NULL || !Gc_perm_new_basis_updated_ ) {
425 G_perm = G_full->perm_view(
426 P_row.get(),row_part,num_row_part
427 ,P_col.
get(),col_part,num_col_part
431 G_perm = G_full->perm_view_update(
432 P_row.get(),row_part,num_row_part
433 ,P_col.
get(),col_part,num_col_part
437 Gc_perm_new_basis_updated_ =
true;
443 G_aggr.initialize(G_full,P_row,P_col,G_perm);
452 ,
"NLPSerialPreprocessExplJac : The nlp has not been initialized yet" );
457 void NLPSerialPreprocessExplJac::imp_fill_jacobian_entries(
461 ,
const index_type col_offset
462 ,
const value_type *val_orig
463 ,
const value_type *val_orig_end
464 ,
const index_type *ivect_orig
465 ,
const index_type *jvect_orig
468 ,index_type *ivect_itr
469 ,index_type *jvect_itr
475 for( ; val_orig != val_orig_end ; ++val_orig, ++ivect_orig, ++jvect_orig) {
485 *val_itr++ = *val_orig;
487 *ivect_itr++ = var_idx;
488 *jvect_itr++ = col_offset + (*jvect_orig);
495 for( ; val_orig != val_orig_end ; ++val_orig, ++ivect_orig) {
505 *val_itr++ = *val_orig;
void set_basis(const Permutation &P_var, const Range1D &var_dep, const Permutation *P_equ, const Range1D *equ_decomp)
void set_basis(const Permutation &P_var, const Range1D &var_dep, const Permutation *P_equ, const Range1D *equ_decomp)
virtual bool imp_nlp_has_changed() const
Return if the definition of the NLP has changed since the last call to initialize() ...
const IVector & var_full_to_remove_fixed() const
Inverse permutation vector of var_remove_fixed_to_full().
NLP node subclass complementing NLPSerialPreprocess for explicit Jacobians.
MatrixOp * Gc
Pointer to Jacobian of equality constraints Gc (may be NULL if not set)
void initialize(bool test_setup)
Initialize the NLP for its first use.
virtual size_type imp_m_orig() const =0
Return the number of general equality constraints in the original problem.
const basis_sys_ptr_t basis_sys() const
Calls basis_sys_fcty()->create()
const IVector & equ_perm() const
Permutes from the original constriant ordering to the current basis selection.
void set_factory_Gc_full(const factory_mat_ptr_t &factory_Gc_full)
Initialize with matrix factory for original matrices Gc.
NLPSerialPreprocessExplJac(const basis_sys_fcty_ptr_t &basis_sys_fcty=Teuchos::rcp(new BasisSystemFactoryStd()), const factory_mat_ptr_t &factory_Gc_full=Teuchos::null)
Calls this->set_basis_sys_fcty() and this->set_mat_factories() methods.
Range1D equ_decomp() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
T_To & dyn_cast(T_From &from)
const FirstOrderExplInfo first_order_expl_info() const
virtual size_type imp_n_orig() const =0
Return the number of variables in the original problem (including those fixed by bounds) ...
const mat_fcty_ptr_t factory_Gc() const
bool get_next_basis(Permutation *P_var, Range1D *var_dep, Permutation *P_equ, Range1D *equ_decomp)
const IVector & var_perm() const
Permutes from the compated variable vector (removing fixed variables) to the current basis selection...
DVectorSlice x_full() const
Give reference to current x_full.
bool is_initialized() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void imp_calc_Gh_orig(const DVectorSlice &x_full, bool newx, const FirstOrderExplInfo &first_order_expl_info) const =0
Calculate the COOR matrix for the gradient for all of the h(x) constaints in the original NLP...
const options_ptr_t & get_options() const
Struct for zero and first order quantities (pointers)
void initialize(bool test_setup)
virtual size_type imp_Gh_nz_orig() const =0
Return the number of nonzero elements in Gh before elements are removed for fixed variables...
void set_Gc(MatrixOp *Gc)
Validates the type of Gc is correct.
Thrown if any member functions are called before initialize() has been called.
virtual size_type imp_mI_orig() const =0
Return the number of general inequality constraints in the original problem.
virtual void imp_calc_Gc_orig(const DVectorSlice &x_full, bool newx, const FirstOrderExplInfo &first_order_expl_info) const =0
Calculate the COOR matrix for the gradient for all of the c(x) constaints in the original NLP...
virtual size_type imp_Gc_nz_orig() const =0
Return the number of nonzero elements in Gc before elements are removed for fixed variables...
void set_options(const options_ptr_t &options)
Passes these options on to this->basis_sys_fcty().set_options(options).
void assert_initialized() const
Assert if we have been initizlized (throws UnInitialized)
void set_x_full(const DVectorSlice &x, bool newx, DVectorSlice *x_full) const
Set the full x vector if newx == true
void imp_calc_Gc(const Vector &x, bool newx, const FirstOrderInfo &first_order_info) const
virtual void set_Gc(MatrixOp *Gc)
Set a pointer to a matrix object to be updated when this->calc_Gc() is called.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
void initialize(bool test_setup)
bool get_next_basis(Permutation *P_var, Range1D *var_dep, Permutation *P_equ, Range1D *equ_decomp)