How to read dictionary variable in 0/U file in OpenFOAM - openfoam

I wanted to read the viscosity variable "nu" of "transportProperties" dictionary in the inlet boundary in 0/U as follows:
boundaryField
{
inlet
{
type codedFixedValue;
value uniform (0.006 0 0);
name parabolicInlet;
code
#{
// ... some lines of code ...
scalar nu = readScalar(this->db().lookupObject<IOdictionary>("transportProperties").lookup("nu"));
// ... some lines of code ...
#};
}
}
and I got this this wrong token type error:
--> FOAM FATAL IO ERROR:
wrong token type - expected Scalar, found on line 22 the punctuation token '['
file: /home/behzadb/research/Simulations/ConfinedCylinder2D/TestCase-01/constant/transportProperties/nu at line 22.
From function Foam::Istream& Foam::operator>>(Foam::Istream&, Foam::doubleScalar&)
in file lnInclude/Scalar.C at line 101.
FOAM exiting
I would appreciate knowing how I should call the dictionary and read that dimensioned scalar variable "nu" from it to use for some calculations (like the Reynolds number)?
Thanks a lot,
BB

Try the following:
boundaryField
{
inlet
{
type codedFixedValue;
value uniform (0.006 0 0);
name parabolicInlet;
code
#{
// ... some lines of code ...
dimensionedScalar nu
(
"nu",
dimViscosity,
this->db().lookupObject<IOdictionary>("transportProperties").lookup("nu")
);
// ... some lines of code ...
#};
}
}
You are getting that error because you're trying to read the kinematic viscosity as a scalar. Instead, use a dimensionedScalar.
If you would like to use the scalar value of nu, you can access it with:
scalar nu_value = nu.value();

Related

Need to change an element of a multidimensional string vector in C++

I have a multidimensional string vector, gameState. The contents of gameState can be seen below. I would like to "move" A up one space by making [2][1] = " " and [1][1] = "A". But I'm getting an error (error: invalid conversion from 'const char*' to '__gnu_cxx::__alloc_traitsstd::allocator<char, char>::value_type' {aka 'char'} [-fpermissive]). Not sure if there is a way to use vector.at() in this case. Here is a snippet. The full code reads in the initial game state from a text file and assigns it to the string vector gameState. I get the row and col indexes for each letter in the grid below and assign them to variables (eg. Arow and Acol). I'm very new to C++, so any help would be appreciated. If you have any questions about the rest of the code, I'd be happy to elaborate. Thanks in advance.
int Arow;
int Acol;
vector<string> gameState;
string tempU = gameState[Arow-1][Acol];
if (tempU != "#") {
// if no wall is up, move up
gameState[Arow][Acol] = " ";
gameState[Arow-1][Acol] = "A";
Arow = Arow-1;
Amovesequence.push_back("U");
}
###########
#...#P...B#
#A#.$.###*#
#...#D...R#
###########
The type of gameState[Arow-1][Acol] is a char, not a string. Modify the code accordingly, e.g.: char tempU = ... and replace " " with ' 'etc.

Q# Program does not contain a static 'Main' method suitable for an entry point

I'm creating a program in Q#.
Problem
You are given two qubits in state |00⟩. Your task is to create the following state on them:
1/3–√(|00⟩+|01⟩+|10⟩)
You have to implement an operation which takes an array of 2 qubits as an input and has no output. The "output" of your solution is the state in which it left the input qubits.
Code
namespace Solution {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
operation Solve (qs : Qubit[]) : Unit
{
body
{
Ry(ArcCos(Sqrt(2.0/3.0))*2.0,qs[0]);
(ControlledOnInt(0,H))([qs[0]],qs[1]);
}
}
}
But when I run it show me the following error.
Error
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point
[C:\Users\Pawar\Desktop\HK\codeforces\Q#\Solution\Solution.csproj]
So I tried to put EntryPoint() before the method declaration . Which shows me different error as
error QS6231: Invalid entry point. Values of type Qubit may not be used as arguments or return values to entry points. [C:\Users\Pawar\Desktop\HK\codeforces\Q#\Solution\Solution.csproj]
Please help me how to run it properly ?
thanks ✌️
In order to run a Q# program as an executable, you need to have an #EntryPoint() operation defined. You can read more in this excellent blog post: https://qsharp.community/blog/qsharp-entrypoint/.
Specifically, in your case, the error message indicates that Qubit[] is not a valid parameter to the main entry point of your program. Which makes sense, because it doesn't make sense to pass an array of qubits when executing a program from the command line. And also, your operation doesn't print anything or return any results, so you won't be able to see what it's doing.
You should probably create an #EntryPoint() wrapper operation that invokes your existing operation with the appropriate parameters, maybe prints some diagnostics, and then returns some result. In your case, you could perhaps do something like this (note the additional namespaces you need to open):
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Measurement;
#EntryPoint()
operation SolveForTwoQubits() : Result[]
{
using (qubits = Qubit[2])
{
Solve(qubits); // invoke your existing Solve operation
DumpMachine(); // outputs the state of your qubits
let results = MultiM(qubits); // measure the qubits
ResetAll(qubits); // reset the qubits to the initial state
return results; // return the measured results
}
}
This will give some output that looks like:
# wave function for qubits with ids (least to most significant): 0;1
∣0❭: 0.577350 + 0.000000 i == ******* [ 0.333333 ] --- [ 0.00000 rad ]
∣1❭: 0.577350 + 0.000000 i == ******* [ 0.333333 ] --- [ 0.00000 rad ]
∣2❭: 0.577350 + 0.000000 i == ******* [ 0.333333 ] --- [ 0.00000 rad ]
∣3❭: 0.000000 + 0.000000 i == [ 0.000000 ]
[Zero,One]

What type is a points vector in objective C

I have the following declaration for a function and I don't know what I am doing wrong with regards to the type declaration:
//function is defined in the OpenCVWrapper.mm file .....no errors
+ (NSArray *)analysePoints:(std::vector<cv::Point> )pointsVector{
.......
}
//error is in the OpenCVWrapper.h file
#interface OpenCVWrapper : NSObject
+ (NSArray *)analysePoints:(NSMutableArray *)mutableArray:(std::vector<cv::Point>)pointsArray;
//red marker under the std
#end
I am getting the error "expecting type" for the vector. What am I doing wrong here?
Actually, I found the solution through user11118321 input to look at the bigger picture. I am using this set up in a swift app that uses openCV through a bridging header. It is actually not possible to import or use a vector in swift.

Rcpp try-catch, how to stop error message from showing up? [duplicate]

Running the following code still produces an error message that goes to stdout (not stderr) although the exception was successfully caught:
Mat<double> matrix_quantiles(const vector<double> & quantiles,
const Mat<double> & m) {
Mat<double> sorted_matrix;
try {
sorted_matrix = arma::sort(arma::cor(m));
} catch(std::logic_error & e) {
/*
Sometimes a col is constant, causing the correlation to be
infinite. If that happens, add normal random jitter to the
values and retry.
*/
const Mat<double> jitter = Mat<double>(
m.n_rows, m.n_cols, arma::fill::randn);
return matrix_quantiles(quantiles, 1.e-3 * jitter + m);
}
etc.
The error message is:
error: sort(): given object has non-finite elements
The code runs fine, and the jittering strategy is good enough for what I do, but I have to filter out the error message if I write the output to stdout.
Thank you.
To disable printing of error messagaes, define a macro named ARMA_DONT_PRINT_ERRORS before including the Armadillo header. For example:
#define ARMA_DONT_PRINT_ERRORS
#include <armadillo>

How does the assignment part work in the following line of code in Angular2?

I am learning from the project angular2-rxjs-chat application ong github. In the code here there is a line of code given below:
threads[message.thread.id] = threads[message.thread.id] ||
message.thread;
where threads has earlier been defined on line 29 in the code as shown below:
let threads: {[key: string]: Thread} = {};
The comments in the code states that "store the message's thread in our acuuculator 'threads'. I need a little bit explanation of how does the assignment works on line 31 as on both sides of the assignment operator we have the same thing i.e., threads[message.thread.id]. If the statement on line 31 was like
(threads[message.thread.id] = message.thread;)
then I would explain it as a value is being assigned to a key in the map "threads". But I don't understand the full line.
This means if threads[message.thread.id] already has a value then keep it, otherwise set the value to meassage.thread.
If the part before || evaluates to a value that is truthy (not null, undefined, false, ...)then the part after||is not evaluated and the result from the part before||is returned otherwise the result from the expression after||` is returned.
You could also write it as
if(!threads[message.thread.id]) {
threads[message.thread.id] = message.thread;
}

Resources