48 #if !defined(HAVE_MUELU_MATLAB) || !defined(HAVE_MUELU_EPETRA) || !defined(HAVE_MUELU_TPETRA)
49 #error "Muemex types require MATLAB, Epetra and Tpetra."
53 #if(defined(MX_API_VER) && MX_API_VER >= 0x07030000)
59 using namespace Teuchos;
64 template class MuemexData<RCP<Xpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
65 template class MuemexData<RCP<Xpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
66 template class MuemexData<RCP<Xpetra::Matrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
67 template class MuemexData<RCP<Xpetra::Matrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
68 template class MuemexData<RCP<MAggregates>>;
69 template class MuemexData<RCP<MAmalInfo>>;
70 template class MuemexData<int>;
71 template class MuemexData<bool>;
72 template class MuemexData<complex_t>;
73 template class MuemexData<string>;
74 template class MuemexData<double>;
75 template class MuemexData<RCP<Tpetra::CrsMatrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
76 template class MuemexData<RCP<Tpetra::CrsMatrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
77 template class MuemexData<RCP<Epetra_MultiVector> >;
78 template class MuemexData<RCP<Tpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
79 template class MuemexData<RCP<Tpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
80 template class MuemexData<RCP<Xpetra::Vector<mm_LocalOrd, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>>;
89 for(
int i = 0; i < N; i++)
90 rv[i] = (
int) mwi_array[i];
100 return mxCreateSparse(numRows, numCols, nnz, mxREAL);
105 return mxCreateSparse(numRows, numCols, nnz, mxCOMPLEX);
110 memcpy(mxGetPr(mxa), array, n *
sizeof(
double));
115 double* pr = mxGetPr(mxa);
116 double* pi = mxGetPi(mxa);
117 for(
int i = 0; i < n; i++)
119 pr[i] = std::real<double>(array[i]);
120 pi[i] = std::imag<double>(array[i]);
130 int result = mexEvalString(
function.c_str());
132 mexPrintf(
"An error occurred while running a MATLAB command.");\
137 using Teuchos::rcp_static_cast;
138 mxArray** matlabArgs =
new mxArray* [args.size()];
139 mxArray** matlabOutput =
new mxArray* [numOutputs];
140 std::vector<RCP<MuemexArg>> output;
142 for(
int i = 0; i < int(args.size()); i++)
146 switch(args[i]->type)
152 matlabArgs[i] = rcp_static_cast<
MuemexData<int>, MuemexArg>(args[i])->convertToMatlab();
155 matlabArgs[i] = rcp_static_cast<
MuemexData<double>, MuemexArg>(args[i])->convertToMatlab();
207 #ifdef HAVE_MUELU_INTREPID2
208 case FIELDCONTAINER_ORDINAL:
214 catch (std::exception& e)
216 mexPrintf(
"An error occurred while converting arg #%d to MATLAB:\n", i);
217 std::cout << e.what() << std::endl;
218 mexPrintf(
"Passing 0 instead.\n");
219 matlabArgs[i] = mxCreateDoubleScalar(0);
223 int result = mexCallMATLAB(numOutputs, matlabOutput, args.size(), matlabArgs,
function.c_str());
225 mexPrintf(
"Matlab encountered an error while running command through muemexCallbacks.\n");
227 for(
int i = 0; i < numOutputs; i++)
233 catch(std::exception& e)
235 mexPrintf(
"An error occurred while converting output #%d from MATLAB:\n", i);
236 std::cout << e.what() << std::endl;
239 delete[] matlabOutput;
250 return mxCreateDoubleMatrix(numRows, numCols, mxREAL);
255 return mxCreateDoubleMatrix(numRows, numCols, mxCOMPLEX);
260 throw runtime_error(
"AmalgamationInfo not supported in MueMex yet.");
261 return mxCreateDoubleScalar(0);
266 bool isValidAggregates =
true;
269 int numFields = mxGetNumberOfFields(mxa);
271 isValidAggregates =
false;
272 if(isValidAggregates)
274 const char* mem1 = mxGetFieldNameByNumber(mxa, 0);
275 if(mem1 == NULL || strcmp(mem1,
"nVertices") != 0)
276 isValidAggregates =
false;
277 const char* mem2 = mxGetFieldNameByNumber(mxa, 1);
278 if(mem2 == NULL || strcmp(mem2,
"nAggregates") != 0)
279 isValidAggregates =
false;
280 const char* mem3 = mxGetFieldNameByNumber(mxa, 2);
281 if(mem3 == NULL || strcmp(mem3,
"vertexToAggID") != 0)
282 isValidAggregates =
false;
283 const char* mem4 = mxGetFieldNameByNumber(mxa, 3);
284 if(mem3 == NULL || strcmp(mem4,
"rootNodes") != 0)
285 isValidAggregates =
false;
286 const char* mem5 = mxGetFieldNameByNumber(mxa, 4);
287 if(mem4 == NULL || strcmp(mem5,
"aggSizes") != 0)
288 isValidAggregates =
false;
290 return isValidAggregates;
295 bool isValidGraph =
true;
298 int numFields = mxGetNumberOfFields(mxa);
300 isValidGraph =
false;
303 const char* mem1 = mxGetFieldNameByNumber(mxa, 0);
304 if(mem1 == NULL || strcmp(mem1,
"edges") != 0)
305 isValidGraph =
false;
306 const char* mem2 = mxGetFieldNameByNumber(mxa, 1);
307 if(mem2 == NULL || strcmp(mem2,
"boundaryNodes") != 0)
308 isValidGraph =
false;
316 vector<string> rlist;
317 const char* delims =
",";
318 char* copy = (
char*) malloc(params.length() + 1);
319 strcpy(copy, params.c_str());
320 char* mark = (
char*) strtok(copy, delims);
324 char* tail = mark + strlen(mark) - 1;
327 while(*tail ==
' ' && tail > mark)
332 rlist.push_back(tok);
333 mark = strtok(NULL, delims);
341 using namespace Teuchos;
346 validParamList->set<
RCP<const FactoryBase>>(
"Ptent", Teuchos::null,
"Factory for the tentative (unsmoothed) prolongator.");
347 validParamList->set<
RCP<const FactoryBase>>(
"Coordinates", Teuchos::null,
"Factory for the node coordinates.");
350 validParamList->set<
RCP<const FactoryBase>>(
"UnamalgamationInfo", Teuchos::null,
"Factory for amalgamation.");
351 #ifdef HAVE_MUELU_INTREPID2
352 validParamList->set<
RCP<const FactoryBase>>(
"pcoarsen: element to node map", Teuchos::null,
"Generating factory of the element to node map");
354 return validParamList;
359 switch(mxGetClassID(mxa))
365 case mxLOGICAL_CLASS:
370 if(mxGetM(mxa) == 1 && mxGetN(mxa) == 1)
373 else if(mxGetM(mxa) != 1 || mxGetN(mxa) != 1)
377 throw std::runtime_error(
"Error: Don't know what to do with integer array.\n");
380 if(mxGetM(mxa) == 1 && mxGetN(mxa) == 1)
389 else if(mxIsSparse(mxa))
418 throw runtime_error(
"Invalid aggregates or graph struct passed in from MATLAB.");
419 return Teuchos::null;
424 throw std::runtime_error(
"MATLAB returned an unsupported type as a function output.\n");
425 return Teuchos::null;
449 template RCP<MAggregates> loadDataFromMatlab<RCP<MAggregates>>(
const mxArray* mxa);
450 template RCP<MAmalInfo> loadDataFromMatlab<RCP<MAmalInfo>>(
const mxArray* mxa);
471 template vector<RCP<MuemexArg>>
processNeeds<double>(
const Factory* factory,
string& needsParam, Level& lvl);
472 template vector<RCP<MuemexArg>>
processNeeds<complex_t>(
const Factory* factory,
string& needsParam, Level& lvl);
473 template void processProvides<double>(vector<RCP<MuemexArg>>& mexOutput,
const Factory* factory,
string& providesParam, Level& lvl);
474 template void processProvides<complex_t>(vector<RCP<MuemexArg>>& mexOutput,
const Factory* factory,
string& providesParam, Level& lvl);
477 #endif // HAVE_MUELU_MATLAB
bool isValidMatlabAggregates(const mxArray *mxa)
template mxArray * saveDataToMatlab(bool &data)
std::vector< std::string > tokenizeList(const std::string ¶ms)
mxArray * createMatlabSparse< complex_t >(int numRows, int numCols, int nnz)
template vector< RCP< MuemexArg > > processNeeds< complex_t >(const Factory *factory, string &needsParam, Level &lvl)
mxArray * saveAmalInfo(RCP< MAmalInfo > &amalInfo)
template vector< RCP< MuemexArg > > processNeeds< double >(const Factory *factory, string &needsParam, Level &lvl)
void fillMatlabArray< double >(double *array, const mxArray *mxa, int n)
bool isValidMatlabGraph(const mxArray *mxa)
template string loadDataFromMatlab< string >(const mxArray *mxa)
Teuchos::RCP< Teuchos::ParameterList > getInputParamList()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void fillMatlabArray< complex_t >(complex_t *array, const mxArray *mxa, int n)
mxArray * createMatlabSparse< double >(int numRows, int numCols, int nnz)
int * mwIndex_to_int(int N, mwIndex *mwi_array)
template complex_t loadDataFromMatlab< complex_t >(const mxArray *mxa)
template int loadDataFromMatlab< int >(const mxArray *mxa)
template bool loadDataFromMatlab< bool >(const mxArray *mxa)
mxArray * createMatlabMultiVector< complex_t >(int numRows, int numCols)
template void processProvides< complex_t >(vector< RCP< MuemexArg >> &mexOutput, const Factory *factory, string &providesParam, Level &lvl)
void callMatlabNoArgs(std::string function)
std::complex< double > complex_t
mxArray * createMatlabMultiVector< double >(int numRows, int numCols)
template double loadDataFromMatlab< double >(const mxArray *mxa)
template void processProvides< double >(vector< RCP< MuemexArg >> &mexOutput, const Factory *factory, string &providesParam, Level &lvl)
std::vector< RCP< MuemexArg > > callMatlab(std::string function, int numOutputs, std::vector< RCP< MuemexArg >> args)
Teuchos::RCP< MuemexArg > convertMatlabVar(const mxArray *mxa)