12 #if !defined(HAVE_MUELU_MATLAB) || !defined(HAVE_MUELU_EPETRA)
13 #error "Muemex types require MATLAB, Epetra and Tpetra."
17 #if (defined(MX_API_VER) && MX_API_VER >= 0x07030000)
23 using namespace Teuchos;
28 template class MuemexData<RCP<Xpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
29 template class MuemexData<RCP<Xpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
30 template class MuemexData<RCP<Xpetra::Matrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
31 template class MuemexData<RCP<Xpetra::Matrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
32 template class MuemexData<RCP<MAggregates> >;
33 template class MuemexData<RCP<MAmalInfo> >;
34 template class MuemexData<int>;
35 template class MuemexData<bool>;
36 template class MuemexData<complex_t>;
37 template class MuemexData<string>;
38 template class MuemexData<double>;
39 template class MuemexData<RCP<Tpetra::CrsMatrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
40 template class MuemexData<RCP<Tpetra::CrsMatrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
41 template class MuemexData<RCP<Epetra_MultiVector> >;
42 template class MuemexData<RCP<Tpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
43 template class MuemexData<RCP<Tpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
44 template class MuemexData<RCP<Xpetra::Vector<mm_LocalOrd, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
52 for (
int i = 0; i < N; i++)
53 rv[i] = (
int)mwi_array[i];
63 return mxCreateSparse(numRows, numCols, nnz, mxREAL);
68 return mxCreateSparse(numRows, numCols, nnz, mxCOMPLEX);
73 memcpy(mxGetPr(mxa), array, n *
sizeof(
double));
78 double* pr = mxGetPr(mxa);
79 double* pi = mxGetPi(mxa);
80 for (
int i = 0; i < n; i++) {
81 pr[i] = std::real<double>(array[i]);
82 pi[i] = std::imag<double>(array[i]);
91 int result = mexEvalString(
function.c_str());
93 mexPrintf(
"An error occurred while running a MATLAB command.");
97 using Teuchos::rcp_static_cast;
98 mxArray** matlabArgs =
new mxArray*[args.size()];
99 mxArray** matlabOutput =
new mxArray*[numOutputs];
100 std::vector<RCP<MuemexArg> > output;
102 for (
int i = 0; i < int(args.size()); i++) {
104 switch (args[i]->type) {
109 matlabArgs[i] = rcp_static_cast<
MuemexData<int>, MuemexArg>(args[i])->convertToMatlab();
112 matlabArgs[i] = rcp_static_cast<
MuemexData<double>, MuemexArg>(args[i])->convertToMatlab();
164 #ifdef HAVE_MUELU_INTREPID2
165 case FIELDCONTAINER_ORDINAL:
170 }
catch (std::exception& e) {
171 mexPrintf(
"An error occurred while converting arg #%d to MATLAB:\n", i);
172 std::cout << e.what() << std::endl;
173 mexPrintf(
"Passing 0 instead.\n");
174 matlabArgs[i] = mxCreateDoubleScalar(0);
178 int result = mexCallMATLAB(numOutputs, matlabOutput, args.size(), matlabArgs,
function.c_str());
180 mexPrintf(
"Matlab encountered an error while running command through muemexCallbacks.\n");
182 for (
int i = 0; i < numOutputs; i++) {
185 }
catch (std::exception& e) {
186 mexPrintf(
"An error occurred while converting output #%d from MATLAB:\n", i);
187 std::cout << e.what() << std::endl;
190 delete[] matlabOutput;
201 return mxCreateDoubleMatrix(numRows, numCols, mxREAL);
206 return mxCreateDoubleMatrix(numRows, numCols, mxCOMPLEX);
210 throw runtime_error(
"AmalgamationInfo not supported in MueMex yet.");
211 return mxCreateDoubleScalar(0);
215 bool isValidAggregates =
true;
216 if (!mxIsStruct(mxa))
218 int numFields = mxGetNumberOfFields(mxa);
220 isValidAggregates =
false;
221 if (isValidAggregates) {
222 const char* mem1 = mxGetFieldNameByNumber(mxa, 0);
223 if (mem1 == NULL || strcmp(mem1,
"nVertices") != 0)
224 isValidAggregates =
false;
225 const char* mem2 = mxGetFieldNameByNumber(mxa, 1);
226 if (mem2 == NULL || strcmp(mem2,
"nAggregates") != 0)
227 isValidAggregates =
false;
228 const char* mem3 = mxGetFieldNameByNumber(mxa, 2);
229 if (mem3 == NULL || strcmp(mem3,
"vertexToAggID") != 0)
230 isValidAggregates =
false;
231 const char* mem4 = mxGetFieldNameByNumber(mxa, 3);
232 if (mem3 == NULL || strcmp(mem4,
"rootNodes") != 0)
233 isValidAggregates =
false;
234 const char* mem5 = mxGetFieldNameByNumber(mxa, 4);
235 if (mem4 == NULL || strcmp(mem5,
"aggSizes") != 0)
236 isValidAggregates =
false;
238 return isValidAggregates;
242 bool isValidGraph =
true;
243 if (!mxIsStruct(mxa))
245 int numFields = mxGetNumberOfFields(mxa);
247 isValidGraph =
false;
249 const char* mem1 = mxGetFieldNameByNumber(mxa, 0);
250 if (mem1 == NULL || strcmp(mem1,
"edges") != 0)
251 isValidGraph =
false;
252 const char* mem2 = mxGetFieldNameByNumber(mxa, 1);
253 if (mem2 == NULL || strcmp(mem2,
"boundaryNodes") != 0)
254 isValidGraph =
false;
261 vector<string> rlist;
262 const char* delims =
",";
263 char* copy = (
char*)malloc(params.length() + 1);
264 strcpy(copy, params.c_str());
265 char* mark = (
char*)strtok(copy, delims);
266 while (mark != NULL) {
268 char* tail = mark + strlen(mark) - 1;
271 while (*tail ==
' ' && tail > mark)
276 rlist.push_back(tok);
277 mark = strtok(NULL, delims);
284 using namespace Teuchos;
289 validParamList->set<
RCP<const FactoryBase> >(
"Ptent", Teuchos::null,
"Factory for the tentative (unsmoothed) prolongator.");
290 validParamList->set<
RCP<const FactoryBase> >(
"Coordinates", Teuchos::null,
"Factory for the node coordinates.");
293 validParamList->set<
RCP<const FactoryBase> >(
"UnamalgamationInfo", Teuchos::null,
"Factory for amalgamation.");
294 #ifdef HAVE_MUELU_INTREPID2
295 validParamList->set<
RCP<const FactoryBase> >(
"pcoarsen: element to node map", Teuchos::null,
"Generating factory of the element to node map");
297 return validParamList;
301 switch (mxGetClassID(mxa)) {
306 case mxLOGICAL_CLASS:
311 if (mxGetM(mxa) == 1 && mxGetN(mxa) == 1)
314 else if (mxGetM(mxa) != 1 || mxGetN(mxa) != 1)
318 throw std::runtime_error(
"Error: Don't know what to do with integer array.\n");
321 if (mxGetM(mxa) == 1 && mxGetN(mxa) == 1) {
322 if (mxIsComplex(mxa))
328 }
else if (mxIsSparse(mxa))
331 if (mxIsComplex(mxa))
339 if (mxIsComplex(mxa))
345 case mxSTRUCT_CLASS: {
354 throw runtime_error(
"Invalid aggregates or graph struct passed in from MATLAB.");
355 return Teuchos::null;
360 throw std::runtime_error(
"MATLAB returned an unsupported type as a function output.\n");
361 return Teuchos::null;
385 template RCP<MAggregates> loadDataFromMatlab<RCP<MAggregates> >(
const mxArray* mxa);
386 template RCP<MAmalInfo> loadDataFromMatlab<RCP<MAmalInfo> >(
const mxArray* mxa);
407 template vector<RCP<MuemexArg> >
processNeeds<double>(
const Factory* factory,
string& needsParam, Level& lvl);
408 template vector<RCP<MuemexArg> >
processNeeds<complex_t>(
const Factory* factory,
string& needsParam, Level& lvl);
409 template void processProvides<double>(vector<RCP<MuemexArg> >& mexOutput,
const Factory* factory,
string& providesParam, Level& lvl);
410 template void processProvides<complex_t>(vector<RCP<MuemexArg> >& mexOutput,
const Factory* factory,
string& providesParam, Level& lvl);
413 #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)
template void processProvides< complex_t >(vector< RCP< MuemexArg > > &mexOutput, const Factory *factory, string &providesParam, Level &lvl)
mxArray * saveAmalInfo(RCP< MAmalInfo > &amalInfo)
std::vector< RCP< MuemexArg > > callMatlab(std::string function, int numOutputs, std::vector< RCP< MuemexArg > > args)
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()
template void processProvides< double >(vector< RCP< MuemexArg > > &mexOutput, const Factory *factory, string &providesParam, Level &lvl)
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)
void callMatlabNoArgs(std::string function)
std::complex< double > complex_t
mxArray * createMatlabMultiVector< double >(int numRows, int numCols)
template double loadDataFromMatlab< double >(const mxArray *mxa)
Teuchos::RCP< MuemexArg > convertMatlabVar(const mxArray *mxa)